-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
467 lines (444 loc) · 17.5 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* --------0.Project information--------------------
* Line follower
* Author : TRAN MINH THUAN
* Supported by: TRAN ANH TAI, NGUYEN TRAN MINH NGHIA
* Date: July 7th 2019
* Project associate with TM4C123, CCS version 9.
---------------------------------------------------*/
/* --------1.System requirement---------------------
1.Setup driver for 2 H-bridges to control 4 motors (UserLibraries/Double_H_Bridges)
a. GPIO Initialization for IN pins H-bridge 1 & IN pins H-bridge 2 to control motors direction
(PE0->PE3) (PD0 -> PD3)
b. PWM Initialization for ENA, ENB H-bridge 1 & ENC, END H-bridge 2 to control motors speed
(PC4,PC5) ()
c. Update_Speed function for changing the PWM duty cycles to vary speed of motors
d. Motors 1->4 direction functions
2. MECANUM wheels direction control
3. SYSTICK timer for timing purpose
4. Reading value of 5 IR Sensors (PB0 - PB4)
5. Algorithm for line-follower as smooth as possible (use PID if needed)
6. SRF05 sensor to detect object
7. Control cylinder to lift gripper (PE4-PE5) //CHANGE IMMEDIATELY
8. L3G4200D gyro sensor (PA6,PA7)
8. Running all together (with or without operating system, RTOS maybe)
---------------------------------------------------*/
////// ----------------2. Pre-processor Directives Section--------------------///////
#include <UserLibraries/Userlibs.h>
#include "UserLibraries/Double_H_Bridges.h"
#include "UserLibraries/Mecanum.h"
#include "UserLibraries/Delay_Systick.h"
#include "UserLibraries/IR_sensor.h"
#include "UserLibraries/Linear_Actuator.h"
#include "UserLibraries/I2C.h"
#include "UserLibraries/L3G4200D.h"
#include "UserLibraries/SRF05.h"
#include "UserLibraries/Buzzer.h"
#define MAX_ANGLE 4
#define MIN_ANGLE -4
#define DEFLECT_CW 1 //RIGHT
#define DEFLECT_CCW 2 //LEFT
#define BELOW_STOPA 10
#define AT_STOPA 11
#define BELOW_CONTAINER 12
#define BELOW_STOPB 13
#define AT_STOPB 14
#define RETURN 15
#define RETURN_AFTER_B 16
#define RETURN_AFTER_A 17
#define DISTANCE_2_OBSTACLE 45
//////------------------------------------------------------------------------///////
//testing variables
uint8_t ui8_test=4;
uint8_t ui8_LineStatus=0;
uint32_t prev_status=0;
uint32_t i=0;
uint16_t ui16_testmotor=0;
uint8_t ui8_M1Duty=25;
uint8_t ui8_M2Duty=32;
uint8_t ui8_M3Duty=25;
uint8_t ui8_M4Duty=32;
uint8_t Record[100]={0};
//system variables
uint8_t ui8Flag_L3G4200D_Wait=0;
uint8_t ui8Flag_ReadIRSensors_Wait=0;
uint8_t ui8Flag_DetectObstacle_Wait=0;
uint8_t ui8Flag_Buzzer_Wait=0;
uint8_t ui8Flag_StartCount=0;
uint8_t ui8_Position=BELOW_STOPA;
uint8_t ui8_Angle_Balanced=0;
uint8_t ui8_Obstacle=0;
uint8_t ui8_BuzzerOn=0;
uint8_t ui8_LineFollow=0;
uint16_t ui16_Obstacle_Distance=0;
unsigned long Tick=0;
unsigned long Tick_L3G4200D=0;
//unsigned long Tick_Calib_Rotation=0;
unsigned long Tick_ReadIRSensors=0;
unsigned long Tick_DetectObstacle=0;
unsigned long Tick_Buzzer=0;
unsigned long Tick_Counting=0;
float fl_gyrorate=0;
float fl_cur_angle=0;
float fl_offsetz=0;
////// ----------------3.Global Declarations Section--------------------------///////
//////------------------------------------------------------------------------///////
////// ----------------4. Subroutines Section---------------------------------///////
void main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_2_5| SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //80MHz
Systick_Init();
LA_GPIO_Init();
LA_Down();
BridgeH_GPIO_Init(); //Bridge H 1,2 GPIO(control reversing) Initialization
BridgeH_PWM_Init(); //Bridge H 1,2 PWM (control speed) Initialization
IR_GPIO_Init(); //IR sensors Initialization
Buzzer_Init();
SRF05_Init();
I2C_Init();
L3G4200D_Init();
// fl_offsetz=L3G4200D_Cal_Offset(500);
if (ui8_test==0) LA_Down();
if (ui8_test==1) LA_Up();
if (ui8_test==2) LA_Pause();
while(1)
{
//----------------------------------------------Task Scheduler---------------------------------------------------//
/////-----------------------------------Main tasks-------------------------------/////
//
////---------------------------Determine angle flag-------------------------------//
if( (fl_cur_angle >=MAX_ANGLE) || (fl_cur_angle<= MIN_ANGLE))
{
if(fl_cur_angle >=MAX_ANGLE) ui8_Angle_Balanced=DEFLECT_CW;
if(fl_cur_angle <=MIN_ANGLE) ui8_Angle_Balanced=DEFLECT_CCW;
}
//---------------------------Rotation to "0" angle-------------------------------//
if (ui8_Angle_Balanced==DEFLECT_CW)
{
Update_Speed(25, 29, 25, 29);
Turn_L();
if (fl_cur_angle<=0) ui8_Angle_Balanced=0;
}
if (ui8_Angle_Balanced==DEFLECT_CCW)
{
Update_Speed(25, 29, 25, 29);
Turn_R();
if (fl_cur_angle>=0) ui8_Angle_Balanced=0;
}
////---------------------------Detect obstacle-------------------------------//
if( (ui16_Obstacle_Distance>0) && (ui16_Obstacle_Distance<=DISTANCE_2_OBSTACLE ))
{
ui8_Obstacle=1;
}
else
{
ui8_Obstacle=0;
}
//---------------------------Handler obstacle-------------------------------//
if( (ui8_Obstacle==1) && (ui8_Position==BELOW_STOPA) )
{
Stop();
ui8_BuzzerOn=1;
}
else
{
ui8_BuzzerOn=0;
}
//--------------------------Line follow permission------------------------------//
if(ui8_Angle_Balanced==0)
{
if( (ui8_Obstacle==0) || (ui8_Position!=BELOW_STOPA) )
{
ui8_LineFollow=1;
}
else
{
ui8_LineFollow=0;
}
}
else //angle !=0
{
ui8_LineFollow=0;
}
//--------------------------Reach Stop A-------------------------------//
if(ui8_Position==AT_STOPA)
{
if(ui8_Obstacle==1)
{
ui8_BuzzerOn=0;
ui8_Position=BELOW_CONTAINER;
}
else
{
ui8Flag_StartCount=1;
if(Tick_Counting>=500000)
{
ui8_BuzzerOn=1;
}
}
}
//--------------------------Below Container-------------------------------//
if(ui8_Position==BELOW_CONTAINER)
{
if(ui16_Obstacle_Distance<=7)
{
Stop();
ui8_LineFollow=0;
LA_Up();
delay_us(100000);
LA_Pause();
ui8_Position=BELOW_STOPB;
ui8_LineFollow=1;
}
}
//--------------------------At Stop B-------------------------------//
if(ui8_Position==AT_STOPB)
{
Stop();
ui8_LineFollow=0;
LA_Down();
delay_us(300000);
ui8_Position=RETURN;
}
////---------------------------Line follow-------------------------------//
//
///////------------------------------------Periodic tasks----------------------------------------------/////
//---------------------------Calculate distance to obstacle-------------------------------//
if(ui8Flag_DetectObstacle_Wait==0)
{
ui16_Obstacle_Distance=SRF05_GetDistance();
ui8Flag_DetectObstacle_Wait=1;
}
//---------------------------Read IR sensors--------------------------//
if( (ui8Flag_ReadIRSensors_Wait==0) && (ui8_LineFollow==1) )
{
ui8_LineStatus=Get_Status();
ui8Flag_ReadIRSensors_Wait=1;
if(ui8_Position< RETURN)
{
switch (ui8_LineStatus)
{
case LEFT100:
Forward_R();
Update_Speed(0, 33, 38, 0); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case LEFT75:
Forward_R();
Update_Speed(0, 33, 38, 0); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case LEFT50:
Forward();
Update_Speed(28, 35, 28, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case LEFT25:
Forward();
Update_Speed(28, 35, 28, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case BALANCED:
Forward();
Update_Speed(28, 35, 28, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT25:
Forward();
Update_Speed(28, 35, 28, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT50:
Forward();
Update_Speed(28, 35, 28, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT75:
Forward_L();
Update_Speed(33, 0, 0, 38); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT100:
Forward_L();
Update_Speed(33, 0, 0, 38); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case 0:
if(ui8_Position==BELOW_STOPA)
{
Stop();
ui8_Position=AT_STOPA;
}
if(ui8_Position==BELOW_CONTAINER)
{
Forward();
Update_Speed(28, 35, 28, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
}
if(ui8_Position==BELOW_STOPB)
{
Stop();
ui8_Position=AT_STOPB;
}
break;
}
}
if(ui8_Position>= RETURN)
{
switch (ui8_LineStatus)
{
case LEFT100:
Backward_R();
Update_Speed(31, 0, 0, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case LEFT75:
Backward_R();
Update_Speed(31, 0, 0, 35); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case LEFT50:
Backward();
Update_Speed(26, 33, 26, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case LEFT25:
Backward();
Update_Speed(26, 33, 26, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case BALANCED:
Backward();
Update_Speed(26, 33, 26, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT25:
Backward();
Update_Speed(26, 33, 26, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT50:
Backward();
Update_Speed(26, 33, 26, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT75:
Backward_L();
Update_Speed(0, 31, 35, 0); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case RIGHT100:
Backward_L();
Update_Speed(0, 31, 35, 0); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
break;
case 0:
if(ui8_Position==RETURN)
{
Backward();
Update_Speed(25, 33, 25, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
ui8_Position=RETURN_AFTER_B;
}
if(ui8_Position==RETURN_AFTER_B)
{
Backward();
Update_Speed(25, 33, 25, 33); // Wheel 1,3 LEFT. Wheel 2, 4 RIGHT
ui8_Position=RETURN_AFTER_A;
}
break;
case OFFLINE:
if(ui8_Position==RETURN_AFTER_A)
Stop();
break;
}
}
if(prev_status!=ui8_LineStatus) //Backup old position
{
Record[i]=ui8_LineStatus;
i++;
prev_status=ui8_LineStatus;
}
}
//---------------------------Update current orientation--------------------------//
if(ui8Flag_L3G4200D_Wait==0)
{
fl_gyrorate=L3G4200D_Get_RawValue();
if(fl_gyrorate!=0)
{
fl_gyrorate=(fl_gyrorate-OFFSETZ)*0.00875;
fl_cur_angle+=fl_gyrorate*0.005;
ui8Flag_L3G4200D_Wait=1;
}
}
//---------------------------Buzzer control--------------------------//
if(ui8Flag_Buzzer_Wait==0)
{
if(ui8_BuzzerOn==1)
{
Buzzer_Toggle();
}
else
{
Buzzer_OFF();
}
ui8Flag_Buzzer_Wait=1;
}
//---------------------------------------------- Test modules---------------------------------------------------
// LINE FOLLOWER TEST 2
// ui8_LineStatus=Get_Status();
// if(ui8_center==0)
// {
// while( (ui8_LineStatus==25) || (ui8_LineStatus>=28 ) ) //lech phai
// {
// Forward_L();
// ui8_LineStatus=Get_Status();
// if ( (ui8_LineStatus==BALANCED) || (ui8_LineStatus==LEFT25) || (ui8_LineStatus==LEFT25))
// {
// ui8_center=1;
// break;
// }
// delay_us(100);
// }
// while (ui8_LineStatus<=23)
// {
// Forward_R();
// ui8_LineStatus=Get_Status();
// if ( (ui8_LineStatus==BALANCED) || (ui8_LineStatus==LEFT25) || (ui8_LineStatus==LEFT25))
// {
// ui8_center=1;
// break;
// }
// delay_us(100);
// }
// if ( (ui8_LineStatus==BALANCED) || (ui8_LineStatus==LEFT25) || (ui8_LineStatus==LEFT25))
// {
// ui8_center=1;
// }
// }
// if(ui8_center==1)
// {
// Forward();
// }
// if((ui8_LineStatus==LEFT75) || (ui8_LineStatus==LEFT100) || (ui8_LineStatus==RIGHT75) || (ui8_LineStatus==RIGHT100))
// {
// ui8_center=0;
// }
// delay_us(10000);
// MECANUM rotation test
// if(ui8_test==0) Stop();
// if(ui8_test==2) Forward();
// if(ui8_test==8) Backward();
// if(ui8_test==6) Left();
// if(ui8_test==4) Right();
// if(ui8_test==3) Forward_L();
// if(ui8_test==1) Forward_R();
// if(ui8_test==9) Backward_L();
// if(ui8_test==7) Backward_R();
// if(ui8_test==16) Turn_L();
// if(ui8_test==14) Turn_R();
// Update_Speed(ui8_M1Duty,ui8_M2Duty,ui8_M3Duty,ui8_M4Duty);
// MOTORs test
// if(ui16_testmotor==10) Motor1_Stop();
// if(ui16_testmotor==20) Motor2_Stop();
// if(ui16_testmotor==30) Motor3_Stop();
// if(ui16_testmotor==40) Motor4_Stop();
//
// if(ui16_testmotor==11) Motor1_Forward();
// if(ui16_testmotor==21) Motor2_Forward();
// if(ui16_testmotor==31) Motor3_Forward();
// if(ui16_testmotor==41) Motor4_Forward();
//
// if(ui16_testmotor==12) Motor1_Backward();
// if(ui16_testmotor==22) Motor2_Backward();
// if(ui16_testmotor==32) Motor3_Backward();
// if(ui16_testmotor==42) Motor4_Backward();
// Update_Speed(ui8_M1Duty,ui8_M2Duty,ui8_M3Duty,ui8_M4Duty);
// LA TEST
// if (ui8_test==0) LA_Down();
// if (ui8_test==1) LA_Up();
// if (ui8_test==2) LA_Pause();
// delay_us(3000000);
// Mobile_Stop();
}
}
//////------------------------------------------------------------------------///////