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. */
17 #include <robot_orte.h>
22 #include "map_handling.h"
24 #include "actuators.h"
30 //UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
33 #define MOTION_CONTROL_INIT_ONLY
34 #include "motion-control.h"
36 /* Global definition of robot structure */
39 static void block_signals()
45 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
46 sigaddset(&sigset, i);
48 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
51 static void int_handler(int sig)
56 void fill_in_known_areas_in_map()
58 /* Do not plan path close to edges */
59 //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
60 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
61 //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
62 //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
65 static void trans_callback(struct robo_fsm *fsm)
67 /* if (fsm == &robot.fsm.main) {
68 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
69 ORTEPublicationSend(robot.orte.publication_fsm_main);
70 } else if (fsm == &robot.fsm.motion) {
71 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
72 ORTEPublicationSend(robot.orte.publication_fsm_motion);
73 } else if (fsm == &robot.fsm.act) {
74 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
75 ORTEPublicationSend(robot.orte.publication_fsm_act);
81 * Initializes the robot.
82 * Setup fields in robot structure, initializes FSMs and ORTE.
89 pthread_mutexattr_t mattr;
91 rv = pthread_mutexattr_init(&mattr);
92 #ifdef HAVE_PRIO_INHERIT
93 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
95 pthread_mutex_init(&robot.lock, &mattr);
96 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
97 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
98 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
99 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
100 pthread_mutex_init(&robot.lock_joy_data, &mattr);
101 pthread_mutex_init(&robot.lock_disp, &mattr);
103 MOTION = sched.create_processor<FSMMotion>();
105 robot.team_color = BLUE;
107 set_est_pos_trans(1, 1, DEG2RAD(0));
109 robot.ignore_hokuyo = false;
110 robot.map = ShmapInit(1);
111 fill_in_known_areas_in_map();
113 signal(SIGINT, int_handler);
114 signal(SIGTERM, int_handler);
118 robot.orte.motion_speed.left = 0xFFFF;
119 robot.orte.motion_speed.right = 0xFFFF;
121 robot.orte.pwr_ctrl.voltage33 = 1;
122 robot.orte.pwr_ctrl.voltage50 = 1;
123 robot.orte.pwr_ctrl.voltage80 = 1;
125 robot.orte.camera_control.on = false;
128 /* Only activate display if it is configured */
130 robot.sercom = uoled_init(serial_comm);
131 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
132 robot.fsm.display.state = &fsm_state_disp_init;
135 robot.obstacle_avoidance_enabled = true;
136 robot.use_rear_bumper = true;
137 robot.use_left_bumper = true;
138 robot.use_right_bumper = true;
139 robot.start_state = POWER_ON;
140 robot.check_turn_safety = true;
142 /* init ORTE domain, create publishers, subscribers, .. */
143 rv = robot_init_orte();
144 act_init(&robot.orte);
150 * Starts the robot FSMs and threads.
158 pthread_attr_t tattr;
159 struct sched_param param;
160 pthread_t thr_obstacle_forgeting;
163 ret = motion_control_init();
165 perror("motion_control_init");
170 /* Obstacle forgeting thread */
171 pthread_attr_init (&tattr);
172 pthread_attr_getschedparam (&tattr, ¶m);
173 pthread_attr_getschedparam (&tattr, ¶m);
174 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
175 param.sched_priority = OBST_FORGETING_PRIO;
176 rv = pthread_attr_setschedparam (&tattr, ¶m);
178 perror("robot_start: pthread_attr_setschedparam()");
181 rv = pthread_create(&thr_obstacle_forgeting,
182 &tattr, thread_obstacle_forgeting, NULL);
184 perror("robot_start: pthread_create");
194 * Signals all the robot threads to finish.
202 * Stops the robot. All resources alocated by robot_init() or
203 * robot_start() are dealocated here.
205 void Robot::destroy()
207 motion_control_done();
209 // FIXME: set actuators to well defined position (FJ)
211 robottype_roboorte_destroy(&robot.orte);
214 ul_logdeb("robofsm: stop.\n");
217 void Robot::get_est_pos_trans(double &x, double &y, double &phi)
219 get_est_pos(x, y, phi);
222 phi = trans_angle(phi);
225 void Robot::get_est_pos(double &x, double &y, double &phi)
227 if (robot.indep_odometry_works) {
228 Guard g(robot.lock_est_pos_indep_odo);
229 x = robot.est_pos_indep_odo.x;
230 y = robot.est_pos_indep_odo.y;
231 phi = robot.est_pos_indep_odo.phi;
232 } else if (robot.odometry_works) {
233 Guard g(robot.lock_est_pos_odo);
234 x = robot.est_pos_odo.x;
235 y = robot.est_pos_odo.y;
236 phi = robot.est_pos_odo.phi;
238 Guard g(robot.lock_ref_pos);
241 phi = robot.ref_pos.phi;
245 /** Sets actual position of the robot and with respoect to color of
246 * the team. Should be used for setting initial position of the
248 void Robot::set_est_pos(double x, double y, double phi) {
250 if (x>PLAYGROUND_WIDTH_M) x=PLAYGROUND_WIDTH_M;
252 if (y>PLAYGROUND_HEIGHT_M) y=PLAYGROUND_HEIGHT_M;
255 Guard g(lock_est_pos_indep_odo);
256 est_pos_indep_odo.x = x;
257 est_pos_indep_odo.y = y;
258 est_pos_indep_odo.phi = phi;
262 Guard g(lock_est_pos_odo);
265 est_pos_odo.phi = phi;
269 Guard g(lock_ref_pos);