-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
1231 lines (1132 loc) · 48.2 KB
/
index.js
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const SPI = require('pi-spi');
const FIRMWARE_VERSION_REQUIRED = "1.4.x"; // Make sure the top 2 of 3 numbers match
const spi = SPI.initialize('/dev/spidev0.1');
spi.clockSpeed(500000);
class SensorError extends Error {
constructor(message) {
super(message);
}
}
class IOError extends Error {
constructor(message) {
super(message);
}
}
const BPSPI_MESSAGE_TYPE = {
NONE: 0,
GET_MANUFACTURER : 1,
GET_NAME: 2,
GET_HARDWARE_VERSION: 3,
GET_FIRMWARE_VERSION: 4,
GET_ID: 5,
SET_LED: 6,
GET_VOLTAGE_3V3: 7,
GET_VOLTAGE_5V: 8,
GET_VOLTAGE_9V: 9,
GET_VOLTAGE_VCC: 10,
SET_ADDRESS: 11,
SET_SENSOR_TYPE: 12,
GET_SENSOR_1: 13,
GET_SENSOR_2: 14,
GET_SENSOR_3: 15,
GET_SENSOR_4: 16,
I2C_TRANSACT_1: 17,
I2C_TRANSACT_2: 18,
I2C_TRANSACT_3: 19,
I2C_TRANSACT_4: 20,
SET_MOTOR_POWER: 21,
SET_MOTOR_POSITION: 22,
SET_MOTOR_POSITION_KP: 23,
SET_MOTOR_POSITION_KD: 24,
SET_MOTOR_DPS: 25,
SET_MOTOR_DPS_KP: 26,
SET_MOTOR_DPS_KD: 27,
SET_MOTOR_LIMITS: 28,
OFFSET_MOTOR_ENCODER: 29,
GET_MOTOR_A_ENCODER: 30,
GET_MOTOR_B_ENCODER: 31,
GET_MOTOR_C_ENCODER: 32,
GET_MOTOR_D_ENCODER: 33,
GET_MOTOR_A_STATUS: 34,
GET_MOTOR_B_STATUS: 35,
GET_MOTOR_C_STATUS: 36,
GET_MOTOR_D_STATUS: 37,
};
/**
* Set the SPI address of the BrickPi3
*
* @param {number} address the new SPI address to use (1 to 255)
* @param {string} id the BrickPi3's unique serial number ID (so that the address can be set while multiple BrickPi3s are stacked on a Raspberry Pi).
* @return {Promise}
*/
function set_address(address, id) {
return new Promise((resolve, reject) => {
address = parseInt(address);
if (typeof id === 'undefined') id = '';
if (address < 1 || address > 255) {
throw new IOError('brickpi3.set_address error: SPI address must be in the range of 1 to 255')
}
let id_arr;
if (id.length !== 32) {
if (id === '') {
id = '00000000000000000000000000000000';
} else {
throw new IOError('brickpi3.set_address error: wrong serial number id length. Must be a 32-digit hex string.');
}
}
// noinspection JSCheckFunctionSignatures
id_arr = Buffer.from(id, "hex");
if (id_arr.byteLength !== 16) {
throw new IOError('brickpi3.set_address error: unknown serial number id problem. Make sure to use a valid 32-digit hex string serial number.');
}
const buffer = Buffer.from([0, BPSPI_MESSAGE_TYPE.SET_ADDRESS, address, ...id_arr]);
spi.transfer(buffer, (e, responseBuffer) => {
if (e) {
reject(e);
}
resolve(responseBuffer);
});
});
}
/**
* Do any necessary configuration, and optionally detect the BrickPi3
*
* Optionally specify the SPI address as something other than 1
* Optionally disable the detection of the BrickPi3 hardware. This can be used for debugging and testing when the BrickPi3 would otherwise not pass the detection tests.
*
* @param address
* @constructor
*/
function BrickPi3(address = 1) {
this.PORT_1 = 0x01;
this.PORT_2 = 0x02;
this.PORT_3 = 0x04;
this.PORT_4 = 0x08;
this.PORT_A = 0x01;
this.PORT_B = 0x02;
this.PORT_C = 0x04;
this.PORT_D = 0x08;
this.MOTOR_FLOAT = -128;
this.SensorType = [0, 0, 0, 0];
this.I2CInBytes = [0, 0, 0, 0];
this.BPSPI_MESSAGE_TYPE = BPSPI_MESSAGE_TYPE;
this.SENSOR_TYPE = {
NONE: 1,
I2C: 2,
CUSTOM: 3,
TOUCH: 4,
NXT_TOUCH: 5,
EV3_TOUCH: 6,
NXT_LIGHT_ON: 7,
NXT_LIGHT_OFF: 8,
NXT_COLOR_RED: 9,
NXT_COLOR_GREEN: 10,
NXT_COLOR_BLUE: 11,
NXT_COLOR_FULL: 12,
NXT_COLOR_OFF: 13,
NXT_ULTRASONIC: 14,
EV3_GYRO_ABS: 15,
EV3_GYRO_DPS: 16,
EV3_GYRO_ABS_DPS: 17,
EV3_COLOR_REFLECTED: 18,
EV3_COLOR_AMBIENT: 19,
EV3_COLOR_COLOR: 20,
EV3_COLOR_RAW_REFLECTED: 21,
EV3_COLOR_COLOR_COMPONENTS: 22,
EV3_ULTRASONIC_CM: 23,
EV3_ULTRASONIC_INCHES: 24,
EV3_ULTRASONIC_LISTEN: 25,
EV3_INFRARED_PROXIMITY: 26,
EV3_INFRARED_SEEK: 27,
EV3_INFRARED_REMOTE: 28,
};
this.SENSOR_STATE = {
VALID_DATA: 0,
NOT_CONFIGURED: 1,
CONFIGURING: 2,
NO_DATA: 3,
I2C_ERROR: 4
};
// noinspection JSUnusedGlobalSymbols
/**
* Flags for use with SENSOR_TYPE.CUSTOM
*
* PIN1_9V
* Enable 9V out on pin 1 (for LEGO NXT Ultrasonic sensor).
*
* PIN5_OUT
* Set pin 5 state to output. Pin 5 will be set to input if this flag is not set.
*
* PIN5_STATE
* If PIN5_OUT is set, this will set the state to output high, otherwise the state will
* be output low. If PIN5_OUT is not set, this flag has no effect.
*
* PIN6_OUT
* Set pin 6 state to output. Pin 6 will be set to input if this flag is not set.
*
* PIN6_STATE
* If PIN6_OUT is set, this will set the state to output high, otherwise the state will
* be output low. If PIN6_OUT is not set, this flag has no effect.
*
* PIN1_ADC
* Enable the analog/digital converter on pin 1 (e.g. for NXT analog sensors).
*
* PIN6_ADC
* Enable the analog/digital converter on pin 6.
*
* @type {{PIN1_9V: number, PIN5_OUT: number, PIN5_STATE: number, PIN6_OUT: number, PIN6_STATE: number, PIN1_ADC: number, PIN6_ADC: number}}
*/
this.SENSOR_CUSTOM = {
PIN1_9V: 0x0002,
PIN5_OUT: 0x0010,
PIN5_STATE: 0x0020,
PIN6_OUT: 0x0100,
PIN6_STATE: 0x0200,
PIN1_ADC: 0x1000,
PIN6_ADC: 0x4000
};
this.SENSOR_I2C_SETTINGS = {
MID_CLOCK: 0x01, // Send the clock pulse between reading and writing. Required by the NXT US sensor.
PIN1_9V: 0x02, // 9v pullup on pin 1
SAME: 0x04, // Keep performing the same transaction e.g. keep polling a sensor
ALLOW_STRETCH_ACK: 3,
ALLOW_STRETCH_ANY: 4
};
// noinspection JSUnusedGlobalSymbols
this.MOTOR_STATUS_FLAG = {
LOW_VOLTAGE_FLOAT: 0x01, //If the motors are floating due to low battery voltage
OVERLOADED: 0x02, // If the motors aren't close to the target (applies to position control and dps speed control).
};
if (address < 1 || address > 255) {
throw new IOError('error: SPI address must be in the range of 1 to 255');
}
this.SPI_Address = address;
// noinspection JSUnusedGlobalSymbols
this.detect = () => {
return new Promise((resolve, reject) => {
let manufacturer, board, vfw;
this.get_manufacturer().then((value) => {
manufacturer = value;
return this.get_board();
}).then((value) => {
board = value;
return this.get_version_firmware();
}).then((value) => {
vfw = value;
if (manufacturer !== 'Dexter Industries' || board !== 'BrickPi3') {
reject('No SPI response');
} else if (vfw.split('.')[0] !== FIRMWARE_VERSION_REQUIRED.split('.')[0] || vfw.split('.')[1] !== FIRMWARE_VERSION_REQUIRED.split('.')[0]) {
reject('BrickPi3 firmware needs to be version ' + FIRMWARE_VERSION_REQUIRED + ' but is currently version ' + vfw);
} else {
resolve(true);
}
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Conduct a SPI transaction
*
* @param {Array} data_out a list of bytes to send. The length of the list will determine how many bytes are transferred.
* @return {Promise.<Buffer>}
*/
this.spi_transfer_array = (data_out) => {
return new Promise((resolve, reject) => {
spi.transfer(Buffer.from(data_out), (e, responseBuffer) => {
if (e) {
reject(e);
}
let responseArray = [...responseBuffer];
resolve(responseArray);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Send an 8-bit-value over SPI
*
* @param {number<BPSPI_MESSAGE_TYPE>} MessageType
* @param {number} Value
* @return {Promise}
*/
this.spi_write_8 = (MessageType, Value) => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, MessageType, (Value & 0xFF)]).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read a 16-bit value over SPI
*
* @param {number.<BPSPI_MESSAGE_TYPE>} MessageType
* @return {Promise.<number>}
*/
this.spi_read_16 = (MessageType) => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, MessageType, 0, 0, 0, 0]).then((reply) => {
if (reply[3] === 0xA5) {
resolve(parseInt((reply[4] << 8) | reply[5]));
} else {
reject('No SPI response');
}
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Send an 16-bit-value over SPI
*
* @param {number<BPSPI_MESSAGE_TYPE>} MessageType
* @param {number} Value
* @return {Promise}
*/
this.spi_write_16 = (MessageType, Value) => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, MessageType, ((Value >> 8) & 0xFF), (Value & 0xFF)]).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Send an 24-bit-value over SPI
*
* @param {number<BPSPI_MESSAGE_TYPE>} MessageType
* @param {number} Value
* @return {Promise}
*/
this.spi_write_24 = (MessageType, Value) => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, MessageType, ((Value >> 16) & 0xFF), ((Value >> 8) & 0xFF), (Value & 0xFF)]).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read a 32-bit value over SPI
*
* @param {number.<BPSPI_MESSAGE_TYPE>} MessageType
* @return {Promise.<number>}
*/
this.spi_read_32 = (MessageType) => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, MessageType, 0, 0, 0, 0, 0, 0]).then((reply) => {
if (reply[3] === 0xA5) {
resolve(parseInt((reply[4] << 24) | (reply[5] << 16) | (reply[6] << 8) | reply[7]));
} else {
reject('No SPI response');
}
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Send an 32-bit-value over SPI
*
* @param {number<BPSPI_MESSAGE_TYPE>} MessageType
* @param {number} Value
* @return {Promise}
*/
this.spi_write_32 = (MessageType, Value) => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, MessageType, ((Value >> 24) & 0xFF), ((Value >> 16) & 0xFF), ((Value >> 8) & 0xFF), (Value & 0xFF)]).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read the 20 character BrickPi3 manufacturer name
*
* @return {Promise.<string>} BrickPi3 manufacturer name string
*/
this.get_manufacturer = () => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, this.BPSPI_MESSAGE_TYPE.GET_MANUFACTURER, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).then((reply) => {
if (reply[3] === 0xA5) {
let name = '';
for (let i = 4; i <= 24; i++) {
if (reply[i] === 0x00) break;
name += String.fromCharCode(reply[i]);
}
resolve(name);
}
reject('No SPI response');
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read the 20 character BrickPi3 board name
*
* @return {Promise.<string>} BrickPi3 board name string
*/
this.get_board = () => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, this.BPSPI_MESSAGE_TYPE.GET_NAME, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).then((reply) => {
if (reply[3] === 0xA5) {
let name = '';
for (let i = 4; i <= 24; i++) {
if (reply[i] === 0x00) break;
name += String.fromCharCode(reply[i]);
}
resolve(name);
}
reject('No SPI response');
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read the hardware version
*
* @return {Promise.<String>} hardware version
*/
this.get_version_hardware = () => {
return new Promise((resolve, reject) => {
this.spi_read_32(this.BPSPI_MESSAGE_TYPE.GET_HARDWARE_VERSION).then((version) => {
resolve(Math.round(version / 1000000) + '.' + (Math.round(version / 1000) % 1000) + '.' + (version % 1000));
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read the firmware version
*
* @return {Promise.<String>} firmware version
*/
this.get_version_firmware = () => {
return new Promise((resolve, reject) => {
this.spi_read_32(this.BPSPI_MESSAGE_TYPE.GET_FIRMWARE_VERSION).then((version) => {
resolve(Math.round(version / 1000000) + '.' + (Math.round(version / 1000) % 1000) + '.' + (version % 1000));
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read the 128-bit BrickPi hardware serial number
*
* @return {Promise.<string>} serial number as 32 char HEX formatted string
*/
this.get_id = () => {
return new Promise((resolve, reject) => {
this.spi_transfer_array([this.SPI_Address, this.BPSPI_MESSAGE_TYPE.GET_ID, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).then((reply) => {
if (reply[3] === 0xA5) {
let id = '';
for (let i = 4; i <= 19; i++) {
const char = reply[i].toString(16);
id += (char.length < 2 ? '0' : '') + char;
}
resolve(id);
}
reject('No SPI response');
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Control the onboard LED
*
* @param {number} value the value (in percent) to set the LED brightness to. -1 returns control of the LED to the firmware.
* @return {Promise}
*/
this.set_led = (value) => {
return this.spi_write_8(this.BPSPI_MESSAGE_TYPE.SET_LED, value);
};
// noinspection JSUnusedGlobalSymbols
/**
* Get the 3.3v circuit voltage
*
* @return {Promise.<number>} 3.3v circuit voltage
*/
this.get_voltage_3v3 = () => {
return new Promise((resolve, reject) => {
this.spi_read_16(this.BPSPI_MESSAGE_TYPE.GET_VOLTAGE_3V3).then((value) => {
return value / 1000;
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Get the 5v circuit voltage
*
* @return {Promise.<number>} 5v circuit voltage
*/
this.get_voltage_5v = () => {
return new Promise((resolve, reject) => {
this.spi_read_16(this.BPSPI_MESSAGE_TYPE.GET_VOLTAGE_5V).then((value) => {
return value / 1000;
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Get the 9v circuit voltage
*
* @return {Promise.<number>} 9v circuit voltage
*/
this.get_voltage_9v = () => {
return new Promise((resolve, reject) => {
this.spi_read_16(this.BPSPI_MESSAGE_TYPE.GET_VOLTAGE_9V).then((value) => {
return value / 1000;
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Get the battery voltage
*
* @return {Promise.<number>} battery voltage
*/
this.get_voltage_battery = () => {
return new Promise((resolve, reject) => {
this.spi_read_16(this.BPSPI_MESSAGE_TYPE.GET_VOLTAGE_VCC).then((value) => {
return value / 1000;
}).catch((err) => {
reject(err);
});
});
};
this.wait_until_configuration_is_finished = (args, configurationTimeout) => {
configurationTimeout = configurationTimeout || 3000;
return new Promise(async (resolve) => {
let run = true;
const timeout = setTimeout( () => {
const msg = `timeout reached: sensor configuration not successfully within ${configurationTimeout}ms`;
run = false;
throw new Error(msg);
}, configurationTimeout);
while(run) {
const reply = await this.spi_transfer_array(args);
if(reply[5] === this.SENSOR_STATE.VALID_DATA){
clearTimeout(timeout);
return resolve(reply);
}
}
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Set the sensor type.
*
* params is used for the following sensor types:
* CUSTOM -- a 16-bit integer used to configure the hardware.
* I2C -- a list of settings:
* params[0] -- Settings/flags
* params[1] -- target Speed in microseconds (0-255). Realistically the speed will vary.
* if SENSOR_I2C_SETTINGS_SAME flag set in I2C Settings:
* params[2] -- Delay in microseconds between transactions.
* params[3] -- Address
* params[4] -- List of bytes to write
* params[5] -- Number of bytes to read
*
* @param {number} port The sensor port(s). PORT_1, PORT_2, PORT_3, and/or PORT_4.
* @param {number} type The sensor type
* @param {*} params the parameters needed for some sensor types.
* @return {Promise}
*/
this.set_sensor_type = (port, type, params = 0) => {
return new Promise((resolve, reject) => {
for (let p = 0; p < 4; p++) {
// noinspection JSBitwiseOperatorUsage
if (port & (1 << p)) {
this.SensorType[p] = type;
}
}
let outArray;
if (type === this.SENSOR_TYPE.CUSTOM) {
outArray = [this.SPI_Address, this.BPSPI_MESSAGE_TYPE.SET_SENSOR_TYPE, parseInt(port), type, ((params[0] >> 8) & 0xFF), (params[0] & 0xFF)];
} else if (type === this.SENSOR_TYPE.I2C) {
if (params instanceof Array && params.length >= 2) {
outArray = [this.SPI_Address, this.BPSPI_MESSAGE_TYPE.SET_SENSOR_TYPE, parseInt(port), type, params[0], params[1]];
if (params[0] & this.SENSOR_I2C_SETTINGS.SAME && params.length >= 6) {
outArray.push((params[2] >> 24) & 0xFF);
outArray.push((params[2] >> 16) & 0xFF);
outArray.push((params[2] >> 8) & 0xFF) ;
outArray.push(params[2] & 0xFF);
outArray.push(params[3] & 0xFF);
outArray.push(params[5] & 0xFF);
for (let p = 0; p < 4; p++) {
// noinspection JSBitwiseOperatorUsage
if (port & (1 << p)) {
this.I2CInBytes[p] = params[5] & 0xFF;
}
}
outArray.push(params[4].length);
for (let i = 0; i < params[4].length; i++) {
outArray.push(params[4][i]);
}
}
}
} else {
outArray = [this.SPI_Address, this.BPSPI_MESSAGE_TYPE.SET_SENSOR_TYPE, parseInt(port), type];
}
this.spi_transfer_array(outArray).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Conduct an I2C transaction
*
* @param {number} port The sensor port (one at a time). PORT_1, PORT_2, PORT_3, or PORT_4.
* @param {number} Address The I2C address for the device. Bits 1-7, not 0-6.
* @param {Array.<number>} OutArray A list of bytes to write to the device
* @param {number} InBytes The number of bytes to read from the device
*
* @return {Promise}
*/
this.transact_i2c = (port, Address, OutArray, InBytes) => {
return new Promise((resolve, reject) => {
let message_type, port_index;
if (port === this.PORT_1) {
message_type = this.BPSPI_MESSAGE_TYPE.I2C_TRANSACT_1;
port_index = 0;
} else if (port === this.PORT_2) {
message_type = this.BPSPI_MESSAGE_TYPE.I2C_TRANSACT_2;
port_index = 1;
} else if (port === this.PORT_3) {
message_type = this.BPSPI_MESSAGE_TYPE.I2C_TRANSACT_3;
port_index = 2;
} else if (port === this.PORT_4) {
message_type = this.BPSPI_MESSAGE_TYPE.I2C_TRANSACT_4;
port_index = 3;
} else {
throw new IOError('transact_i2c error. Must be one sensor port at a time. PORT_1, PORT_2, PORT_3, or PORT_4.');
}
if (this.SensorType[port_index] !== this.SENSOR_TYPE.I2C) {
reject();
return;
}
let outArray = [this.SPI_Address, message_type, Address, InBytes];
this.I2CInBytes[port_index] = InBytes;
let OutBytes = OutArray.length;
if (OutBytes > 16) {
outArray.push(16);
for (let i = 0; i < 16; i++) {
outArray.push(OutArray[i]);
}
} else {
outArray.push(OutBytes);
for (let i = 0; i < OutBytes; i++) {
outArray.push(OutArray[i]);
}
}
this.spi_transfer_array(outArray).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Read a sensor value
*
* Returns the value(s) for the specified sensor.
* The following sensor types each return a single value:
* NONE ----------------------- 0
* TOUCH ---------------------- 0 or 1 (released or pressed)
* NXT_TOUCH ------------------ 0 or 1 (released or pressed)
* EV3_TOUCH ------------------ 0 or 1 (released or pressed)
* NXT_ULTRASONIC ------------- distance in CM
* NXT_LIGHT_ON -------------- reflected light
* NXT_LIGHT_OFF -------------- ambient light
* NXT_COLOR_RED -------------- red reflected light
* NXT_COLOR_GREEN ------------ green reflected light
* NXT_COLOR_BLUE ------------- blue reflected light
* NXT_COLOR_OFF -------------- ambient light
* EV3_GYRO_ABS --------------- absolute rotation position in degrees
* EV3_GYRO_DPS --------------- rotation rate in degrees per second
* EV3_COLOR_REFLECTED -------- red reflected light
* EV3_COLOR_AMBIENT ---------- ambient light
* EV3_COLOR_COLOR ------------ detected color
* EV3_ULTRASONIC_CM ---------- distance in CM
* EV3_ULTRASONIC_INCHES ------ distance in inches
* EV3_ULTRASONIC_LISTEN ------ 0 or 1 (no other ultrasonic sensors or another ultrasonic sensor detected)
* EV3_INFRARED_PROXIMITY ----- distance 0-100%
*
* The following sensor types each return a list of values
* CUSTOM --------------------- Pin 1 ADC (5v scale from 0 to 4095), Pin 6 ADC (3.3v scale from 0 to 4095), Pin 5 digital, Pin 6 digital
* I2C ------------------------ the I2C bytes read
* NXT_COLOR_FULL ------------- detected color, red light reflected, green light reflected, blue light reflected, ambient light
* EV3_GYRO_ABS_DPS ----------- absolute rotation position in degrees, rotation rate in degrees per second
* EV3_COLOR_RAW_REFLECTED ---- red reflected light, unknown value (maybe a raw ambient value?)
* EV3_COLOR_COLOR_COMPONENTS - red reflected light, green reflected light, blue reflected light, unknown value (maybe a raw value?)
* EV3_INFRARED_SEEK ---------- a list for each of the four channels. For each channel heading (-25 to 25), distance (-128 or 0 to 100)
* EV3_INFRARED_REMOTE -------- a list for each of the four channels. For each channel red up, red down, blue up, blue down, boadcast
*
* @param {number} port The sensor port (one at a time). PORT_1, PORT_2, PORT_3, or PORT_4.
* @return {Promise.<number|Array.<number>>}
*/
this.get_sensor = (port, timeLimit) => {
return new Promise((resolve, reject) => {
let message_type, port_index;
if (port === this.PORT_1) {
message_type = this.BPSPI_MESSAGE_TYPE.GET_SENSOR_1;
port_index = 0;
} else if (port === this.PORT_2) {
message_type = this.BPSPI_MESSAGE_TYPE.GET_SENSOR_2;
port_index = 1;
} else if (port === this.PORT_3) {
message_type = this.BPSPI_MESSAGE_TYPE.GET_SENSOR_3;
port_index = 2;
} else if (port === this.PORT_4) {
message_type = this.BPSPI_MESSAGE_TYPE.GET_SENSOR_4;
port_index = 3;
} else {
throw new IOError('get_sensor error. Must be one sensor port at a time. PORT_1, PORT_2, PORT_3, or PORT_4.');
}
if (this.SensorType[port_index] === this.SENSOR_TYPE.CUSTOM) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
resolve([(((reply[8] & 0x0F) << 8) | reply[9]), (((reply[8] >> 4) & 0x0F) | (reply[7] << 4)), (reply[6] & 0x01), ((reply[6] >> 1) & 0x01)]);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if (this.SensorType[port_index] === this.SENSOR_TYPE.I2C) {
let outArray = [this.SPI_Address, message_type, 0, 0, 0, 0];
for (let i = 0; i < this.I2CInBytes[port_index]; i++) {
outArray.push(0);
}
this.wait_until_configuration_is_finished(outArray, timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA && reply.length - 6 === this.I2CInBytes[port_index]) {
let values = [];
for (let i = 6; i < reply.length; i++) {
values.push(reply[i]);
}
resolve(values);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if ([
this.SENSOR_TYPE.TOUCH,
this.SENSOR_TYPE.NXT_TOUCH,
this.SENSOR_TYPE.EV3_TOUCH,
this.SENSOR_TYPE.NXT_ULTRASONIC,
this.SENSOR_TYPE.EV3_COLOR_REFLECTED,
this.SENSOR_TYPE.EV3_COLOR_AMBIENT,
this.SENSOR_TYPE.EV3_COLOR_COLOR,
this.SENSOR_TYPE.EV3_ULTRASONIC_LISTEN,
this.SENSOR_TYPE.EV3_INFRARED_PROXIMITY
].indexOf(this.SensorType[port_index]) > -1) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if ((reply[4] === this.SensorType[port_index] || (this.SensorType[port_index] === this.SENSOR_TYPE.TOUCH && (reply[4] === this.SENSOR_TYPE.NXT_TOUCH || reply[4] === this.SENSOR_TYPE.EV3_TOUCH))) && reply[5] === this.SENSOR_STATE.VALID_DATA) {
resolve(reply[6]);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if (this.SensorType[port_index] === this.SENSOR_TYPE.NXT_COLOR_FULL) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
resolve([reply[6], ((reply[7] << 2) | ((reply[11] >> 6) & 0x03)), ((reply[8] << 2) | ((reply[11] >> 4) & 0x03)), ((reply[9] << 2) | ((reply[11] >> 2) & 0x03)), ((reply[10] << 2) | (reply[11] & 0x03))]);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if ([
this.SENSOR_TYPE.NXT_LIGHT_ON,
this.SENSOR_TYPE.NXT_LIGHT_OFF,
this.SENSOR_TYPE.NXT_COLOR_RED,
this.SENSOR_TYPE.NXT_COLOR_GREEN,
this.SENSOR_TYPE.NXT_COLOR_BLUE,
this.SENSOR_TYPE.NXT_COLOR_OFF,
this.SENSOR_TYPE.EV3_GYRO_ABS,
this.SENSOR_TYPE.EV3_GYRO_DPS,
this.SENSOR_TYPE.EV3_ULTRASONIC_CM,
this.SENSOR_TYPE.EV3_ULTRASONIC_INCHES
].indexOf(this.SensorType[port_index]) > -1) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
let value = parseInt((reply[6] << 8) | reply[7]);
if (this.SensorType[port_index] === this.SENSOR_TYPE.EV3_ULTRASONIC_CM || this.SensorType[port_index] === this.SENSOR_TYPE.EV3_ULTRASONIC_INCHES) {
value = value / 10;
}
resolve(value);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if ([
this.SENSOR_TYPE.EV3_COLOR_RAW_REFLECTED,
this.SENSOR_TYPE.EV3_GYRO_ABS_DPS
].indexOf(this.SensorType[port_index]) > -1) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
resolve([parseInt((reply[6] << 8) | reply[7]), parseInt((reply[8] << 8) | reply[9])]);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if (this.SensorType[port_index] === this.SENSOR_TYPE.EV3_COLOR_COLOR_COMPONENTS) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
resolve([parseInt((reply[6] << 8) | reply[7]), parseInt((reply[8] << 8) | reply[9]), parseInt((reply[10] << 8) | reply[11]), parseInt((reply[12] << 8) | reply[13])]);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if (this.SensorType[port_index] === this.SENSOR_TYPE.EV3_INFRARED_SEEK) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
resolve([[parseInt(reply[6]), parseInt(reply[7])], [parseInt(reply[8]), parseInt(reply[9])], [parseInt(reply[10]), parseInt(reply[11])], [parseInt(reply[12]), parseInt(reply[13])]]);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else if (this.SensorType[port_index] === this.SENSOR_TYPE.EV3_INFRARED_REMOTE) {
this.wait_until_configuration_is_finished([this.SPI_Address, message_type, 0, 0, 0, 0, 0, 0, 0, 0], timeLimit).then((reply) => {
if (reply[3] === 0xA5) {
if (reply[4] === this.SensorType[port_index] && reply[5] === this.SENSOR_STATE.VALID_DATA) {
let results = [0, 0, 0, 0];
for (let r = 0; r < results.length; r++) {
let value = parseInt(reply[6 + r]);
if (value === 1) {
results[r] = [1, 0, 0, 0, 0];
} else if (value === 2) {
results[r] = [0, 1, 0, 0, 0];
} else if (value === 3) {
results[r] = [0, 0, 1, 0, 0];
} else if (value === 4) {
results[r] = [0, 0, 0, 1, 0];
} else if (value === 5) {
results[r] = [1, 0, 1, 0, 0];
} else if (value === 6) {
results[r] = [1, 0, 0, 1, 0];
} else if (value === 7) {
results[r] = [0, 1, 1, 0, 0];
} else if (value === 8) {
results[r] = [0, 1, 0, 1, 0];
} else if (value === 9) {
results[r] = [0, 0, 0, 0, 1];
} else if (value === 10) {
results[r] = [1, 1, 0, 0, 0];
} else if (value === 11) {
results[r] = [0, 0, 1, 1, 0];
} else {
results[r] = [0, 0, 0, 0, 0];
}
}
resolve(results);
} else {
throw new SensorError('get_sensor error: Invalid sensor data');
}
} else {
throw new IOError('get_sensor error: No SPI response');
}
}).catch((err) => {
reject(err);
});
} else {
throw new IOError('get_sensor error: Sensor not configured or not supported.');
}
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Set the motor power in percent
*
* @param {number} port The Motor port(s). PORT_A, PORT_B, PORT_C, and/or PORT_D.