-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbnav.ts
279 lines (263 loc) · 9.72 KB
/
cbnav.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* Blocks for controlling the Cyberbot
*/
namespace cyberbot {
export enum NavDirection {
//% block="forward"
Forward,
//% block="reverse"
Reverse,
//% block="left"
Left,
//% block="right"
Right
}
let navLightIsOn = false;
let leftIsRunning = false;
let rightIsRunning = false;
let leftServo = ServoPin.P18
let rightServo = ServoPin.P19
let leftForwardSpeed = 75
let rightForwardSpeed = -75
let leftReverseSpeed = -75
let rightReverseSpeed = 75
/**
* Toggle the navigation lights
* @param control The lights state.
*/
//% blockId="cyberbot_nav_light_toggle" block="navigation light %control"
//% subcategory="navigation"
//% control.shadow="toggleOnOff"
//% weight=180
//% group="Settings"
export function navLightToggle(control: boolean): void {
navLightIsOn = control;
}
function leftNavLight(speed: number) {
while (leftIsRunning) {
if (speed > 0) {
for (let i = 2; i <= 6; i++) {
if (leftIsRunning) {
led.toggle(4, i % 5)
pause(100)
led.toggle(4, i % 5)
}
else { break }
}
}
else if (speed < 0) {
for (let i = 7; i >= 3; i--) {
if (leftIsRunning) {
led.toggle(4, i % 5)
pause(100)
led.toggle(4, i % 5)
}
else { break }
}
}
}
}
function rightNavLight(speed: number) {
while (rightIsRunning) {
if (speed < 0) {
for (let i = 2; i <= 6; i++) {
if (rightIsRunning) {
led.toggle(0, i % 5)
pause(100)
led.toggle(0, i % 5)
}
else { break }
}
}
else if (speed > 0) {
for (let i = 7; i >= 3; i--) {
if (rightIsRunning) {
led.toggle(0, i % 5)
pause(100)
led.toggle(0, i % 5)
}
else { break }
}
}
}
}
/**
* Choose the pins connected to each wheel's servo.
* @param leftPin is the pin number connected to the left wheel servo, eg: cyberbot.ServoPin.P18
* @param rightPin is the pin number connected to the right wheel servo, eg: cyberbot.ServoPin.P19
*/
//% blockId="cyberbot_set_left_servo" block="set left wheel %leftPin set right wheel %rightPin"
//% weight=200
//% subcategory="navigation"
//% inlineInputMode="external"
//% group="Settings"
export function setLeftServo(leftPin: ServoPin, rightPin: ServoPin): void {
leftServo = leftPin;
rightServo = rightPin;
}
/**
* Calibrate the servos
* @param fAdjustment Forward adjustments
* @param rAdjustment Reverse adjustments
*/
//% blockId="cyberbot_calibrate" block="calibrate forward: %fAdjustment calibrate backward: %bAdjustment"
//% fAdjustment.min=-15
//% fAdjustment.max=15
//% rAdjustment.min=-15
//% rAdjustment.max=15
//% inlineInputMode="external"
//% weight=190
//% subcategory="navigation"
//% group="Settings"
export function calibrate(fAdjustment: number, rAdjustment: number): void {
leftForwardSpeed -= fAdjustment;
rightForwardSpeed -= fAdjustment;
leftReverseSpeed += rAdjustment;
rightReverseSpeed += rAdjustment;
}
/**
* Set the bot on a path. It will not stop unless told to stop.
* @param direction, eg: cyberbot.NavDirection.Forward
*/
//% blockId="cyberbot_nav_forever" block="go %direction"
//% subcategory="navigation"
//% group="Directional"
//% weight=170
export function navForever(direction: NavDirection): void {
leftIsRunning = false;
rightIsRunning = false;
stop();
pause(100)
let leftSpeed: number;
let rightSpeed: number;
if (direction === NavDirection.Forward) {
leftSpeed = leftForwardSpeed;
rightSpeed = rightForwardSpeed;
}
else if (direction === NavDirection.Reverse) {
leftSpeed = leftReverseSpeed;
rightSpeed = rightReverseSpeed;
}
else if (direction === NavDirection.Left) {
leftSpeed = leftReverseSpeed;
rightSpeed = rightForwardSpeed;
}
else if (direction === NavDirection.Right) {
leftSpeed = leftForwardSpeed;
rightSpeed = rightReverseSpeed;
}
sendCommand(leftServo, null, SERVO_SPEED, 0, leftSpeed);
sendCommand(rightServo, null, SERVO_SPEED, 0, rightSpeed);
leftIsRunning = true;
rightIsRunning = true;
if (navLightIsOn) {
control.inBackground(() => leftNavLight(leftSpeed));
control.inBackground(() => rightNavLight(rightSpeed));
}
pause(10);
}
/**
* Move the bot in a direction for a certain number of seconds.
* @param direction, eg: cyberbot.NavDirection.Forward
* @param duration Duration of time in seconds, eg: 1
*/
//% blockId="cyberbot_nav_duration" block="go %direction for %duration seconds"
//% subcategory="navigation"
//% group="Directional"
//% weight=160
export function navDuration(direction: NavDirection, duration: number): void {
let leftSpeed: number;
let rightSpeed: number;
if (direction === NavDirection.Forward) {
leftSpeed = leftForwardSpeed;
rightSpeed = rightForwardSpeed;
}
else if (direction === NavDirection.Reverse) {
leftSpeed = leftReverseSpeed;
rightSpeed = rightReverseSpeed;
}
else if (direction === NavDirection.Left) {
leftSpeed = leftReverseSpeed;
rightSpeed = rightForwardSpeed;
}
else if (direction === NavDirection.Right) {
leftSpeed = leftForwardSpeed;
rightSpeed = rightReverseSpeed;
}
sendCommand(leftServo, null, SERVO_SPEED, 0, leftSpeed);
sendCommand(rightServo, null, SERVO_SPEED, 0, rightSpeed);
pause(duration * 1000)
stop()
}
/**
* Set the bot on a path at a certain percent of full speed.
* @param direction, eg: cyberbot.NavDirection.Forward
* @param speed Percentage of full speed, eg: 100
*/
//% speed.min=0
//% speed.max=100
//% blockId="cyberbot_nav_speed" block="go %direction at %speed \\% full speed"
//% subcategory="navigation"
//% weight=150
//% group="Directional"
export function navSpeed(direction: NavDirection, speed: number): void {
if (speed > 100) { speed = 100; }
if (speed < 0) { speed = 0; }
let leftSpeed: number;
let rightSpeed: number;
if (direction === NavDirection.Forward) {
leftSpeed = speed * leftForwardSpeed / 100;
rightSpeed = speed * rightForwardSpeed / 100;
}
else if (direction === NavDirection.Reverse) {
leftSpeed = speed * leftReverseSpeed / 100;
rightSpeed = speed * rightReverseSpeed / 100;
}
else if (direction === NavDirection.Left) {
leftSpeed = speed * leftReverseSpeed / 100;
rightSpeed = speed * rightForwardSpeed / 100;
}
else if (direction === NavDirection.Right) {
leftSpeed = speed * leftForwardSpeed / 100;
rightSpeed = speed * rightReverseSpeed / 100;
}
sendCommand(leftServo, null, SERVO_SPEED, 0, leftSpeed);
sendCommand(rightServo, null, SERVO_SPEED, 0, rightSpeed);
pause(10)
}
/**
* Stop the left and right wheels' servos.
*/
//% blockId="cyberbot_stop" block="stop wheels"
//% subcategory="navigation"
//% weight=130
//% group="Directional"
export function stop(): void {
leftIsRunning = false;
rightIsRunning = false;
sendCommand(leftServo, null, SERVO_DISABLE, 0, null);
sendCommand(rightServo, null, SERVO_DISABLE, 0, null);
}
/**
* Drive by specifying how fast each wheel should spin and for how long.
* @param leftSpeed A percentage of the left wheel's full speed, eg: 100
* @param rightSpeed A percentage of the right wheel's full speed, eg: 100
* @param time Time in seconds, eg: 1
*/
//% blockId="cyberbot_precision_drive" block="set left wheel power %leftSpeed set right wheel power %rightSpeed drive for duration (sec) $time"
//% subcategory="navigation"
//% weight=140
//% leftSpeed.min=-100 leftSpeed.max=100 leftSpeed.shadow="speedPicker"
//% rightSpeed.min=-100 rightSpeed.max=100 rightSpeed.shadow="speedPicker"
//% inlineInputMode="external"
//% group="Controlled"
export function precisionDrive(leftSpeed: number, rightSpeed: number, time: number): void {
leftSpeed = leftSpeed
rightSpeed = -1 * rightSpeed
sendCommand(leftServo, null, SERVO_SPEED, 0, leftSpeed);
sendCommand(rightServo, null, SERVO_SPEED, 0, rightSpeed);
pause(time * 1000)
sendCommand(leftServo, null, SERVO_DISABLE, 0, null);
sendCommand(rightServo, null, SERVO_DISABLE, 0, null);
}
}