]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
robofsm: Added map handling functions for updating map from sharps
authorMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 1 May 2008 16:11:13 +0000 (18:11 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 1 May 2008 16:11:13 +0000 (18:11 +0200)
src/pathplan/map.c
src/robodim/eb2008/robodim_eb2008.c
src/robodim/eb2008/robodim_eb2008.h
src/robofsm/eb2008/Makefile.omk
src/robofsm/eb2008/map_handling.c [new file with mode: 0644]
src/robofsm/eb2008/map_handling.h [new file with mode: 0644]
src/robofsm/eb2008/robot_orte.c

index 0ceb7e504040de862836d8ffe21f1c7a2f39c236..29f2c6d8e2558d918d4cb00b9e1d1eb0df1df2d1 100644 (file)
@@ -135,33 +135,6 @@ struct map_cell *ShmapCellAtPoint(double x_m, double y_m)
         return &(map->cells[j][i]);
 }
 
-/**
- * Decrease map.detected_obstacle by val with saturation on zero. It
- * also clears #MAP_FLAG_DET_OBST if value reaches zero.
- * 
- * @param val Value to decrease obstacle life.
- * @see map #MAP_NEW_OBSTACLE
- * @todo Do faster this process. Use a cell's list to update.
- */
-void ShmapUpdateTmpObstacles(map_cell_detobst_t val){
-       int i,j;
-       if (!map)
-                return;
-        /// Go across the map.
-        for (j=0;j<MAP_HEIGHT;j++){
-                for(i=0;i<MAP_WIDTH;i++){
-                        /// Get cell value.
-                        struct map_cell *cell = &map->cells[j][i];
-                        if (val < cell->detected_obstacle) cell->detected_obstacle -= val;
-                        else {
-                                cell->detected_obstacle = 0;
-                                cell->flags &= ~MAP_FLAG_DET_OBST;
-                        }
-                }
-        }
-
-}
-
 /**
  * @brief Give information about if a cell is free.
  * @param x    Coordonate of a cell
index 562f774a217a3f3cc6c4ed3fed31703ee9a1fc63..da45549b4a88d12780c8b095447cb22ce168cf58 100644 (file)
@@ -1,4 +1,5 @@
 #include "robodim_eb2008.h"
+#include <robomath.h>
 
 /* Beacon positions. For some version of MCL, this must be sorted in
  * the same order as laser rotation. */
@@ -20,3 +21,12 @@ const struct beacon_pos beacon_red[BEACON_CNT] = {
        /* S3 */ { 0,                  0 },
 };
 
+
+ const struct sharp_pos sharp[SHARP_COUNT] = {
+       { ROBOT_AXIS_TO_FRONT_M-0.02, +ROBOT_WIDTH_M/2.0, DEG2RAD(+90) }, /* Left */
+       { ROBOT_AXIS_TO_FRONT_M-0.02, -ROBOT_WIDTH_M/2.0, DEG2RAD(-90) }, /* Right */
+       { ROBOT_AXIS_TO_FRONT_M, +ROBOT_WIDTH_M/2.0, DEG2RAD(0) }, /* Front left */
+       { ROBOT_AXIS_TO_FRONT_M, -ROBOT_WIDTH_M/2.0, DEG2RAD(0) }, /* Front right */
+       { ROBOT_AXIS_TO_BACK_M, +ROBOT_WIDTH_M/2.0, DEG2RAD(180) }, /* Back left */
+       { ROBOT_AXIS_TO_BACK_M, -ROBOT_WIDTH_M/2.0, DEG2RAD(180) }, /* Back right */
+};
index 8b8d40751223905455b072ffcb3110b49d1d6b04..1081e7f28eb48125ddeec32057b65192f6a0f96c 100644 (file)
@@ -16,8 +16,8 @@
  * FIXME: update robot's dimensions !!!
  *
  * ROBOT DIMENSIONS 
- *
- *          +--------------------------+
+ *                                   S0
+ *       S4 +--------------------------+ S2
  *        ^ |   |     |                |---|
  *     ^  | |   -------    ABE         |   |
  *   RR|  | |      :<--------------------->|
  *        | |<---->:<----------------->|   |
  *        | |   -------                |   |
  *        v |   |     |                |---|
- *          +--------------------------+
- *                 <-->
+ *       S5 +--------------------------+ S3
+ *                 <-->              S1
  *                  WR
+ * 
+ * Sx - Sharp sensor
  */
 
 #define ROBOT_WIDTH_MM 290     /* W*/
@@ -108,4 +110,14 @@ struct beacon_pos {
 extern const struct beacon_pos beacon_blue[BEACON_CNT];
 extern const struct beacon_pos beacon_red[BEACON_CNT];
 
+
+/* Shaprs */
+struct sharp_pos {
+       float x, y, ang;
+};
+
+#define SHARP_COUNT 6
+extern const struct sharp_pos sharp[SHARP_COUNT];
+
+
 #endif /* ROBODIM_EB2008_H */
index 6605d0a34d7a7e8c32f894c62f82ee162e21d3dd..b07533200532ea0ecbb9082698c25fdcb6bb70e6 100644 (file)
@@ -14,7 +14,7 @@ robomain_SOURCES = fsmmain.cc
 lib_LIBRARIES += robot_eb2008
 robot_eb2008_SOURCES = robot_orte.c servos_eb2008.c robot_eb2008.c \
                       fsmmove.cc fsmjoy.c \
-               movehelper_eb2008.cc fsmdisplay.cc fsmloc.c
+               movehelper_eb2008.cc fsmdisplay.cc fsmloc.c map_handling.c
 robot_eb2008_GEN_SOURCES = roboevent_eb2008.c
 include_GEN_HEADERS += roboevent_eb2008.h
 
diff --git a/src/robofsm/eb2008/map_handling.c b/src/robofsm/eb2008/map_handling.c
new file mode 100644 (file)
index 0000000..bc016dd
--- /dev/null
@@ -0,0 +1,160 @@
+#include <robot_eb2008.h>
+#include <robodim_eb2008.h>
+#include <map.h>
+#include <robomath.h>
+
+/*******************************************************************************
+ * Parameters of Obstacle detection
+ *******************************************************************************/
+
+#define OBS_PERIOD_WATCH       100000          /**< Time period for watching the sensors in us*/
+#define OBS_PERIOD_BETWEEN_DET 1000000         /**< Minimal time period between two detections in us. */
+#define OBS_AREA               (200/MAP_CELL_SIZE_MM)  /**< Configuration Space of the obstacle in cell units. In this case 200mm = 2 cells. @warning It should be an integer */
+#define OBS_AREA_M (OBS_AREA*MAP_CELL_SIZE_MM)
+#define OBS_FORGET_PERIOD      500             /**< The period of thread_obstacle_forgeting [ms] */
+
+#define OBS_FORGET_SEC 15                      /**< Time to completely forget detected obstacle. */
+
+#define OBS_OFFSET     0.6
+
+
+void obstacle_detected_at(double x, double y)
+{
+       int i,j, xcell, ycell;
+       struct est_pos_type est_pos;
+       struct map *map = robot.map;
+       double xx, yy;
+
+       if (!map)
+               return;
+
+       ShmapPoint2Cell(x, y, &xcell, &ycell);
+       if (!ShmapIsCellInMap(xcell, ycell))
+               return;
+
+       //DBG("Cell %d, %d\n", xcell, ycell);
+
+       /** Then all the cells arround obstacle cell are set as
+        * #MAP_NEW_OBSTACLE. Cells of current robot position are not
+        * set to avoid path planning deadlock. If there are a path
+        * cell between them, the path will be recalculated. @see
+        * #OBS_CSPACE. */
+
+       ROBOT_LOCK(est_pos);
+       est_pos = robot.est_pos;
+       ROBOT_UNLOCK(est_pos);
+
+       for (i=(xcell-OBS_AREA); i <= xcell+OBS_AREA; i++) {
+               for (j=(ycell- OBS_AREA); j <=ycell + OBS_AREA; j++) {
+                       if (!ShmapIsCellInMap(i, j)) continue;
+                       ShmapCell2Point(i, j, &xx, &yy);
+                       if (distance(xx, yy, est_pos.x, est_pos.y) > 0.2) {
+                               if (i==xcell && j==ycell) 
+                                       map->cells[ycell][xcell].flags |= MAP_FLAG_DET_OBST;
+                               map->cells[j][i].detected_obstacle = MAP_NEW_OBSTACLE;
+                       }
+               }
+       }
+}
+/**
+ * A thread running the trajectory recalc
+ *
+ * This (low-medium priority) thread updates the map with sensors information.
+ * If it is necesary, it recalculate the path.
+ *
+ * @param arg
+ *
+ * @return
+ */
+
+void obst_coord(struct est_pos_type *e, const struct sharp_pos  *s, double v, double *x, double *y)
+{
+       double sx, sy, sa;
+       sx = e->x + s->x*cos(e->phi) - s->y*sin(e->phi);
+       sy = e->y + s->x*sin(e->phi) + s->y*cos(e->phi);
+       sa = e->phi + s->ang;
+
+       *x = sx+v*cos(sa);
+       *y = sy+v*sin(sa);
+}
+
+void update_map(struct sharps_type *s)
+{
+       double val[SHARP_COUNT] = { s->left, s->right, s->front_left, s->front_right,
+                                   s->back_left, s->back_right };
+       int sharp80[] = {0,1,2,3};
+       double x, y;
+       //Pos p;
+       struct est_pos_type e;
+       int i, j;
+
+       ROBOT_LOCK(est_pos);
+       e = robot.est_pos;
+       ROBOT_UNLOCK(est_pos);
+
+       for (i=0; i<sizeof(sharp80)/sizeof(sharp80[0]); i++) {
+               j = sharp80[i];
+               if (val[j] < 0.7) {
+                       obst_coord(&e, &sharp[j], val[j], &x, &y);
+                       obstacle_detected_at(x, y);
+
+                       /* The second end of oponents robot */
+                       obst_coord(&e, &sharp[j], val[j]+0.3, &x, &y);
+                       obstacle_detected_at(x, y);
+               }
+       }
+}
+
+/**
+ * Decrease map.detected_obstacle by val with saturation on zero. It
+ * also clears #MAP_FLAG_DET_OBST if value reaches zero.
+ * 
+ * @param val Value to decrease obstacle life.
+ * @see map #MAP_NEW_OBSTACLE
+ * @todo Do faster this process. Use a cell's list to update.
+ */
+static void forget_obstacles(map_cell_detobst_t val){
+       int i,j;
+       struct map *map = robot.map;
+
+        for (j=0;j<MAP_HEIGHT;j++){
+                for(i=0;i<MAP_WIDTH;i++){
+                        struct map_cell *cell = &map->cells[j][i];
+                        if (val < cell->detected_obstacle) cell->detected_obstacle -= val;
+                        else {
+                                cell->detected_obstacle = 0;
+                                cell->flags &= ~MAP_FLAG_DET_OBST;
+                        }
+                }
+        }
+}
+
+static void gettimeofday_ts(struct timespec *ts)
+{
+       struct timeval tv;
+       gettimeofday(&tv, NULL);
+       /* Convert from timeval to timespec */
+       ts->tv_sec = tv.tv_sec;
+       ts->tv_nsec = tv.tv_usec * 1000;
+}
+
+/**
+ * A thread updating the map
+ */
+static void * thread_obstacle_forgeting(void * arg)
+{
+       struct timespec ts;
+       sem_t timer;
+       int val = (long long)MAP_NEW_OBSTACLE/(OBS_FORGET_SEC*1000/OBS_FORGET_PERIOD);
+       if (val == 0) val = 1;
+
+       gettimeofday_ts(&ts);
+       
+       sem_init(&timer, 0, 0);
+       while (1) {
+               __fsm_timespec_add_ms(&ts, NULL, OBS_FORGET_PERIOD);
+               sem_timedwait(&timer, &ts);
+
+               forget_obstacles(val);
+       }
+}
diff --git a/src/robofsm/eb2008/map_handling.h b/src/robofsm/eb2008/map_handling.h
new file mode 100644 (file)
index 0000000..1e9dcf8
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef _MAP_HANDLING_H
+#define _MAP_HANDLING_H
+
+#include <robodim_eb2008.h>
+
+void update_map(struct sharps_type *s);
+
+#endif
index e7fc7b0354b909daa8c8f6ad71e9b608bbefb799..48e6679a2faddd5d571d1374d94b7f20fe4550ad 100644 (file)
@@ -17,6 +17,7 @@
 #include <math.h>
 #include <robomath.h>
 #include <laser-nav.h>
+#include "map_handling.h"
 
 /* ---------------------------------------------------------------------- 
  * PUBLISHER CALLBACKS - GENERIC
@@ -260,6 +261,23 @@ void rcv_bumper_cb(const ORTERecvInfo *info, void *vinstance,
        }
 }
 
+void rcv_sharps_cb(const ORTERecvInfo *info, void *vinstance,
+                       void *recvCallBackParam)
+{
+       struct sharps_type *instance = (struct sharps_type *)vinstance;
+       //struct sharps_type *for_mcl;
+
+       switch (info->status) {
+               case NEW_DATA: {
+                       update_map(instance);
+                       break;
+               }
+               case DEADLINE:
+                       DBG("%s: ORTE deadline occurred\n", __FUNCTION__);
+                       break;
+       }
+}
+
 void robot_init_orte()
 {
        robot.orte.strength = 20;
@@ -295,5 +313,6 @@ void robot_init_orte()
        eb2008_subscriber_laser_data_create(&robot.orte, rcv_laser_data_cb, &robot.orte);
        eb2008_subscriber_cmu_create(&robot.orte, rcv_cmu_cb, &robot.orte);
        eb2008_subscriber_bumper_create(&robot.orte, rcv_bumper_cb, &robot.orte);
+       eb2008_subscriber_sharps_create(&robot.orte, rcv_sharps_cb, &robot.orte);
 }