44
48
volatile uint8_t rcTick = 0;
45
49
volatile uint8_t newData1 = 0;
46
50
volatile uint8_t newData2 = 0;
51
volatile uint8_t pwmState = 0;
48
53
//Stored data variables
50
54
volatile uint16_t rcLow = RC_LOW;
52
55
volatile uint16_t rcHigh = RC_HIGH;
54
56
volatile uint16_t rcMid = (RC_LOW + RC_HIGH)/2;
56
57
volatile uint8_t maxSlew = MAX_SLEW;
58
58
volatile uint8_t tempLimit = TEMP_LIMIT;
60
59
volatile uint8_t goExpo = EXPO;
64
61
//Define memory states
66
62
uint16_t EEMEM storedRcLow = RC_LOW;
68
63
uint16_t EEMEM storedRcHigh = RC_HIGH;
70
64
uint8_t EEMEM storedSlewRate = MAX_SLEW;
72
65
uint8_t EEMEM storedExpo = EXPO;
74
66
uint8_t EEMEM storedTempLimit = TEMP_LIMIT;
78
#define LOW_PORT_A LOW_A_PORT
79
#define LOW_PORT_B LOW_B_PORT
80
#define LOW_PORT_C LOW_C_PORT
82
#define HIGH_PORT_A HIGH_A_PORT
83
#define HIGH_PORT_B HIGH_B_PORT
84
#define HIGH_PORT_C HIGH_C_PORT
86
inline void setALow();
88
inline void clrALow();
90
inline void setAHigh();
92
inline void clrAHigh();
93
inline void setBLow();
95
inline void clrBLow();
97
inline void setBHigh();
99
inline void clrBHigh();
100
inline void setCLow();
102
inline void clrCLow();
104
inline void setCHigh();
106
inline void clrCHigh();
110
69
void goForwards();
130
88
SET_LOW_C_PORT |= (1 << LOW_C);
145
98
DDRD &= ~(1 <<PD3);
146
99
PORTD &= ~(1 << PD3);
148
uart_init( UART_BAUD_SELECT(9600,F_CPU) );
102
uart_init( UART_BAUD_SELECT(9600,F_CPU) );
151
106
TCCR0 = 0b00000011; //Setup Timer 0 - scaling of ???? (go check the datasheet and fill it in)
160
115
GICR |= (1 << INT0)|(1 << INT1); //Enable INT0 & INT1
163
uart_puts("Running UART Debug on 2in1\n");
119
uart_puts("Running UART Debug on 2in1\n");
164
121
//End initialisation
168
125
//Here we manage what mode we are in everything else is done in interrupts
170
itoa(timeCH1, send_buffer, 10); // convert interger into string (decimal format)
172
uart_puts(send_buffer); // and transmit string to UART
175
itoa(timeCH2, send_buffer, 10); // convert interger into string (decimal format)
177
uart_puts(send_buffer); // and transmit string to UART
129
itoa(timeCH1, send_buffer, 10); // convert interger into string (decimal format)
130
uart_puts(send_buffer); // and transmit string to UART
133
itoa(timeCH2, send_buffer, 10); // convert interger into string (decimal format)
134
uart_puts(send_buffer); // and transmit string to UART
180
138
uint8_t deadzone = 40;
182
139
uint8_t buffer = 20;
187
142
if(newData1 == 1 && newData2 == 1){
189
144
if ((timeCH1 > (rcLow - buffer)) && (timeCH1 < (rcHigh + buffer)) && (timeCH2 > (rcLow - buffer)) && (timeCH2 < (rcHigh + buffer))){
192
146
if (failsafe > 10){ //Need 10 good pulses before we start
194
147
failsafe = 15; //Set valid pulse counter (15-10 = 5) therfore we can receive 5 bad pulses before we stop
196
148
timeout = 50; //Set timeout counter
206
158
uart_puts("Vaild\n");
209
//Need to handle states here
210
//Work out the magnitude of the RC signal.
212
uint16_t squared = pow((timeCH1/2),2) + pow((timeCH2/2),2); //this gives a squared distance up to 15 bits.
213
//SQRT for linear or just scale it to keep exponential rates
214
uint8_t magnitude = squared >> 7; //Scale to 8 bit from 15.
216
//If we are in the center
217
if(magnitude < deadzone){
220
else if ( abs(timeCH1 - RC_MID) > abs(timeCH2 - RC_MID) ){
221
int16_t normForward = timeCH1 - RC_MID;
222
int16_t normSteer = timeCH2 - RC_MID;
224
if (normSteer > deadzone){
228
if (abs(timeCH1 - RC_MID) > maxOut){
232
OCR2 = (timeCH1 - RC_MID-deadzone)*256/(RC_HIGH-RC_MID-deadzone);
236
if (timeCH1 > RC_MID){
249
if (abs(timeCH2 - RC_MID) > maxOut){
253
OCR2 = (timeCH2 - RC_MID-deadzone)*256/(RC_HIGH-RC_MID-deadzone);
256
if (timeCH2 > RC_MID){
272
165
uart_puts("Failsafe\n");
291
184
//This interrupt manages RC pulses
295
186
//Read in the current time
297
187
uint16_t time = TCNT0; //Read lower
299
188
time = time + time0*256; //Add upper
303
190
if (RC_PORT & ( 1 << RC_PIN)){
305
191
prevTime = time; //then save the current time
311
194
timeCH1 = time - prevTime;
321
201
//Read in the current time
323
202
uint16_t time = TCNT0; //Read lower
325
203
time = time + time0*256; //Add upper
329
205
if (RC_PORT & ( 1 << RC_PIN2)){
331
206
prevTime1 = time; //then save the current time
337
209
timeCH2 = time - prevTime1;
345
215
ISR(TIMER2_COMP_vect){ //When comparator is triggered clear low ports (off part of PWM cycle)
216
if (pwmState == MODE_A){
347
217
if (state != brake){
356
226
ISR(TIMER2_OVF_vect ){ //When timer overflows enable low port for current state
358
if (state == forward){
227
if ( A > 10 && state != brake){
228
if (state == forward){
231
} else if (state == backward){
234
} else if (state == spinLeft){
237
} else if (state == spinRight){
243
else if ( B > 10 && state != brake){
244
if (state == forward){
361
247
} else if (state == backward){
363
249
} else if (state == spinLeft){
365
251
} else if (state == spinRight){
367
} else if (state == brake){
374
if (OCR2 > 250 && state != brake){
261
if ((A > 250 || B > 250) && state != brake){
378
263
if (lets_get_high > 50){
385
268
lets_get_high = 0;
393
//Motor Pin functions
394
inline void setALow(){
398
LOW_A_PORT &= ~(1 << LOW_A);
404
LOW_A_PORT |= (1 << LOW_A);
412
inline void clrALow(){
416
LOW_A_PORT |= (1 << LOW_A);
422
LOW_A_PORT &= ~(1 << LOW_A);
430
inline void setAHigh(){
432
if (HIGH_ACTIVE_LOW){
434
HIGH_A_PORT &= ~(1 << HIGH_A);
440
HIGH_A_PORT |= (1 << HIGH_A);
448
inline void clrAHigh(){
450
if (HIGH_ACTIVE_LOW){
452
HIGH_A_PORT |= (1 << HIGH_A);
458
HIGH_A_PORT &= ~(1 << HIGH_A);
464
inline void setBLow(){
468
LOW_B_PORT &= ~(1 << LOW_B);
474
LOW_B_PORT |= (1 << LOW_B);
482
inline void clrBLow(){
486
LOW_B_PORT |= (1 << LOW_B);
492
LOW_B_PORT &= ~(1 << LOW_B);
500
inline void setBHigh(){
502
if (HIGH_ACTIVE_LOW){
504
HIGH_B_PORT &= ~(1 << HIGH_B);
510
HIGH_B_PORT |= (1 << HIGH_B);
518
inline void clrBHigh(){
520
if (HIGH_ACTIVE_LOW){
522
HIGH_B_PORT |= (1 << HIGH_B);
528
HIGH_B_PORT &= ~(1 << HIGH_B);
534
inline void setCLow(){
538
LOW_C_PORT &= ~(1 << LOW_C);
544
LOW_C_PORT |= (1 << LOW_C);
552
inline void clrCLow(){
556
LOW_C_PORT |= (1 << LOW_C);
562
LOW_C_PORT &= ~(1 << LOW_C);
570
inline void setCHigh(){
572
if (HIGH_ACTIVE_LOW){
574
HIGH_C_PORT &= ~(1 << HIGH_C);
580
HIGH_C_PORT |= (1 << HIGH_C);
588
inline void clrCHigh(){
590
if (HIGH_ACTIVE_LOW){
592
HIGH_C_PORT |= (1 << HIGH_C);
598
HIGH_C_PORT &= ~(1 << HIGH_C);
606
276
void goForwards(){
614
284
_delay_us(200); //Fet switch delay
616
285
setBLow(); //Enable Common pin
618
287
_delay_us(200); //Fet switch delay
619
288
setAHigh(); //Enable one motor
621
//Both motors are enabled at slightly different times
622
//to hopefully be a little bit nicer to the fets
623
//200us should have no noticible effect on driving straight
624
290
_delay_us(200); //Fet switch delay
625
291
setCHigh; //Enable 2nd motor