-
Notifications
You must be signed in to change notification settings - Fork 0
/
TechDemo4.c
428 lines (342 loc) · 9.59 KB
/
TechDemo4.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
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
// <editor-fold defaultstate="collapsed" desc="0. Code information">
//Author: Nicolo Frisiani
//Project: ESP-13
//Year: 2017
//Version: 5.0
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="1. File inclusions required">
#include "xc_configuration_bits.h"
#include "adc.h"
#include "timers.h"
#include "delays.h"
#include "math.h"
#include "pwm.h"
#include "capture.h"
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="2. Function Declarations">
//Configuration functions
void config_PWM(void);
void config_LS(void);
void config_PS(void);
//Motor functions
void stop(void);
void move(int PID_error);
void scan(int error);
void turn180 (void);
void Rmotor(int power);
void Lmotor(int power);
//Line sensor functions
void LEDarray_on(void);
void LEDarray_off(void);
void LSarray_read(void);
void LEDarray_write(unsigned char x);
unsigned char LEDarray_breakdetected(void);
//PID functions
int computeError(void);
int error_switch(int sum);
int PID(int error);
//Proximity sensor functions
void interrupt isr(void);
void enable_global_interrupts(void);
//Speed encoder functions
//none required yet
//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="3. Global variables">
int x = 0;
int time180 = 1000;
int integral = 0;
int prev_error = 0;
int DTimer = 0;
int P = 0;
int I = 0;
int D = 0;
int Ki = 45;
int Kd = 4;
int Kp = 12;
volatile char y=0,s=0;
volatile unsigned int logic_high =0;
unsigned char LS_val[6] = {0, 0, 0, 0, 0, 0};
unsigned char last_val = 0;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="4. Main Line Code">
int main(void)
{
config_LS();
config_PS();
config_PWM();
enable_global_interrupts();
OpenADC(ADC_FOSC_16 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0);
while (1)
{
LSarray_read();
int error = computeError();
int PID_error = PID(error);
int temp = 0;
for(int i = 0; i < 6; i++)
{
temp += LS_val[i];
}
if(temp == 0)
{
Delay10KTCYx(15);
LSarray_read();
for(int i = 0; i < 6; i++)
{
temp += LS_val[i];
}
if(temp == 0)
{
stop();
Delay10KTCYx(20);
INTCONbits.GIE = 0;
INTCONbits.PEIE = 0;
PORTHbits.RH3 = 0;
while(1);
}
}
move(PID_error);
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="5. Functions">
//Configuration functions
void config_PWM(void)
{
TRISGbits.RG3 = 0;
TRISGbits.RG4 = 0;
TRISH = 0b10010100;
//enable bit
PORTHbits.RH3 = 0;
//unipolar setting
PORTHbits.RH0 = 1;
PORTHbits.RH1 = 1;
//direction bits
PORTHbits.RH5 = 0;
PORTHbits.RH6 = 0;
//timer configuration
OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
//OpenPWM2
OpenPWM4(252);
OpenPWM5(252);
}
void config_LS(void)
{
ADCON1 = 0x00;
TRISA = 0b00000000;
}
void config_PS(void)
{
OpenTimer3(TIMER_INT_OFF & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_ON & T1_SYNC_EXT_OFF);
OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1);
WriteTimer0(12536);
OpenCapture3(CAPTURE_INT_ON & C3_EVERY_RISE_EDGE);
TRISB =0x00;
TRISC = 0x00;
}
//Motor functions
void Rmotor(int power)
{
SetDCPWM4(power);
}
void Lmotor(int power)
{
SetDCPWM5(power);
}
void stop(void)
{
Rmotor(350);
Lmotor(350);
}
void move(int PID_error)
{
PORTHbits.RH3 = 1;
if(PID_error > 820)
{
PID_error = 820;
}
else if(PID_error < -820)
{
PID_error = -820;
}
if(PID_error >= 0)
{
Rmotor(820);
Lmotor(820-PID_error);
}
else
{
Rmotor(820+PID_error);
Lmotor(820);
}
}
void scan(int error)
{
PORTHbits.RH3 = 1;
Rmotor(500+error);
Lmotor(500-error);
}
void turn180(void)
{
PORTHbits.RH3 = 1;
Rmotor(350);
Lmotor(650);
Delay10KTCYx(120);
integral = 0;
unsigned char t = 0;
while(t == 0)
{
LSarray_read();
t = LS_val[0] + LS_val[1] + LS_val[2] + LS_val[3] + LS_val[4] + LS_val[5];
}
}
//Line sensor functions
void LEDarray_on(void)
{
LATA = 0b00111111;
}
void LEDarray_off(void)
{
LATA = 0b00000000;
}
void LSarray_read(void)
{
int value = 0;
for(int i = 0; i < 6; i++)
{
value = 0;
switch(i)
{
case 0: SetChanADC(ADC_CH5);
break;
case 1: SetChanADC(ADC_CH6);
break;
case 2: SetChanADC(ADC_CH7);
break;
case 3: SetChanADC(ADC_CH8);
break;
case 4: SetChanADC(ADC_CH9);
break;
case 5: SetChanADC(ADC_CH10);
break;
default:break;
}
ConvertADC();
while(BusyADC());
value = ReadADC();
unsigned char temp = i;
if(value > 800)
LS_val[i] = 1;
else
LS_val[i] = 0;
}
}
void LEDarray_write(unsigned char x)
{
LATA = x;
}
unsigned char LEDarray_breakdetected(void)
{
unsigned char breakdetected = 0;
breakdetected = 1;
return breakdetected;
}
//Proximity sensor functions
void enable_global_interrupts(void)
{
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
}
void interrupt isr(void)
{
if(INTCONbits.TMR0IF) //Proximity trigger signal
{
INTCONbits.TMR0IF =0;
s = s^1;
LATBbits.LATB0 = s; //J2 13
WriteTimer0(40563);
}
if(PIR3bits.CCP3IF == 1)
{
PIR3bits.CCP3IF = 0; //CCP3 interrupt bit zeroed
y= y^1; //switched between 1 and 0
if(y==1)
{
CCP3CON = 4; //configure CCP3 to interrupt on falling edge
WriteTimer3(0); //refresh timer3
}
else
{
logic_high = ReadTimer3();
CCP3CON = 5;
if(logic_high < 1550)
{
turn180();
}
}
}
}
//2e. PID functions
int computeError(void)
{
int close_left = 0;
int mid_left = 0;
int far_left = 0;
int close_right = 0;
int mid_right = 0;
int far_right = 0;
int error = 0;
int sum = LS_val[0] + LS_val[1] + LS_val[2] + LS_val[3] + LS_val[4] + LS_val[5];
if(sum == 1)
{
close_left = -1*LS_val[3];
mid_left = -15*LS_val[4];
far_left = -75*LS_val[5];
close_right = 1*LS_val[2];
mid_right = 15*LS_val[1];
far_right = 75*LS_val[0];
error = (close_left + mid_left + far_left + close_right + mid_right + far_right);
}
else if(sum == 2)
{
far_left = -45*LS_val[5] + -10*LS_val[4];
close_left = -10*LS_val[4] + 0*LS_val[3];
close_right = 0*LS_val[2] + 10*LS_val[1];
far_right = 10*LS_val[1] + 45*LS_val[0];
error = (close_left + far_left + close_right + far_right);
}
else if(sum == 3)
{
far_left = -20*LS_val[5] + -16*LS_val[4] + -9*LS_val[3];
close_left = -8*LS_val[4] + 0*LS_val[3] + 0*LS_val[2];
close_right = 8*LS_val[1] + 0*LS_val[2] + 0*LS_val[3];
far_right = 9*LS_val[2] + 16*LS_val[1] + 20*LS_val[0];
error = (close_left + far_left + close_right + far_right);
}
else if(sum == 4)
{
far_left = -10*LS_val[5] + 0*LS_val[4] + 0*LS_val[3] + 0*LS_val[2];
far_right = 0*LS_val[2] + 0*LS_val[2] + 0*LS_val[1] + 10*LS_val[0];
error = (close_left + far_left + close_right + far_right);
}
return error;
}
int PID(int error)
{
int output;
integral += (error - prev_error);
P = Kp*error;
I = 0;
//I = (integral + (Ki - 1))/Ki;
D = Kd*(error - prev_error);
DTimer++;
if(DTimer == 10)
{
prev_error = error;
DTimer = 0;
}
output = P + I + D;
return output;
}
//2e. Speed encoder functions
//none required yet
// </editor-fold>