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 #define DBG(format, ...) printf(format, ##__VA_ARGS__)
32 #define DBG(format, ...)
35 #define MOTION_CONTROL_INIT_ONLY
36 #include "motion-control.h"
38 /* Global definition of robot structure */
41 #ifdef CONFIG_LOCK_CHECKING
42 struct lock_log robot_lock_log;
45 static void block_signals()
51 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
52 sigaddset(&sigset, i);
54 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
57 static void int_handler(int sig)
62 void fill_in_known_areas_in_map()
64 /* Do not plan path close to edges */
65 //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
66 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
67 ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
68 //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
70 /* Container surroundings */
71 ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
72 ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
74 /* Ignore other obstacles at edges */
75 //ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
76 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
77 //ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
78 //ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
80 ShmapSetRectangleFlag(0.6, 1.45, 2.45, 2.1, 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
91 static void trans_callback(struct robo_fsm *fsm)
93 if (fsm == &robot.fsm.main) {
94 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
95 ORTEPublicationSend(robot.orte.publication_fsm_main);
96 } else if (fsm == &robot.fsm.motion) {
97 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
98 ORTEPublicationSend(robot.orte.publication_fsm_motion);
99 } else if (fsm == &robot.fsm.act) {
100 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
101 ORTEPublicationSend(robot.orte.publication_fsm_act);
107 * Initializes the robot.
108 * Setup fields in robot structure, initializes FSMs and ORTE.
115 pthread_mutexattr_t mattr;
117 rv = pthread_mutexattr_init(&mattr);
118 #ifdef HAVE_PRIO_INHERIT
119 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
121 pthread_mutex_init(&robot.lock, &mattr);
122 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
123 pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
124 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
125 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
126 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
127 pthread_mutex_init(&robot.lock_joy_data, &mattr);
128 pthread_mutex_init(&robot.lock_disp, &mattr);
130 fsm_main_loop_init(&robot.main_loop);
132 /* FSM initialization */
133 /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
134 fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
135 fsm_init(&robot.fsm.display, "display", &robot.main_loop);
136 fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
137 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
138 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
139 fsm_init(&robot.fsm.display, "\tdisp", &robot.main_loop);
140 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
141 robot.fsm.main.transition_callback = trans_callback;
142 robot.fsm.act.transition_callback = trans_callback;
143 robot.fsm.motion.transition_callback = trans_callback;
145 robot.team_color = BLUE;
147 if (robot.team_color == YELLOW) {
148 printf("We are YELLOW!\n");
150 printf("We are BLUE!\n");
153 robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
155 robot.ignore_hokuyo = false;
156 robot.map = ShmapInit(1);
157 fill_in_known_areas_in_map();
159 signal(SIGINT, int_handler);
160 signal(SIGTERM, int_handler);
164 robot.orte.motion_speed.left = 0;
165 robot.orte.motion_speed.right = 0;
167 robot.orte.pwr_ctrl.voltage33 = 1;
168 robot.orte.pwr_ctrl.voltage50 = 1;
169 robot.orte.pwr_ctrl.voltage80 = 1;
171 robot.orte.camera_control.on = true;
173 robot.fsm.motion.state = &fsm_state_motion_init;
175 robot.fsm.act.state = &fsm_state_act_wait_for_command; // actuator FSM's initial state
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 = false; /* Switched on sime time after start */
186 robot.state = POWER_ON;
188 /* init ORTE domain, create publishers, subscribers, .. */
189 rv = robot_init_orte();
190 act_init(&robot.orte);
196 * Starts the robot FSMs and threads.
204 pthread_attr_t tattr;
205 struct sched_param param;
206 pthread_t thr_obstacle_forgeting;
209 ret = motion_control_init();
211 perror("motion_control_init");
216 /* Obstacle forgeting thread */
217 pthread_attr_init (&tattr);
218 pthread_attr_getschedparam (&tattr, ¶m);
219 pthread_attr_getschedparam (&tattr, ¶m);
220 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
221 param.sched_priority = OBST_FORGETING_PRIO;
222 rv = pthread_attr_setschedparam (&tattr, ¶m);
224 perror("robot_start: pthread_attr_setschedparam()");
227 rv = pthread_create(&thr_obstacle_forgeting,
228 &tattr, thread_obstacle_forgeting, NULL);
230 perror("robot_start: pthread_create");
234 fsm_main_loop(&robot.main_loop);
241 * Signals all the robot threads to finish.
246 fsm_exit(&robot.fsm.main);
247 fsm_exit(&robot.fsm.motion);
248 fsm_exit(&robot.fsm.display);
249 fsm_exit(&robot.fsm.act);
253 * Stops the robot. All resources alocated by robot_init() or
254 * robot_start() are dealocated here.
258 motion_control_done();
260 // FIXME: set actuators to well defined position (FJ)
262 robottype_roboorte_destroy(&robot.orte);
264 fsm_destroy(&robot.fsm.main);
265 fsm_destroy(&robot.fsm.motion);
266 fsm_destroy(&robot.fsm.display);
267 fsm_destroy(&robot.fsm.act);
269 DBG("robofsm: stop.\n");
272 void robot_get_est_pos(double *x, double *y, double *phi)
274 if (robot.indep_odometry_works) {
275 ROBOT_LOCK(est_pos_indep_odo);
276 *x = robot.est_pos_indep_odo.x;
277 *y = robot.est_pos_indep_odo.y;
278 *phi = robot.est_pos_indep_odo.phi;
279 ROBOT_UNLOCK(est_pos_indep_odo);
280 } else if (robot.odometry_works) {
281 ROBOT_LOCK(est_pos_odo);
282 *x = robot.est_pos_odo.x;
283 *y = robot.est_pos_odo.y;
284 *phi = robot.est_pos_odo.phi;
285 ROBOT_UNLOCK(est_pos_odo);
288 *x = robot.ref_pos.x;
289 *y = robot.ref_pos.y;
290 *phi = robot.ref_pos.phi;
291 ROBOT_UNLOCK(ref_pos);