-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRIZM.cpp
1455 lines (1115 loc) · 43.6 KB
/
PRIZM.cpp
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
/* TETRIX PRIZM robotics controller cpp File for PRIZM Arduino Library
Written by: Paul W. Uttley
01/03/2018
Version 3.0
=============== Added support for DC and Servo Motor EXPANSIONansion Controllers
Default address for DC box is 1. Default for Servo box is 2.
Additional boxes can be added in a daisy chain, and if so the addresses will need to be set different
Valid address range for PRIZM to support is address 1 - 4.
=============== Version 3 - fixed minor servo jitter related to PRIZM version 1 firmware ===============
A workaround to fix is to change each servo channel position update to always update all servo channels with one I2C transaction.
*/
#include <Arduino.h>
#include <avr/wdt.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <Wire.h>
#include "PRIZM.h"
//#include "utility/WSWire.h"
void EXPANSION::controllerEnable(int address){
Wire.beginTransmission(address); // Send an "Enable" Byte to EXPANSION controller at address/ID
Wire.write(0x25);
Wire.endTransmission();
delay(25);
}
void EXPANSION::controllerReset(int address){
Wire.beginTransmission(address); // Send an "reset" Byte to EXPANSION controller at address/ID
Wire.write(0x27);
Wire.endTransmission();
delay(25);
}
void EXPANSION::WDT_STOP (int address){ //===== This forces a watch dog timer HARD STOP on the expansion controller at address #
Wire.beginTransmission(address);
Wire.write(0x23);
Wire.endTransmission();
delay(50);
}
void EXPANSION::setExpID(int newID){ // === command to change ID/ I2C address of a TETRIX Expansion DC or Servo Controller.
// There can only be one expansion box connected to PRIZM when changing ID's.
int oldID; // No other I2C devices can be connected to sensor ports when executing this command.
for (int i = 1; i < 120; i++) { // Spin up I2C addresses from 1 - 120
Wire.beginTransmission (i);
if ((Wire.endTransmission () == 0) && ((i < 5) || (i > 6))) // Capture response from expansion controller (ignore 5 and 6 - they are used by PRIZM)
{
oldID = i;
delay (10);
}
}
Wire.beginTransmission(oldID); // Send new ID/address to the found Expansion Controller
Wire.write(0x24);
Wire.write(newID);
Wire.endTransmission();
delay(10);
pinMode(6, OUTPUT); //===== RED LED is on pin 6
digitalWrite(6, HIGH); // Flash PRIZM Red LED when finished
delay(250);
digitalWrite(6, LOW);
delay(250);
digitalWrite(6, HIGH);
delay(250);
digitalWrite(6, LOW);
}
int EXPANSION::readExpID(){ // === command to get the I2C address / ID from a TETRIX DC or Servo Expansion Controller
// There can only be one expansion box connected to PRIZM when using this function
int ID; // All other I2C devices must also be disconnected from sensor ports when using this function
for (int i = 1; i < 120; i++) { // Spin up I2C addresses from 1 - 120
Wire.beginTransmission (i);
if ((Wire.endTransmission () == 0) && ((i < 5) || (i > 6))) // Capture response from expansion controller (ignore 5 and 6 - they are used by PRIZM)
{
ID = i;
delay (10);
}
}
return ID;
}
void PRIZM::setMotorSpeedPID(int P, int I, int D){ //=== Change the speed PID parameters for DC Chip
int lobyteP;
int hibyteP;
int lobyteI;
int hibyteI;
int lobyteD;
int hibyteD;
lobyteP = lowByte(P);
hibyteP = highByte(P);
lobyteI = lowByte(I);
hibyteI = highByte(I);
lobyteD = lowByte(D);
hibyteD = highByte(D);
Wire.beginTransmission(5);
Wire.write(0X56);
Wire.write(hibyteP);
Wire.write(lobyteP);
Wire.write(hibyteI);
Wire.write(lobyteI);
Wire.write(hibyteD);
Wire.write(lobyteD);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorSpeedPID(int address, int P, int I, int D){ //=== Change the speed PID parameters for DC EXPANSIONANSION
int lobyteP;
int hibyteP;
int lobyteI;
int hibyteI;
int lobyteD;
int hibyteD;
lobyteP = lowByte(P);
hibyteP = highByte(P);
lobyteI = lowByte(I);
hibyteI = highByte(I);
lobyteD = lowByte(D);
hibyteD = highByte(D);
Wire.beginTransmission(address);
Wire.write(0X56);
Wire.write(hibyteP);
Wire.write(lobyteP);
Wire.write(hibyteI);
Wire.write(lobyteI);
Wire.write(hibyteD);
Wire.write(lobyteD);
Wire.endTransmission();
delay(10);
}
void PRIZM::setMotorTargetPID(int P, int I, int D){ //=== Change the target PID parameters for DC chip
int lobyteP;
int hibyteP;
int lobyteI;
int hibyteI;
int lobyteD;
int hibyteD;
lobyteP = lowByte(P);
hibyteP = highByte(P);
lobyteI = lowByte(I);
hibyteI = highByte(I);
lobyteD = lowByte(D);
hibyteD = highByte(D);
Wire.beginTransmission(5); //transmit to DC address
Wire.write(0X57);
Wire.write(hibyteP);
Wire.write(lobyteP);
Wire.write(hibyteI);
Wire.write(lobyteI);
Wire.write(hibyteD);
Wire.write(lobyteD);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorTargetPID(int address, int P, int I, int D){ //=== Change the target PID parameters for DC EXPANSIONANSION
int lobyteP;
int hibyteP;
int lobyteI;
int hibyteI;
int lobyteD;
int hibyteD;
lobyteP = lowByte(P);
hibyteP = highByte(P);
lobyteI = lowByte(I);
hibyteI = highByte(I);
lobyteD = lowByte(D);
hibyteD = highByte(D);
Wire.beginTransmission(address);
Wire.write(0X57);
Wire.write(hibyteP);
Wire.write(lobyteP);
Wire.write(hibyteI);
Wire.write(lobyteI);
Wire.write(hibyteD);
Wire.write(lobyteD);
Wire.endTransmission();
delay(10);
}
int PRIZM::readDCFirmware(){ //==== Request firmware version of DC motor chip
int byte1;
int DCversion;
Wire.beginTransmission(5);
Wire.write(0x26);
Wire.endTransmission();
delay(10);
Wire.requestFrom(5, 1);
byte1 = Wire.read();
DCversion=byte1;
delay(10);
return DCversion;
}
int EXPANSION::readDCFirmware(int address){ //==== Request firmware version of DC EXPANSIONANSION
int byte1;
int DCversion;
Wire.beginTransmission(address);
Wire.write(0x26);
Wire.endTransmission();
delay(10);
Wire.requestFrom(address, 1);
byte1 = Wire.read();
DCversion=byte1;
delay(10);
return DCversion;
}
int PRIZM::readSVOFirmware(){ //==== Request firmware version of Servo motor chip
int byte1;
int SVOversion;
Wire.beginTransmission(6);
Wire.write(0x26);
Wire.endTransmission();
delay(10);
Wire.requestFrom(6, 1);
byte1 = Wire.read();
SVOversion=byte1;
delay(10);
return SVOversion;
}
int EXPANSION::readSVOFirmware(int address){ //==== Request firmware version of Servo EXPANSIONANSION
int byte1;
int SVOversion;
Wire.beginTransmission(address);
Wire.write(0x26);
Wire.endTransmission();
delay(10);
Wire.requestFrom(address, 1);
byte1 = Wire.read();
SVOversion=byte1;
delay(10);
return SVOversion;
}
void PRIZM::PrizmBegin(){ //======= Send a SW reset to all EXPANSIONansion port I2C
Wire.begin();
delay(500); // Give EXPANSION controllers time to reset
// SW reset on Expansion and DC + Servo chips at addresses 5 and 6 (7 is not used)
Wire.beginTransmission(5); // Supported I2C addresses for EXPANSIONansion controllers is 1 - 4 (4 boxes total)
Wire.write(0x27); // If additional boxes above that are added at different addresses, 0x27 writes for each need to be added as well.
Wire.endTransmission(); // No guarantee that more than 4 boxes will work on single I2C bus because of cable capacitance
delay(10);
Wire.beginTransmission(6);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(1);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(2);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(3);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(4);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
setGreenLED(HIGH); // Turn on when we're reset
while(readStartButton()==0){}; // wait for the program start (green) button pressed
setGreenLED(LOW); // turn green off
Wire.beginTransmission(5); // Supported I2C addresses for EXPANSIONansion controllers is 1 - 4 (4 boxes total)
Wire.write(0x27); // Do a reset on Expansions and PRIZM Motor chips after green button start
Wire.endTransmission(); // If additional boxes above that are added at different addresses, 0x27 writes for each need to be added as well.
delay(10); // No guarantee that more than 4 boxes will work on single I2C bus because of cable capacitance
Wire.beginTransmission(6);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(1);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(2);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(3);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(4);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
delay(1000); // 1 second delay between time GO button is pushed and program starts gives time for resets
Wire.beginTransmission(5); // Send an "Enable" Byte to DC and Servo controller chips and EXPANSIONansion controllers
Wire.write(0x25); // enable command so that the robots won't move without a PrizmBegin statement
Wire.endTransmission();
delay(10);
Wire.beginTransmission(6);
Wire.write(0x25);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(1);
Wire.write(0x25);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(2);
Wire.write(0x25);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(3);
Wire.write(0x25);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(4);
Wire.write(0x25);
Wire.endTransmission();
delay(10);
}
void PRIZM::PrizmEnd(){ //======= Send a SW reset to all I2C devices(resets everything) This is done mainly to stop all motors
Wire.beginTransmission(5); // Supported I2C addresses for EXPANSIONansion controllers is 1 - 4
Wire.write(0x27); // 5 and 6 is PRIZM DC and Servo chips
Wire.endTransmission();
delay(10);
Wire.beginTransmission(6);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(1);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(2);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(3);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(4);
Wire.write(0x27);
Wire.endTransmission();
delay(10);
wdt_enable(WDTO_15MS); // set the wdt to timeout after 15ms automatically resets
for(;;)
{
}
}
int PRIZM::readBatteryVoltage(){
int Bvoltage = analogRead(0)*2;
return Bvoltage;
}
int EXPANSION::readBatteryVoltage(int address){
int Bvoltage;
byte byte1;
byte byte2;
Wire.beginTransmission(address);
Wire.write(0x53);
Wire.endTransmission();
delay(10);
Wire.requestFrom(address, 2);
byte1 = Wire.read();
byte2 = Wire.read();
Bvoltage = byte1;
Bvoltage = (Bvoltage*256)+byte2;
delay(10);
return Bvoltage;
}
int PRIZM::readLineSensor(int pin){ // Can sense black or white (follow the edge of a black line on a white surface)
int BWstate; // black or white
pinMode(pin, INPUT);
if(HIGH == digitalRead(pin)){BWstate = 1;} else {BWstate = 0;}
return BWstate;
}
int PRIZM::readSonicSensorCM(int pin){ // Returns distance of object from sensor in Centimeters
delayMicroseconds(1000); // added in version 2 to help with reading accuracy, can't read sonic sensors very fast
int duration;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(2);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin,LOW);
pinMode(pin,INPUT);
duration = pulseIn(pin,HIGH);
return duration/29/2; // convert time of echo to centimeters distance
}
int PRIZM::readSonicSensorIN(int pin){ // Returns distance of object from sensor in Inches
delayMicroseconds(1000); // added in version 2 to help with reading accuracy, can't read sonic sensors very fast
int duration;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(2);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin,LOW);
pinMode(pin,INPUT);
duration = pulseIn(pin,HIGH);
return duration/74/2; // convert time of echo to centimeters distance
}
void PRIZM::setGreenLED (int state){
pinMode(7, OUTPUT); //===== GREEN LED is on pin 7
if (state==1){digitalWrite(7, HIGH);}
if (state==0){digitalWrite(7, LOW);}
}
void PRIZM::setRedLED (int state){
pinMode(6, OUTPUT); //===== RED LED is on pin 6
if (state==1){digitalWrite(6, HIGH);}
if (state==0){digitalWrite(6, LOW);}
}
int PRIZM::readStartButton(){ //============== function returns; unpressed button == 0; pressed button == 1
pinMode(8, INPUT); // Button is on pin 8; unpressed = high, pressed = low
int StartBtn = digitalRead(8);
StartBtn = !StartBtn; // toggle variable to make sense;
return StartBtn;
}
void PRIZM::setServoSpeed (int channel, int servospeed){ //=========== function for setting PRIZM servo speeds individually
if(channel==1){channel= 0x28;}
if(channel==2){channel= 0x29;}
if(channel==3){channel= 0x2A;}
if(channel==4){channel= 0x2B;}
if(channel==5){channel= 0x2C;}
if(channel==6){channel= 0x2D;}
Wire.beginTransmission(6);
Wire.write(channel);
Wire.write(servospeed);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setServoSpeed (int address, int channel, int servospeed){ //=========== function for setting servo speeds individually for servo EXPANSIONANSION
if(channel==1){channel= 0x28;}
if(channel==2){channel= 0x29;}
if(channel==3){channel= 0x2A;}
if(channel==4){channel= 0x2B;}
if(channel==5){channel= 0x2C;}
if(channel==6){channel= 0x2D;}
Wire.beginTransmission(address);
Wire.write(channel);
Wire.write(servospeed);
Wire.endTransmission();
delay(10);
}
void PRIZM::setServoSpeeds (int servospeed1, int servospeed2, int servospeed3, int servospeed4, int servospeed5, int servospeed6){ // function to set all PRIZM servo speeds at once
Wire.beginTransmission(6);
Wire.write(0x2E);
Wire.write(servospeed1);
Wire.write(servospeed2);
Wire.write(servospeed3);
Wire.write(servospeed4);
Wire.write(servospeed5);
Wire.write(servospeed6);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setServoSpeeds (int address, int servospeed1, int servospeed2, int servospeed3, int servospeed4, int servospeed5, int servospeed6){ // function to set all EXPANSIONANSION servo speeds at once
Wire.beginTransmission(address);
Wire.write(0x2E);
Wire.write(servospeed1);
Wire.write(servospeed2);
Wire.write(servospeed3);
Wire.write(servospeed4);
Wire.write(servospeed5);
Wire.write(servospeed6);
Wire.endTransmission();
delay(10);
}
void PRIZM::setServoPosition (int channel, int servoposition){ //function to set PRIZM servo positions individually
int xmit = 0;
if(channel==1 && servoposition != lastPosition_1) {channel= 0x2F; xmit = 1; lastPosition_1 = servoposition;} // gotta see if new position is different than last
if(channel==2 && servoposition != lastPosition_2) {channel= 0x30; xmit = 1; lastPosition_2 = servoposition;}
if(channel==3 && servoposition != lastPosition_3) {channel= 0x31; xmit = 1; lastPosition_3 = servoposition;}
if(channel==4 && servoposition != lastPosition_4) {channel= 0x32; xmit = 1; lastPosition_4 = servoposition;}
if(channel==5 && servoposition != lastPosition_5) {channel= 0x33; xmit = 1; lastPosition_5 = servoposition;}
if(channel==6 && servoposition != lastPosition_6) {channel= 0x34; xmit = 1; lastPosition_6 = servoposition;}
/*
if(channel==1){channel= 0x2F;}
if(channel==2){channel= 0x30;}
if(channel==3){channel= 0x31;}
if(channel==4){channel= 0x32;}
if(channel==5){channel= 0x33;}
if(channel==6){channel= 0x34;}
Wire.beginTransmission(6);
Wire.write(channel);
Wire.write(servoposition);
Wire.endTransmission();
delay(10);
*/
if(xmit == 1){ // no need to send if positions have not changed - result frees up I2C bus
Wire.beginTransmission(6); // Even though this is a single position command, sending all channels at once works around minor servo glitching in version 1 PRIZM servo chip firmware.
Wire.write(0x35);
Wire.write(lastPosition_1);
Wire.write(lastPosition_2);
Wire.write(lastPosition_3);
Wire.write(lastPosition_4);
Wire.write(lastPosition_5);
Wire.write(lastPosition_6);
Wire.endTransmission();
delay(10);
xmit = 0;
}
}
void EXPANSION::setServoPosition (int address, int channel, int servoposition){ //function to set EXPANSION servo positions individually
if(channel==1){channel= 0x2F;}
if(channel==2){channel= 0x30;}
if(channel==3){channel= 0x31;}
if(channel==4){channel= 0x32;}
if(channel==5){channel= 0x33;}
if(channel==6){channel= 0x34;}
Wire.beginTransmission(address);
Wire.write(channel);
Wire.write(servoposition);
Wire.endTransmission();
delay(10);
}
void PRIZM::setServoPositions (int servoposition1,int servoposition2,int servoposition3,int servoposition4,int servoposition5,int servoposition6){ // Sets all PRIZM servo positions at once
lastPosition_1 = servoposition1;
lastPosition_2 = servoposition2;
lastPosition_3 = servoposition3;
lastPosition_4 = servoposition4;
lastPosition_5 = servoposition5;
lastPosition_6 = servoposition6;
Wire.beginTransmission(6);
Wire.write(0x35);
Wire.write(servoposition1);
Wire.write(servoposition2);
Wire.write(servoposition3);
Wire.write(servoposition4);
Wire.write(servoposition5);
Wire.write(servoposition6);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setServoPositions (int address, int servoposition1,int servoposition2,int servoposition3,int servoposition4,int servoposition5,int servoposition6){ // Sets all EXPANSIONANSION servo positions at once
Wire.beginTransmission(address);
Wire.write(0x35);
Wire.write(servoposition1);
Wire.write(servoposition2);
Wire.write(servoposition3);
Wire.write(servoposition4);
Wire.write(servoposition5);
Wire.write(servoposition6);
Wire.endTransmission();
delay(10);
}
void PRIZM::setCRServoState (int channel, int servospeed){ // function to set PRIZM CR servos speed and direction -100|0|100
if(channel==1){channel= 0x36;} // CRservo 1
if(channel==2){channel= 0x37;} // CRservo 2
Wire.beginTransmission(6);
Wire.write(channel);
Wire.write(servospeed);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setCRServoState (int address, int channel, int servospeed){ // function to set EXPANSIONANSION CR servos speed and direction -100|0|100
if(channel==1){channel= 0x36;} // CRservo 1
if(channel==2){channel= 0x37;} // CRservo 2
Wire.beginTransmission(address);
Wire.write(channel);
Wire.write(servospeed);
Wire.endTransmission();
delay(10);
}
int PRIZM::readServoPosition (int channel){ // read the servo PRIZM servo positions
int readServoPosition; // return value variable
if(channel==1){channel= 0x38;}
if(channel==2){channel= 0x39;}
if(channel==3){channel= 0x3A;}
if(channel==4){channel= 0x3B;}
if(channel==5){channel= 0x3C;}
if(channel==6){channel= 0x3D;}
Wire.beginTransmission(6);
Wire.write(channel);
Wire.endTransmission();
delay(10);
Wire.requestFrom(6, 1);
readServoPosition = Wire.read();
delay(10);
return readServoPosition;
}
int EXPANSION::readServoPosition (int address, int channel){ // read the servo EXPANSIONANSION servo positions
int readServoPosition; // return value variable
if(channel==1){channel= 0x38;}
if(channel==2){channel= 0x39;}
if(channel==3){channel= 0x3A;}
if(channel==4){channel= 0x3B;}
if(channel==5){channel= 0x3C;}
if(channel==6){channel= 0x3D;}
Wire.beginTransmission(address);
Wire.write(channel);
Wire.endTransmission();
delay(10);
Wire.requestFrom(address, 1);
readServoPosition = Wire.read();
delay(10);
return readServoPosition;
}
void PRIZM::setMotorPower(int channel, int power) // set Motor Channel power on PRIZM
{
if(channel==1){channel = 0x40;} // DC channel 1
if(channel==2){channel = 0x41;} // DC channel 2
Wire.beginTransmission(5);
Wire.write(channel);
Wire.write(power);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorPower(int address, int channel, int power) // set Motor Channel power on DC EXPANSIONANSION
{
if(channel==1){channel = 0x40;} // DC channel 1
if(channel==2){channel = 0x41;} // DC channel 2
Wire.beginTransmission(address);
Wire.write(channel);
Wire.write(power);
Wire.endTransmission();
delay(10);
}
void PRIZM::setMotorPowers (int power1, int power2){ //power only Block Command for PRIZM Motor 1 and 2 (both in one transmission)
Wire.beginTransmission(5);
Wire.write(0x42);
Wire.write(power1);
Wire.write(power2);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorPowers (int address, int power1, int power2){ //power only Block Command for EXPANSIONANSION Motor 1 and 2 (both in one transmission)
Wire.beginTransmission(address);
Wire.write(0x42);
Wire.write(power1);
Wire.write(power2);
Wire.endTransmission();
delay(10);
}
void PRIZM::setMotorSpeed (int channel, long Mspeed){ // === set speed of each PRIZM DC motor == requires a 1440 CPR installed encoder to do the PID
int lobyte;
int hibyte;
lobyte = lowByte(Mspeed);
hibyte = highByte(Mspeed);
if(channel==1){channel= 0x43;} // DC channel 1
if(channel==2){channel= 0x44;} // DC channel 2
Wire.beginTransmission(5);
Wire.write(channel);
Wire.write(hibyte);
Wire.write(lobyte);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorSpeed (int address, int channel, long Mspeed){ // === set speed of each EXPANSIONANSION DC motor == requires a 1440 CPR installed encoder to do the PID
int lobyte;
int hibyte;
lobyte = lowByte(Mspeed);
hibyte = highByte(Mspeed);
if(channel==1){channel= 0x43;} // DC channel 1
if(channel==2){channel= 0x44;} // DC channel 2
Wire.beginTransmission(address);
Wire.write(channel);
Wire.write(hibyte);
Wire.write(lobyte);
Wire.endTransmission();
delay(10);
}
void PRIZM::setMotorSpeeds (long Mspeed1, long Mspeed2){ // === BLOCK write to set speeds of both PRIZM motors at once == 1440 CPR encoder must be installed to do PID
int lobyte1;
int hibyte1;
int lobyte2;
int hibyte2;
lobyte1 = lowByte(Mspeed1);
hibyte1 = highByte(Mspeed1);
lobyte2 = lowByte(Mspeed2);
hibyte2 = highByte(Mspeed2);
Wire.beginTransmission(5);
Wire.write(0x45);
Wire.write(hibyte1);
Wire.write(lobyte1);
Wire.write(hibyte2);
Wire.write(lobyte2);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorSpeeds (int address, long Mspeed1, long Mspeed2){ // === BLOCK write to set speeds of both EXPANSIONANSION motors at once == 1440 CPR encoder must be installed to do PID
int lobyte1;
int hibyte1;
int lobyte2;
int hibyte2;
lobyte1 = lowByte(Mspeed1);
hibyte1 = highByte(Mspeed1);
lobyte2 = lowByte(Mspeed2);
hibyte2 = highByte(Mspeed2);
Wire.beginTransmission(address);
Wire.write(0x45);
Wire.write(hibyte1);
Wire.write(lobyte1);
Wire.write(hibyte2);
Wire.write(lobyte2);
Wire.endTransmission();
delay(10);
}
void PRIZM::setMotorTarget (int channel, long Mspeed, long Mtarget){ // === set speed and encoder target of each PRIZM DC motor == requires a 1440 CPR encoder to do the PID
int lobyte;
int hibyte;
lobyte = lowByte(Mspeed);
hibyte = highByte(Mspeed);
byte four = (Mtarget);
byte three = (Mtarget>>8);
byte two = (Mtarget>>16);
byte one = (Mtarget>>24);
if(channel==1){channel= 0x46;} // DC channel 1
if(channel==2){channel= 0x47;} // DC channel 2
Wire.beginTransmission(5);
Wire.write(channel);
Wire.write(hibyte);
Wire.write(lobyte);
Wire.write(one);
Wire.write(two);
Wire.write(three);
Wire.write(four);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorTarget (int address, int channel, long Mspeed, long Mtarget){ // === set speed and encoder target of each EXPANSIONANSION DC motor == requires a 1440 CPR encoder to do the PID
int lobyte;
int hibyte;
lobyte = lowByte(Mspeed);
hibyte = highByte(Mspeed);
byte four = (Mtarget);
byte three = (Mtarget>>8);
byte two = (Mtarget>>16);
byte one = (Mtarget>>24);
if(channel==1){channel= 0x46;} // DC channel 1
if(channel==2){channel= 0x47;} // DC channel 2
Wire.beginTransmission(address);
Wire.write(channel);
Wire.write(hibyte);
Wire.write(lobyte);
Wire.write(one);
Wire.write(two);
Wire.write(three);
Wire.write(four);
Wire.endTransmission();
delay(10);
}
void PRIZM::setMotorTargets (long Mspeed1, long Mtarget1, long Mspeed2, long Mtarget2){ // === BLOCK WRITE set speed and encoder target of both PRIZM DC motors == requires a 1440 CPR encoder to do the PID
int lobyte1;
int hibyte1;
int lobyte2;
int hibyte2;
lobyte1 = lowByte(Mspeed1);
hibyte1 = highByte(Mspeed1);
lobyte2 = lowByte(Mspeed2);
hibyte2 = highByte(Mspeed2);
byte four1 = (Mtarget1);
byte three1 = (Mtarget1>>8);
byte two1 = (Mtarget1>>16);
byte one1 = (Mtarget1>>24);
byte four2 = (Mtarget2);
byte three2 = (Mtarget2>>8);
byte two2 = (Mtarget2>>16);
byte one2 = (Mtarget2>>24);
Wire.beginTransmission(5);
Wire.write(0x48);
Wire.write(hibyte1);
Wire.write(lobyte1);
Wire.write(one1);
Wire.write(two1);
Wire.write(three1);
Wire.write(four1);
Wire.write(hibyte2);
Wire.write(lobyte2);
Wire.write(one2);
Wire.write(two2);
Wire.write(three2);
Wire.write(four2);
Wire.endTransmission();
delay(10);
}
void EXPANSION::setMotorTargets (int address, long Mspeed1, long Mtarget1, long Mspeed2, long Mtarget2){ // === BLOCK WRITE set speed and encoder target of both EXPANSIONANSION DC motors == requires a 1440 CPR encoder to do the PID
int lobyte1;
int hibyte1;
int lobyte2;
int hibyte2;
lobyte1 = lowByte(Mspeed1);
hibyte1 = highByte(Mspeed1);
lobyte2 = lowByte(Mspeed2);
hibyte2 = highByte(Mspeed2);
byte four1 = (Mtarget1);
byte three1 = (Mtarget1>>8);
byte two1 = (Mtarget1>>16);
byte one1 = (Mtarget1>>24);
byte four2 = (Mtarget2);
byte three2 = (Mtarget2>>8);
byte two2 = (Mtarget2>>16);
byte one2 = (Mtarget2>>24);
Wire.beginTransmission(address);
Wire.write(0x48);
Wire.write(hibyte1);
Wire.write(lobyte1);
Wire.write(one1);
Wire.write(two1);
Wire.write(three1);
Wire.write(four1);
Wire.write(hibyte2);