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"
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()
63 ShmapSetRectangleFlag(0.0, PLAYGROUND_HEIGHT_M / 2.0, PLAYGROUND_WIDTH_M - 0.5, PLAYGROUND_HEIGHT_M / 2.0, MAP_FLAG_WALL, 0);
64 ShmapSetRectangleFlag(0.4, 0.0, 0.4, PLAYGROUND_HEIGHT_M / 4.0, MAP_FLAG_WALL, 0);
65 ShmapSetRectangleFlag(0.9, PLAYGROUND_HEIGHT_M / 4.0, 0.9, PLAYGROUND_HEIGHT_M / 2.0, MAP_FLAG_WALL, 0);
67 /* playground walls */
68 ShmapSetRectangleFlag(0.0, 0.0, PLAYGROUND_WIDTH_M, 0.0, MAP_FLAG_WALL, 0);
69 ShmapSetRectangleFlag(0.0, 0.0, 0.0, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0);
71 ShmapSetRectangleFlag(0.0, PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0);
72 ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, 0.0, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0);
75 static void trans_callback(struct robo_fsm *fsm)
77 if (fsm == &robot.fsm.main) {
78 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
79 ORTEPublicationSend(robot.orte.publication_fsm_main);
80 } else if (fsm == &robot.fsm.motion) {
81 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
82 ORTEPublicationSend(robot.orte.publication_fsm_motion);
83 } else if (fsm == &robot.fsm.act) {
84 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
85 ORTEPublicationSend(robot.orte.publication_fsm_act);
91 * Initializes the robot.
92 * Setup fields in robot structure, initializes FSMs and ORTE.
99 pthread_mutexattr_t mattr;
100 rv = pthread_mutexattr_init(&mattr);
101 #ifdef HAVE_PRIO_INHERIT
102 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
104 pthread_mutex_init(&robot.lock, &mattr);
105 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
106 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
107 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
108 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
109 pthread_mutex_init(&robot.lock_joy_data, &mattr);
110 pthread_mutex_init(&robot.lock_disp, &mattr);
112 fsm_main_loop_init(&robot.main_loop);
114 /* FSM initialization */
115 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
116 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
117 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
118 robot.fsm.main.transition_callback = trans_callback;
119 robot.fsm.act.transition_callback = trans_callback;
120 robot.fsm.motion.transition_callback = trans_callback;
122 robot.team_color = TC_WHITE;
124 switch (robot.team_color) {
126 ul_loginf("We are WHITE!\n");
129 ul_loginf("We are GREEN!\n");
132 ul_loginf("We are YELLOW!\n");
136 robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
138 robot.ignore_hokuyo = false;
139 robot.ignore_sick = false;
140 robot.map = ShmapInit(1);
141 fill_in_known_areas_in_map();
143 signal(SIGINT, int_handler);
144 signal(SIGTERM, int_handler);
148 robot.orte.motion_speed.left = 0;
149 robot.orte.motion_speed.right = 0;
151 robot.orte.pwr_ctrl.voltage33 = 1;
152 robot.orte.pwr_ctrl.voltage50 = 1;
153 robot.orte.pwr_ctrl.voltage80 = 1;
155 robot.orte.camera_control.on = false;
157 robot.fsm.motion.state = &fsm_state_motion_init;
159 /* Only activate display if it is configured */
161 robot.sercom = uoled_init(serial_comm);
162 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
163 robot.fsm.display.state = &fsm_state_disp_init;
166 robot.obstacle_avoidance_enabled = true;
167 robot.use_back_bumpers = true;
168 robot.use_left_bumper = true;
169 robot.use_right_bumper = true;
170 robot.start_state = POWER_ON;
171 robot.check_turn_safety = true;
173 /* init ORTE domain, create publishers, subscribers, .. */
174 rv = robot_init_orte();
175 act_init(&robot.orte);
181 * Starts the robot FSMs and threads.
189 pthread_attr_t tattr;
190 struct sched_param param;
191 pthread_t thr_obstacle_forgeting;
194 ret = motion_control_init();
196 perror("motion_control_init");
201 /* Obstacle forgeting thread */
202 pthread_attr_init (&tattr);
203 pthread_attr_getschedparam (&tattr, ¶m);
204 pthread_attr_getschedparam (&tattr, ¶m);
205 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
206 param.sched_priority = OBST_FORGETING_PRIO;
207 rv = pthread_attr_setschedparam (&tattr, ¶m);
209 perror("robot_start: pthread_attr_setschedparam()");
212 rv = pthread_create(&thr_obstacle_forgeting,
213 &tattr, thread_obstacle_forgeting, NULL);
215 perror("robot_start: pthread_create");
219 sem_init(&robot.start, 0, 0);
221 fsm_main_loop(&robot.main_loop);
228 * Signals all the robot threads to finish.
233 fsm_exit(&robot.fsm.main);
234 fsm_exit(&robot.fsm.motion);
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.act);
254 ul_logdeb("robofsm: stop.\n");
257 void robot_get_est_pos_trans(double *x, double *y, double *phi)
259 robot_get_est_pos(x, y, phi);
262 *phi = __trans_ang(*phi);
265 void robot_get_est_pos(double *x, double *y, double *phi)
267 if (robot.indep_odometry_works) {
268 ROBOT_LOCK(est_pos_indep_odo);
269 *x = robot.est_pos_indep_odo.x;
270 *y = robot.est_pos_indep_odo.y;
271 *phi = robot.est_pos_indep_odo.phi;
272 ROBOT_UNLOCK(est_pos_indep_odo);
273 } else if (robot.odometry_works) {
274 ROBOT_LOCK(est_pos_odo);
275 *x = robot.est_pos_odo.x;
276 *y = robot.est_pos_odo.y;
277 *phi = robot.est_pos_odo.phi;
278 ROBOT_UNLOCK(est_pos_odo);
281 *x = robot.ref_pos.x;
282 *y = robot.ref_pos.y;
283 *phi = robot.ref_pos.phi;
284 ROBOT_UNLOCK(ref_pos);