]> rtime.felk.cvut.cz Git - eurobot/public.git/blobdiff - src/robofsm/robot.c
Odometry autocalibration
[eurobot/public.git] / src / robofsm / robot.c
index be1bde92a822ff09c3895817442545832e45c5ea..57e690a8b1701e9890f5addb90254d41fc566c8d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * robot_eb2008.c                      08/04/20
+ * robot_demo2011.c
  *
  * Robot's generic initialization and clean up functions.
  *
 #include "map_handling.h"
 #include <string.h>
 #include "actuators.h"
-#include "corns_configs.h"
 #include <robodim.h>
 #include <ul_log.h>
 
-extern UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogf + name of the file */
+UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
 
 
 #define MOTION_CONTROL_INIT_ONLY
 #include "motion-control.h"
+#include "robot.h"
 
 /* Global definition of robot structure */
 struct robot robot;
@@ -59,36 +59,25 @@ static void int_handler(int sig)
 
 void fill_in_known_areas_in_map()
 {
-       /* Do not plan path close to edges */
-       //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
-       //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
-       //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
-       //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
-
-       /* Container surroundings */
-       ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
-       ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
-
+       /*----- playground width 3.0 m and playground height 2.0 m -----*/
        /* Ignore other obstacles at edges */
-       ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
+       ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* left */
        ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
-       ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
-       ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
-       ShmapSetRectangleFlag(1.0, 1.5, 1.95, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* rised area */
-
-       ShmapSetRectangleFlag(0.6, 1.45, 2.45, 1.55, MAP_FLAG_WALL, 0); /* plateau, slopes */
-       ShmapSetRectangleFlag(1.46, PLAYGROUND_HEIGHT_M - 0.5, 1.57, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0); /* plateau, slopes */
+       ShmapSetRectangleFlag(0.0, 1.91, 3.0, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* top */
+       ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* right */
+
+       /* Small walls that cannot be detected by hokuyo */
+       ShmapSetRectangleFlag(0.0, 1.49, 0.39, 1.432, MAP_FLAG_WALL, 0);
+       ShmapSetRectangleFlag(3.0, 1.49, 2.61, 1.432, MAP_FLAG_WALL, 0);
+       ShmapSetRectangleFlag(0.325, 0.0, 0.38, 0.74, MAP_FLAG_WALL, 0);
+       ShmapSetRectangleFlag(2.675, 0.0, 2.62, 0.74, MAP_FLAG_WALL, 0);
        
-       /* corns */
-       struct corns_group *corns = get_all_corns(0, 0);
-       struct corn * corn;
-       for(corn = corns->corns; corn < &corns->corns[corns->corns_count]; corn++) {
-               ShmapSetCircleFlag(corn->position.x, corn->position.y, CORN_NEIGHBOURHOOD_RADIUS_M, MAP_FLAG_WALL, 0);
-       }
-       dispose_corns_group(corns); // robot.corns will be set later, when the corns' configuration is known
-
-       ShmapSetRectangleFlag(0.32, 0.25, 0.38, 0.55, 0, MAP_FLAG_WALL); /* destination position near blue container */
-       ShmapSetRectangleFlag(2.62, 0.25, 2.68, 0.55, 0, MAP_FLAG_WALL); /* destination position near yellow container */
+       /* Palm tree */
+       //ShmapSetCircleFlag(1.5, 1.0, 0.075, MAP_FLAG_WALL, 0);
+       
+       /* Totems and palm tree */
+       ShmapSetRectangleFlag(0.975, 0.875, 2.025, 1.125, MAP_FLAG_WALL, 0);
+       //ShmapSetRectangleFlag(1.775, 0.875, 2.025, 1.125, MAP_FLAG_WALL, 0);
 }
 
 static void trans_callback(struct robo_fsm *fsm)
@@ -103,27 +92,26 @@ static void trans_callback(struct robo_fsm *fsm)
                strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
                ORTEPublicationSend(robot.orte.publication_fsm_act);
        }
-       
+
 }
 
-/** 
+/**
  * Initializes the robot.
  * Setup fields in robot structure, initializes FSMs and ORTE.
- * 
+ *
  * @return 0
  */
 int robot_init()
 {
        int rv;
        pthread_mutexattr_t mattr;
-
+       robot_calibrate_odometry();
        rv = pthread_mutexattr_init(&mattr);
 #ifdef HAVE_PRIO_INHERIT
        rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
 #endif
        pthread_mutex_init(&robot.lock, &mattr);
        pthread_mutex_init(&robot.lock_ref_pos, &mattr);
-       pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
        pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
        pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
        pthread_mutex_init(&robot.lock_meas_angles, &mattr);
@@ -131,12 +119,8 @@ int robot_init()
        pthread_mutex_init(&robot.lock_disp, &mattr);
 
        fsm_main_loop_init(&robot.main_loop);
-       
+
        /* FSM initialization */
-       /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
-       fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
-       fsm_init(&robot.fsm.display, "display", &robot.main_loop);
-       fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
        fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
        fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
        fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
@@ -144,16 +128,16 @@ int robot_init()
        robot.fsm.act.transition_callback = trans_callback;
        robot.fsm.motion.transition_callback = trans_callback;
 
-       robot.team_color = BLUE;
+       robot.team_color = RED;
 
-       if (robot.team_color == YELLOW) {
-               ul_loginf("We are YELLOW!\n");
+       if (robot.team_color == RED) {
+               ul_loginf("We are RED!\n");
        } else {
                ul_loginf("We are BLUE!\n");
        }
 
-       robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
-       
+       robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
+
        robot.ignore_hokuyo = false;
        robot.map = ShmapInit(1);
        fill_in_known_areas_in_map();
@@ -171,7 +155,7 @@ int robot_init()
        robot.orte.pwr_ctrl.voltage80 = 1;
 
        robot.orte.camera_control.on = true;
-       
+
        robot.fsm.motion.state = &fsm_state_motion_init;
 
        /* Only activate display if it is configured */
@@ -182,9 +166,9 @@ int robot_init()
        */
 
        robot.obstacle_avoidance_enabled = true;
-       robot.use_back_switch = true;
-       robot.use_left_switch = true;
-       robot.use_right_switch = true;
+       robot.use_back_bumpers = true;
+       robot.use_left_bumper = true;
+       robot.use_right_bumper = true;
        robot.start_state = POWER_ON;
        robot.check_turn_safety = true;
 
@@ -195,7 +179,7 @@ int robot_init()
        return rv;
 }
 
-/** 
+/**
  * Starts the robot FSMs and threads.
  *
  * @return 0
@@ -227,7 +211,7 @@ int robot_start()
                perror("robot_start: pthread_attr_setschedparam()");
                goto err;
        }
-       rv = pthread_create(&thr_obstacle_forgeting, 
+       rv = pthread_create(&thr_obstacle_forgeting,
                            &tattr, thread_obstacle_forgeting, NULL);
        if (rv) {
                perror("robot_start: pthread_create");
@@ -242,7 +226,7 @@ err:
        return rv;
 }
 
-/** 
+/**
  * Signals all the robot threads to finish.
  */
 void robot_exit()
@@ -253,7 +237,7 @@ void robot_exit()
        fsm_exit(&robot.fsm.act);
 }
 
-/** 
+/**
  * Stops the robot. All resources alocated by robot_init() or
  * robot_start() are dealocated here.
  */
@@ -262,14 +246,14 @@ void robot_destroy()
        motion_control_done();
 
        // FIXME: set actuators to well defined position (FJ)
-       
+
        robottype_roboorte_destroy(&robot.orte);
 
        fsm_destroy(&robot.fsm.main);
        fsm_destroy(&robot.fsm.motion);
        fsm_destroy(&robot.fsm.act);
        ShmapFree();
-       ul_logdbg("robofsm: stop.\n");
+       ul_logdeb("robofsm: stop.\n");
 }
 
 void robot_get_est_pos_trans(double *x, double *y, double *phi)
@@ -302,3 +286,38 @@ void robot_get_est_pos(double *x, double *y, double *phi)
                ROBOT_UNLOCK(ref_pos);
        }
 }
+
+void robot_calibrate_odometry()
+{
+       robot.odo_distance_a = 0;
+       robot.odo_distance_b = 0;
+       FILE *file;     
+       file = fopen ("odometry_cal_data", "r");
+       if (file == NULL)
+       { 
+               robot.odo_cal_a = 1;
+               robot.odo_cal_b = 1;
+               fprintf(stderr, "File not found. \n\n");
+               return;
+       }
+       char line [15];
+       float a = 0;
+       float b = 0;
+       int num = 0;
+       while (fgets (line, 15 , file)) {
+               a += atof(strtok(line," "));
+               fgets (line, 15 , file);
+               b += atof(strtok(NULL," "));
+               num ++;
+       }
+       fclose (file);
+       if(a == 0 || b == 0) {
+               robot.odo_cal_a = 1;
+               robot.odo_cal_b = 1;
+               return;
+       }
+       robot.odo_cal_a = a / num;
+       robot.odo_cal_b = b / num;
+       printf("calibrated value left: %f\n",robot.odo_cal_a);
+       printf("calibrated value right: %f\n",robot.odo_cal_b);
+}