forked from ArcBotics/Sparki
/
Sparki.cpp
executable file
·1834 lines (1561 loc) · 49.2 KB
/
Sparki.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
#include "Sparki.h"
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
#include <SPI.h>
#include "SparkiWire.h"
#include "SparkiEEPROM.h"
#include "Sparkii2c.h"
static int8_t step_dir[3]; // -1 = ccw, 1 = cw
static volatile uint8_t speed_index[3]; // counter controlling motor speed
static uint8_t motor_speed[3]; // stores last set motor speed (0-100%)
static volatile int8_t step_index[3]; // index into _steps array
static uint8_t _steps_right[9]; // bytes defining stepper coil activations
static uint8_t _steps_left[9]; // bytes defining stepper coil activations
static volatile uint32_t remainingSteps[3]; // number of steps before stopping motor
static volatile uint32_t isRunning[3]; // tells if motor is running
static volatile int speedCounter[3]; // variable used in maintaing speed
static volatile int speedCount[3]; // what speedCount is set at when speed cycle resets
static volatile uint8_t shift_outputs[3]; // tells if motor is running
// initialize the RGB timer variables
static volatile uint8_t RGB_vals[3];
static volatile uint16_t RGB_timer;
static volatile uint8_t irSwitch;
static volatile uint8_t irSwitch2;
// variables for communication between the IR read function and its interrupt
#define MAX_IR_PULSE 20000
volatile long timeSinceLastPulse = 0;
volatile long lastPulseTime = 0;
volatile uint16_t pulsesIR[50][2]; // LOW,HIGH
volatile uint8_t currentPulse = 0;
volatile uint8_t haltIRRead = 0;
// shares the values of the accelerometers
volatile float xAxisAccel;
volatile float yAxisAccel;
volatile float zAxisAccel;
// variables for the magnetometer
volatile uint8_t mag_buffer[RawMagDataLength];
// values for the servo
volatile int8_t servo_deg_offset = 0;
SparkiClass sparki;
//static volatile int speedCounter;
SparkiClass::SparkiClass()
{
begin();
}
void SparkiClass::begin( ) {
Serial.begin(9600);
Serial1.begin(9600);
// set up the Status LED
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, LOW);
// Setup Buzzer
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
// Setup Analog Multiplexer
pinMode(MUX_ANALOG, INPUT);
pinMode(MUX_A, OUTPUT);
pinMode(MUX_B, OUTPUT);
pinMode(MUX_C, OUTPUT);
// Setup IR Send
pinMode(IR_SEND, OUTPUT);
// Setup Ultrasonic
pinMode(ULTRASONIC_TRIG, OUTPUT);
pinMode(ULTRASONIC_ECHO, INPUT);
// Setup Servo
pinMode(SERVO, OUTPUT);
//startServoTimer();
if( EEPROM.read(0) > 127) {
servo_deg_offset = -256+EEPROM.read(0);
}
else{
servo_deg_offset = EEPROM.read(0);
}
//servo(SERVO_CENTER);
// Setup the SPI bus for the shift register
// !!! Need to remove the essential functions from the SPI Library,
// !!! and include in the main code
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2);
// Set the shift-register clock select pin to output
DDRD |= (1<<5);
// Clear out the shift registers
PORTD &= 0xDF; // pull PD5 low
SPI.transfer(shift_outputs[1]);
SPI.transfer(shift_outputs[0]);
PORTD |= 0x20; // pull PD5 high to latch in spi transfers
// Setup the IR Switch
irSwitch = 0;
// defining steps for the stepper motors
_steps_left[0] = 0x10;
_steps_left[1] = 0x30;
_steps_left[2] = 0x20;
_steps_left[3] = 0x60;
_steps_left[4] = 0x40;
_steps_left[5] = 0xC0;
_steps_left[6] = 0x80;
_steps_left[7] = 0x90;
_steps_left[8] = 0x00;
_steps_right[0] = 0x01;
_steps_right[1] = 0x03;
_steps_right[2] = 0x02;
_steps_right[3] = 0x06;
_steps_right[4] = 0x04;
_steps_right[5] = 0x0C;
_steps_right[6] = 0x08;
_steps_right[7] = 0x09;
_steps_right[8] = 0x00;
beginDisplay();
updateLCD();
// Setup initial Stepper settings
motor_speed[MOTOR_LEFT] = motor_speed[MOTOR_RIGHT] = motor_speed[MOTOR_GRIPPER] =100;
// Set up the scheduler routine to run every 100uS, based off Timer4 interrupt
cli(); // disable all interrupts
TCCR4A = 0;
TCCR4B = 0;
TCNT4 = 0;
OCR4A = 48; // compare match register 16MHz/32/10000Hz
TCCR4B |= (1 << WGM12); // CTC mode
TCCR4B = 0x06; // CLK/32 prescaler (32 = 2^(0110-1))
TIMSK4 |= (1 << OCIE4A); // enable Timer4 compare interrupt A
sei(); // enable all interrupts
// Setup the IR Remote Control pin and pin interrupt
noInterrupts();
pinMode(IR_RECEIVE, INPUT);
// Setup the pin interrupt for INT6 (Pin 7) to trigger the IR function
EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (CHANGE << ISC60);
EIMSK |= (1 << INT6);
interrupts();
initAccelerometer();
WireWrite(ConfigurationRegisterB, (0x01 << 5));
WireWrite(ModeRegister, Measurement_Continuous);
readMag(); // warm it up
}
void SparkiClass::setMux(uint8_t A, uint8_t B, uint8_t C){
digitalWrite(MUX_A, A);
digitalWrite(MUX_B, B);
digitalWrite(MUX_C, C);
delay(1);
}
/*
* Light Sensors
*/
int SparkiClass::lightRight(){
setMux(LIGHT_RIGHT);
return analogRead(MUX_ANALOG);
}
int SparkiClass::lightCenter(){
setMux(LIGHT_CENTER);
return analogRead(MUX_ANALOG);
}
int SparkiClass::lightLeft(){
setMux(LIGHT_LEFT);
return analogRead(MUX_ANALOG);
}
/*
* Infrared line sensors
*/
int SparkiClass::edgeRight(){
setMux(IR_EDGE_RIGHT);
return readSensorIR(MUX_ANALOG);
}
int SparkiClass::lineRight(){
setMux(IR_LINE_RIGHT);
return readSensorIR(MUX_ANALOG);
}
int SparkiClass::lineCenter(){
setMux(IR_LINE_CENTER);
return readSensorIR(MUX_ANALOG);
}
int SparkiClass::lineLeft(){
setMux(IR_LINE_LEFT);
return readSensorIR(MUX_ANALOG);
}
int SparkiClass::edgeLeft(){
setMux(IR_EDGE_LEFT);
return readSensorIR(MUX_ANALOG);
}
int SparkiClass::readSensorIR(int pin){
int read = 0;
onIR();
read = analogRead(pin);
offIR();
return read;
}
void SparkiClass::onIR() // turns off the IR Detection LEDs
{
irSwitch = 1;
delay(1); // give time for a scheduler cycle to run
}
void SparkiClass::offIR() // turns off the IR Detection LEDs
{
irSwitch = 0;
delay(1); // give time for a scheduler cycle to run
}
int SparkiClass::readBlindSensorIR(int pin0, int pin1, int pin2){
int read = 0;
setMux(pin0, pin1, pin2);
delay(1);
read = analogRead(MUX_ANALOG);
delay(1);
return read;
}
int SparkiClass::diffIR(int pin0, int pin1, int pin2){
setMux(pin0, pin1, pin2);
delay(1);
int readOff = analogRead(MUX_ANALOG);
delay(10);
onIR();
int readOn = analogRead(MUX_ANALOG);
offIR();
return readOff-readOn;
}
void SparkiClass::beep(){
tone(BUZZER, 4000, 200);
}
void SparkiClass::beep(int freq){
tone(BUZZER, freq, 200);
}
void SparkiClass::beep(int freq, int time){
tone(BUZZER, freq, time);
}
void SparkiClass::noBeep(){
noTone(BUZZER);
}
/*
* motor control (non-blocking, except when moving distances)
* speed is percent 0-100
*/
void SparkiClass::RGB(uint8_t R, uint8_t G, uint8_t B)
{
if(R > 100){
R = 100;
}
if(G > 100){
G = 100;
}
if(B > 100){
B = 100;
}
RGB_vals[0] = R;
RGB_vals[1] = G;
RGB_vals[2] = B;
}
void SparkiClass::moveRight(float deg)
{
float turn = 22.667184*deg;
if(deg == 0){
moveRight();
}
else{
if(deg < 0){
moveLeft(-deg);
}
else{
moveRight();
delay(long(turn));
moveStop();
}
}
}
void SparkiClass::moveRight()
{
motorRotate(MOTOR_LEFT, DIR_CCW, 100);
motorRotate(MOTOR_RIGHT, DIR_CCW, 100);
}
void SparkiClass::moveLeft(float deg)
{
float turn = 22.667184*deg;
if(deg == 0){
moveLeft();
}
else{
if(deg < 0){
moveRight(-deg);
}
else{
moveLeft();
delay(long(turn));
moveStop();
}
}
}
void SparkiClass::moveLeft()
{
motorRotate(MOTOR_LEFT, DIR_CW, 100);
motorRotate(MOTOR_RIGHT, DIR_CW, 100);
}
void SparkiClass::moveForward(float cm)
{
float run = 303.797428*cm;
if(cm == 0){
moveForward();
}
else{
if(cm < 0){
moveBackward(-cm);
}
else{
moveForward();
delay(long(run));
moveStop();
}
}
}
void SparkiClass::moveForward()
{
motorRotate(MOTOR_LEFT, DIR_CCW, 100);
motorRotate(MOTOR_RIGHT, DIR_CW, 100);
}
void SparkiClass::moveBackward(float cm)
{
float run = 303.797428*cm;
if(cm == 0){
moveBackward();
}
else{
if(cm < 0){
moveForward(-cm);
}
else{
moveBackward();
delay(long(run));
moveStop();
}
}
}
void SparkiClass::moveBackward()
{
motorRotate(MOTOR_LEFT, DIR_CW, 100);
motorRotate(MOTOR_RIGHT, DIR_CCW, 100);
}
void SparkiClass::moveStop()
{
motorStop(MOTOR_LEFT);
motorStop(MOTOR_RIGHT);
}
void SparkiClass::gripperOpen()
{
motorRotate(MOTOR_GRIPPER, DIR_CCW, 100);
}
void SparkiClass::gripperClose()
{
motorRotate(MOTOR_GRIPPER, DIR_CW, 100);
}
void SparkiClass::gripperStop()
{
motorStop(MOTOR_GRIPPER);
}
void SparkiClass::motorRotate(int motor, int direction, int speed)
{
//Serial.print("Motor ");Serial.print(motor); Serial.print(" rotate, dir= "); Serial.println(direction);
uint8_t oldSREG = SREG;
cli();
motor_speed[motor] = speed;
step_dir[motor] = direction;
remainingSteps[motor] = ULONG_MAX; // motor stops after this many steps, almost 50 days of stepping if motor not stopped
isRunning[motor] = true;
speedCount[motor] = int(100.0/float(motor_speed[motor])*5.0);
speedCounter[motor] = speedCount[motor];
SREG = oldSREG;
sei();
delay(10);
}
void SparkiClass::motorStop(int motor)
{
//Serial.println("Motor Stop");
motor_speed[motor] = 0;
setSteps(motor, 0);
}
void SparkiClass::motorsRotateSteps( int leftDir, int rightDir, int speed, uint32_t steps, bool wait)
{
uint8_t oldSREG = SREG;
cli();
motor_speed[MOTOR_LEFT] = motor_speed[MOTOR_RIGHT] = speed;
step_dir[MOTOR_LEFT] = leftDir;
step_dir[MOTOR_RIGHT] = rightDir;
remainingSteps[MOTOR_LEFT] = remainingSteps[MOTOR_RIGHT] = steps;
isRunning[MOTOR_LEFT] = isRunning[MOTOR_RIGHT] = true;
SREG = oldSREG;
sei();
if( wait)
{
while ( areMotorsRunning() ){
delay(10); // remainingSteps is decrimented in the timer callback
}
}
}
// returns true if one or both motors a still stepping
bool SparkiClass::areMotorsRunning()
{
bool result;
uint8_t oldSREG = SREG;
cli();
result = isRunning[MOTOR_LEFT] || isRunning[MOTOR_RIGHT] || isRunning[MOTOR_GRIPPER] ;
SREG = oldSREG;
sei();
return result;
}
int SparkiClass::ping_single(){
long duration;
float cm;
digitalWrite(ULTRASONIC_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG, LOW);
uint8_t bit = digitalPinToBitMask(ULTRASONIC_ECHO);
uint8_t port = digitalPinToPort(ULTRASONIC_ECHO);
uint8_t stateMask = (HIGH ? bit : 0);
unsigned long startCount = 0;
unsigned long endCount = 0;
unsigned long width = 0; // keep initialization out of time critical area
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
unsigned long numloops = 0;
unsigned long maxloops = 5000;
// wait for any previous pulse to end
while ((*portInputRegister(port) & bit) == stateMask)
if (numloops++ == maxloops)
return -1;
// wait for the pulse to start
while ((*portInputRegister(port) & bit) != stateMask)
if (numloops++ == maxloops)
return -1;
startCount = micros();
// wait for the pulse to stop
while ((*portInputRegister(port) & bit) == stateMask) {
if (numloops++ == maxloops)
return -1;
delayMicroseconds(10); //loop 'jams' without this
if((micros() - startCount) > 58000 ){ // 58000 = 1000CM
return -1;
break;
}
}
duration = micros() - startCount;
//--------- end pulsein
cm = (float)duration / 29.0 / 2.0;
return int(cm);
}
int SparkiClass::ping(){
int attempts = 5;
float distances [attempts];
for(int i=0; i<attempts; i++){
distances[i] = ping_single();
delay(20);
}
// sort them in order
int i, j;
float temp;
for (i = (attempts - 1); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (distances[j-1] > distances[j])
{
temp = distances[j-1];
distances[j-1] = distances[j];
distances[j] = temp;
}
}
}
// return the middle entry
return int(distances[(int)ceil((float)attempts/2.0)]);
}
void SparkiClass::startServoTimer(){
char oldSREG = SREG;
noInterrupts(); // Disable interrupts for 16 bit register access
TCCR1A = 0; // clear control register A
TCCR1B = _BV(WGM13); // set mode 8: phase and frequency correct pwm, stop the timer
ICR1 = 20000; // ICR1 is TOP in p & f correct pwm mode
TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
TCCR1B |= 0x02; // reset clock select register, and starts the clock
DDRB |= _BV(PORTB1); // sets data direction register for pwm output pin
TCCR1A |= _BV(COM1A1); // activates the output pin
interrupts(); // re-enable interrupts
SREG = oldSREG;
}
void SparkiClass::servo(int deg)
{
startServoTimer();
int duty = int((((float(-deg+servo_deg_offset)*2000/180)+1500)/20000)*1024); // compute the duty cycle for the servo
//0 = 26
//180 = 128
unsigned long dutyCycle = 20000;
dutyCycle *= duty;
dutyCycle >>= 10;
char oldSREG = SREG;
noInterrupts();
OCR1A = dutyCycle;
SREG = oldSREG;
interrupts();
}
/*
Returns the current IR Code.
Uses the interrupt on INT6 (PE6, Pin 7) to do the signal reading
If there is no code waiting, pass -1 back immediately.
If there is a code, but its still reading, wait it out then return code
Wipes the current stored code once read.
NEC IR code details here:
http://wiki.altium.com/display/ADOH/NEC+Infrared+Transmission+Protocol
*/
int SparkiClass::readIR(){
uint8_t code = 0;
if(currentPulse != 0){ // check there's a code waiting
while( micros()-lastPulseTime <= MAX_IR_PULSE){
delayMicroseconds(MAX_IR_PULSE);
}; // wait for the reading to time out
// tell the interrupt to not take any more codes
haltIRRead = 1;
// decode the signal
for(int i=0; i<8; i++){
if(pulsesIR[17+i][1] > 1000){
code |= (1<<i);
}
}
currentPulse = 0; // 'reset' the current IR pulse reading
haltIRRead = 0;
return int(code); // return the decoded value
}
else{
return -1; // no signal found, return -1
}
}
SIGNAL(INT6_vect) {
if(haltIRRead != 1){
long currentTime = micros(); // take the current time
int pinStatus = PINE & (1 << 6); // read the current status of the IR Pin
timeSinceLastPulse = currentTime-lastPulseTime;
// Tell if its the start of the reading cycle or not (time since last pulse), starts low
if( (timeSinceLastPulse >= MAX_IR_PULSE) && (pinStatus == LOW)){
// if reading new pulse, set up. Wipes out the last pulse
currentPulse = 0;
}
else{
// otherwise, read the current code
if(pinStatus){ //(PE6 high) pulse has risen from the end of a low pulse
pulsesIR[currentPulse][0] = timeSinceLastPulse;
}
else{ //(PE6 low) pulse has fallen from the end of a high pulse
pulsesIR[currentPulse][1] = timeSinceLastPulse;
currentPulse++;
}
}
lastPulseTime = currentTime;
}
}
// setups up timer to pulse 38khz on and off in a pre-described sequence according to NEC
// protocol
// http://wiki.altium.com/display/ADOH/NEC+Infrared+Transmission+Protocol
void SparkiClass::sendIR(uint8_t code){
// setup PD7 (6) to 38khz on pin using TIMER4 COMPD
OCR4D = 13; // compare match register 16MHz/32/38000Hz
TCCR4D |= (1 << WGM12); // CTC mode
TCCR4D = 0x06; // CLK/32 prescaler (32 = 2^(0110-1))
TIMSK4 |= (1 << OCIE4D); // enable Timer4 compare interrupt D - need to switch to PWM
// go through each bit in byte, pulse appropriate IR
//leading pulse Xms on, Yms off
//leading pulse of all 0
for(uint8_t bit = 0; bit < 8; bit++){ // for each bit in the byte
if(code & (1<<bit) > 0){ // determine if bit is 1
// bit==1: pulse for Xms on, off for Yms
}
else{
// bit==0: pulse for Xms on, off for Yms
}
}
// re-establish output on Timer 4 for 10khz control loop
}
float SparkiClass::accelX(){
readAccelData();
return -xAxisAccel*9.8;
}
float SparkiClass::accelY(){
readAccelData();
return -yAxisAccel*9.8;
}
float SparkiClass::accelZ(){
readAccelData();
return -zAxisAccel*9.8;
}
float SparkiClass::readMag(){
WireRead(DataRegisterBegin, RawMagDataLength);
xAxisMag = ((mag_buffer[0] << 8) | mag_buffer[1]) * M_SCALE;
zAxisMag = ((mag_buffer[2] << 8) | mag_buffer[3]) * M_SCALE;
yAxisMag = ((mag_buffer[4] << 8) | mag_buffer[5]) * M_SCALE;
}
float SparkiClass::compass(){
readMag();
float heading = atan2(yAxisMag,xAxisMag);
if(heading < 0)
heading += 2*PI;
if(heading > 2*PI)
heading -= 2*PI;
float headingDegrees = heading * 180/M_PI;
return headingDegrees;
}
float SparkiClass::magX(){
readMag();
return xAxisMag;
}
float SparkiClass::magY(){
readMag();
return yAxisMag;
}
float SparkiClass::magZ(){
readMag();
return zAxisMag;
}
void SparkiClass::WireWrite(int address, int data){
Wire.beginTransmission(HMC5883L_Address);
Wire.write(address);
Wire.write(data);
Wire.endTransmission();
}
uint8_t* SparkiClass::WireRead(int address, int length){
Wire.beginTransmission(HMC5883L_Address);
Wire.write(DataRegisterBegin);
Wire.endTransmission();
Wire.beginTransmission(HMC5883L_Address);
Wire.requestFrom(HMC5883L_Address, RawMagDataLength);
if(Wire.available() == RawMagDataLength)
{
for(uint8_t i = 0; i < RawMagDataLength; i++)
{
mag_buffer[i] = Wire.read();
}
}
Wire.endTransmission();
}
/*
* private functions
*/
// set the number if steps for the given motor
void SparkiClass::setSteps(uint8_t motor, uint32_t steps)
{
uint8_t oldSREG = SREG;
cli();
remainingSteps[motor] = steps; // motor stops after this many steps
isRunning[motor] = steps > 0;
SREG = oldSREG;
sei();
}
uint32_t SparkiClass::getSteps(uint8_t motor )
{
uint8_t oldSREG = SREG;
cli();
uint32_t steps = remainingSteps[motor];
SREG = oldSREG;
sei();
return steps;
}
/***********************************************************************************
The Scheduler
Every 100uS (10,000 times a second), we update the 2 shift registers used to increase
the amount of outputs the processor has
***********************************************************************************/
ISR(TIMER4_COMPA_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{
//void SparkiClass::scheduler(){
uint8_t oldSREG = SREG;
// Clear the timer interrupt counter
TCNT4=0;
// clear the shift register values so we can re-write them
shift_outputs[0] = 0x00;
shift_outputs[1] = 0x00;
// Update the RGB leds
if(RGB_timer < RGB_vals[0]){ // update Red led
shift_outputs[RGB_SHIFT] |= RGB_R;
}
if(RGB_timer < RGB_vals[1]){ // update Green led
shift_outputs[RGB_SHIFT] |= RGB_G;
}
if(RGB_timer < RGB_vals[2]){ // update Blue led
shift_outputs[RGB_SHIFT] |= RGB_B;
}
RGB_timer++;
if(RGB_timer == 100){
RGB_timer = 0;
}
// IR Detection Switch
if(irSwitch == 0){
shift_outputs[1] &= 0xF7;
}
else{
shift_outputs[1] |= 0x08;
}
//// Motor Control ////
// Determine what state the stepper coils are in
for(byte motor=0; motor<3; motor++){
if( remainingSteps[motor] > 1 ){ // check if finished stepping
if( speedCounter[motor] == 0) { //
step_index[motor] += step_dir[motor];
remainingSteps[motor]--;
speedCounter[motor] = speedCount[motor];
}
else{
speedCounter[motor] = speedCounter[motor]-1;
}
}
else { // if this was the last step
remainingSteps[motor] = 0;
isRunning[motor] = false;
step_index[motor] = 8;
speedCounter[motor] = -1;
}
// keep indicies from rolling over or under
if( step_index[motor] >= 8){
step_index[motor] = 0;
}
else if( step_index[motor] < 0){
step_index[motor] = 7;
}
if(isRunning[motor] == false){
step_index[motor] = 8;
}
}
shift_outputs[0] |= _steps_right[step_index[MOTOR_RIGHT]];
shift_outputs[0] |= _steps_left[step_index[MOTOR_GRIPPER]];
shift_outputs[1] |= _steps_left[step_index[MOTOR_LEFT]];
//output values to shift registers
PORTD &= ~(1<<5); // pull PD5 (shift-register latch) low
SPI.transfer(shift_outputs[1]);
SPI.transfer(shift_outputs[0]);
PORTD |= (1<<5); // pull PD5 (shift-register latch) high
SREG = oldSREG;
}
/***********************************************************************************
Display Library
***********************************************************************************/
#define ST7565_STARTBYTES 1
uint8_t is_reversed = 0;
// a handy reference to where the pages are on the screen
const uint8_t pagemap[] = { 3, 2, 1, 0, 7, 6, 5, 4 };
// a 5x7 font table
uint8_t font[] PROGMEM = {
0x0, 0x0, 0x0, 0x0, 0x0, // Ascii 0
0x7C, 0xDA, 0xF2, 0xDA, 0x7C, //ASC(01)
0x7C, 0xD6, 0xF2, 0xD6, 0x7C, //ASC(02)
0x38, 0x7C, 0x3E, 0x7C, 0x38,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x38, 0xEA, 0xBE, 0xEA, 0x38,
0x38, 0x7A, 0xFE, 0x7A, 0x38,
0x0, 0x18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,
0x0, 0x0, 0x6, 0x6, 0x0,
0x4, 0x8, 0x10, 0x20, 0x40,
0x7C, 0x8A, 0x92, 0xA2, 0x7C,
0x0, 0x42, 0xFE, 0x2, 0x0,
0x4E, 0x92, 0x92, 0x92, 0x62,
0x84, 0x82, 0x92, 0xB2, 0xCC,
0x18, 0x28, 0x48, 0xFE, 0x8,
0xE4, 0xA2, 0xA2, 0xA2, 0x9C,
0x3C, 0x52, 0x92, 0x92, 0x8C,
0x82, 0x84, 0x88, 0x90, 0xE0,
0x6C, 0x92, 0x92, 0x92, 0x6C,
0x62, 0x92, 0x92, 0x94, 0x78,
0x0, 0x0, 0x28, 0x0, 0x0,
0x0, 0x2, 0x2C, 0x0, 0x0,
0x0, 0x10, 0x28, 0x44, 0x82,
0x28, 0x28, 0x28, 0x28, 0x28,
0x0, 0x82, 0x44, 0x28, 0x10,
0x40, 0x80, 0x9A, 0x90, 0x60,
0x7C, 0x82, 0xBA, 0x9A, 0x72,
0x3E, 0x48, 0x88, 0x48, 0x3E,
0xFE, 0x92, 0x92, 0x92, 0x6C,
0x7C, 0x82, 0x82, 0x82, 0x44,
0xFE, 0x82, 0x82, 0x82, 0x7C,
0xFE, 0x92, 0x92, 0x92, 0x82,
0xFE, 0x90, 0x90, 0x90, 0x80,
0x7C, 0x82, 0x82, 0x8A, 0xCE,
0xFE, 0x10, 0x10, 0x10, 0xFE,
0x0, 0x82, 0xFE, 0x82, 0x0,
0x4, 0x2, 0x82, 0xFC, 0x80,
0xFE, 0x10, 0x28, 0x44, 0x82,
0xFE, 0x2, 0x2, 0x2, 0x2,
0xFE, 0x40, 0x38, 0x40, 0xFE,
0xFE, 0x20, 0x10, 0x8, 0xFE,
0x7C, 0x82, 0x82, 0x82, 0x7C,
0xFE, 0x90, 0x90, 0x90, 0x60,
0x7C, 0x82, 0x8A, 0x84, 0x7A,
0xFE, 0x90, 0x98, 0x94, 0x62,
0x64, 0x92, 0x92, 0x92, 0x4C,
0xC0, 0x80, 0xFE, 0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