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 */
68 static void trans_callback(struct robo_fsm *fsm)
70 if (fsm == &robot.fsm.main) {
71 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
72 ORTEPublicationSend(robot.orte.publication_fsm_main);
73 } else if (fsm == &robot.fsm.motion) {
74 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
75 ORTEPublicationSend(robot.orte.publication_fsm_motion);
76 } else if (fsm == &robot.fsm.act) {
77 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
78 ORTEPublicationSend(robot.orte.publication_fsm_act);
84 * Initializes the robot.
85 * Setup fields in robot structure, initializes FSMs and ORTE.
92 pthread_mutexattr_t mattr;
94 rv = pthread_mutexattr_init(&mattr);
95 #ifdef HAVE_PRIO_INHERIT
96 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
98 pthread_mutex_init(&robot.lock, &mattr);
99 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
100 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
101 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
102 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
103 pthread_mutex_init(&robot.lock_joy_data, &mattr);
104 pthread_mutex_init(&robot.lock_disp, &mattr);
106 fsm_main_loop_init(&robot.main_loop);
108 /* FSM initialization */
109 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
110 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
111 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
112 robot.fsm.main.transition_callback = trans_callback;
113 robot.fsm.act.transition_callback = trans_callback;
114 robot.fsm.motion.transition_callback = trans_callback;
116 robot.team_color = BLUE;
118 robot_set_est_pos_trans(1, 1, DEG2RAD(0));
120 robot.ignore_hokuyo = false;
121 robot.map = ShmapInit(1);
122 fill_in_known_areas_in_map();
124 signal(SIGINT, int_handler);
125 signal(SIGTERM, int_handler);
129 robot.orte.motion_speed.left = 0xFFFF;
130 robot.orte.motion_speed.right = 0xFFFF;
132 robot.orte.pwr_ctrl.voltage33 = 1;
133 robot.orte.pwr_ctrl.voltage50 = 1;
134 robot.orte.pwr_ctrl.voltage80 = 1;
136 robot.orte.camera_control.on = false;
138 robot.fsm.motion.state = &fsm_state_motion_init;
140 /* Only activate display if it is configured */
142 robot.sercom = uoled_init(serial_comm);
143 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
144 robot.fsm.display.state = &fsm_state_disp_init;
147 robot.obstacle_avoidance_enabled = true;
148 robot.use_rear_bumper = true;
149 robot.use_left_bumper = true;
150 robot.use_right_bumper = true;
151 robot.start_state = POWER_ON;
152 robot.check_turn_safety = true;
154 /* init ORTE domain, create publishers, subscribers, .. */
155 rv = robot_init_orte();
156 act_init(&robot.orte);
162 * Starts the robot FSMs and threads.
170 pthread_attr_t tattr;
171 struct sched_param param;
172 pthread_t thr_obstacle_forgeting;
175 ret = motion_control_init();
177 perror("motion_control_init");
182 /* Obstacle forgeting thread */
183 pthread_attr_init (&tattr);
184 pthread_attr_getschedparam (&tattr, ¶m);
185 pthread_attr_getschedparam (&tattr, ¶m);
186 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
187 param.sched_priority = OBST_FORGETING_PRIO;
188 rv = pthread_attr_setschedparam (&tattr, ¶m);
190 perror("robot_start: pthread_attr_setschedparam()");
193 rv = pthread_create(&thr_obstacle_forgeting,
194 &tattr, thread_obstacle_forgeting, NULL);
196 perror("robot_start: pthread_create");
200 sem_init(&robot.start, 0, 0);
202 fsm_main_loop(&robot.main_loop);
209 * Signals all the robot threads to finish.
214 fsm_exit(&robot.fsm.main);
215 fsm_exit(&robot.fsm.motion);
216 fsm_exit(&robot.fsm.act);
220 * Stops the robot. All resources alocated by robot_init() or
221 * robot_start() are dealocated here.
225 motion_control_done();
227 // FIXME: set actuators to well defined position (FJ)
229 robottype_roboorte_destroy(&robot.orte);
231 fsm_destroy(&robot.fsm.main);
232 fsm_destroy(&robot.fsm.motion);
233 fsm_destroy(&robot.fsm.act);
235 ul_logdeb("robofsm: stop.\n");
238 void robot_get_est_pos_trans(double *x, double *y, double *phi)
240 robot_get_est_pos(x, y, phi);
243 *phi = __trans_ang(*phi);
246 void robot_get_est_pos(double *x, double *y, double *phi)
248 if (robot.indep_odometry_works) {
249 ROBOT_LOCK(est_pos_indep_odo);
250 *x = robot.est_pos_indep_odo.x;
251 *y = robot.est_pos_indep_odo.y;
252 *phi = robot.est_pos_indep_odo.phi;
253 ROBOT_UNLOCK(est_pos_indep_odo);
254 } else if (robot.odometry_works) {
255 ROBOT_LOCK(est_pos_odo);
256 *x = robot.est_pos_odo.x;
257 *y = robot.est_pos_odo.y;
258 *phi = robot.est_pos_odo.phi;
259 ROBOT_UNLOCK(est_pos_odo);
262 *x = robot.ref_pos.x;
263 *y = robot.ref_pos.y;
264 *phi = robot.ref_pos.phi;
265 ROBOT_UNLOCK(ref_pos);