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()
62 /*----- playground width 3.0 m and playground height 2.0 m -----*/
63 /* Ignore other obstacles at edges */
64 ShmapSetRectangleFlag(0.0, 0.0, 0.40, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* left */
65 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
66 ShmapSetRectangleFlag(0.0, 1.91, 3.0, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* top */
67 ShmapSetRectangleFlag(2.6, 0.0, 3.0, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* right */
69 /* Small walls that cannot be detected by hokuyo */
70 ShmapSetRectangleFlag(0.0, 1.49, 0.39, 1.432, MAP_FLAG_WALL, 0);
71 ShmapSetRectangleFlag(3.0, 1.49, 2.61, 1.432, MAP_FLAG_WALL, 0);
72 ShmapSetRectangleFlag(0.325, 0.0, 0.38, 0.74, MAP_FLAG_WALL, 0);
73 ShmapSetRectangleFlag(2.675, 0.0, 2.62, 0.74, MAP_FLAG_WALL, 0);
76 //ShmapSetCircleFlag(1.5, 1.0, 0.075, MAP_FLAG_WALL, 0);
78 /* Totems and palm tree */
79 ShmapSetRectangleFlag(0.975, 0.875, 2.025, 1.125, MAP_FLAG_WALL, 0);
80 ShmapSetCircleFlag(1.1, 1.0, 0.3, MAP_FLAG_WALL, 0);
81 ShmapSetCircleFlag(2.0, 1.0, 0.3, MAP_FLAG_WALL, 0);
83 //ShmapSetRectangleFlag(1.775, 0.875, 2.025, 1.125, MAP_FLAG_WALL, 0);
86 static void trans_callback(struct robo_fsm *fsm)
88 if (fsm == &robot.fsm.main) {
89 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
90 ORTEPublicationSend(robot.orte.publication_fsm_main);
91 } else if (fsm == &robot.fsm.motion) {
92 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
93 ORTEPublicationSend(robot.orte.publication_fsm_motion);
94 } else if (fsm == &robot.fsm.act) {
95 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
96 ORTEPublicationSend(robot.orte.publication_fsm_act);
102 * Initializes the robot.
103 * Setup fields in robot structure, initializes FSMs and ORTE.
110 pthread_mutexattr_t mattr;
111 rv = pthread_mutexattr_init(&mattr);
112 #ifdef HAVE_PRIO_INHERIT
113 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
115 pthread_mutex_init(&robot.lock, &mattr);
116 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
117 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
118 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
119 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
120 pthread_mutex_init(&robot.lock_joy_data, &mattr);
121 pthread_mutex_init(&robot.lock_disp, &mattr);
123 fsm_main_loop_init(&robot.main_loop);
125 /* FSM initialization */
126 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
127 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
128 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
129 robot.fsm.main.transition_callback = trans_callback;
130 robot.fsm.act.transition_callback = trans_callback;
131 robot.fsm.motion.transition_callback = trans_callback;
133 robot.team_color = VIOLET;
135 if (robot.team_color == VIOLET) {
136 ul_loginf("We are VIOLET!\n");
138 ul_loginf("We are RED!\n");
141 robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
143 robot.ignore_hokuyo = false;
144 robot.map = ShmapInit(1);
145 fill_in_known_areas_in_map();
147 signal(SIGINT, int_handler);
148 signal(SIGTERM, int_handler);
152 robot.orte.motion_speed.left = 0;
153 robot.orte.motion_speed.right = 0;
155 robot.orte.pwr_ctrl.voltage33 = 1;
156 robot.orte.pwr_ctrl.voltage50 = 1;
157 robot.orte.pwr_ctrl.voltage80 = 1;
159 robot.orte.camera_control.on = true;
161 robot.fsm.motion.state = &fsm_state_motion_init;
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_bumpers = true;
172 robot.use_left_bumper = true;
173 robot.use_right_bumper = true;
174 robot.start_state = POWER_ON;
175 robot.check_turn_safety = true;
177 /* init ORTE domain, create publishers, subscribers, .. */
178 rv = robot_init_orte();
179 act_init(&robot.orte);
185 * Starts the robot FSMs and threads.
193 pthread_attr_t tattr;
194 struct sched_param param;
195 pthread_t thr_obstacle_forgeting;
198 ret = motion_control_init();
200 perror("motion_control_init");
205 /* Obstacle forgeting thread */
206 pthread_attr_init (&tattr);
207 pthread_attr_getschedparam (&tattr, ¶m);
208 pthread_attr_getschedparam (&tattr, ¶m);
209 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
210 param.sched_priority = OBST_FORGETING_PRIO;
211 rv = pthread_attr_setschedparam (&tattr, ¶m);
213 perror("robot_start: pthread_attr_setschedparam()");
216 rv = pthread_create(&thr_obstacle_forgeting,
217 &tattr, thread_obstacle_forgeting, NULL);
219 perror("robot_start: pthread_create");
223 sem_init(&robot.start, 0, 0);
225 fsm_main_loop(&robot.main_loop);
232 * Signals all the robot threads to finish.
237 fsm_exit(&robot.fsm.main);
238 fsm_exit(&robot.fsm.motion);
239 fsm_exit(&robot.fsm.act);
243 * Stops the robot. All resources alocated by robot_init() or
244 * robot_start() are dealocated here.
248 motion_control_done();
250 // FIXME: set actuators to well defined position (FJ)
252 robottype_roboorte_destroy(&robot.orte);
254 fsm_destroy(&robot.fsm.main);
255 fsm_destroy(&robot.fsm.motion);
256 fsm_destroy(&robot.fsm.act);
258 ul_logdeb("robofsm: stop.\n");
261 void robot_get_est_pos_trans(double *x, double *y, double *phi)
263 robot_get_est_pos(x, y, phi);
266 *phi = __trans_ang(*phi);
269 void robot_get_est_pos(double *x, double *y, double *phi)
271 if (robot.indep_odometry_works) {
272 ROBOT_LOCK(est_pos_indep_odo);
273 *x = robot.est_pos_indep_odo.x;
274 *y = robot.est_pos_indep_odo.y;
275 *phi = robot.est_pos_indep_odo.phi;
276 ROBOT_UNLOCK(est_pos_indep_odo);
277 } else if (robot.odometry_works) {
278 ROBOT_LOCK(est_pos_odo);
279 *x = robot.est_pos_odo.x;
280 *y = robot.est_pos_odo.y;
281 *phi = robot.est_pos_odo.phi;
282 ROBOT_UNLOCK(est_pos_odo);
285 *x = robot.ref_pos.x;
286 *y = robot.ref_pos.y;
287 *phi = robot.ref_pos.phi;
288 ROBOT_UNLOCK(ref_pos);
292 void robot_calibrate_odometry()
294 robot.odo_distance_a = 0;
295 robot.odo_distance_b = 0;
297 file = fopen ("odometry_cal_data", "r");
302 fprintf(stderr, "ODO calibration file not found! \n\n");
309 while (fgets (line, 15 , file)) {
310 a += atof(strtok(line," "));
311 fgets (line, 15 , file);
312 b += atof(strtok(NULL," "));
316 if(a == 0 || b == 0) {
321 robot.odo_cal_a = a / num;
322 robot.odo_cal_b = b / num;
323 printf("ODO calibrated value A: %f\n",robot.odo_cal_a);
324 printf("ODO calibrated value B: %f\n",robot.odo_cal_b);