2 * robot_eb2008.c 08/04/20
4 * Robot's generic initialization and clean up functions.
6 * Copyright: (c) 2008 CTU Dragons
7 * CTU FEE - Department of Control Engineering
11 #define _XOPEN_SOURCE 500 /* For PTHREAD_PRIO_INHERIT etc. */
14 #include <movehelper.h>
18 #include <robot_orte.h>
23 #include "map_handling.h"
25 #include "actuators.h"
28 #define DBG(format, ...) printf(format, ##__VA_ARGS__)
30 #define DBG(format, ...)
33 #define MOTION_CONTROL_INIT_ONLY
34 #include "motion-control.h"
36 /* Global definition of robot structure */
39 #ifdef CONFIG_LOCK_CHECKING
40 struct lock_log robot_lock_log;
43 static void block_signals()
49 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
50 sigaddset(&sigset, i);
52 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
55 static void int_handler(int sig)
60 void fill_in_known_areas_in_map()
62 /* Do not plan path close to edges */
63 //ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); /* left */
64 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
65 //ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
66 //ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
68 /* Ignore other obstacles at edges */
69 //ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
70 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
71 //ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
72 //ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
74 ShmapSetRectangleFlag(0.74, 1.6, 2.259, 2.1, MAP_FLAG_WALL, 0); /* plateau, slopes */
78 static void trans_callback(struct robo_fsm *fsm)
80 if (fsm == &robot.fsm.main) {
81 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
82 ORTEPublicationSend(robot.orte.publication_fsm_main);
83 } else if (fsm == &robot.fsm.motion) {
84 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
85 ORTEPublicationSend(robot.orte.publication_fsm_motion);
86 } else if (fsm == &robot.fsm.act) {
87 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
88 ORTEPublicationSend(robot.orte.publication_fsm_act);
94 * Initializes the robot.
95 * Setup fields in robot structure, initializes FSMs and ORTE.
102 pthread_mutexattr_t mattr;
104 rv = pthread_mutexattr_init(&mattr);
105 #ifdef HAVE_PRIO_INHERIT
106 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
108 pthread_mutex_init(&robot.lock, &mattr);
109 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
110 pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
111 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
112 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
113 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
114 pthread_mutex_init(&robot.lock_joy_data, &mattr);
115 pthread_mutex_init(&robot.lock_disp, &mattr);
117 fsm_main_loop_init(&robot.main_loop);
119 /* FSM initialization */
120 /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
121 fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
122 fsm_init(&robot.fsm.display, "display", &robot.main_loop);
123 fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
124 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
125 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
126 fsm_init(&robot.fsm.display, "\tdisp", &robot.main_loop);
127 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
128 robot.fsm.main.transition_callback = trans_callback;
129 robot.fsm.act.transition_callback = trans_callback;
130 robot.fsm.motion.transition_callback = trans_callback;
132 robot.team_color = BLUE;
134 if (robot.team_color == YELLOW) {
135 printf("We are YELLOW!\n");
137 printf("We are BLUE!\n");
140 robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
142 robot.map = ShmapInit(1);
143 fill_in_known_areas_in_map();
145 signal(SIGINT, int_handler);
146 signal(SIGTERM, int_handler);
150 robot.orte.motion_speed.left = 0;
151 robot.orte.motion_speed.right = 0;
153 robot.orte.pwr_ctrl.voltage33 = 1;
154 robot.orte.pwr_ctrl.voltage50 = 1;
155 robot.orte.pwr_ctrl.voltage80 = 1;
157 robot.orte.camera_control.on = true;
159 robot.fsm.motion.state = &fsm_state_motion_init;
161 robot.fsm.act.state = &fsm_state_act_wait_for_command; // actuator FSM's initial state
163 /* Only activate display if it is configured */
165 robot.sercom = uoled_init(serial_comm);
166 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
167 robot.fsm.display.state = &fsm_state_disp_init;
170 robot.obstacle_avoidance_enabled = true;
171 robot.use_back_switch = false; /* Switched on sime time after start */
172 robot.state = POWER_ON;
174 /* init ORTE domain, create publishers, subscribers, .. */
175 rv = robot_init_orte();
176 act_init(&robot.orte);
182 * Starts the robot FSMs and threads.
190 pthread_attr_t tattr;
191 struct sched_param param;
192 pthread_t thr_obstacle_forgeting;
195 ret = motion_control_init();
197 perror("motion_control_init");
202 /* Obstacle forgeting thread */
203 pthread_attr_init (&tattr);
204 pthread_attr_getschedparam (&tattr, ¶m);
205 pthread_attr_getschedparam (&tattr, ¶m);
206 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
207 param.sched_priority = OBST_FORGETING_PRIO;
208 rv = pthread_attr_setschedparam (&tattr, ¶m);
210 perror("robot_start: pthread_attr_setschedparam()");
213 rv = pthread_create(&thr_obstacle_forgeting,
214 &tattr, thread_obstacle_forgeting, NULL);
216 perror("robot_start: pthread_create");
220 fsm_main_loop(&robot.main_loop);
227 * Signals all the robot threads to finish.
232 fsm_exit(&robot.fsm.main);
233 fsm_exit(&robot.fsm.motion);
234 fsm_exit(&robot.fsm.display);
235 fsm_exit(&robot.fsm.act);
239 * Stops the robot. All resources alocated by robot_init() or
240 * robot_start() are dealocated here.
244 motion_control_done();
246 // FIXME: set actuators to well defined position (FJ)
248 robottype_roboorte_destroy(&robot.orte);
250 fsm_destroy(&robot.fsm.main);
251 fsm_destroy(&robot.fsm.motion);
252 fsm_destroy(&robot.fsm.display);
253 fsm_destroy(&robot.fsm.act);
255 DBG("robofsm: stop.\n");
258 void robot_get_est_pos(double *x, double *y, double *phi)
260 if (robot.indep_odometry_works) {
261 ROBOT_LOCK(est_pos_indep_odo);
262 *x = robot.est_pos_indep_odo.x;
263 *y = robot.est_pos_indep_odo.y;
264 *phi = robot.est_pos_indep_odo.phi;
265 ROBOT_UNLOCK(est_pos_indep_odo);
266 } else if (robot.odometry_works) {
267 ROBOT_LOCK(est_pos_odo);
268 *x = robot.est_pos_odo.x;
269 *y = robot.est_pos_odo.y;
270 *phi = robot.est_pos_odo.phi;
271 ROBOT_UNLOCK(est_pos_odo);
274 *x = robot.ref_pos.x;
275 *y = robot.ref_pos.y;
276 *phi = robot.ref_pos.phi;
277 ROBOT_UNLOCK(ref_pos);