-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
390 lines (346 loc) · 8.66 KB
/
main.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
#include "thor.h"
void main ( void )
{
init_sumo();
#ifdef MEASURE_TIME
test = 0;
T1CONbits.TMR1CS = 0; // Timer1 Clock Sourceis instruction clock
T1CONbits.T1CKPS = 0; // 1:8 Prescaler value
T1CONbits.nT1SYNC = 1; // Disable Sync
PIE1bits.TMR1IE = 1;
PIR1bits.TMR1IF = 0;
contador.Val = 0;
TMR1H = TMR1L = 0x00;
T1CONbits.TMR1ON = 1; //Turn on Timer1 Module
#else
if ( !isDebug )
{
for ( i = 0; i < 4; i++ )
{
LED_TX = !DIR_SPIN;
LED_RX = DIR_SPIN;
delay_ms(100);
LED_TX = LED_RX = 1;
delay_ms(100);
}
LED_TX = !DIR_SPIN;
LED_RX = DIR_SPIN;
}
while ( !START );
#endif
delay_s(5); // wait_5s();
#ifdef MEASURE_TIME
contador.HB = TMR1H;
contador.LB = TMR1L;
resultado = ((float) test * 65535.0 + (float) contador.Val) * (4.0 / 16000000.0);
printf("Tiempo de inicio Sumo ->(0xFFFF x %u segundos) + %u * TCY = %f\r\n", test, contador.Val, resultado);
#endif
for (;; )
{
get_sensors(); // Sensor sampling code
if ( !isDebug )
{
if ( Sumo.atFrontLeftEdge || Sumo.atFrontRightEdge || Sumo.atBackLeftEdge || Sumo.atBackRightEdge )
Sumo.state = SURVIVE;
switch ( Sumo.state )
{
case SURVIVE:
if ( Sumo.atFrontLeftEdge && Sumo.atFrontRightEdge )
{
sumo_move(BACKWARD);
delay_ms(300); //delay_ms(300);//Delay10KTCYx(125);
Sumo.searchClockwise = TRUE;
}
else if ( Sumo.atFrontLeftEdge )
{
sumo_move(BACKWARD);
delay_ms(300); //Delay10KTCYx(125);
Sumo.searchClockwise = TRUE;
}
else if ( Sumo.atFrontRightEdge )
{
sumo_move(BACKWARD);
delay_ms(300); //Delay10KTCYx(125);
Sumo.searchClockwise = FALSE;
}
else if ( Sumo.atBackLeftEdge && Sumo.atBackRightEdge )
{
sumo_move(FORWARD);
}
else if ( Sumo.atBackLeftEdge || Sumo.atBackRightEdge )
{
sumo_move(RIGHT);
delay_ms(185); //Delay10KTCYx(75);
}
else if ( Sumo.atBackRightEdge )
{
sumo_move(LEFT);
delay_ms(185); //Delay10KTCYx(75);
}
Sumo.state = HUNT;
break;
case HUNT:
if ( rangeAverage > mAttackThreshold )
{
Sumo.state = ATTACK;
}
if ( Sumo.first_time )
{
if ( DIR_SPIN )
Sumo.searchClockwise = 1;
else
Sumo.searchClockwise = 0;
Sumo.first_time = 0;
}
if ( Sumo.searchClockwise )
sumo_move(RIGHT);
else
sumo_move(LEFT);
break;
case ATTACK:
sumo_move(FORWARD);
if ( rangeAverage < mTargetThreshold )
{
Sumo.state = HUNT;
}
break;
default:
Sumo.state = SURVIVE;
}
}
}
}
void init_sumo ( void )
{
#if defined __16F1937
OSCCONbits.SCS = 0;
OSCCONbits.IRCF = 0b1111; // INTOSC to 16MHz
OSCCONbits.SPLLEN = 0; // Enable PLL
#endif
ML_IN1 = ML_IN2 = MR_IN1 = MR_IN2 = M_ENB = 1; // Motor Break
ML_IN1_TRIS = ML_IN2_TRIS = MR_IN1_TRIS = MR_IN2_TRIS = M_ENB_TRIS = 0; // Motor Control pins as outputs
DIR_SPIN_TRIS = 1;
DIR_SPIN_ANSEL = 0;
START_TRIS = 1;
START_ANSEL = 0;
if ( START )
{
isDebug = TRUE;
}
//ENABLE_TRIS = 1;
#if defined __18F4550
OpenADC(ADC_FOSC_16 & ADC_RIGHT_JUST & ADC_4_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS, ADC_11ANA);
#elif defined __16F1937
OL_TRIS = FL_TRIS = BL_TRIS = OR_TRIS = FR_TRIS = BR_TRIS = 1;
OL_ANSEL = FL_ANSEL = BL_ANSEL = OR_ANSEL = FR_ANSEL = BR_ANSEL = 1;
OpenADC();
#endif
if ( isDebug )
{
#if defined __18F4550
CloseUSART(); // Turn off usart if was previously on
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & // 9600,8,n,1: 129 - 20 MHz; 1249 - 48MHz; 1666 - 64MHz
USART_ASYNCH_MODE & USART_EIGHT_BIT & // 19200,8,n,1: 832 - 64MHz
USART_CONT_RX & USART_BRGH_HIGH, BAUD_GEN); // 115200,8,n,1: 138 - 64MHz
baudUSART(BAUD_16_BIT_RATE & BAUD_IDLE_CLK_LOW &
BAUD_WAKEUP_OFF & BAUD_AUTO_OFF & 0x48); // 16 bit BRG
#elif defined __16F1937
init_comms();
#endif
printf("Robot de Sumo Thor - Gary Servin 2012\r\n");
#ifdef MEASURE_TIME
printf("Iniciando medicion de duracion de tiempo reglamentario...\r\n");
#endif
}
else
{
mInitUARTLEDs();
}
// RCONbits.IPEN = 0; // Interruption Priority Disabled
INTCONbits.PEIE = 1; // Peripherial Interrupt Enabled
INTCONbits.GIE = 1; // Global Interrupt Enable
return;
}
void sumo_move ( char direction )
{
//L_IN1 = L_IN2 = R_IN1 = R_IN2 = 1; // Motor Break
if ( !isDebug )
{
switch ( direction )
{
case FORWARD:
ML_IN1 = MR_IN1 = 1;
ML_IN2 = MR_IN2 = 0;
break;
case BACKWARD:
ML_IN1 = MR_IN1 = 0;
ML_IN2 = MR_IN2 = 1;
break;
case LEFT:
ML_IN2 = MR_IN1 = 1;
ML_IN1 = MR_IN2 = 0;
break;
case RIGHT:
ML_IN1 = MR_IN2 = 1;
ML_IN2 = MR_IN1 = 0;
break;
case STOP:
ML_IN1 = ML_IN2 = MR_IN1 = MR_IN2 = 1;
break;
default:
ML_IN1 = ML_IN2 = MR_IN1 = MR_IN2 = 1;
break;
}
}
return;
}
void get_sensors ( void )
{
char i;
UINT16_VAL mFrontLeftLine, mFrontRightLine, mBackLeftLine, mBackRightLine, mLeftRange, mCenterRange, mRightRange;
mFrontLeftLine.Val = 0;
mFrontRightLine.Val = 0;
mBackLeftLine.Val = 0;
mBackRightLine.Val = 0;
mLeftRange.Val = 0;
mRightRange.Val = 0;
Sumo.atFrontLeftEdge = 0;
Sumo.atFrontRightEdge = 0;
Sumo.atBackLeftEdge = 0;
Sumo.atBackRightEdge = 0;
for ( i = 0; i < 6; i++ )
{
mLeftRange = ReadADC(OL_SENSOR);
}
for ( i = 0; i < 6; i++ )
{
mRightRange = ReadADC(OR_SENSOR);
}
for ( i = 0; i < 6; i++ )
{
mFrontLeftLine = ReadADC(FL_SENSOR);
}
for ( i = 0; i < 6; i++ )
{
mFrontRightLine = ReadADC(FR_SENSOR);
}
for ( i = 0; i < 6; i++ )
{
mBackLeftLine = ReadADC(BL_SENSOR);
}
for ( i = 0; i < 6; i++ )
{
mBackRightLine = ReadADC(BR_SENSOR);
}
// for ( i = 0; i < 4; i++ )
// {
// mLeftRange.Val += read_a2d(OL_SENSOR).Val;
// // __delay_ms(1);
// mRightRange.Val += read_a2d(OR_SENSOR).Val;
// // __delay_ms(1);
// mFrontLeftLine.Val += read_a2d(FL_SENSOR).Val;
// // __delay_ms(1);
// mFrontRightLine.Val += read_a2d(FR_SENSOR).Val;
// // __delay_ms(1);
// mBackLeftLine.Val += read_a2d(BL_SENSOR).Val;
// // __delay_ms(1);
// mBackRightLine.Val += read_a2d(BR_SENSOR).Val;
// // __delay_ms(1);
// }
// mLeftRange.Val >>= 2;
// mRightRange.Val >>= 2;
// mFrontLeftLine.Val >>= 2;
// mFrontRightLine.Val >>= 2;
// mBackLeftLine.Val >>= 2;
// mBackRightLine.Val >>= 2;
Sumo.atFrontLeftEdge = (mFrontLeftLine.Val < mEdgeFrontLeftThreshold);
Sumo.atFrontRightEdge = (mFrontRightLine.Val < mEdgeFrontRightThreshold);
Sumo.atBackLeftEdge = (mBackLeftLine.Val < mEdgeBackLeftThreshold);
Sumo.atBackRightEdge = (mBackRightLine.Val < mEdgeBackRightThreshold);
rangeDifference = mLeftRange.Val - mRightRange.Val;
rangeAverage = (mLeftRange.Val + mRightRange.Val) >> 1;
if ( isDebug )
{
//printf((string) "S%2.2X,T%2.2X,U00,V00,W00,X00,Y00,Z00\r\n", mLeftLine >> 2, mRightLine >> 2);
//printf((string) "L=%4d LR=%4d R=%4d RR=%4d OP=%4d OPR=%4d\r", mBackLeftLine, mEdgeBackLeftThreshold, mBackRightLine, mEdgeBackRightThreshold, mCenterRange, mTargetThreshold);
printf("FL= %4d FR= %4d BL= %4d BR= %4d OP_FL= %4d OP_FR= %4d\r\n", mFrontLeftLine.Val, mFrontRightLine.Val, mBackLeftLine.Val, mBackRightLine.Val, mLeftRange.Val, mRightRange.Val);
//printf("%d,%d,%d,%d,%d,%d\r\n", mFrontLeftLine, mFrontRightLine, mBackLeftLine, mBackRightLine, mLeftRange, mRightRange);
//printf((string) "OPPONENT = %u\r\n", mCenterRange);
// Delay1KTCYx(10);
}
return;
}
#if defined __18F4550
void wait_5s ( void )
{
//char i;
/*OpenTimer1(TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_8 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
for ( i = 1; i <= 50; i++ )
{
WriteTimer1(0x7E9A);
while ( !Sumo.timeout );
Sumo.timeout = 0;
if ( i == 10 || i == 20 || i == 30 || i == 40 )
{
LED_TX = !LED_TX;
}
else if ( i == 50 )
{
LED_TX = 1;
}
}
CloseTimer1();*/
//return;
int t;
for ( t = 0; t < 9; t++ )
{
Delay10KTCYx(230);
}
return;
}
void putch ( char data )
{
while ( !PIR1bits.TXIF )
continue;
TXREG = data;
}
int read_a2d ( unsigned char channel )
{
ADCON0 = (ADCON0 & 0b11000011) | ((channel << 2) & 0b00111100);
ADCON0bits.GO = 1; // Start conversion
Delay10TCYx(3);
while ( BusyADC() == 1 ); // While conversion is not finished
return ReadADC(); // Return 10 bit value
}
#elif defined __16F1937
void delay_ms ( UINT16 miliseconds )
{
UINT8 temp;
for ( temp = miliseconds; temp >= 1; temp-- )
__delay_ms(1);
}
void delay_s ( UINT16 seconds )
{
UINT8 temp;
for ( temp = seconds; temp >= 1; temp-- )
{
LED_TX = LED_RX = 1;
__delay_ms(250);
__delay_ms(250);
__delay_ms(250);
LED_TX = LED_RX = 0;
__delay_ms(240);
}
}
#endif
void interrupt IRS ( void )
{
#if defined MEASURE_TIME
if ( PIE1bits.TMR1IE && PIR1bits.TMR1IF )
{
test++;
PIR1bits.TMR1IF = 0;
}
#endif
}