1 #include <robot_eb2008.h>
2 #include <robodim_eb2008.h>
6 /*******************************************************************************
7 * Parameters of Obstacle detection
8 *******************************************************************************/
10 #define OBS_SIZE_M 0.2 /**< Expected size of detected obstacle */
11 #define IGNORE_CLOSER_THAN_M 0.2 /**< Ignore detected which are closer than this to the robot (avoid path planning deadlock) */
12 #define OBS_FORGET_PERIOD 500 /**< The period of thread_obstacle_forgeting [ms] */
13 #define OBS_FORGET_SEC 5 /**< Time to completely forget detected obstacle. */
14 #define OBS_OFFSET 0.6
16 void obstacle_detected_at(double x, double y)
18 int i,j, xcell, ycell;
19 struct est_pos_type est_pos;
20 struct map *map = robot.map;
27 ShmapPoint2Cell(x, y, &xcell, &ycell, &valid);
31 /* The obstacle was detected here */
32 map->cells[ycell][xcell].flags |= MAP_FLAG_DET_OBST;
34 /** Then all the cells arround obstacle cell are set as
35 * #MAP_NEW_OBSTACLE. Cells of current robot position are not
36 * set to avoid path planning deadlock. If there are a path
37 * cell between them, the path will be recalculated. @see
40 /* Mark "protected" area around the obstacle */
42 est_pos = robot.est_pos;
43 ROBOT_UNLOCK(est_pos);
44 int obst_size_cell = (int)ceil(OBS_SIZE_M/MAP_CELL_SIZE_M);
45 for (i=(xcell-obst_size_cell); i <= xcell+obst_size_cell; i++) {
46 for (j=(ycell- obst_size_cell); j <=ycell + obst_size_cell; j++) {
47 if (!ShmapIsCellInMap(i, j)) continue;
48 ShmapCell2Point(i, j, &xx, &yy);
49 if ((distance(xx, yy, est_pos.x, est_pos.y) > IGNORE_CLOSER_THAN_M) &&
50 (distance(xx, yy, x, y) < OBS_SIZE_M)) { /* We expect cirtular area around obstacle */
51 map->cells[j][i].detected_obstacle = MAP_NEW_OBSTACLE;
57 * A thread running the trajectory recalc
59 * This (low-medium priority) thread updates the map with sensors information.
60 * If it is necesary, it recalculate the path.
67 void obst_coord(struct est_pos_type *e, const struct sharp_pos *s, double v, double *x, double *y)
70 sx = e->x + s->x*cos(e->phi) - s->y*sin(e->phi);
71 sy = e->y + s->x*sin(e->phi) + s->y*cos(e->phi);
78 void update_map(struct sharps_type *s)
80 double val[SHARP_COUNT] = { s->left, s->right, s->front_left, s->front_right,
81 s->back_left, s->back_right };
82 int sharp80[] = {0,1,2,3};
85 struct est_pos_type e;
90 ROBOT_UNLOCK(est_pos);
92 for (i=0; i<sizeof(sharp80)/sizeof(sharp80[0]); i++) {
95 obst_coord(&e, &sharp[j], val[j], &x, &y);
96 obstacle_detected_at(x, y);
98 /* The second end of oponents robot */
99 obst_coord(&e, &sharp[j], val[j]+0.3, &x, &y);
100 obstacle_detected_at(x, y);
106 * Decrease map.detected_obstacle by val with saturation on zero. It
107 * also clears #MAP_FLAG_DET_OBST if value reaches zero.
109 * @param val Value to decrease obstacle life.
110 * @see map #MAP_NEW_OBSTACLE
111 * @todo Do faster this process. Use a cell's list to update.
113 static void forget_obstacles(map_cell_detobst_t val){
115 struct map *map = robot.map;
117 for (j=0;j<MAP_HEIGHT;j++){
118 for(i=0;i<MAP_WIDTH;i++){
119 struct map_cell *cell = &map->cells[j][i];
120 if (val < cell->detected_obstacle) cell->detected_obstacle -= val;
122 cell->detected_obstacle = 0;
123 cell->flags &= ~MAP_FLAG_DET_OBST;
129 static void gettimeofday_ts(struct timespec *ts)
132 gettimeofday(&tv, NULL);
133 /* Convert from timeval to timespec */
134 ts->tv_sec = tv.tv_sec;
135 ts->tv_nsec = tv.tv_usec * 1000;
139 * A thread updating the map
141 void * thread_obstacle_forgeting(void * arg)
145 int val = (long long)MAP_NEW_OBSTACLE/(OBS_FORGET_SEC*1000/OBS_FORGET_PERIOD);
146 if (val == 0) val = 1;
148 gettimeofday_ts(&ts);
150 sem_init(&timer, 0, 0);
152 __fsm_timespec_add_ms(&ts, NULL, OBS_FORGET_PERIOD);
153 sem_timedwait(&timer, &ts);
155 forget_obstacles(val);