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"
29 UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
32 #define MOTION_CONTROL_INIT_ONLY
33 #include "motion-control.h"
35 /* Global definition of robot structure */
38 #ifdef CONFIG_LOCK_CHECKING
39 struct lock_log robot_lock_log;
42 static void block_signals()
48 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
49 sigaddset(&sigset, i);
51 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
54 static void int_handler(int sig)
59 void fill_in_known_areas_in_map()
61 /* Do not plan path close to edges */
62 //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
63 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
64 //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
65 //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
67 // /* Container surroundings */
68 // ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
69 // ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
71 // /* Ignore other obstacles at edges */
72 // ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
73 // ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
74 // ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
75 // ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
76 // ShmapSetRectangleFlag(1.0, 1.5, 1.95, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* rised area */
78 /* start blocs + green area */
79 ShmapSetRectangleFlag(0, 0, 0.45, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0);
80 ShmapSetRectangleFlag(2.55, 0, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0);
83 ShmapSetRectangleFlag(0, 0, PLAYGROUND_WIDTH_M, 0.4, MAP_FLAG_WALL, 0);
85 /* plan trajectory on */
87 /* playing area walls */
88 // ShmapSetRectangleFlag(0, 0, PLAYGROUND_WIDTH_M, 0.1, MAP_FLAG_IGNORE_OBST, 0);
89 // ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, 0, PLAYGROUND_WIDTH_M - 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
90 // ShmapSetRectangleFlag(0 ,PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M - 0.1, MAP_FLAG_IGNORE_OBST, 0);
91 // ShmapSetRectangleFlag(0 , 0, 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
94 static void trans_callback(struct robo_fsm *fsm)
96 if (fsm == &robot.fsm.main) {
97 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
98 ORTEPublicationSend(robot.orte.publication_fsm_main);
99 } else if (fsm == &robot.fsm.motion) {
100 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
101 ORTEPublicationSend(robot.orte.publication_fsm_motion);
102 } else if (fsm == &robot.fsm.act) {
103 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
104 ORTEPublicationSend(robot.orte.publication_fsm_act);
110 * Initializes the robot.
111 * Setup fields in robot structure, initializes FSMs and ORTE.
118 pthread_mutexattr_t mattr;
120 rv = pthread_mutexattr_init(&mattr);
121 #ifdef HAVE_PRIO_INHERIT
122 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
124 pthread_mutex_init(&robot.lock, &mattr);
125 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
126 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
127 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
128 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
129 pthread_mutex_init(&robot.lock_joy_data, &mattr);
130 pthread_mutex_init(&robot.lock_disp, &mattr);
132 fsm_main_loop_init(&robot.main_loop);
134 /* FSM initialization */
135 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
136 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
137 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
138 robot.fsm.main.transition_callback = trans_callback;
139 robot.fsm.act.transition_callback = trans_callback;
140 robot.fsm.motion.transition_callback = trans_callback;
142 robot.team_color = RED;
144 if (robot.team_color == RED) {
145 ul_loginf("We are RED!\n");
147 ul_loginf("We are BLUE!\n");
150 robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
152 robot.ignore_hokuyo = false;
153 robot.map = ShmapInit(1);
154 fill_in_known_areas_in_map();
156 signal(SIGINT, int_handler);
157 signal(SIGTERM, int_handler);
161 robot.orte.motion_speed.left = 0;
162 robot.orte.motion_speed.right = 0;
164 robot.orte.pwr_ctrl.voltage33 = 1;
165 robot.orte.pwr_ctrl.voltage50 = 1;
166 robot.orte.pwr_ctrl.voltage80 = 1;
168 robot.orte.camera_control.on = true;
170 robot.fsm.motion.state = &fsm_state_motion_init;
172 /* Only activate display if it is configured */
174 robot.sercom = uoled_init(serial_comm);
175 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
176 robot.fsm.display.state = &fsm_state_disp_init;
179 robot.obstacle_avoidance_enabled = true;
180 robot.use_back_bumpers = true;
181 robot.use_left_bumper = true;
182 robot.use_right_bumper = true;
183 robot.start_state = POWER_ON;
184 robot.check_turn_safety = true;
186 /* init ORTE domain, create publishers, subscribers, .. */
187 rv = robot_init_orte();
188 act_init(&robot.orte);
194 * Starts the robot FSMs and threads.
202 pthread_attr_t tattr;
203 struct sched_param param;
204 pthread_t thr_obstacle_forgeting;
207 ret = motion_control_init();
209 perror("motion_control_init");
214 /* Obstacle forgeting thread */
215 pthread_attr_init (&tattr);
216 pthread_attr_getschedparam (&tattr, ¶m);
217 pthread_attr_getschedparam (&tattr, ¶m);
218 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
219 param.sched_priority = OBST_FORGETING_PRIO;
220 rv = pthread_attr_setschedparam (&tattr, ¶m);
222 perror("robot_start: pthread_attr_setschedparam()");
225 rv = pthread_create(&thr_obstacle_forgeting,
226 &tattr, thread_obstacle_forgeting, NULL);
228 perror("robot_start: pthread_create");
232 sem_init(&robot.start, 0, 0);
234 fsm_main_loop(&robot.main_loop);
241 * Signals all the robot threads to finish.
246 fsm_exit(&robot.fsm.main);
247 fsm_exit(&robot.fsm.motion);
248 fsm_exit(&robot.fsm.act);
252 * Stops the robot. All resources alocated by robot_init() or
253 * robot_start() are dealocated here.
257 motion_control_done();
259 // FIXME: set actuators to well defined position (FJ)
261 robottype_roboorte_destroy(&robot.orte);
263 fsm_destroy(&robot.fsm.main);
264 fsm_destroy(&robot.fsm.motion);
265 fsm_destroy(&robot.fsm.act);
267 ul_logdeb("robofsm: stop.\n");
270 void robot_get_est_pos_trans(double *x, double *y, double *phi)
272 robot_get_est_pos(x, y, phi);
275 *phi = __trans_ang(*phi);
278 void robot_get_est_pos(double *x, double *y, double *phi)
280 if (robot.indep_odometry_works) {
281 ROBOT_LOCK(est_pos_indep_odo);
282 *x = robot.est_pos_indep_odo.x;
283 *y = robot.est_pos_indep_odo.y;
284 *phi = robot.est_pos_indep_odo.phi;
285 ROBOT_UNLOCK(est_pos_indep_odo);
286 } else if (robot.odometry_works) {
287 ROBOT_LOCK(est_pos_odo);
288 *x = robot.est_pos_odo.x;
289 *y = robot.est_pos_odo.y;
290 *phi = robot.est_pos_odo.phi;
291 ROBOT_UNLOCK(est_pos_odo);
294 *x = robot.ref_pos.x;
295 *y = robot.ref_pos.y;
296 *phi = robot.ref_pos.phi;
297 ROBOT_UNLOCK(ref_pos);