]> rtime.felk.cvut.cz Git - eurobot/public.git/blobdiff - src/robofsm/robot.c
robofsm: Map for this year
[eurobot/public.git] / src / robofsm / robot.c
index 225744044b9a871145c1fe31f74e0ff515ba0b55..52ea3736e608be0a317fabe997a9a5f184cee6a6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * robot_eb2008.c                      08/04/20
+ * robot_demo2011.c
  *
  * Robot's generic initialization and clean up functions.
  *
 #include <time.h>
 #include <unistd.h>
 #include "map_handling.h"
-#include <uoled.h>
 #include <string.h>
 #include "actuators.h"
+#include <robodim.h>
+#include <ul_log.h>
+
+UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
 
-#ifdef ROBOT_DEBUG
-   #define DBG(format, ...) printf(format, ##__VA_ARGS__)
-#else
-   #define DBG(format, ...)
-#endif
 
 #define MOTION_CONTROL_INIT_ONLY
 #include "motion-control.h"
@@ -60,44 +58,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.199, 2.1, MAP_FLAG_WALL, 0); */
-/*     ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
-/*     ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
-/*     ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
-
-       //ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
-       //ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
-
-       //ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
-       //ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
-
+       /*----- 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(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
-       //ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
-
-       /* Construction areas (Building zones) */
-       //ShmapSetRectangleFlag(ACROPOLIS_CENTER_X - ACROPOLIS_RADIUS, ACROPOLIS_CENTER_Y - ACROPOLIS_RADIUS, ACROPOLIS_CENTER_X + ACROPOLIS_RADIUS, ACROPOLIS_CENTER_Y + ACROPOLIS_RADIUS, MAP_FLAG_WALL, 0);           /* Central construction area */
-       //ShmapSetRectangleFlag(1.35, 0.9, 1.65, 1.2, MAP_FLAG_WALL, 0);                 /* Central construction area */
-
-       // Enclosure wall
-       ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); /* left */
-       ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
-       ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
-       ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */      
-       // Bottom-middle unloading area
-       ShmapSetRectangleFlag(1.0, 0.0, 2.0, 0.3, MAP_FLAG_WALL, 0); /* bottom */
-       // Unloading area - dwarf wall
-       ShmapSetRectangleFlag(0.4, 0.0, 0.8, 0.3, MAP_FLAG_WALL, 0); 
-       ShmapSetRectangleFlag(2.2, 0.0, 2.6, 0.3, MAP_FLAG_WALL, 0); 
-       // Acropolis wall
-       ShmapSetCircleFlag(ACROPOLIS_CENTER_X, ACROPOLIS_CENTER_Y, ACROPOLIS_DIAMETER + 0.09, MAP_FLAG_WALL, 0); 
-
+       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);
+       
+       /* Palm tree */
+       ShmapSetCircleFlag(1.5, 1.0, 0.15, MAP_FLAG_WALL, 0);
+       
+       /* Totems */
+       ShmapSetRectangleFlag(1.01, 0.91, 1.19, 1.09, MAP_FLAG_WALL, 0);
+       ShmapSetRectangleFlag(1.81, 0.91, 1.99, 1.09, MAP_FLAG_WALL, 0);
 }
 
 static void trans_callback(struct robo_fsm *fsm)
@@ -112,13 +91,13 @@ 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()
@@ -132,37 +111,33 @@ int robot_init()
 #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);
        pthread_mutex_init(&robot.lock_joy_data, &mattr);
        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.display, "\tdisp", &robot.main_loop);
        fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
        robot.fsm.main.transition_callback = trans_callback;
        robot.fsm.act.transition_callback = trans_callback;
        robot.fsm.motion.transition_callback = trans_callback;
 
-       robot.team_color = GREEN;
+       robot.team_color = RED;
 
        if (robot.team_color == RED) {
-               printf("We are RED!\n");
+               ul_loginf("We are RED!\n");
        } else {
-               printf("We are GREEN!\n");
+               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();
 
@@ -179,19 +154,22 @@ int robot_init()
        robot.orte.pwr_ctrl.voltage80 = 1;
 
        robot.orte.camera_control.on = true;
-       
-       robot.fsm.motion.state = &fsm_state_motion_init;
 
-       robot.fsm.act.state = &fsm_state_act_wait_for_command; // puck handling actuators FSM's initial state
+       robot.fsm.motion.state = &fsm_state_motion_init;
 
        /* Only activate display if it is configured */
+       /* FIXME: display
        robot.sercom = uoled_init(serial_comm);
        if (strcmp(robot.sercom->devname, "/dev/null") != 0)
                robot.fsm.display.state = &fsm_state_disp_init;
+       */
 
        robot.obstacle_avoidance_enabled = true;
-       robot.use_back_switch = false; /* Switched on sime time after start */
-       robot.state = POWER_ON;
+       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;
 
        /* init ORTE domain, create publishers, subscribers, .. */
        rv = robot_init_orte();
@@ -200,7 +178,7 @@ int robot_init()
        return rv;
 }
 
-/** 
+/**
  * Starts the robot FSMs and threads.
  *
  * @return 0
@@ -224,26 +202,30 @@ int robot_start()
        /* Obstacle forgeting thread */
        pthread_attr_init (&tattr);
        pthread_attr_getschedparam (&tattr, &param);
+       pthread_attr_getschedparam (&tattr, &param);
+       pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
        param.sched_priority = OBST_FORGETING_PRIO;
        rv = pthread_attr_setschedparam (&tattr, &param);
        if (rv) {
                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");
                goto err;
        }
 
+       sem_init(&robot.start, 0, 0);
+
        fsm_main_loop(&robot.main_loop);
 
 err:
        return rv;
 }
 
-/** 
+/**
  * Signals all the robot threads to finish.
  */
 void robot_exit()
@@ -251,11 +233,10 @@ void robot_exit()
        /* stop FSMs */
        fsm_exit(&robot.fsm.main);
        fsm_exit(&robot.fsm.motion);
-       fsm_exit(&robot.fsm.display);
        fsm_exit(&robot.fsm.act);
 }
 
-/** 
+/**
  * Stops the robot. All resources alocated by robot_init() or
  * robot_start() are dealocated here.
  */
@@ -263,15 +244,44 @@ void robot_destroy()
 {
        motion_control_done();
 
-       act_chelae(CHELA_OPEN, CHELA_OPEN);
-       act_holder(HOLDER_OPENED);
-       
+       // 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.display);
        fsm_destroy(&robot.fsm.act);
        ShmapFree();
-       DBG("robofsm: stop.\n");
+       ul_logdeb("robofsm: stop.\n");
+}
+
+void robot_get_est_pos_trans(double *x, double *y, double *phi)
+{
+       robot_get_est_pos(x, y, phi);
+       *x = __trans_x(*x);
+       *y = __trans_y(*y);
+       *phi = __trans_ang(*phi);
+}
+
+void robot_get_est_pos(double *x, double *y, double *phi)
+{
+       if (robot.indep_odometry_works) {
+               ROBOT_LOCK(est_pos_indep_odo);
+               *x = robot.est_pos_indep_odo.x;
+               *y = robot.est_pos_indep_odo.y;
+               *phi = robot.est_pos_indep_odo.phi;
+               ROBOT_UNLOCK(est_pos_indep_odo);
+       } else if (robot.odometry_works) {
+               ROBOT_LOCK(est_pos_odo);
+               *x = robot.est_pos_odo.x;
+               *y = robot.est_pos_odo.y;
+               *phi = robot.est_pos_odo.phi;
+               ROBOT_UNLOCK(est_pos_odo);
+       } else {
+               ROBOT_LOCK(ref_pos);
+               *x = robot.ref_pos.x;
+               *y = robot.ref_pos.y;
+               *phi = robot.ref_pos.phi;
+               ROBOT_UNLOCK(ref_pos);
+       }
 }