-
Notifications
You must be signed in to change notification settings - Fork 4
/
reromicro.ts
380 lines (328 loc) · 12.3 KB
/
reromicro.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
enum LineSensors {
//% block=left
Left = 0,
//% block=center
Center = 1,
//% block=right
Right = 2
}
enum Motors {
//% block="left motor"
Left = 0,
//% block="right motor"
Right = 1,
//% block="both motors"
Both = 2
}
/**
* rero:micro Blocks
*/
//% weight=99 color=#ff8000 icon="\uf121" block="rero:micro"
namespace reromicro {
//==============================================
// Obtain micro:bit board version
// Ref: https://support.microbit.org/support/solutions/articles/19000130254-identify-the-version-number-of-the-micro-bit-in-your-program
//==============================================
let board_ver = control.hardwareVersion()
//==============================================
// Ultrasonic Sensor (HC-SR04)
//==============================================
let trig = DigitalPin.P2
let echo = DigitalPin.P2
let maxCmDistance = 255
let const_2divspeed = 58
// special tuning for microbit v1
// to get a more accurate value in cm
if (board_ver == "1") {
const_2divspeed = 40
}
/**
* Read distance in centimeters (cm) with ultrasonic sensor.
* Distance = 3cm - 255cm.
* Note: It returns '255' if distance >255cm or no echo is detected.
*/
//% subcategory=Sensors
//% blockId=rero-micro-read-ultrasonic block="ultrasonic distance(cm)"
//% blockGap=30
//% weight=90
export function ReadUltrasonic(): number {
// send pulse
pins.setPull(trig, PinPullMode.PullNone);
pins.digitalWritePin(trig, 0);
control.waitMicros(2);
pins.digitalWritePin(trig, 1);
control.waitMicros(10);
pins.digitalWritePin(trig, 0);
// read pulse
// The maximum duration need to add in 20ms of deadzone.
const d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * const_2divspeed + 20000);
if (d == 0) {
return 255
}
return Math.idiv(d, const_2divspeed)
}
//==============================================
// I2C EEPROM
//==============================================
const AT24_I2C_ADDR = 80;
const lineEepromAddress = 100
/**
* read 32-bit data from address
* @param addr eeprom address, eg: 4
*/
function EE_Read32bit(addr: number): number {
pins.i2cWriteNumber(AT24_I2C_ADDR, addr, NumberFormat.UInt8BE);
return pins.i2cReadNumber(AT24_I2C_ADDR, NumberFormat.UInt32BE);
}
//==============================================
// Line Sensors
//==============================================
let rightLineSensor = DigitalPin.P13
let centerLineSensor = DigitalPin.P14
let leftLineSensor = DigitalPin.P15
// X[left, center, right]
let lineSensorPins = [leftLineSensor, centerLineSensor, rightLineSensor]
let lineSensorMax = [1500, 1500, 1500]
let lineSensorMin = [0, 0, 0]
let lineSensorValues = [0, 0, 0]
let lineSensorThreshold = [500, 500, 500]
// read line sensors max & min values from EEPROM
let ui32EeData = 0
ui32EeData = EE_Read32bit(lineEepromAddress)
// verify magic number 'rero'
if (ui32EeData == 1919251055) {
for (let i = 0; i < 3; i++) {
ui32EeData = EE_Read32bit(lineEepromAddress + 4 + (i * 4))
lineSensorMax[i] = ui32EeData >> 16
lineSensorMin[i] = ui32EeData & 0xFFFF
lineSensorThreshold[i] = ((lineSensorMax[i] + lineSensorMin[i]) >> 1) - ((lineSensorMax[i] - lineSensorMin[i]) >> 2)
}
}
/**
* Read line sensors.
* This function reads all three IR pairs (line sensors) and stores the values in variables.
* Retrieve these values using "X sensor detects line" and/or "X line sensor IR intensity" blocks.
*/
//% subcategory=Sensors
//% blockId=rero-micro-line-readsensors
//% block="read line sensors"
//% blockGap=10
//% weight=85
export function ReadLineSensors(): void {
let nTimer = 1500
let nMaxTimer = 1500
let nStartTime = 0
let nElapsedTime = 0
// Read sensors
for (let i = 0; i < 3; i++) {
do {
nStartTime = input.runningTimeMicros()
pins.digitalWritePin(lineSensorPins[i], 1)
control.waitMicros(70)
pins.setPull(lineSensorPins[i], PinPullMode.PullNone)
while (1) {
nElapsedTime = input.runningTimeMicros() - nStartTime
if (pins.digitalReadPin(lineSensorPins[i]) == 0) {
nTimer = nElapsedTime
break
}
else if (nElapsedTime >= nMaxTimer) {
nTimer = nMaxTimer
break
}
else if (nElapsedTime < 0) {
break
}
}
} while (nElapsedTime < 0)
lineSensorValues[i] = Math.clamp(0, nMaxTimer, nTimer)
}
}
/**
* ! Use "read line sensors" function first before this.
* This function returns ''true'' if the sensor detects dark line, otherwise ''false''
* @param sensor position, eg: LineSensors.Left
*/
//% subcategory=Sensors
//% blockId=rero-micro-line-sensordetectsline
//% block="|%sensor|sensor detects line"
//% blockGap=10
//% sensor.fieldEditor="gridpicker" sensor.fieldOptions.columns=3
//% sensor.fieldOptions.width="200"
//% weight=84
export function LineSensorDetectsLine(sensor: LineSensors): boolean {
return ((lineSensorValues[sensor] > lineSensorThreshold[sensor]) ? true : false)
}
/**
* ! Use "read line sensors" function first before this.
* This function returns a single sensor's reflected infrared intensity value,
* between 0 and 1500.
* @param sensor position, eg: LineSensors.Left
*/
//% subcategory=Sensors
//% blockId=rero-micro-line-irintensity
//% block="|%sensor|line sensor IR intensity"
//% blockGap=20
//% sensor.fieldEditor="gridpicker" sensor.fieldOptions.columns=3
//% sensor.fieldOptions.width="200"
//% weight=83
export function LineIrIntensity(sensor: LineSensors): number {
return lineSensorValues[sensor]
}
/**
* Only use this function once in "on start" if your robot doesn't detect the line properly.
* This function sets the threshold value for each IR pair to determine white and black surfaces.
* @param leftThreshold value, eg: 500
* @param centerThreshold value, eg: 500
* @param rightThreshold value, eg: 500
*/
//% subcategory=Sensors
//% blockId=rero-micro-line-adjustthresholds
//% block="calibrate line sensors: left|%leftThreshold| center|%centerThreshold| right|%rightThreshold|"
//% leftThreshold.min=0 leftThreshold.max=1500
//% centerThreshold.min=0 centerThreshold.max=1500
//% rightThreshold.min=0 rightThreshold.max=1500
//% blockGap=20
//% weight=80
export function LineAdjustThresholds(leftThreshold: number, centerThreshold: number, rightThreshold: number): void {
lineSensorThreshold[0] = leftThreshold
lineSensorThreshold[1] = centerThreshold
lineSensorThreshold[2] = rightThreshold
}
//==============================================
// Motors
//==============================================
// Initialization
// Set PWM frequency to 10kHz.
// Note: If set to 20kHz, PWM duty cycle may be inverted in certain condition.
Brake()
pins.analogSetPeriod(AnalogPin.P8, 100)
pins.analogSetPeriod(AnalogPin.P16, 100)
/**
* Move Forward.
* Speed = 0 - 100
* @param speed selected speed, eg: 50
*/
//% subcategory=Motors
//% blockId=rero-micro-move-forward
//% block="move forward at|%speed|"
//% speed.min=0 speed.max=100
//% blockGap=10
//% weight=99
export function MoveForward(speed: number): void {
speed = Math.clamp(0, 100, speed)
speed = 511 + speed * 512 / 100
pins.analogWritePin(AnalogPin.P8, speed)
pins.analogWritePin(AnalogPin.P16, speed)
// pins.analogSetPeriod(AnalogPin.P8, 50)
// pins.analogSetPeriod(AnalogPin.P16, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
/**
* Move Backward.
* Speed = 0 - 100
* @param speed selected speed, eg: 50
*/
//% subcategory=Motors
//% blockId=rero-micro-move-backward
//% block="move backward at|%speed|"
//% speed.min=0 speed.max=100
//% blockGap=10
//% weight=98
export function MoveBackward(speed: number): void {
speed = Math.clamp(0, 100, speed)
speed = 512 - speed * 512 / 100
pins.analogWritePin(AnalogPin.P8, speed)
pins.analogWritePin(AnalogPin.P16, speed)
// pins.analogSetPeriod(AnalogPin.P8, 50)
// pins.analogSetPeriod(AnalogPin.P16, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
/**
* Turn to the Left.
* Speed = 0 - 100
* @param speed selected speed, eg: 50
*/
//% subcategory=Motors
//% blockId=rero-micro-turn-left
//% block="turn left at|%speed|"
//% speed.min=0 speed.max=100
//% blockGap=10
//% weight=97
export function TurnLeft(speed: number): void {
speed = Math.clamp(0, 100, speed)
let nLeftSpeed = 512 - speed * 512 / 100
let nRightSpeed = 511 + speed * 512 / 100
pins.analogWritePin(AnalogPin.P8, nLeftSpeed)
pins.analogWritePin(AnalogPin.P16, nRightSpeed)
// pins.analogSetPeriod(AnalogPin.P8, 50)
// pins.analogSetPeriod(AnalogPin.P16, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
/**
* Turn to the Right.
* Speed = 0 - 100
* @param speed selected speed, eg: 50
*/
//% subcategory=Motors
//% blockId=rero-micro-turn-right
//% block="turn right at|%speed|"
//% speed.min=0 speed.max=100
//% blockGap=10
//% weight=96
export function TurnRight(speed: number): void {
speed = Math.clamp(0, 100, speed)
let nLeftSpeed = 511 + speed * 512 / 100
let nRightSpeed = 512 - speed * 512 / 100
pins.analogWritePin(AnalogPin.P8, nLeftSpeed)
pins.analogWritePin(AnalogPin.P16, nRightSpeed)
// pins.analogSetPeriod(AnalogPin.P8, 50)
// pins.analogSetPeriod(AnalogPin.P16, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
/**
* Brake Both Motors.
*/
//% subcategory=Motors
//% blockId=rero-micro-brake block="brake"
//% blockGap=20
//% weight=95
export function Brake(): void {
pins.digitalWritePin(DigitalPin.P12, 0)
pins.analogWritePin(AnalogPin.P8, 512)
pins.analogWritePin(AnalogPin.P16, 512)
}
/**
* Run Motor(s) at selected speed.
* Speed = -100 (reverse) to 100 (forward), 0 to brake.
* @param motor selected motor, eg: Motors.Left
* @param speed selected speed, eg: 50
*/
//% subcategory=Motors
//% blockId=rero-micro-run-motor
//% block="run|%motor| at|%speed|"
//% speed.min=-100 speed.max=100
//% blockGap=10
//% weight=90
export function RunMotor(motor: Motors, speed: number): void {
speed = (100 + speed) * 1024 / 200
speed = Math.clamp(0, 1023, speed)
if (motor == Motors.Left) {
pins.analogWritePin(AnalogPin.P8, speed)
// pins.analogSetPeriod(AnalogPin.P8, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
else if (motor == Motors.Right) {
pins.analogWritePin(AnalogPin.P16, speed)
// pins.analogSetPeriod(AnalogPin.P16, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
else {
pins.analogWritePin(AnalogPin.P8, speed)
pins.analogWritePin(AnalogPin.P16, speed)
// pins.analogSetPeriod(AnalogPin.P8, 50)
// pins.analogSetPeriod(AnalogPin.P16, 50)
pins.digitalWritePin(DigitalPin.P12, 1)
}
}
}