-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathe-puck_line.c
334 lines (286 loc) · 10.5 KB
/
e-puck_line.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
/*
* Copyright 1996-2020 Cyberbotics Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/***************************************************************************
e-puck_line -- Base code for a practical assignment on behavior-based
robotics. When completed, the behavior-based controller should allow
the e-puck robot to follow the black line, avoid obstacles and
recover its path afterwards.
Copyright (C) 2006 Laboratory of Intelligent Systems (LIS), EPFL
Authors: Jean-Christophe Zufferey
Email: [email protected]
Web: http://lis.epfl.ch
This program is free software; any publications presenting results
obtained with this program must mention it and its origin. You
can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
***************************************************************************/
#include <stdio.h>
#include <webots/distance_sensor.h>
#include <webots/motor.h>
#include <webots/robot.h>
// Global defines
#define TRUE 1
#define FALSE 0
#define NO_SIDE -1
#define LEFT 0
#define RIGHT 1
#define WHITE 0
#define BLACK 1
#define TIME_STEP 32 // [ms]
// 8 IR proximity sensors
#define NB_DIST_SENS 8
#define PS_RIGHT_00 0
#define PS_RIGHT_45 1
#define PS_RIGHT_90 2
#define PS_RIGHT_REAR 3
#define PS_LEFT_REAR 4
#define PS_LEFT_90 5
#define PS_LEFT_45 6
#define PS_LEFT_00 7
WbDeviceTag ps[NB_DIST_SENS]; /* proximity sensors */
int ps_value[NB_DIST_SENS] = {0, 0, 0, 0, 0, 0, 0, 0};
const int PS_OFFSET_SIMULATION[NB_DIST_SENS] = {300, 300, 300, 300, 300, 300, 300, 300};
// *** TO BE ADAPTED TO YOUR ROBOT ***
const int PS_OFFSET_REALITY[NB_DIST_SENS] = {480, 170, 320, 500, 600, 680, 210, 640};
// 3 IR ground color sensors
#define NB_GROUND_SENS 3
#define GS_WHITE 900
#define GS_LEFT 0
#define GS_CENTER 1
#define GS_RIGHT 2
WbDeviceTag gs[NB_GROUND_SENS]; /* ground sensors */
unsigned short gs_value[NB_GROUND_SENS] = {0, 0, 0};
// Motors
WbDeviceTag left_motor, right_motor;
// obstacle avoidance strategy
#define PS_A 300 //300
#define PS_B 100 //100
#define PS_C 80 //100
bool ontrack = TRUE;
bool avoiding = FALSE;
bool around = FALSE;
bool recovery = FALSE;
//int maxS4, maxS5, maxS6;
//------------------------------------------------------------------------------
//
// BEHAVIORAL MODULES
//
//------------------------------------------------------------------------------
////////////////////////////////////////////
// LFM - Line Following Module
//
// This module implements a very simple, Braitenberg-like behavior in order
// to follow a black line on the ground. Output speeds are stored in
// lfm_speed[LEFT] and lfm_speed[RIGHT].
int lfm_speed[2];
#define LFM_FORWARD_SPEED 200
#define LFM_K_GS_SPEED 0.4
void LineFollowingModule(void) {
int DeltaS = 0;
DeltaS = gs_value[2] - gs_value[0];
lfm_speed[LEFT] = LFM_FORWARD_SPEED - LFM_K_GS_SPEED * DeltaS;
lfm_speed[RIGHT] = LFM_FORWARD_SPEED + LFM_K_GS_SPEED * DeltaS;
}
////////////////////////////////////////////
// OAM - Obstacle Avoidance Module
//
// The OAM routine first detects obstacles in front of the robot, then records
// their side in "oam_side" and avoid the detected obstacle by
// turning away according to very simple weighted connections between
// proximity sensors and motors. "oam_active" becomes active when as soon as
// an object is detected and "oam_reset" inactivates the module and set
// "oam_side" to NO_SIDE. Output speeds are in oam_speed[LEFT] and oam_speed[RIGHT].
int oam_active, oam_reset;
int oam_speed[2];
int oam_side = NO_SIDE;
#define OAM_OBST_THRESHOLD 100
#define OAM_FORWARD_SPEED 150
#define OAM_K_PS_90 0.2
#define OAM_K_PS_45 0.9
#define OAM_K_PS_00 1.2
#define OAM_K_MAX_DELTAS 600
void ObstacleAvoidanceModule(void) {
int max_ds_value, i;
int Activation[] = {0, 0};
// Module RESET
if (oam_reset) {
oam_active = FALSE;
oam_side = NO_SIDE;
}
oam_reset = 0;
// Determine the presence and the side of an obstacle
max_ds_value = 0;
for (i = PS_RIGHT_00; i <= PS_RIGHT_45; i++) {
if (max_ds_value < ps_value[i])
max_ds_value = ps_value[i];
Activation[RIGHT] += ps_value[i];
}
for (i = PS_LEFT_45; i <= PS_LEFT_00; i++) {
if (max_ds_value < ps_value[i])
max_ds_value = ps_value[i];
Activation[LEFT] += ps_value[i];
}
if (max_ds_value > OAM_OBST_THRESHOLD)
oam_active = TRUE;
if (oam_active && oam_side == NO_SIDE) // check for side of obstacle only when not already detected
{
if (Activation[RIGHT] > Activation[LEFT])
oam_side = RIGHT;
else
oam_side = LEFT;
}
// Forward speed
oam_speed[LEFT] = OAM_FORWARD_SPEED;
oam_speed[RIGHT] = OAM_FORWARD_SPEED;
// Go away from obstacle
if (oam_active) {
int DeltaS = 0;
// The rotation of the robot is determined by the location and the side of the obstacle
if (oam_side == LEFT) {
//(((ps_value[PS_LEFT_90]-PS_OFFSET)<0)?0:(ps_value[PS_LEFT_90]-PS_OFFSET)));
DeltaS -= (int)(OAM_K_PS_90 * ps_value[PS_LEFT_90]);
//(((ps_value[PS_LEFT_45]-PS_OFFSET)<0)?0:(ps_value[PS_LEFT_45]-PS_OFFSET)));
DeltaS -= (int)(OAM_K_PS_45 * ps_value[PS_LEFT_45]);
//(((ps_value[PS_LEFT_00]-PS_OFFSET)<0)?0:(ps_value[PS_LEFT_00]-PS_OFFSET)));
DeltaS -= (int)(OAM_K_PS_00 * ps_value[PS_LEFT_00]);
} else { // oam_side == RIGHT
//(((ps_value[PS_RIGHT_90]-PS_OFFSET)<0)?0:(ps_value[PS_RIGHT_90]-PS_OFFSET)));
DeltaS += (int)(OAM_K_PS_90 * ps_value[PS_RIGHT_90]);
//(((ps_value[PS_RIGHT_45]-PS_OFFSET)<0)?0:(ps_value[PS_RIGHT_45]-PS_OFFSET)));
DeltaS += (int)(OAM_K_PS_45 * ps_value[PS_RIGHT_45]);
//(((ps_value[PS_RIGHT_00]-PS_OFFSET)<0)?0:(ps_value[PS_RIGHT_00]-PS_OFFSET)));
DeltaS += (int)(OAM_K_PS_00 * ps_value[PS_RIGHT_00]);
}
if (DeltaS > OAM_K_MAX_DELTAS)
DeltaS = OAM_K_MAX_DELTAS;
if (DeltaS < -OAM_K_MAX_DELTAS)
DeltaS = -OAM_K_MAX_DELTAS;
// Set speeds
oam_speed[LEFT] -= DeltaS;
oam_speed[RIGHT] += DeltaS;
}
}
//------------------------------------------------------------------------------
//
// CONTROLLER
//
//------------------------------------------------------------------------------
////////////////////////////////////////////
// Main
int main() {
int i, speed[2], ps_offset[NB_DIST_SENS] = {0, 0, 0, 0, 0, 0, 0, 0};
/* intialize Webots */
wb_robot_init();
/* initialization */
char name[20];
for (i = 0; i < NB_DIST_SENS; i++) {
sprintf(name, "ps%d", i);
ps[i] = wb_robot_get_device(name); /* proximity sensors */
wb_distance_sensor_enable(ps[i], TIME_STEP);
}
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);
}
// 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);
for (;;) { // Main loop
// Run one simulation step
wb_robot_step(TIME_STEP);
// read sensors value
for (i = 0; i < NB_DIST_SENS; i++)
ps_value[i] = (((int)wb_distance_sensor_get_value(ps[i]) - ps_offset[i]) < 0) ?
0 :
((int)wb_distance_sensor_get_value(ps[i]) - ps_offset[i]);
for (i = 0; i < NB_GROUND_SENS; i++)
gs_value[i] = wb_distance_sensor_get_value(gs[i]);
// Speed initialization
speed[LEFT] = 0;
speed[RIGHT] = 0;
// *** START OF SUBSUMPTION ARCHITECTURE ***
// LFM - Line Following Module
if(ontrack) LineFollowingModule();
// LineFollowingModule();
// Speed computation
speed[LEFT] = lfm_speed[LEFT];
speed[RIGHT] = lfm_speed[RIGHT];
// Max Sensor Tests
// if(ps_value[4]>maxS4) maxS4 = ps_value[4];
// if(ps_value[5]>maxS5) maxS5 = ps_value[5];
// if(ps_value[6]>maxS6) maxS6 = ps_value[6];
// Sensores de proximidad (PS: proximity sensor)
if(ps_value[7] > PS_A || ps_value[0] > PS_A){
avoiding = TRUE;
}
if(avoiding){
if(ps_value[5] < (PS_A-20)){
speed[LEFT] = 200;
speed[RIGHT] = -200;
}
else if(ps_value[5] >= PS_A){
avoiding = FALSE;
around = TRUE;
}
}
if(around){
if(ps_value[5] < (PS_A-20) && ps_value[6] < PS_B){
speed[LEFT] = -200;
speed[RIGHT] = 100;
}
if(gs_value[0]<400 || gs_value[1]<400 || gs_value[2]<400){
around = FALSE;
recovery = TRUE;
ontrack = FALSE;
}
}
if(recovery){
if(ps_value[5] > PS_C){
speed[LEFT] = 200;
speed[RIGHT] = -200;
}
else{
recovery = FALSE;
ontrack = TRUE;
}
}
// *** END OF SUBSUMPTION ARCHITECTURE ***
// Debug display
// printf("PS[4] = %4d PS[5] = %4d PS[6] = %4d PS[7] = %4d PS[0] = %4d \n", ps_value[4], ps_value[5], ps_value[6], ps_value[7], ps_value[0]);
// printf("PS[5] = %4d PS[6] = %4d maxPS[4] = %4d maxPS[5] = %4d maxPS[6] = %4d \n", ps_value[5], ps_value[6], maxS4, maxS5, maxS6);
// printf("GS[0] = %4d GS[1] = %4d GS[2] = %4d \n", gs_value[0], gs_value[1], gs_value[2]);
printf("Ontrack: %d Avoiding: %d Around: %d Recovey: %d \n", !(avoiding || around || recovery), avoiding, around, recovery);
// Set wheel speeds
wb_motor_set_velocity(left_motor, 0.00628 * speed[LEFT]);
wb_motor_set_velocity(right_motor, 0.00628 * speed[RIGHT]);
}
return 0;
}