-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremoteTX.c
398 lines (296 loc) · 10.1 KB
/
remoteTX.c
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
#define p_length 12
#define chan 1
#define RX_add 0x25
#define TX_add 0x17
#define ADC_DELAY 2000
#include <avr/io.h>
#include "m_imu.h"
#include "m_bus.h"
#include "m_usb.h"
#include "m_general.h"
#include "m_rf.h"
#include <stdlib.h>
#include "m_wireless.h"
//////////////////////////////////////////////////
//
// Global Stuff
//
//////////////////////////////////////////////////
char new = 0;
char receive_buffer[p_length]= {0} ;
char send_buffer[p_length] = {0} ; // [ leftB, rightB, VLadc, = , VRadc, = , HLadc, =, HRadc, =, _ ]
/* send_buffer[0] = leftB
send_buffer[1] = rightB
send_buffer[2 & 3] = VLadc
send_buffer[4 & 5] = VRadc
send_buffer[6 & 7] = HLadc
send_buffer[8 & 9] = HRadc
send_buffer[11] = playMode ( mk = 64 , sj = 1 , dj = 11 , tank = 100 )
*/
////////////////////////////////////////////////////
//
// SETUP methods
//
////////////////////////////////////////////////////
// Generic setupUSB
void setupUSB(){
m_usb_init();
m_green(ON);
while (!m_usb_isconnected());
m_green(OFF);
}
void setup_pins(){
clear(DDRB, 0 ); //set digital pins to inputs for limit switches
clear(DDRB, 4 ); //
clear(ADMUX,REFS1); // set Vref = Vcc
set(ADMUX,REFS0);
set(ADCSRA,ADPS2); // set ADC prescaller to 128 -> a 125Khz
set(ADCSRA,ADPS1);
set(ADCSRA,ADPS0);
set(DIDR0,ADC0D);
set(DIDR0,ADC1D);
set(DIDR0,ADC4D);
set(DIDR0,ADC5D);
}
void setup_timer_3(){
// while (!check(TIFR3, TOV3)); ///// timer 3 runs at 100Hz
// set(TIFR3,OCF3A);
OCR3A = 625; //1600 /625/ 256 >> 100Hz clock
set(TCCR3B, CS32); clear(TCCR3B, CS31); clear(TCCR3B, CS31); // <-- clock Prescaller 16MHz/1024
clear(TCCR3B, WGM33); set(TCCR3B, WGM32);
clear(TCCR3A, WGM31); clear(TCCR3A, WGM30); //(mode 4) UP to OCR3A
}
////////////////////////////////////////////////////
//
// READ BUTTONS AND ADC
//
////////////////////////////////////////////////////
// Check the two buttons to see if pressed.
void check_buttons(){
send_buffer[0]=( check(PINB, 0) ? 1 : 0);
send_buffer[1]=( check(PINB ,4) ? 1 : 0);
}
// For checking the 4 adc channels and returning value - offset (also normalizing direction)
void update_ADC( int VLOff,int HLOff,int VROff, int HROff ){
clear(ADCSRA,ADEN);
clear(ADCSRB, MUX5); // set pin to F0
clear(ADMUX, MUX2);
clear(ADMUX, MUX1);
clear(ADMUX, MUX0);
set(ADCSRA, ADEN); // start conversion process
set(ADCSRA, ADSC);
while(!check(ADCSRA,ADIF)); // wait to finish
set(ADCSRA,ADIF);
int VL = 1023 - (ADC) - VLOff;
send_buffer[2]= *(char*)&VL;
send_buffer[3]= *( ((char*)&VL) + 1);
clear(ADCSRA,ADEN);
clear(ADCSRB, MUX5); // set pin to F1
clear(ADMUX, MUX2);
clear(ADMUX, MUX1);
set(ADMUX, MUX0);
set(ADCSRA, ADEN); // start conversion process
set(ADCSRA, ADSC);
while(!check(ADCSRA,ADIF)); // wait to finish
set(ADCSRA,ADIF);
int VR =1023 - (ADC) - VROff;
send_buffer[4]= *(char*)&VR;
send_buffer[5]= *( ((char*)&VR) + 1);
clear(ADCSRA,ADEN);
clear(ADCSRB, MUX5); // set pin to F4
set(ADMUX, MUX2);
clear(ADMUX, MUX1);
clear(ADMUX, MUX0);
set(ADCSRA, ADEN); // start conversion process
set(ADCSRA, ADSC);
while(!check(ADCSRA,ADIF)); // wait to finish
set(ADCSRA,ADIF);
int HL = 1023- (ADC ) - HLOff;
send_buffer[6]= *(char*)&HL;
send_buffer[7]= * (((char*)&HL)+1);
clear(ADCSRA,ADEN);
clear(ADCSRB, MUX5); // set pin to F5
set(ADMUX, MUX2);
clear(ADMUX, MUX1);
set(ADMUX, MUX0);
set(ADCSRA, ADEN); // start conversion process
set(ADCSRA, ADSC);
while(!check(ADCSRA,ADIF)); // wait to finish
set(ADCSRA,ADIF);
int HR = ADC - HROff;
send_buffer[8]= *((char*)&HR);
send_buffer[9]= *(((char*)&HR)+1);
}
//////////////////////////////////////////////////
//
// drive Modes
//
//////////////////////////////////////////////////
bool Mario = false;
bool single_joy = false;
bool double_joy = false;
bool tank_mode = false;
void set_drive_mode( bool L_bump, bool R_bump);
////////////////////////////////////////////////////
//
// COM
//
////////////////////////////////////////////////////
void TX_comm(){
if (m_rf_send( RX_add, send_buffer, p_length) == 0 ){m_red(1);m_green(1);};
}
//////////////////////////////////////////////////
//
// Debug Stuff
//
//////////////////////////////////////////////////
void DEBUG_vals_usb(void );
void debug_ADC_vals(int Calibration, long VRightSum,long VLeftSum, long HLeftSum, long HRightSum );
void debug_ADC_sums(int timer_3_cnt, long VRightSum, long VLeftSum, long HLeftSum, long HRightSum );
bool debug_ADC = false ;
////////////////////////////////////////////////////
//
// CONFIG
//
////////////////////////////////////////////////////
// for configuring drive mode based on buttons pressed during config.
void set_drive_mode( bool L_bump, bool R_bump){
if ( L_bump && R_bump ) { Mario = true;}
else if (!L_bump && R_bump ) { tank_mode = true;}
else if ( L_bump && !R_bump ) { double_joy = true;}
else if ( !L_bump && !R_bump ) { single_joy = true;}
if ( Mario ) { send_buffer[11] = 64; m_red(1); m_green(1);}
else if ( tank_mode ) { send_buffer[11] =100; m_red(1); m_green(0);}
else if ( double_joy ) { send_buffer[11] = 11; m_red(0); m_green(1);}
else if ( single_joy ) { send_buffer[11] = 1; m_red(0); m_green(0);}
}
int main(void) {
m_clockdivide(0);
m_bus_init();
setup_timer_3();
m_rf_open(chan,TX_add,p_length);
setup_pins();
if (debug_ADC){ setupUSB();}
long VLeftSum=0;
long HLeftSum=0;
long VRightSum=0;
long HRightSum=0;
int cnt_RB = 0;
int cnt_LB = 0;
/////////////////////////////////////////////////////////////////////////////////
//
// give me a half second to go from switching on the m2 to holding the config buttons.
//
//////////////////////////////////////////////////////////////////////////////////
m_green(1);
int timer_3_cnt=0;
int Calibration = 50;
for (timer_3_cnt=0 ; timer_3_cnt< Calibration ; ){
if(check(TIFR3,OCF3A)){ ///// timer 3 runs at 100Hz
set(TIFR3,OCF3A );
timer_3_cnt++;
}
}
m_red(ON);
/////////////////////////////////////////////////////////////////////////////////
//
// a half second
// - Computing the config by counting time when the config buttons are pressed
// - Finding the average middle analog value for the 4 adc channels for offset
//////////////////////////////////////////////////////////////////////////////////
for (timer_3_cnt=0 ; timer_3_cnt<Calibration ; ){
if(check(TIFR3,OCF3A)){ ///// timer 3 runs at 100Hz
set(TIFR3,OCF3A );
timer_3_cnt++;
update_ADC(0,0,0,0);
check_buttons();
if (send_buffer[1]==1){ cnt_RB++ ;}
if (send_buffer[0]==1){ cnt_LB++ ;}
VLeftSum += *(int*)(&send_buffer[2]);
HLeftSum += *(int*)(&send_buffer[6]);
VRightSum += *(int*)(&send_buffer[4]);
HRightSum += *(int*)(&send_buffer[8]);
if (debug_ADC){debug_ADC_sums( timer_3_cnt, VRightSum, VLeftSum, HLeftSum, HRightSum );}
}
}
if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
/////////////////////////////////////////////////////////////////////////////////
// - setting the config by pressing the config buttons.
// - Setting the average middle analog value for the 4 adc channels for offset
//////////////////////////////////////////////////////////////////////////////////
int VLeftOffset = (int)(VLeftSum/Calibration);
int HLeftOffset = (int)(HLeftSum/Calibration);
int VRightOffset= (int)(VRightSum/Calibration);
int HRightOffset= (int)(HRightSum/Calibration);
bool L_bump = ( cnt_LB > Calibration/2 );
bool R_bump = ( cnt_RB > Calibration/2 );
set_drive_mode( L_bump , R_bump );
if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
if (debug_ADC){debug_ADC_vals( Calibration, VRightSum, VLeftSum, HLeftSum, HRightSum );}
m_green(0);
m_red(OFF);
while (1){
check_buttons();
update_ADC(VLeftOffset, HLeftOffset, VRightOffset, HRightOffset);
TX_comm();
if (debug_ADC){DEBUG_vals_usb();}
}
}
// DEBUG
void debug_ADC_sums(int timer_3_cnt, long VRightSum,long VLeftSum, long HLeftSum, long HRightSum ){
m_usb_tx_int(timer_3_cnt);
m_usb_tx_string(" me \t");
m_usb_tx_long(VRightSum);
m_usb_tx_char('\t');
m_usb_tx_long(HRightSum);
m_usb_tx_char('\t');
m_usb_tx_long(VLeftSum);
m_usb_tx_char('\t');
m_usb_tx_long(HLeftSum);
m_usb_tx_string("\t\n\r");
}
void debug_ADC_vals( int Calibration, long VRightSum,long VLeftSum, long HLeftSum, long HRightSum ){
int VLeftOffset = (int)(VLeftSum/Calibration);
int HLeftOffset = (int)(HLeftSum/Calibration);
int VRightOffset= (int)(VRightSum/Calibration);
int HRightOffset= (int)(HRightSum/Calibration);
m_usb_tx_string("VLeftOffset\t");
m_usb_tx_int(VLeftOffset);
m_usb_tx_char('\t');
m_usb_tx_string("HLeftOffset\t");
m_usb_tx_int(HLeftOffset);
m_usb_tx_char('\t');
m_usb_tx_string("VRightOffset\t");
m_usb_tx_int(VRightOffset);
m_usb_tx_char('\t');
m_usb_tx_string("LRightOffset\t");
m_usb_tx_int(HRightOffset);
m_usb_tx_char('\t');
}
/*
* On new values print to usb for debugging.
*/
void DEBUG_vals_usb(void ){
new = 0;
int i; int a;
if (send_buffer[11] == 1 ){ m_usb_tx_string("Single Joystick Driving\t"); }
if (send_buffer[11] ==11 ){ m_usb_tx_string("Double Joystick Driving\t"); }
if (send_buffer[11] ==64 ){ m_usb_tx_string("Mario Kart RACING!\t"); }
if (send_buffer[11] == 3 ){ m_usb_tx_string("Keyboard control \t"); }
m_usb_tx_string("Button L: ");
if (send_buffer[1]==1) {m_usb_tx_string(" on"); }
else {m_usb_tx_string("off");}
m_usb_tx_string(": \t");
m_usb_tx_string("\tButton R: ");
if (send_buffer[0]==1) {m_usb_tx_string(" on") ;}
else {m_usb_tx_string("off");}
m_usb_tx_string(": \t");
for (i=0; i < 4 ; i++){
m_usb_tx_string("\tpot ");
m_usb_tx_int(i*2+1);
m_usb_tx_string(": \t");
a = *(int*)&send_buffer[i*2+2];
m_usb_tx_int(a);
}
m_usb_tx_string("\n\r");
}