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 */
79 ShmapSetRectangleFlag(0, 1.7, 0.4, 1.678, MAP_FLAG_WALL, 0);
80 ShmapSetRectangleFlag(2.55, 1.7, 3, 1.678, MAP_FLAG_WALL, 0);
82 /* opponent start area */
83 if (robot.team_color == RED) {
84 ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M - 0.4 - 0.05 - 0.35, PLAYGROUND_HEIGHT_M - 0.4, MAP_FLAG_WALL, 0);
86 ShmapSetRectangleFlag(0, PLAYGROUND_HEIGHT_M, 0.8, PLAYGROUND_HEIGHT_M - 0.4, MAP_FLAG_WALL, 0);
89 /* protected bloc left */
90 ShmapSetRectangleFlag(0, 0, 1.15, 0.35, MAP_FLAG_WALL, 0);
92 /* protected bloc right */
93 ShmapSetRectangleFlag(1.85, 0, PLAYGROUND_WIDTH_M, 0.35, MAP_FLAG_WALL, 0);
95 /* plan trajectory on */
97 /* playing area walls */
98 // ShmapSetRectangleFlag(0, 0, PLAYGROUND_WIDTH_M, 0.1, MAP_FLAG_IGNORE_OBST, 0);
99 // ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, 0, PLAYGROUND_WIDTH_M - 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
100 // ShmapSetRectangleFlag(0 ,PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M - 0.1, MAP_FLAG_IGNORE_OBST, 0);
101 // ShmapSetRectangleFlag(0 , 0, 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
104 static void trans_callback(struct robo_fsm *fsm)
106 if (fsm == &robot.fsm.main) {
107 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
108 ORTEPublicationSend(robot.orte.publication_fsm_main);
109 } else if (fsm == &robot.fsm.motion) {
110 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
111 ORTEPublicationSend(robot.orte.publication_fsm_motion);
112 } else if (fsm == &robot.fsm.act) {
113 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
114 ORTEPublicationSend(robot.orte.publication_fsm_act);
120 * Initializes the robot.
121 * Setup fields in robot structure, initializes FSMs and ORTE.
128 pthread_mutexattr_t mattr;
130 rv = pthread_mutexattr_init(&mattr);
131 #ifdef HAVE_PRIO_INHERIT
132 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
134 pthread_mutex_init(&robot.lock, &mattr);
135 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
136 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
137 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
138 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
139 pthread_mutex_init(&robot.lock_joy_data, &mattr);
140 pthread_mutex_init(&robot.lock_disp, &mattr);
142 fsm_main_loop_init(&robot.main_loop);
144 /* FSM initialization */
145 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
146 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
147 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
148 robot.fsm.main.transition_callback = trans_callback;
149 robot.fsm.act.transition_callback = trans_callback;
150 robot.fsm.motion.transition_callback = trans_callback;
152 robot.team_color = RED;
154 if (robot.team_color == RED) {
155 ul_loginf("We are RED!\n");
157 ul_loginf("We are BLUE!\n");
160 robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
162 robot.ignore_hokuyo = false;
163 robot.map = ShmapInit(1);
164 fill_in_known_areas_in_map();
166 signal(SIGINT, int_handler);
167 signal(SIGTERM, int_handler);
171 robot.orte.motion_speed.left = 0;
172 robot.orte.motion_speed.right = 0;
174 robot.orte.pwr_ctrl.voltage33 = 1;
175 robot.orte.pwr_ctrl.voltage50 = 1;
176 robot.orte.pwr_ctrl.voltage80 = 1;
178 robot.orte.camera_control.on = true;
180 robot.fsm.motion.state = &fsm_state_motion_init;
182 /* Only activate display if it is configured */
184 robot.sercom = uoled_init(serial_comm);
185 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
186 robot.fsm.display.state = &fsm_state_disp_init;
189 robot.obstacle_avoidance_enabled = true;
190 robot.use_back_bumpers = true;
191 robot.use_left_bumper = true;
192 robot.use_right_bumper = true;
193 robot.start_state = POWER_ON;
194 robot.check_turn_safety = true;
196 /* init ORTE domain, create publishers, subscribers, .. */
197 rv = robot_init_orte();
198 act_init(&robot.orte);
204 * Starts the robot FSMs and threads.
212 pthread_attr_t tattr;
213 struct sched_param param;
214 pthread_t thr_obstacle_forgeting;
217 ret = motion_control_init();
219 perror("motion_control_init");
224 /* Obstacle forgeting thread */
225 pthread_attr_init (&tattr);
226 pthread_attr_getschedparam (&tattr, ¶m);
227 pthread_attr_getschedparam (&tattr, ¶m);
228 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
229 param.sched_priority = OBST_FORGETING_PRIO;
230 rv = pthread_attr_setschedparam (&tattr, ¶m);
232 perror("robot_start: pthread_attr_setschedparam()");
235 rv = pthread_create(&thr_obstacle_forgeting,
236 &tattr, thread_obstacle_forgeting, NULL);
238 perror("robot_start: pthread_create");
242 sem_init(&robot.start, 0, 0);
244 fsm_main_loop(&robot.main_loop);
251 * Signals all the robot threads to finish.
256 fsm_exit(&robot.fsm.main);
257 fsm_exit(&robot.fsm.motion);
258 fsm_exit(&robot.fsm.act);
262 * Stops the robot. All resources alocated by robot_init() or
263 * robot_start() are dealocated here.
267 motion_control_done();
269 // FIXME: set actuators to well defined position (FJ)
271 robottype_roboorte_destroy(&robot.orte);
273 fsm_destroy(&robot.fsm.main);
274 fsm_destroy(&robot.fsm.motion);
275 fsm_destroy(&robot.fsm.act);
277 ul_logdeb("robofsm: stop.\n");
280 void robot_get_est_pos_trans(double *x, double *y, double *phi)
282 robot_get_est_pos(x, y, phi);
285 *phi = __trans_ang(*phi);
288 void robot_get_est_pos(double *x, double *y, double *phi)
290 if (robot.indep_odometry_works) {
291 ROBOT_LOCK(est_pos_indep_odo);
292 *x = robot.est_pos_indep_odo.x;
293 *y = robot.est_pos_indep_odo.y;
294 *phi = robot.est_pos_indep_odo.phi;
295 ROBOT_UNLOCK(est_pos_indep_odo);
296 } else if (robot.odometry_works) {
297 ROBOT_LOCK(est_pos_odo);
298 *x = robot.est_pos_odo.x;
299 *y = robot.est_pos_odo.y;
300 *phi = robot.est_pos_odo.phi;
301 ROBOT_UNLOCK(est_pos_odo);
304 *x = robot.ref_pos.x;
305 *y = robot.ref_pos.y;
306 *phi = robot.ref_pos.phi;
307 ROBOT_UNLOCK(ref_pos);