-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.ts
460 lines (403 loc) · 13.8 KB
/
custom.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
/**
* Use this file to define custom functions and blocks.
* Read more at https://makecode.microbit.org/blocks/custom
*/
//% color="#4C97FF" icon="\uf0a4"
//% groups="['Wheel Encoder', 'Navigate', 'Color & Light', ' MPU9250 IMU', 'SMBus']"
namespace OrientBit {
class wheelEnc {
is_enabled: boolean
lpulse_cnt: number
rpulse_cnt: number
lrot_cnt: number
rrot_cnt: number
rot_cnt: number[]
setup (lPort: DigitalPin, rPort: DigitalPin): void {
if(!this.is_enabled) {
this.resetCnt()
pins.setPull(lPort, PinPullMode.PullUp)
pins.setPull(rPort, PinPullMode.PullUp)
this.is_enabled = true
}
}
getCnt(): number[] {
this.rot_cnt = [this.lrot_cnt, this.rrot_cnt]
return this.rot_cnt
}
resetCnt(): void {
this.lrot_cnt = 0
this.rrot_cnt = 0
this.lpulse_cnt = 0
this.rpulse_cnt = 0
}
disableEncoder(): void {
this.is_enabled = false
this.resetCnt()
}
};
let _wheelEnc: wheelEnc = new wheelEnc()
let lPort: DigitalPin
let rPort: DigitalPin
/**
* Count the number of times the wheel encoder pulses
* This function returns an array of 2 numbers which are speeds for left and right motor
*/
//% block="reset wheel rotation count"
//% group="Wheel Encoder"
export function resetWheelRotCnt (): void {
_wheelEnc.resetCnt()
}
//% block="get wheel rotation count"
//% group="Wheel Encoder"
export function getRotationCount (): number[] {
return _wheelEnc.getCnt()
}
//% block="disable encoders"
//% group="Wheel Encoder"
export function disableEncoders ():void {
_wheelEnc.disableEncoder()
}
//% block="enable encoder %DigitalPin %DigitalPin"
//% group="Wheel Encoder"
export function enableEncoder (lport: DigitalPin, rport: DigitalPin):void {
_wheelEnc.setup(lport, rport)
}
pins.onPulsed(rPort, PulseValue.High, function rCntr() {
if(_wheelEnc.is_enabled ){
_wheelEnc.rpulse_cnt++
if (_wheelEnc.rpulse_cnt >= 8) {
_wheelEnc.rpulse_cnt = 0
_wheelEnc.rrot_cnt++
}
}
})
pins.onPulsed(lPort, PulseValue.High, function lCntr() {
if(_wheelEnc.is_enabled ){
_wheelEnc.lpulse_cnt++
if (_wheelEnc.lpulse_cnt >= 8) {
_wheelEnc.lpulse_cnt = 0
_wheelEnc.lrot_cnt++
}
}
})
/**
* Orient in the direction of the value specified and move forward..
* @param is the preferred_heading angle to orient to.
* This function returns an array of 2 numbers which are speeds for left and right motor
*/
//% block="set heading direction to %preferred_heading degrees at speed %fwd_speed"
//% group="Navigate"
//% preferred_heading.min=0 preferred_heading.max=359
//% preferred_heading.defl=90
//% fwd_speed.min=28 fwd_speed.max=100
//% fwd_speed.defl=35
export function course_correct (preferred_heading: number, fwd_speed: number): number[] {
big_diff_speed = 30
correction_speed_low = 28
correction_speed_high = 35
fwd_speed_norm = fwd_speed
small_err_bounds = 1
big_err_bounds = 15
while (!(ready)) {
basic.pause(2)
}
get_reading = current_heading
if (Math.abs(get_reading - preferred_heading) > big_err_bounds) {
if (get_reading <= preferred_heading) {
Left_motor_speed = big_diff_speed
Right_motor_speed = 0
} else if (get_reading >= preferred_heading) {
Left_motor_speed = 0
Right_motor_speed = big_diff_speed
}
} else {
if (get_reading <= preferred_heading - small_err_bounds) {
Left_motor_speed = correction_speed_high
Right_motor_speed = correction_speed_low
} else if (get_reading >= preferred_heading + small_err_bounds) {
Left_motor_speed = correction_speed_low
Right_motor_speed = correction_speed_high
} else {
Left_motor_speed = fwd_speed_norm
Right_motor_speed = fwd_speed_norm
}
}
motor_speed_l_r = [Left_motor_speed, Right_motor_speed]
return motor_speed_l_r
}
let sampler = 0
let motor_speed_l_r: number[] = []
let Right_motor_speed = 0
let Left_motor_speed = 0
let current_heading = 0
let get_reading = 0
let ready = false
let big_err_bounds = 0
let small_err_bounds = 0
let fwd_speed_norm = 0
let correction_speed_high = 0
let correction_speed_low = 0
let big_diff_speed = 0
control.inBackground(function () {
while (true) {
for (let index = 0; index <= 4; index++) {
sampler += input.compassHeading()
basic.pause(2)
}
current_heading = sampler / 5
sampler = 0
ready = true
}
})
class mpu9250 {
is_setup: boolean
addr: number
constructor(addr: number) {
this.is_setup = false
this.addr = addr
}
identify(): number {
let result: number = smbus_readNumber(this.addr, 0x75, pins.sizeOf(NumberFormat.UInt8LE), true)
return result
}
getRegAddr(regAddr: number): number {
let result: number = smbus_readNumber(this.addr, regAddr, pins.sizeOf(NumberFormat.UInt8LE), true)
return result
}
setup(): void {
if (this.is_setup) return
this.is_setup = true
this.identify()
smbus_writeByte(this.addr, 0x6B, 0x80)
smbus_writeByte(this.addr, 0x6B, 0x0)
smbus_writeByte(this.addr, 0x1B, 0x3 << 3)
smbus_writeByte(this.addr, 0x1A, 0x0 << 6)
smbus_writeByte(this.addr, 0x6C, 0x00)
/* smbus_writeByte(this.addr, 0x23, 0x70)
smbus_writeByte(this.addr, 0x6A, 0x40)*/
}
light(): number {
return this.raw()[0]
}
xyz(): number[] {
let result: number[] = this.raw()
/*for (let x: number = 0; x < result.length; x++) {
result[x] = result[x] * 255
}*/
return result
}
raw(): number[] {
this.setup()
let result: Buffer = smbus_readBuffer(this.addr, 0x43, pins.sizeOf(NumberFormat.UInt16LE) * 3, true)
return smbus_unpack("bbb", result)
}
};
let _mpu9250: mpu9250 = new mpu9250(0x68)
//% blockId=MINTGenieBit_get_regVal
//% block="get Reg Val at %regAddr"
//% group="MPU9250 IMU"
//% subcategory="Expert"
export function getVal(regAddr: number): number {
return (_mpu9250.getRegAddr(regAddr))
}
//% blockId=MINTGenieBit_get_identity
//% block="get ident"
//% group="MPU9250 IMU"
export function getident(): number {
return (_mpu9250.identify())
}
/**
* Get the amount of red the colour sensor sees
*/
//% blockId=MINTGenieBit_getGyroX
//% block="get GyroX"
//% group="MPU9250 IMU"
export function getGyroX(): number {
return (_mpu9250.xyz()[0])
}
/**
* Get the amount of green the colour sensor sees
*/
//% blockId=MINTGenieBit_getGyroY
//% block="get GyroY"
//% group="MPU9250 IMU"
export function getGyroY(): number {
return (_mpu9250.xyz()[1])
}
/**
* Get the amount of blue the colour sensor sees
*/
//% blockId=MINTGenieBit_get_GyroZ
//% block="get GyroZ"
//% group="MPU9250 IMU"
export function getGyroZ(): number {
return (_mpu9250.xyz()[2])
}
class tcs34725 {
is_setup: boolean
addr: number
constructor(addr: number) {
this.is_setup = false
this.addr = addr
}
setup(): void {
if (this.is_setup) return
this.is_setup = true
smbus_writeByte(this.addr, 0x80, 0x03)
smbus_writeByte(this.addr, 0x81, 0x2b)
}
setIntegrationTime(time: number): void {
this.setup()
time = Math.clamp(0, 255, time * 10 / 24)
smbus_writeByte(this.addr, 0x81, 255 - time)
}
light(): number {
return this.raw()[0]
}
rgb(): number[] {
let result: number[] = this.raw()
let clear: number = result.shift()
for (let x: number = 0; x < result.length; x++) {
result[x] = result[x] * 255 / clear
}
return result
}
raw(): number[] {
this.setup()
let result: Buffer = smbus_readBuffer(this.addr, 0xb4, pins.sizeOf(NumberFormat.UInt16LE) * 4, false)
return smbus_unpack("HHHH", result)
}
}
let _tcs34725: tcs34725 = new tcs34725(0x29)
/**
* Get the light level
*/
//% blockId=MINTGenieBit_get_light_clear
//% block="get light"
//% group="Colour & Light"
export function getLight(): number {
return Math.round(_tcs34725.light())
}
/**
* Get the amount of red the colour sensor sees
*/
//% blockId=MINTGenieBit_get_light_red
//% block="get red"
//% group="Colour & Light"
export function getRed(): number {
return Math.round(_tcs34725.rgb()[0])
}
/**
* Get the amount of green the colour sensor sees
*/
//% blockId=MINTGenieBit_get_light_green
//% block="get green"
//% group="Colour & Light"
export function getGreen(): number {
return Math.round(_tcs34725.rgb()[1])
}
/**
* Set the integration time of the colour sensor in ms
*/
//% blockId=MINTGenieBit_set_integration_time
//% block="set colour integration time %time ms"
//% time.min=0 time.max=612 value.defl=500
//% group="Colour & Light"
//% subcategory="Expert"
//% weight=90
export function setColourIntegrationTime(time: number): void {
return _tcs34725.setIntegrationTime(time)
}
/**
* Get the amount of blue the colour sensor sees
*/
//% blockId=MINTGenieBit_get_light_blue
//% block="get blue"
//% group="Colour & Light"
export function getBlue(): number {
return Math.round(_tcs34725.rgb()[2])
}
/**
* SMBus functions
*/
//% block="create Buffer of size %size"
//% group="SMBus"
//% subcategory="Expert"
//% weight=80
export function createBuf(sz: number):Buffer {
let temp = pins.createBuffer(sz + 1);
return temp
}
//% block="write to Device addr %addr at Reg address %register a value %value"
//% group="SMBus"
//% subcategory="Expert"
//% weight=40
export function smbus_writeByte(addr: number, register: number, value: number): void {
let temp = pins.createBuffer(2);
temp[0] = register;
temp[1] = value;
pins.i2cWriteBuffer(addr, temp, false);
}
//% block="write to Device addr %addr at Reg address %register a buffer %value"
//% group="SMBus"
//% subcategory="Expert"
//% weight=30
export function smbus_writeBuffer(addr: number, register: number, value: Buffer): void {
let temp = pins.createBuffer(value.length + 1);
temp[0] = register;
for (let x = 0; x < value.length; x++) {
temp[x + 1] = value[x];
}
pins.i2cWriteBuffer(addr, temp, false);
}
//% block="read buffer from Device addr %addr at Reg address %register with length %fmt - using repeat start %repeat_start"
//% repeat_start.defl=true
//% group="SMBus"
//% subcategory="Expert"
//% weight=20
export function smbus_readBuffer(addr: number, register: number, len: number, repeat_start: boolean): Buffer {
let temp = pins.createBuffer(1);
temp[0] = register;
pins.i2cWriteBuffer(addr, temp, repeat_start);
return pins.i2cReadBuffer(addr, len, false);
}
//% block="read value from Device addr %addr at Reg address %register in format %fmt - using repeat start %repeat_start"
//% repeat_start.defl=true
//% group="SMBus"
//% subcategory="Expert"
//% weight=10
export function smbus_readNumber(addr: number, register: number, fmt: NumberFormat = NumberFormat.UInt8LE, repeat_start: boolean): number {
let temp = pins.createBuffer(1);
temp[0] = register;
pins.i2cWriteBuffer(addr, temp, repeat_start);
return pins.i2cReadNumber(addr, fmt, false);
}
function smbus_unpack(fmt: string, buf: Buffer): number[] {
let le: boolean = true;
let offset: number = 0;
let result: number[] = [];
let num_format: NumberFormat = 0;
for (let c = 0; c < fmt.length; c++) {
switch (fmt.charAt(c)) {
case '<':
le = true;
continue;
case '>':
le = false;
continue;
case 'c':
case 'B':
num_format = le ? NumberFormat.UInt8LE : NumberFormat.UInt8BE; break;
case 'b':
num_format = le ? NumberFormat.Int8LE : NumberFormat.Int8BE; break;
case 'H':
num_format = le ? NumberFormat.UInt16LE : NumberFormat.UInt16BE; break;
case 'h':
num_format = le ? NumberFormat.Int16LE : NumberFormat.Int16BE; break;
}
result.push(buf.getNumber(num_format, offset));
offset += pins.sizeOf(num_format);
}
return result;
}
}