forked from elecfreaks/pxt-cutebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cutebot.ts
513 lines (502 loc) · 14.7 KB
/
cutebot.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
* Functions to Cutebot by ELECFREAKS Co.,Ltd.
*/
//% weight=5 color=#0fbc11 icon="\uf207"
namespace cuteBot {
const STM8_ADDRESSS = 0x10
let IR_Val = 0
let _initEvents = true
/**
* Unit of Ultrasound Module
*/
export enum SonarUnit {
//% block="cm"
Centimeters,
//% block="inches"
Inches
}
/**
* Select the motor on the left or right
*/
export enum MotorsList {
//% blockId="M1" block="M1"
M1 = 0,
//% blockId="M2" block="M2"
M2 = 1
}
/**
* Select the servo on the S1 or S2
*/
export enum ServoList {
//% block="S1"
S1 = 0,
//% block="S2"
S2 = 1
}
/**
* Select the RGBLights on the left or right
*/
export enum RGBLights {
//% blockId="Right_RGB" block="Right_RGB"
RGB_L = 1,
//% blockId="Left_RGB" block="Left_RGB"
RGB_R = 0,
//% blockId="ALL" block="ALL"
ALL = 3
}
/**
* Status List of Tracking Modules
*/
export enum TrackingState {
//% block="● ●" enumval=0
L_R_line,
//% block="◌ ●" enumval=1
L_unline_R_line,
//% block="● ◌" enumval=2
L_line_R_unline,
//% block="◌ ◌" enumval=3
L_R_unline
}
export enum Direction {
//% block="Forward" enumval=0
forward,
//% block="Backward" enumval=1
backward,
//% block="Left" enumval=2
left,
//% block="Right" enumval=3
right
}
/**
* Line Sensor events MICROBIT_PIN_EVT_RISE
*/
export enum MbEvents {
//% block="Found"
FindLine = DAL.MICROBIT_PIN_EVT_FALL,
//% block="Lost"
LoseLine = DAL.MICROBIT_PIN_EVT_RISE
}
/**
* Pins used to generate events
*/
export enum MbPins {
//% block="Left"
Left = DAL.MICROBIT_ID_IO_P13,
//% block="Right"
Right = DAL.MICROBIT_ID_IO_P14
}
/**
* IR controller button
*/
export enum IRButtons {
//% blcok="Menu"
Menu = 2,
//% blcok="Up"
Up = 5,
//% blcok="Left"
Left = 8,
//% blcok="Right"
Right = 10,
//% blcok="Down"
Down = 13,
//% blcok="OK"
OK = 9,
//% blcok="Plus"
Plus = 4,
//% blcok="Minus"
Minus = 12,
//% blcok="Back"
Back = 6,
//% block="0"
Zero = 14,
//% block="1"
One = 16,
//% block="2"
Two = 17,
//% block="3"
Three = 18,
//% block="4"
Four = 20,
//% block="5"
Five = 21,
//% block="6"
Six = 22,
//% block="7"
Seven = 24,
//% block="8"
Eight = 25,
//% block="9"
Nine = 26
}
/**
* TODO: Set the speed of left and right wheels.
* @param lspeed Left wheel speed , eg: 100
* @param rspeed Right wheel speed, eg: -100
*/
//% blockId=MotorRun block="Set left wheel speed %lspeed\\% |right wheel speed %rspeed\\%"
//% lspeed.min=-100 lspeed.max=100
//% rspeed.min=-100 rspeed.max=100
//% weight=100
export function motors(lspeed: number = 50, rspeed: number = 50): void {
let buf = pins.createBuffer(4);
if (lspeed > 100) {
lspeed = 100;
} else if (lspeed < -100) {
lspeed = -100;
}
if (rspeed > 100) {
rspeed = 100;
} else if (rspeed < -100) {
rspeed = -100;
}
if (lspeed > 0) {
buf[0] = 0x01; //左右轮 0x01左轮 0x02右轮
buf[1] = 0x02; //正反转0x02前进 0x01后退
buf[2] = lspeed; //速度
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
else {
buf[0] = 0x01;
buf[1] = 0x01;
buf[2] = lspeed * -1;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
if (rspeed > 0) {
buf[0] = 0x02;
buf[1] = 0x02;
buf[2] = rspeed;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
else {
buf[0] = 0x02;
buf[1] = 0x01;
buf[2] = rspeed * -1;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
}
/**
* TODO: Full speed operation lasts for 10 seconds,speed is 100.
* @param dir Driving direction, eg: Direction.forward
* @param speed Running speed, eg: 50
* @param time Travel time, eg: 5
*/
//% blockId=cutebot_move_time block="Go %dir at speed%speed\\% for %time seconds"
//% weight=95
export function moveTime(dir: Direction, speed: number, time: number): void {
if (dir == 0) {
motors(speed, speed);
basic.pause(time * 1000)
motors(0, 0)
}
if (dir == 1) {
motors(-speed, -speed);
basic.pause(time * 1000)
motors(0, 0)
}
if (dir == 2) {
motors(-speed, speed);
basic.pause(time * 1000)
motors(0, 0)
}
if (dir == 3) {
motors(speed, -speed);
basic.pause(time * 1000)
motors(0, 0)
}
}
/**
* TODO: full speed move forward,speed is 100.
*/
//% blockId=cutebot_forward block="Go straight at full speed"
//% weight=90
export function forward(): void {
// Add code here
let buf = pins.createBuffer(4);
buf[0] = 0x01;
buf[1] = 0x02;
buf[2] = 80;
buf[3] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x02;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
/**
* TODO: full speed move back,speed is -100.
*/
//% blockId=cutebot_back block="Reverse at full speed"
//% weight=85
export function backforward(): void {
// Add code here
let buf = pins.createBuffer(4);
buf[0] = 0x01;
buf[1] = 0x01;
buf[2] = 80;
buf[3] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x02;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
/**
* TODO: full speed turnleft.
*/
//% blockId=cutebot_left block="Turn left at full speed"
//% weight=80
export function turnleft(): void {
// Add code here
let buf = pins.createBuffer(4);
buf[0] = 0x02;
buf[1] = 0x02;
buf[2] = 80;
buf[3] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x01;
buf[2] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
/**
* TODO: full speed turnright.
*/
//% blockId=cutebot_right block="Turn right at full speed"
//% weight=75
export function turnright(): void {
// Add code here
let buf = pins.createBuffer(4);
buf[0] = 0x01;
buf[1] = 0x02;
buf[2] = 80;
buf[3] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x02;
buf[2] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
/**
* TODO: stopcar
*/
//% blockId=cutebot_stopcar block="Stop car immediatly"
//% weight=70
export function stopcar(): void {
motors(0, 0)
}
/**
* TODO: Set LED headlights.
*/
//% block="Set LED headlights %light color $color"
//% color.shadow="colorNumberPicker"
//% weight=65
export function colorLight(light: RGBLights, color: number) {
let r: number, g: number, b: number = 0
r = color >> 16
g = (color >> 8) & 0xFF
b = color & 0xFF
singleheadlights(light, r, g, b)
}
/**
* TODO: Select a headlights and set the RGB color.
* @param R R color value of RGB color, eg: 0
* @param G G color value of RGB color, eg: 128
* @param B B color value of RGB color, eg: 255
*/
//% inlineInputMode=inline
//% blockId=RGB block="Set LED headlights %light color R:%r G:%g B:%b"
//% r.min=0 r.max=255
//% g.min=0 g.max=255
//% b.min=0 b.max=255
//% weight=60
export function singleheadlights(light: RGBLights, r: number, g: number, b: number): void {
let buf = pins.createBuffer(4);
if (light == 3) {
buf[0] = 0x04;
buf[1] = r;
buf[2] = g;
buf[3] = b;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x08;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
else {
if (light == 0) {
buf[0] = 0x04;
}
if (light == 1) {
buf[0] = 0x08;
}
buf[1] = r;
buf[2] = g;
buf[3] = b;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
}
/**
* Close all headlights.
*/
//% inlineInputMode=inline
//% block="Turn off all LED headlights"
//% weight=55
export function closeheadlights(): void {
let buf = pins.createBuffer(4);
buf[0] = 0x04;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x08;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
/**
* Judging the Current Status of Tracking Module.
* @param state Four states of tracking module, eg: TrackingState.L_R_line
*/
//% blockId=ringbitcar_tracking block="Tracking state is %state"
//% weight=50
export function tracking(state: TrackingState): boolean {
pins.setPull(DigitalPin.P13, PinPullMode.PullNone)
pins.setPull(DigitalPin.P14, PinPullMode.PullNone)
let left_tracking = pins.digitalReadPin(DigitalPin.P13);
let right_tracking = pins.digitalReadPin(DigitalPin.P14);
if (left_tracking == 0 && right_tracking == 0 && state == 0) {
return true;
}
else if (left_tracking == 1 && right_tracking == 0 && state == 1) {
return true;
}
else if (left_tracking == 0 && right_tracking == 1 && state == 2) {
return true;
}
else if (left_tracking == 1 && right_tracking == 1 && state == 3) {
return true;
}
else {
return false;
}
}
/**
* TODO: track one side
* @param side Line sensor edge , eg: MbPins.Left
* @param state Line sensor status, eg: MbEvents.FindLine
*/
//% block="%side line sensor %state"
//% state.fieldEditor="gridpicker" state.fieldOptions.columns=2
//% side.fieldEditor="gridpicker" side.fieldOptions.columns=2
//% weight=45
export function trackSide(side: MbPins, state: MbEvents): boolean {
pins.setPull(DigitalPin.P13, PinPullMode.PullNone)
pins.setPull(DigitalPin.P14, PinPullMode.PullNone)
let left_tracking = pins.digitalReadPin(DigitalPin.P13);
let right_tracking = pins.digitalReadPin(DigitalPin.P14);
if (side == 113 && state == 2 && left_tracking == 1) {
return true;
}
else if (side == 113 && state == 3 && left_tracking == 0) {
return true;
}
else if (side == 114 && state == 2 && right_tracking == 1) {
return true;
}
else if (side == 114 && state == 3 && right_tracking == 0) {
return true;
}
else {
return false;
}
}
/**
* TODO: Runs when line sensor finds or loses.
*/
//% block="On %sensor| line %event"
//% sensor.fieldEditor="gridpicker" sensor.fieldOptions.columns=2
//% event.fieldEditor="gridpicker" event.fieldOptions.columns=2
//% weight=40
export function trackEvent(sensor: MbPins, event: MbEvents, handler: Action) {
initEvents();
control.onEvent(<number>sensor, <number>event, handler);
}
/**
* Cars can extend the ultrasonic function to prevent collisions and other functions..
* @param Sonarunit two states of ultrasonic module, eg: Centimeters
*/
//% blockId=ultrasonic block="HC-SR04 Sonar unit %unit"
//% weight=35
export function ultrasonic(unit: SonarUnit, maxCmDistance = 500): number {
// send pulse
pins.setPull(DigitalPin.P8, PinPullMode.PullNone);
pins.digitalWritePin(DigitalPin.P8, 0);
control.waitMicros(2);
pins.digitalWritePin(DigitalPin.P8, 1);
control.waitMicros(10);
pins.digitalWritePin(DigitalPin.P8, 0);
// read pulse
const d = pins.pulseIn(DigitalPin.P12, PulseValue.High, maxCmDistance * 50);
switch (unit) {
case SonarUnit.Centimeters:
return Math.floor(d * 34 / 2 / 1000);
case SonarUnit.Inches:
return Math.floor(d * 34 / 2 / 1000 * 0.3937);
default:
return d;
}
}
/**
* TODO: Set the angle of servo.
* @param Servo ServoList , eg: cuteBot.ServoList.S1
* @param angle angle of servo, eg: 90
*/
//% blockId=cutebot_servo block="Set servo %servo angle to %angle °"
//% angle.shadow="protractorPicker"
//% weight=30
export function setServo(Servo: ServoList, angle: number = 180): void {
let buf = pins.createBuffer(4);
if (Servo == ServoList.S1) {
buf[0] = 0x05;
buf[1] = angle;
buf[2] = 0;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
else {
buf[0] = 0x06;
buf[1] = angle;
buf[2] = 0;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
}
}
//% shim=IRV2::irCode
function irCode(): number {
return 0;
}
//% weight=25
//% block="On IR receiving"
export function IR_callback(handler: () => void) {
pins.setPull(DigitalPin.P16, PinPullMode.PullUp)
control.onEvent(98, 3500, handler)
control.inBackground(() => {
while (true) {
IR_Val = irCode()
if (IR_Val != 0xff00) {
control.raiseEvent(98, 3500, EventCreationMode.CreateAndFire)
}
basic.pause(20)
}
})
}
/**
* TODO: Get IR value
*/
//% block="IR Button %Button is pressed"
//% weight=15
export function IR_Button(Button: IRButtons): boolean {
return (IR_Val & 0x00ff) == Button
}
function initEvents(): void {
if (_initEvents) {
pins.setEvents(DigitalPin.P13, PinEventType.Edge);
pins.setEvents(DigitalPin.P14, PinEventType.Edge);
_initEvents = false;
}
}
}