forked from DrakerDG/Webotz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine_Follower_Robot_V4.c
214 lines (169 loc) · 5.24 KB
/
Line_Follower_Robot_V4.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
/*
* File: Line_Follower_Robot_V4.c
* Date: September-20-2020
* Description: This is the controller to simulate the operation
of the robot, similar to the physical model
* Author: DrakerDG (https://www.youtube.com/user/DrakerDG)
* Modifications: V4
*/
#include <stdio.h>
#include <webots/robot.h>
#include <webots/motor.h>
#include <webots/distance_sensor.h>
#include <webots/led.h>
#include <webots/camera.h>
#define LEFT 0
#define RIGHT 1
#define TIME_STEP 32
// 5 IR ground color sensors
#define NB_GROUND_SENS 5
#define NB_LEDS 5
#define MAX_GS 880 //840
#define MIN_GS 300
#define NEW_GS 1000
// Overlap factor
#define OL 200
// IR Ground Sensors
WbDeviceTag gs[NB_GROUND_SENS];
unsigned short gs_value[NB_GROUND_SENS] = {0, 0, 0, 0, 0};
short gs_new[NB_GROUND_SENS] = {0, 0, 0, 0, 0};
// Test array
unsigned short maxGS[NB_GROUND_SENS]= {500, 500, 500, 500, 500};
unsigned short minGS[NB_GROUND_SENS]= {500, 500, 500, 500, 500};
// LEDs
WbDeviceTag led[NB_LEDS];
// Motors
WbDeviceTag left_motor, right_motor;
// Reading Ground Sensors Module
unsigned long Position = 0;
bool online = false;
void ReadGroudSensors(void){
online = false;
unsigned long avgS = 0;
unsigned int sumS = 0;
for(int i=0; i<NB_GROUND_SENS; i++){
gs_value[i] = wb_distance_sensor_get_value(gs[i]);
// Max & Min detection
if(gs_value[i]<minGS[i]) minGS[i]=gs_value[i];
if(gs_value[i]>maxGS[i]) maxGS[i]=gs_value[i];
// linear Interpolation
gs_new[i] = ((float)gs_value[i]-MIN_GS)/(MAX_GS-MIN_GS)*NEW_GS;
// Limited values between 0 and 1000 (NEW_GS)
if(gs_new[i]>NEW_GS) gs_new[i]=NEW_GS;
if(gs_new[i]<0) gs_new[i]=0;
if(gs_new[i]>200)online = true;
if(gs_new[i]>50){
// Average groud sensor value
avgS += (unsigned long)gs_new[i]*(i*NEW_GS);
// Sum ground sensor value
sumS += gs_new[i];
}
}
if(online)Position = avgS/sumS; // Position Calculation
else if(Position < NEW_GS)Position = 0; // Left Sensor Memory Position
else Position = NEW_GS*4; // Right Sensor Memory Position
}
int lfm_speed[2];
long P=0, I=0, D=0, pErr=0, PID=0;
float Kp=0.80; // 0.90
float Ki=0.00; // 0.00
float Kd=0.02; // 0.02
#define LFM_FS 2000 //2000
void LineFollowingModule(void) {
// Error Position Calculation & PID
P = Position - NEW_GS*2;
I = P + pErr;
D = P - pErr;
PID = Kp*P + Ki*I + Kd*D;
pErr = P;
// printf("GS: %4d %4d %4d; Max: %4d %4d %4d; Min: %4d %4d %4d \n", gs_value[0], gs_value[1], gs_value[2], maxGS[0], maxGS[1], maxGS[2], minGS[0], minGS[1], minGS[2]);
lfm_speed[LEFT] = LFM_FS + PID;
lfm_speed[RIGHT] = LFM_FS - PID;
}
int timeLED = 0;
void PosLED(){
if((Position>(NEW_GS*1.5))&&(Position<(NEW_GS*2.5))) wb_led_set(led[1], 1);
else wb_led_set(led[1], 0);
if(online){
if(Position<(NEW_GS*2-OL)) wb_led_set(led[0], 1);
else wb_led_set(led[0], 0);
if(Position>(NEW_GS*2+OL)) wb_led_set(led[2], 1);
else wb_led_set(led[2], 1);
}
else{
if((Position<(NEW_GS*2-OL))&&(timeLED>10)){
wb_led_set(led[0], !wb_led_get(led[0]));
timeLED = 0;
}
if((Position>(NEW_GS*2+OL))&&(timeLED>10)){
wb_led_set(led[2], !wb_led_get(led[2]));
timeLED = 0;
}
timeLED++;
}
}
/*
* This is the main program.
*/
int main() {
int i, speed[2];
/* necessary to initialize webots stuff */
wb_robot_init();
/* get and enable the camera and accelerometer */
WbDeviceTag camera = wb_robot_get_device("camera");
wb_camera_enable(camera, 64);
/* initialization */
char name[20];
for (i = 0; i < NB_GROUND_SENS; i++) {
sprintf(name, "gs%d", i);
gs[i] = wb_robot_get_device(name); /* ground sensors */
wb_distance_sensor_enable(gs[i], TIME_STEP);
}
for (i = 0; i < NB_LEDS; i++) {
sprintf(name, "led%d", i);
led[i] = wb_robot_get_device(name);
wb_led_set(led[i], 1);
}
// motors
left_motor = wb_robot_get_device("left wheel motor");
right_motor = wb_robot_get_device("right wheel motor");
wb_motor_set_position(left_motor, INFINITY);
wb_motor_set_position(right_motor, INFINITY);
wb_motor_set_velocity(left_motor, 0.0);
wb_motor_set_velocity(right_motor, 0.0);
/* main loop
* Perform simulation steps of TIME_STEP milliseconds
* and leave the loop when the simulation is over
*/
for (;;) {
// Run one simulation step
wb_robot_step(TIME_STEP);
ReadGroudSensors();
PosLED();
// Speed initialization
speed[LEFT] = 0;
speed[RIGHT] = 0;
// *** START OF SUBSUMPTION ARCHITECTURE ***
// LFM - Line Following Module
LineFollowingModule();
speed[LEFT] = lfm_speed[LEFT];
speed[RIGHT] = lfm_speed[RIGHT];
// Routines used when detecting the line
if(!online){
if(P == -NEW_GS){
speed[LEFT] = -LFM_FS;
speed[RIGHT] = LFM_FS;
}
if(P == NEW_GS){
speed[LEFT] = LFM_FS;
speed[RIGHT] = -LFM_FS;
}
}
printf("%4d %4d %4d %4d %4d %4d %5d OnLine: %d \n", gs_new[0], gs_new[1], gs_new[2], gs_new[3], gs_new[4], (int)Position, (int)P, online);
// Speed computation
wb_motor_set_velocity(left_motor, 0.00628 * speed[LEFT]);
wb_motor_set_velocity(right_motor, 0.00628 * speed[RIGHT]);
};
wb_robot_cleanup();
return 0;
}