2 * robot_eb2008.c 08/04/20
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"
26 #include "corns_configs.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 #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 /* Do not plan path close to edges */
63 //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
64 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
65 //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
66 //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
68 /* Container surroundings */
69 ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
70 ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
72 /* Ignore other obstacles at edges */
73 ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
74 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
75 ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
76 ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
77 ShmapSetRectangleFlag(1.0, 1.5, 1.95, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* rised area */
79 ShmapSetRectangleFlag(0.6, 1.45, 2.45, 1.55, MAP_FLAG_WALL, 0); /* plateau, slopes */
80 ShmapSetRectangleFlag(1.46, PLAYGROUND_HEIGHT_M - 0.5, 1.57, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0); /* plateau, slopes */
83 struct corns_group *corns = get_all_corns(0, 0);
85 for(corn = corns->corns; corn < &corns->corns[corns->corns_count]; corn++) {
86 ShmapSetCircleFlag(corn->position.x, corn->position.y, CORN_NEIGHBOURHOOD_RADIUS_M, MAP_FLAG_WALL, 0);
88 dispose_corns_group(corns); // robot.corns will be set later, when the corns' configuration is known
90 ShmapSetRectangleFlag(0.32, 0.25, 0.38, 0.55, 0, MAP_FLAG_WALL); /* destination position near blue container */
91 ShmapSetRectangleFlag(2.62, 0.25, 2.68, 0.55, 0, MAP_FLAG_WALL); /* destination position near yellow container */
94 static void trans_callback(struct robo_fsm *fsm)
96 if (fsm == &robot.fsm.main) {
97 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
98 ORTEPublicationSend(robot.orte.publication_fsm_main);
99 } else if (fsm == &robot.fsm.motion) {
100 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
101 ORTEPublicationSend(robot.orte.publication_fsm_motion);
102 } else if (fsm == &robot.fsm.act) {
103 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
104 ORTEPublicationSend(robot.orte.publication_fsm_act);
110 * Initializes the robot.
111 * Setup fields in robot structure, initializes FSMs and ORTE.
118 pthread_mutexattr_t mattr;
120 rv = pthread_mutexattr_init(&mattr);
121 #ifdef HAVE_PRIO_INHERIT
122 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
124 pthread_mutex_init(&robot.lock, &mattr);
125 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
126 pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
127 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
128 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
129 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
130 pthread_mutex_init(&robot.lock_joy_data, &mattr);
131 pthread_mutex_init(&robot.lock_disp, &mattr);
133 fsm_main_loop_init(&robot.main_loop);
135 /* FSM initialization */
136 /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
137 fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
138 fsm_init(&robot.fsm.display, "display", &robot.main_loop);
139 fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
140 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
141 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
142 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
143 robot.fsm.main.transition_callback = trans_callback;
144 robot.fsm.act.transition_callback = trans_callback;
145 robot.fsm.motion.transition_callback = trans_callback;
147 robot.team_color = BLUE;
149 if (robot.team_color == YELLOW) {
150 ul_loginf("We are YELLOW!\n");
152 ul_loginf("We are BLUE!\n");
155 robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
157 robot.ignore_hokuyo = false;
158 robot.map = ShmapInit(1);
159 fill_in_known_areas_in_map();
161 signal(SIGINT, int_handler);
162 signal(SIGTERM, int_handler);
166 robot.orte.motion_speed.left = 0;
167 robot.orte.motion_speed.right = 0;
169 robot.orte.pwr_ctrl.voltage33 = 1;
170 robot.orte.pwr_ctrl.voltage50 = 1;
171 robot.orte.pwr_ctrl.voltage80 = 1;
173 robot.orte.camera_control.on = true;
175 robot.fsm.motion.state = &fsm_state_motion_init;
177 /* Only activate display if it is configured */
179 robot.sercom = uoled_init(serial_comm);
180 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
181 robot.fsm.display.state = &fsm_state_disp_init;
184 robot.obstacle_avoidance_enabled = true;
185 robot.use_back_switch = true;
186 robot.use_left_switch = true;
187 robot.use_right_switch = true;
188 robot.start_state = POWER_ON;
189 robot.check_turn_safety = true;
191 /* init ORTE domain, create publishers, subscribers, .. */
192 rv = robot_init_orte();
193 act_init(&robot.orte);
199 * Starts the robot FSMs and threads.
207 pthread_attr_t tattr;
208 struct sched_param param;
209 pthread_t thr_obstacle_forgeting;
212 ret = motion_control_init();
214 perror("motion_control_init");
219 /* Obstacle forgeting thread */
220 pthread_attr_init (&tattr);
221 pthread_attr_getschedparam (&tattr, ¶m);
222 pthread_attr_getschedparam (&tattr, ¶m);
223 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
224 param.sched_priority = OBST_FORGETING_PRIO;
225 rv = pthread_attr_setschedparam (&tattr, ¶m);
227 perror("robot_start: pthread_attr_setschedparam()");
230 rv = pthread_create(&thr_obstacle_forgeting,
231 &tattr, thread_obstacle_forgeting, NULL);
233 perror("robot_start: pthread_create");
237 sem_init(&robot.start, 0, 0);
239 fsm_main_loop(&robot.main_loop);
246 * Signals all the robot threads to finish.
251 fsm_exit(&robot.fsm.main);
252 fsm_exit(&robot.fsm.motion);
253 fsm_exit(&robot.fsm.act);
257 * Stops the robot. All resources alocated by robot_init() or
258 * robot_start() are dealocated here.
262 motion_control_done();
264 // FIXME: set actuators to well defined position (FJ)
266 robottype_roboorte_destroy(&robot.orte);
268 fsm_destroy(&robot.fsm.main);
269 fsm_destroy(&robot.fsm.motion);
270 fsm_destroy(&robot.fsm.act);
272 ul_logdeb("robofsm: stop.\n");
275 void robot_get_est_pos_trans(double *x, double *y, double *phi)
277 robot_get_est_pos(x, y, phi);
280 *phi = __trans_ang(*phi);
283 void robot_get_est_pos(double *x, double *y, double *phi)
285 if (robot.indep_odometry_works) {
286 ROBOT_LOCK(est_pos_indep_odo);
287 *x = robot.est_pos_indep_odo.x;
288 *y = robot.est_pos_indep_odo.y;
289 *phi = robot.est_pos_indep_odo.phi;
290 ROBOT_UNLOCK(est_pos_indep_odo);
291 } else if (robot.odometry_works) {
292 ROBOT_LOCK(est_pos_odo);
293 *x = robot.est_pos_odo.x;
294 *y = robot.est_pos_odo.y;
295 *phi = robot.est_pos_odo.phi;
296 ROBOT_UNLOCK(est_pos_odo);
299 *x = robot.ref_pos.x;
300 *y = robot.ref_pos.y;
301 *phi = robot.ref_pos.phi;
302 ROBOT_UNLOCK(ref_pos);