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"
26 #include "actuators.h"
29 #define DBG(format, ...) printf(format, ##__VA_ARGS__)
31 #define DBG(format, ...)
34 #define MOTION_CONTROL_INIT_ONLY
35 #include "motion-control.h"
37 /* Global definition of robot structure */
40 #ifdef CONFIG_LOCK_CHECKING
41 struct lock_log robot_lock_log;
44 static void block_signals()
50 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
51 sigaddset(&sigset, i);
53 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
56 static void int_handler(int sig)
61 void fill_in_known_areas_in_map()
63 /* Do not plan path close to edges */
64 //ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); /* left */
65 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
66 //ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
67 //ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
69 /* Ignore other obstacles at edges */
70 //ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
71 //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
72 //ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
73 //ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
75 ShmapSetRectangleFlag(0.74, 1.6, 2.259, 2.1, MAP_FLAG_WALL, 0); /* plateau, slopes */
79 static void trans_callback(struct robo_fsm *fsm)
81 if (fsm == &robot.fsm.main) {
82 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
83 ORTEPublicationSend(robot.orte.publication_fsm_main);
84 } else if (fsm == &robot.fsm.motion) {
85 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
86 ORTEPublicationSend(robot.orte.publication_fsm_motion);
87 } else if (fsm == &robot.fsm.act) {
88 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
89 ORTEPublicationSend(robot.orte.publication_fsm_act);
95 * Initializes the robot.
96 * Setup fields in robot structure, initializes FSMs and ORTE.
103 pthread_mutexattr_t mattr;
105 rv = pthread_mutexattr_init(&mattr);
106 #ifdef HAVE_PRIO_INHERIT
107 rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
109 pthread_mutex_init(&robot.lock, &mattr);
110 pthread_mutex_init(&robot.lock_ref_pos, &mattr);
111 pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
112 pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
113 pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
114 pthread_mutex_init(&robot.lock_meas_angles, &mattr);
115 pthread_mutex_init(&robot.lock_joy_data, &mattr);
116 pthread_mutex_init(&robot.lock_disp, &mattr);
118 fsm_main_loop_init(&robot.main_loop);
120 /* FSM initialization */
121 /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
122 fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
123 fsm_init(&robot.fsm.display, "display", &robot.main_loop);
124 fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
125 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
126 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
127 fsm_init(&robot.fsm.display, "\tdisp", &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 = BLUE;
135 if (robot.team_color == YELLOW) {
136 printf("We are YELLOW!\n");
138 printf("We are BLUE!\n");
141 robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
143 robot.map = ShmapInit(1);
144 fill_in_known_areas_in_map();
146 signal(SIGINT, int_handler);
147 signal(SIGTERM, int_handler);
151 robot.orte.motion_speed.left = 0;
152 robot.orte.motion_speed.right = 0;
154 robot.orte.pwr_ctrl.voltage33 = 1;
155 robot.orte.pwr_ctrl.voltage50 = 1;
156 robot.orte.pwr_ctrl.voltage80 = 1;
158 robot.orte.camera_control.on = true;
160 robot.fsm.motion.state = &fsm_state_motion_init;
162 robot.fsm.act.state = &fsm_state_act_wait_for_command; // actuator FSM's initial state
164 /* Only activate display if it is configured */
166 robot.sercom = uoled_init(serial_comm);
167 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
168 robot.fsm.display.state = &fsm_state_disp_init;
171 robot.obstacle_avoidance_enabled = true;
172 robot.use_back_switch = false; /* Switched on sime time after start */
173 robot.state = POWER_ON;
175 /* init ORTE domain, create publishers, subscribers, .. */
176 rv = robot_init_orte();
177 act_init(&robot.orte);
183 * Starts the robot FSMs and threads.
191 pthread_attr_t tattr;
192 struct sched_param param;
193 pthread_t thr_obstacle_forgeting;
196 ret = motion_control_init();
198 perror("motion_control_init");
203 /* Obstacle forgeting thread */
204 pthread_attr_init (&tattr);
205 pthread_attr_getschedparam (&tattr, ¶m);
206 pthread_attr_getschedparam (&tattr, ¶m);
207 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
208 param.sched_priority = OBST_FORGETING_PRIO;
209 rv = pthread_attr_setschedparam (&tattr, ¶m);
211 perror("robot_start: pthread_attr_setschedparam()");
214 rv = pthread_create(&thr_obstacle_forgeting,
215 &tattr, thread_obstacle_forgeting, NULL);
217 perror("robot_start: pthread_create");
221 fsm_main_loop(&robot.main_loop);
228 * Signals all the robot threads to finish.
233 fsm_exit(&robot.fsm.main);
234 fsm_exit(&robot.fsm.motion);
235 fsm_exit(&robot.fsm.display);
236 fsm_exit(&robot.fsm.act);
240 * Stops the robot. All resources alocated by robot_init() or
241 * robot_start() are dealocated here.
245 motion_control_done();
247 // FIXME: set actuators to well defined position (FJ)
249 robottype_roboorte_destroy(&robot.orte);
251 fsm_destroy(&robot.fsm.main);
252 fsm_destroy(&robot.fsm.motion);
253 fsm_destroy(&robot.fsm.display);
254 fsm_destroy(&robot.fsm.act);
256 DBG("robofsm: stop.\n");
259 void robot_get_est_pos(double *x, double *y, double *phi)
261 if (robot.indep_odometry_works) {
262 ROBOT_LOCK(est_pos_indep_odo);
263 *x = robot.est_pos_indep_odo.x;
264 *y = robot.est_pos_indep_odo.y;
265 *phi = robot.est_pos_indep_odo.phi;
266 ROBOT_UNLOCK(est_pos_indep_odo);
267 } else if (robot.odometry_works) {
268 ROBOT_LOCK(est_pos_odo);
269 *x = robot.est_pos_odo.x;
270 *y = robot.est_pos_odo.y;
271 *phi = robot.est_pos_odo.phi;
272 ROBOT_UNLOCK(est_pos_odo);
275 *x = robot.ref_pos.x;
276 *y = robot.ref_pos.y;
277 *phi = robot.ref_pos.phi;
278 ROBOT_UNLOCK(ref_pos);