/*
- * robot_eb2008.c 08/04/20
+ * robot_demo2011.c
*
* Robot's generic initialization and clean up functions.
*
* License: GNU GPL v.2
*/
+#define _XOPEN_SOURCE 500 /* For PTHREAD_PRIO_INHERIT etc. */
+
#include <map.h>
#include <movehelper.h>
#include <pthread.h>
#include <robomath.h>
#include <robot.h>
#include <robot_orte.h>
-#include <servos.h>
#include <signal.h>
#include <sys/time.h>
#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 */
+
-static char *fsm_name[] = {
- [FSM_ID_MAIN] = "main",
- [FSM_ID_MOTION] = "motion",
- [FSM_ID_DISP] = "display",
-};
+#define MOTION_CONTROL_INIT_ONLY
+#include "motion-control.h"
-struct robot_eb2008 robot;
+/* Global definition of robot structure */
+struct robot robot;
#ifdef CONFIG_LOCK_CHECKING
struct lock_log robot_lock_log;
int i;
sigemptyset(&sigset);
- for (i=0; i<7; i++)
- sigaddset(&sigset, SIGRTMIN+i);
+ for (i=SIGRTMIN; i<=SIGRTMAX; i++)
+ sigaddset(&sigset, i);
pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
}
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, 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.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)
+{
+ if (fsm == &robot.fsm.main) {
+ strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
+ ORTEPublicationSend(robot.orte.publication_fsm_main);
+ } else if (fsm == &robot.fsm.motion) {
+ strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
+ ORTEPublicationSend(robot.orte.publication_fsm_motion);
+ } else if (fsm == &robot.fsm.act) {
+ strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
+ ORTEPublicationSend(robot.orte.publication_fsm_act);
+ }
- 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 */
}
-/**
+/**
* Initializes the robot.
* Setup fields in robot structure, initializes FSMs and ORTE.
- *
+ *
* @return 0
*/
int robot_init()
{
- int i;
int rv;
+ pthread_mutexattr_t mattr;
- /* robot and competition configuration */
- robot.mode = ROBO_TESTING;
- robot.team_color = BLUE;
- robot.beacon_color = BEACON_BLUE;
-
- pthread_mutex_init(&robot.lock, NULL);
- pthread_mutex_init(&robot.lock_ref_pos, NULL);
- pthread_mutex_init(&robot.lock_est_pos, NULL);
- pthread_mutex_init(&robot.lock_meas_angles, NULL);
- pthread_mutex_init(&robot.lock_joy_data, NULL);
- pthread_mutex_init(&robot.lock_disp, NULL);
+ 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_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 */
- for (i=0; i<FSM_CNT; i++)
- fsm_init(&robot.fsm[i], fsm_name[i], &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);
+ robot.fsm.main.transition_callback = trans_callback;
+ robot.fsm.act.transition_callback = trans_callback;
+ robot.fsm.motion.transition_callback = trans_callback;
- /* FIXME: there should be team color initilization */
-#ifdef WE_ARE_RED
robot.team_color = RED;
-#endif
-#ifdef WE_ARE_BLUE
- robot.team_color = BLUE;
-#endif
if (robot.team_color == RED) {
- printf("We are RED!\n");
+ ul_loginf("We are RED!\n");
} else {
- printf("We are BLUE!\n");
+ ul_loginf("We are BLUE!\n");
}
-#ifdef CONFIG_OPEN_LOOP
- printf("OPEN_LOOP enabled\n");
-#endif
+ 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();
robot.orte.pwr_ctrl.voltage50 = 1;
robot.orte.pwr_ctrl.voltage80 = 1;
- brushes_in();
-
- off_brush_left();
- off_brush_right();
-
- /* MCL initialization */
- robot.laser.width = PLAYGROUND_WIDTH_M; /* in meters */
- robot.laser.height = PLAYGROUND_WIDTH_M; /* in meters */
- /* the noises */
- robot.laser.pred_dnoise = 0.001;
- robot.laser.pred_anoise = DEG2RAD(2);
- robot.laser.aeval_sigma = DEG2RAD(4);
- robot.mcl = mcl_laser_init(&robot.laser, 1000);
+ robot.orte.camera_control.on = true;
- robot.fsm[FSM_ID_MOTION].state = &fsm_state_motion_init;
+ 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[FSM_ID_DISP].state = &fsm_state_disp_init;
+ robot.fsm.display.state = &fsm_state_disp_init;
+ */
robot.obstacle_avoidance_enabled = true;
- robot.laser_enabled = true;
- robot.state = JUST_STARTED;
-
- robot.carousel_cnt = 0;
- robot.carousel_pos = 0;
+ 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();
+ act_init(&robot.orte);
return rv;
}
-/**
- * Starts the robot threads.
+/**
+ * Starts the robot FSMs and threads.
*
* @return 0
*/
pthread_attr_t tattr;
struct sched_param param;
pthread_t thr_obstacle_forgeting;
+ int ret;
+
+ ret = motion_control_init();
+ if(ret) {
+ perror("motion_control_init");
+ robot_exit();
+ }
+
/* Obstacle forgeting thread */
pthread_attr_init (&tattr);
pthread_attr_getschedparam (&tattr, ¶m);
+ pthread_attr_getschedparam (&tattr, ¶m);
+ pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
param.sched_priority = OBST_FORGETING_PRIO;
rv = pthread_attr_setschedparam (&tattr, ¶m);
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()
{
- int i;
-
- /* stop FSM threads */
- for(i=0; i<FSM_CNT; i++) {
- fsm_exit(&robot.fsm[i]);
- }
-
- return 0;
+ /* stop FSMs */
+ fsm_exit(&robot.fsm.main);
+ fsm_exit(&robot.fsm.motion);
+ fsm_exit(&robot.fsm.act);
}
-/**
+/**
* Stops the robot. All resources alocated by robot_init() or
* robot_start() are dealocated here.
*/
void robot_destroy()
{
- int i;
+ motion_control_done();
- robot.orte.laser_cmd.speed = LASER_DRIVE_OFF;
- ORTEPublicationSend(robot.orte.publication_laser_cmd);
- ORTEPublicationSend(robot.orte.publication_laser_cmd);
+ // FIXME: set actuators to well defined position (FJ)
- off_brush_left();
- off_brush_right();
- robot.orte.motion_speed.right = 0;
- robot.orte.motion_speed.left = 0;
- ORTEPublicationSend(robot.orte.publication_motion_speed);
-
robottype_roboorte_destroy(&robot.orte);
- for (i=0; i<FSM_CNT; i++)
- fsm_destroy(&robot.fsm[i]);
- DBG("robofsm: stop.\n");
+ fsm_destroy(&robot.fsm.main);
+ fsm_destroy(&robot.fsm.motion);
+ fsm_destroy(&robot.fsm.act);
+ ShmapFree();
+ ul_logdeb("robofsm: stop.\n");
+}
- return 0;
+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);
+ }
}