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 /* protected bloc left */
83 ShmapSetRectangleFlag(0.45, 0, 1.15, 0.12, MAP_FLAG_WALL, 0);
84 ShmapSetRectangleFlag(0.45, 0.12, 0.472, 0.25, MAP_FLAG_WALL, 0);
85 ShmapSetRectangleFlag(1.128, 0.12, 1.15, 0.25, MAP_FLAG_WALL, 0);
87 /* protected bloc right */
88 ShmapSetRectangleFlag(1.85, 0, 2.55, 0.12, MAP_FLAG_WALL, 0);
89 ShmapSetRectangleFlag(1.85, 0.12, 1.872, 0.25, MAP_FLAG_WALL, 0);
90 ShmapSetRectangleFlag(2.528, 0.12, 2.55, 0.25, MAP_FLAG_WALL, 0);
93 static void trans_callback(struct robo_fsm *fsm)
95 if (fsm == &robot.fsm.main) {
96 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
97 ORTEPublicationSend(robot.orte.publication_fsm_main);
98 } else if (fsm == &robot.fsm.motion) {
99 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
100 ORTEPublicationSend(robot.orte.publication_fsm_motion);
101 } else if (fsm == &robot.fsm.act) {
102 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
103 ORTEPublicationSend(robot.orte.publication_fsm_act);
109 * Initializes the robot.
110 * Setup fields in robot structure, initializes FSMs and ORTE.
117 pthread_mutexattr_t mattr;
119 rv = pthread_mutexattr_init(&mattr);
120 #ifdef HAVE_PRIO_INHERIT
121 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
123 pthread_mutex_init(&robot.lock, &mattr);
124 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
125 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
126 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
127 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
128 pthread_mutex_init(&robot.lock_joy_data, &mattr);
129 pthread_mutex_init(&robot.lock_disp, &mattr);
131 fsm_main_loop_init(&robot.main_loop);
133 /* FSM initialization */
134 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
135 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
136 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
137 robot.fsm.main.transition_callback = trans_callback;
138 robot.fsm.act.transition_callback = trans_callback;
139 robot.fsm.motion.transition_callback = trans_callback;
141 robot.team_color = RED;
143 if (robot.team_color == RED) {
144 ul_loginf("We are RED!\n");
146 ul_loginf("We are BLUE!\n");
149 robot_set_est_pos_trans(1, 1, DEG2RAD(0));
151 robot.ignore_hokuyo = false;
152 robot.map = ShmapInit(1);
153 fill_in_known_areas_in_map();
155 signal(SIGINT, int_handler);
156 signal(SIGTERM, int_handler);
160 robot.orte.motion_speed.left = 0;
161 robot.orte.motion_speed.right = 0;
163 robot.orte.pwr_ctrl.voltage33 = 1;
164 robot.orte.pwr_ctrl.voltage50 = 1;
165 robot.orte.pwr_ctrl.voltage80 = 1;
167 robot.orte.camera_control.on = true;
169 robot.fsm.motion.state = &fsm_state_motion_init;
171 /* Only activate display if it is configured */
173 robot.sercom = uoled_init(serial_comm);
174 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
175 robot.fsm.display.state = &fsm_state_disp_init;
178 robot.obstacle_avoidance_enabled = true;
179 robot.use_back_switch = true;
180 robot.use_left_switch = true;
181 robot.use_right_switch = true;
182 robot.start_state = POWER_ON;
183 robot.check_turn_safety = true;
185 /* init ORTE domain, create publishers, subscribers, .. */
186 rv = robot_init_orte();
187 act_init(&robot.orte);
193 * Starts the robot FSMs and threads.
201 pthread_attr_t tattr;
202 struct sched_param param;
203 pthread_t thr_obstacle_forgeting;
206 ret = motion_control_init();
208 perror("motion_control_init");
213 /* Obstacle forgeting thread */
214 pthread_attr_init (&tattr);
215 pthread_attr_getschedparam (&tattr, ¶m);
216 pthread_attr_getschedparam (&tattr, ¶m);
217 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
218 param.sched_priority = OBST_FORGETING_PRIO;
219 rv = pthread_attr_setschedparam (&tattr, ¶m);
221 perror("robot_start: pthread_attr_setschedparam()");
224 rv = pthread_create(&thr_obstacle_forgeting,
225 &tattr, thread_obstacle_forgeting, NULL);
227 perror("robot_start: pthread_create");
231 sem_init(&robot.start, 0, 0);
233 fsm_main_loop(&robot.main_loop);
240 * Signals all the robot threads to finish.
245 fsm_exit(&robot.fsm.main);
246 fsm_exit(&robot.fsm.motion);
247 fsm_exit(&robot.fsm.act);
251 * Stops the robot. All resources alocated by robot_init() or
252 * robot_start() are dealocated here.
256 motion_control_done();
258 // FIXME: set actuators to well defined position (FJ)
260 robottype_roboorte_destroy(&robot.orte);
262 fsm_destroy(&robot.fsm.main);
263 fsm_destroy(&robot.fsm.motion);
264 fsm_destroy(&robot.fsm.act);
266 ul_logdeb("robofsm: stop.\n");
269 void robot_get_est_pos_trans(double *x, double *y, double *phi)
271 robot_get_est_pos(x, y, phi);
274 *phi = __trans_ang(*phi);
277 void robot_get_est_pos(double *x, double *y, double *phi)
279 if (robot.indep_odometry_works) {
280 ROBOT_LOCK(est_pos_indep_odo);
281 *x = robot.est_pos_indep_odo.x;
282 *y = robot.est_pos_indep_odo.y;
283 *phi = robot.est_pos_indep_odo.phi;
284 ROBOT_UNLOCK(est_pos_indep_odo);
285 } else if (robot.odometry_works) {
286 ROBOT_LOCK(est_pos_odo);
287 *x = robot.est_pos_odo.x;
288 *y = robot.est_pos_odo.y;
289 *phi = robot.est_pos_odo.phi;
290 ROBOT_UNLOCK(est_pos_odo);
293 *x = robot.ref_pos.x;
294 *y = robot.ref_pos.y;
295 *phi = robot.ref_pos.phi;
296 ROBOT_UNLOCK(ref_pos);