forked from waveshare/pxt-AlphaBot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphaBot2.ts
executable file
·427 lines (384 loc) · 13.2 KB
/
AlphaBot2.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
/**
* 使用此文件来定义自定义函数和图形块。
* 想了解更详细的信息,请前往 https://makecode.microbit.org/blocks/custom
*/
enum Motors {
//% block="M1"
M1 = 0x1,
//% block="M2"
M2 = 0x2,
}
enum Sensor {
//% block="Left"
Left = 0x1,
//% block="Right"
Right = 0x2,
}
enum Dir {
//% block="Forward"
forward = 0x1,
//% block="Backward"
backward = 0x2,
//% block="TurnRight"
turnRight = 0x3,
//% block="TurnLeft"
turnLeft = 0x4,
//% block="stop"
stop = 0x5,
}
/**
* 自定义图形块
*/
//% weight=5 color=#0fbc11 icon="\uf113"
namespace AlphaBot2 {
const PCA9685_ADDRESS = 0x40
const MODE1 = 0x00
const MODE2 = 0x01
const SUBADR1 = 0x02
const SUBADR2 = 0x03
const SUBADR3 = 0x04
const PRESCALE = 0xFE
const LED0_ON_L = 0x06
const LED0_ON_H = 0x07
const LED0_OFF_L = 0x08
const LED0_OFF_H = 0x09
const ALL_LED_ON_L = 0xFA
const ALL_LED_ON_H = 0xFB
const ALL_LED_OFF_L = 0xFC
const ALL_LED_OFF_H = 0xFD
const STP_CHA_L = 2047
const STP_CHA_H = 4095
const STP_CHB_L = 1
const STP_CHB_H = 2047
const STP_CHC_L = 1023
const STP_CHC_H = 3071
const STP_CHD_L = 3071
const STP_CHD_H = 1023
let initialized = false
let last_value = 0; // assume initially that the line is left.
let calibratedMax = [650, 650, 650, 650, 650];
let calibratedMin = [100, 100, 100, 100, 100];
function i2cwrite(addr: number, reg: number, value: number) {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2ccmd(addr: number, value: number) {
let buf = pins.createBuffer(1)
buf[0] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2cread(addr: number, reg: number) {
pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE);
let val = pins.i2cReadNumber(addr, NumberFormat.UInt8BE);
return val;
}
function initPCA9685(): void {
i2cwrite(PCA9685_ADDRESS, MODE1, 0x00)
setFreq(500);
setPwm(0, 0, 4095);
for (let idx = 1; idx < 16; idx++) {
setPwm(idx, 0, 0);
}
initialized = true
}
function setFreq(freq: number): void {
// Constrain the frequency
let prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
let prescale = prescaleval; //Math.Floor(prescaleval + 0.5);
let oldmode = i2cread(PCA9685_ADDRESS, MODE1);
let newmode = (oldmode & 0x7F) | 0x10; // sleep
i2cwrite(PCA9685_ADDRESS, MODE1, newmode); // go to sleep
i2cwrite(PCA9685_ADDRESS, PRESCALE, prescale); // set the prescaler
i2cwrite(PCA9685_ADDRESS, MODE1, oldmode);
control.waitMicros(5000);
i2cwrite(PCA9685_ADDRESS, MODE1, oldmode | 0xa1);
}
function setPwm(channel: number, on: number, off: number): void {
if (channel < 0 || channel > 15)
return;
let buf = pins.createBuffer(5);
buf[0] = LED0_ON_L + 4 * channel;
buf[1] = on & 0xff;
buf[2] = (on >> 8) & 0xff;
buf[3] = off & 0xff;
buf[4] = (off >> 8) & 0xff;
pins.i2cWriteBuffer(PCA9685_ADDRESS, buf);
}
//% blockId=AlphaBot2_motor_run block="Motor|%index|speed %speed"
//% speed eg: 2250
//% weight=100
//% speed.min=-4095 speed.max=4095
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function MotorRun(index: Motors, speed: number): void {
if (!initialized) {
initPCA9685()
}
//speed = speed * 16; // map 255 to 4096
if (speed >= 4096) {
speed = 4095
}
if (speed <= -4096) {
speed = -4095
}
if (index == 1) {
if (speed >= 0) {
setPwm(2, 0, 4095)
setPwm(3, 0, 0)
setPwm(1, 0, speed)
} else {
setPwm(2, 0, 0)
setPwm(3, 0, 4095)
setPwm(1, 0, -speed)
}
} else if (index == 2) {
if (speed >= 0) {
setPwm(5, 0, 4095)
setPwm(4, 0, 0)
setPwm(6, 0, speed)
} else {
setPwm(5, 0, 0)
setPwm(4, 0, 4095)
setPwm(6, 0, -speed)
}
}
}
/**
* Execute single motors
* @param speed [-4095-4095] speed of motor; eg: 2250
*/
//% blockId=AlphaBot2_run block="|%index|speed %speed"
//% speed eg: 2250
//% weight=95
//% speed.min=-4095 speed.max=4095 eg: 2250
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Run(index: Dir, speed: number): void {
switch (index) {
case Dir.forward:
MotorRun(Motors.M1, speed);
MotorRun(Motors.M2, speed);
break;
case Dir.backward:
MotorRun(Motors.M1, -speed);
MotorRun(Motors.M2, -speed);
break;
case Dir.turnRight:
MotorRun(Motors.M1, speed);
MotorRun(Motors.M2, -speed);
break;
case Dir.turnLeft:
MotorRun(Motors.M1, -speed);
MotorRun(Motors.M2, speed);
break;
case Dir.stop:
MotorRun(Motors.M1, 0);
MotorRun(Motors.M2, 0);
break;
}
}
/**
* Execute single motors
* @param speed [-4095-4095] speed of motor; eg: 2250
* @param time dalay second time; eg: 2
*/
//% blockId=AlphaBot2_run_delay block="|%index|speed %speed|for %time|sec"
//% speed eg: 2250
//% weight=90
//% speed.min=-4095 speed.max=4095 eg: 2250
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RunDelay(index: Dir, speed: number, time: number): void {
Run(index, speed);
basic.pause(time * 1000);
Run(Dir.stop, 0);
}
//% blockId=AlphaBot2_infrared block="Infrared |%index"
//% weight=80
export function Infrared(index: Sensor): boolean {
let value = true;
pins.setPull(DigitalPin.P12, PinPullMode.PullUp);
pins.setPull(DigitalPin.P16, PinPullMode.PullUp);
if (index == 0x01) {
if (pins.digitalReadPin(DigitalPin.P12)) {
value = false;
}
} else {
if (pins.digitalReadPin(DigitalPin.P16)) {
value = false;
}
}
return value;
}
//% blockId=AlphaBot2_ultrasonic block="Ultrasonic"
//% weight=80
export function Ultrasonic(): number {
// send pulse
pins.setPull(DigitalPin.P1, PinPullMode.PullNone);
pins.digitalWritePin(DigitalPin.P1, 0);
control.waitMicros(2);
pins.digitalWritePin(DigitalPin.P1, 1);
control.waitMicros(10);
pins.digitalWritePin(DigitalPin.P1, 0);
// read pulse
let d = pins.pulseIn(DigitalPin.P2, PulseValue.High, 11600);
return d / 58;
}
//% blockId=AlphaBot2_AnalogRead block="AnalogRead"
//% weight=80 advanced=true
export function AnalogRead(): number[] {
if (!initialized) {
initPCA9685()
}
let i = 0;
let j = 0;
let channel = 0;
let values = [0, 0, 0, 0, 0, 0];
let sensor_values = [0, 0, 0, 0, 0];
//pins.digitalWritePin(DigitalPin.P16, 0);
setPwm(0, 0, 0);
basic.pause(2);
for (i = 0; i < 6; i++) {
for (j = 0; j < 10; j++) {
//0 to 4 clock transfer channel address
if (j < 4) {
if ((i >> (3 - j)) & 0x01) {
pins.digitalWritePin(DigitalPin.P15, 1);
} else {
pins.digitalWritePin(DigitalPin.P15, 0);
}
}
//0 to 10 clock receives the previous conversion result
values[i] <<= 1;
if (pins.digitalReadPin(DigitalPin.P14)) {
values[i] |= 0x01;
}
pins.digitalWritePin(DigitalPin.P13, 1);
pins.digitalWritePin(DigitalPin.P13, 0);
}
for (j = 0; j < 6; j++) {
pins.digitalWritePin(DigitalPin.P13, 1);
pins.digitalWritePin(DigitalPin.P13, 0);
}
}
//pins.digitalWritePin(DigitalPin.P16, 1);
setPwm(0, 0, 4095);
for (i = 0; i < 5; i++) {
sensor_values[i] = values[i + 1];
}
return sensor_values;
}
//% blockId=AlphaBot2_SensorCalibrated block="SensorCalibrated"
//% weight=70
export function SensorCalibrated(): void {
let i = 0;
let j = 0;
let k = 0;
let max_sensor_values = [0, 0, 0, 0, 0];
let min_sensor_values = [0, 0, 0, 0, 0];
for (let i = 0; i < 5; i++) // make the calibration take about 10 seconds
{
calibratedMax[i] = 0;
calibratedMin[i] = 1023;
}
for (let i = 0; i < 100; i++) // make the calibration take about 10 seconds
{
if (i < 25 || i >= 75) {
Run(Dir.turnLeft, 70)
}
else {
Run(Dir.turnRight, 70)
}
// reads all sensors 100 times
for (j = 0; j < 10; j++) {
let sensor_values = AnalogRead();
for (k = 0; k < 5; k++) {
// set the max we found THIS time
if ((j == 0) || (max_sensor_values[k] < sensor_values[k]))
max_sensor_values[k] = sensor_values[k];
// set the min we found THIS time
if ((j == 0) || (min_sensor_values[k] > sensor_values[k]))
min_sensor_values[k] = sensor_values[k];
}
}
// record the min and max calibration value
for (k = 0; k < 5; k++) {
if (min_sensor_values[k] > calibratedMax[k])
calibratedMax[k] = min_sensor_values[k];
if (max_sensor_values[k] < calibratedMin[k])
calibratedMin[k] = max_sensor_values[k];
}
}
Run(Dir.stop, 0);
}
//% blockId=AlphaBot2_ReadSensorMax block="ReadSensorMax"
//% weight=60 advanced=true
export function ReadSensorMax(): number[] {
return calibratedMax;
}
//% blockId=AlphaBot2_ReadSensorMin block="ReadSensorMin"
//% weight=50 advanced=true
export function ReadSensorMin(): number[] {
return calibratedMin;
}
// Returns values calibrated to a value between 0 and 1000, where
// 0 corresponds to the minimum value read by calibrate() and 1000
// corresponds to the maximum value. Calibration values are
// stored separately for each sensor, so that differences in the
// sensors are accounted for automatically.
//% blockId=AlphaBot2_ReadCalibrated block="ReadCalibrated"
//% weight=70 advanced=true
export function readCalibrated(): number[] {
// read the needed values
let sensor_values = AnalogRead();
for (let i = 0; i < 5; i++) {
let denominator = calibratedMax[i] - calibratedMin[i];
let x = ((sensor_values[i] - calibratedMin[i]) * 1000 / denominator);
if (x < 0)
x = 0;
else if (x > 1000)
x = 1000;
sensor_values[i] = x;
}
return sensor_values;
}
//% blockId=AlphaBot2_readLine block="ReadLine"
//% weight=10
export function readLine(): number {
let i = 0;
let on_line = 0;
let avg = 0; // this is for the weighted total, which is long
// before division
let sum = 0; // this is for the denominator which is <= 64000
let white_line = 0;
// readCalibrated(sensor_values);
let sensor_values = readCalibrated();
for (i = 0; i < 5; i++) {
let value = sensor_values[i];
if (!white_line)
value = 1000 - value;
sensor_values[i] = value;
// keep track of whether we see the line at all
if (value > 200) {
on_line = 1;
}
// only average in values that are above a noise threshold
if (value > 50) {
avg += (value) * (i * 1000);
sum += value;
}
}
if (!on_line) {
// If it last read to the left of center, return 0.
if (last_value < (4) * 1000 / 2)
return 0;
// If it last read to the right of center, return the max.
else
return 4 * 1000;
}
last_value = avg / sum;
return last_value;
}
}