-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTD3(LS).c
313 lines (277 loc) · 9.12 KB
/
TD3(LS).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
//0. Code information
//Authors: Thomas Hollis, Charles Shelbourne
//Project: ESP-18
//Year: 2017
//Version: 3.2
//1. File inclusions required
#include "xc_config_settings.h"
#include "plib/adc.h"
#include "plib/timers.h"
#include "plib/delays.h"
#include "math.h"
#include "plib/pwm.h"
#include "timers.h"
#include "capture.h"
//2. Function prototypes
//2a. Configuration functions
void config_PWM(void);
void config_LS(void);
void config_PS(void);
//2b. Motor functions
void move(int angle, int forward);
void Rmotor(int power);
void Lmotor(int power);
//2c. Line sensor functions
void LEDarray_on(void);
void LEDarray_off(void);
unsigned char LSarray_read(void);
void LEDarray_write(unsigned char x);
unsigned char LEDarray_breakdetected(void);
//2d. Proximity sensor functions
void interrupt isr(void);
void enable_global_interrupts(void);
//2e. Speed encoder functions
//none required yet
//3. Global variables
int x = 0;
int time360 = 2000;
volatile char y=0,s=0;
volatile unsigned int logic_high =0;
//4. Main Line Code (MLC)
int main(void)
{
config_LS();
LEDarray_on();
Delay10KTCYx(250);
LEDarray_off();
Delay10KTCYx(250);
config_PS();
enable_global_interrupts();
while (LEDarray_breakdetected() == 0)
{
LEDarray_off();
}
LEDarray_on();
Delay10KTCYx(25);
LEDarray_off();
Delay10KTCYx(25);
LEDarray_on();
Delay10KTCYx(25);
LEDarray_off();
Delay10KTCYx(25);
OpenADC(ADC_FOSC_16 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0);
while (1)
{
LEDarray_write(LSarray_read());
//Delay1KTCYx(25);
}
}
//5. Functions
//2a. Configuration functions
void config_PWM(void)
{
//pwm output
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;
}
//2b. Motor functions
void move(int angle, int forward)
{
int exec_time_ms = ((double)sqrt(angle*angle)/360.0)*time360;
if(forward == 3)
{
PORTHbits.RH3 = 1;
Rmotor(700);
Lmotor(800);
Delay10KTCYx(225);
PORTHbits.RH3 = 0;
}
if(forward == 2)
{
PORTHbits.RH3 = 1;
Rmotor(700);
Lmotor(700);
Delay10KTCYx(125);
Delay10KTCYx(125);
PORTHbits.RH3 = 0;
}
if(forward == 1)
{
PORTHbits.RH3 = 1;
Rmotor(625);
Lmotor(625);
Delay10KTCYx(125);
Delay10KTCYx(125);
PORTHbits.RH3 = 0;
}
else if(forward == -1)
{
PORTHbits.RH3 = 1;
Rmotor(375);
Lmotor(375);
Delay10KTCYx(125);
Delay10KTCYx(125);
PORTHbits.RH3 = 0;
}
else if(forward == 0)
{
if(angle > 0)
{
PORTHbits.RH3 = 1;
Rmotor(700);
Lmotor(300);
for(int i = 0; i < exec_time_ms; i++)
{
Delay10TCYx(200);
Delay10TCYx(200);
}
PORTHbits.RH3 = 0;
}
else
{
PORTHbits.RH3 = 1;
Rmotor(300);
Lmotor(700);
for(int i = 0; i < exec_time_ms; i++)
{
Delay10TCYx(200);
Delay10TCYx(200);
}
PORTHbits.RH3 = 0;
}
}
}
void Rmotor(int power)
{
SetDCPWM4(power);
}
void Lmotor(int power)
{
SetDCPWM5(power);
}
//2c. Line sensor functions
void LEDarray_on(void)
{
LATA = 0b00111111;
}
void LEDarray_off(void)
{
LATA = 0b00000000;
}
unsigned char LSarray_read(void)
{
unsigned char LS_val[6] = {0, 0, 0, 0, 0, 0};
unsigned char LS_array = 0;
int value = 0;
int checking = 0;
for(int i = 0; i < 6; i++)
{
value = 0;
switch(i)
{
case 0: SetChanADC(ADC_CH10);
break;
case 1: SetChanADC(ADC_CH9);
break;
case 2: SetChanADC(ADC_CH8);
break;
case 3: SetChanADC(ADC_CH7);
break;
case 4: SetChanADC(ADC_CH6);
break;
case 5: SetChanADC(ADC_CH5);
break;
default:break;
}
ConvertADC();
while(BusyADC());
value = ReadADC();
if(value > 700)
LS_val[i] = 1;
else
LS_val[i] = 0;
checking = LS_val [i];
LS_array += LS_val[i]*pow(2,i);
}
return LS_array;
}
void LEDarray_write(unsigned char x)
{
LATA = x;
}
unsigned char LEDarray_breakdetected(void)
{
unsigned char breakdetected = 0;
//Tom to Charlie: write a function that detects a small drop in voltage (comparator may be needed, I may join in on this one too - we might also bullshit our way through it)
//Line break ignored since too small to impact TCRT reading
breakdetected = 1;
return breakdetected;
}
//2d. Proximity sensor functions
void enable_global_interrupts(void)
{
INTCONbits.GIE =1;
INTCONbits.PEIE =1;
}
void interrupt isr(void)
{
if(INTCONbits.TMR0IF == 1)
{ //Proximity trigger signal
INTCONbits.TMR0IF =0;
s = s^1;
LATBbits.LATB0 =s; //J2 13
WriteTimer0(40563);
}
if(PIR3bits.CCP3IF == 1)
{
PIR3bits.CCP3IF =0; //CCP4 interrupt bit zeroed
y= y^1; //switched between 1 and 0
if(y==1)
{
CCP3CON = 4; //configure CCP 4 to interrupt on falling edge
WriteTimer3(0); //refresh timer3
}
else
{
logic_high = ReadTimer3();
CCP3CON = 5; //configure to interrupt on rising edge
if(logic_high<1360)
{ //1360 ticks of clock before LED's turn on relates to 544uS that echo signal is high => aprox 11.3cm
LATBbits.LATB1=1; // uS/48 = distance
}
else
{
LATBbits.LATB1=0;
}
}
}
}
//2e. Speed encoder functions
//none required yet