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
12 #include <movehelper_eb2008.h>
15 #include <robot_eb2008.h>
16 #include <robot_orte.h>
17 #include <servos_eb2008.h>
22 #include "map_handling.h"
26 static int thread_priorities[] = {
27 [FSM_ID_MAIN] = THREAD_PRIO_MAIN,
28 [FSM_ID_MOTION] = THREAD_PRIO_MOTION,
29 [FSM_ID_LOC] = THREAD_PRIO_LOC,
30 [FSM_ID_JOY] = THREAD_PRIO_JOY,
31 [FSM_ID_DISP] = THREAD_PRIO_DISP,
34 static char *fsm_name[] = {
35 [FSM_ID_MAIN] = "main",
36 [FSM_ID_MOTION] = "motion",
37 [FSM_ID_LOC] = "localization",
38 [FSM_ID_JOY] = "joystick",
39 [FSM_ID_DISP] = "display",
42 struct robot_eb2008 robot;
44 #ifdef CONFIG_LOCK_CHECKING
45 struct lock_log robot_lock_log;
49 static void block_signals()
56 sigaddset(&sigset, SIGRTMIN+i);
58 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
61 static void int_handler(int sig)
66 void fill_in_known_areas_in_map()
68 /* Do not plan path close to edges */
69 /* ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); */
70 /* ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
71 /* ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
72 /* ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
75 ShmapSetRectangleFlag(0, 1.25, 0.2, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
76 ShmapSetRectangleFlag(2.8, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
78 ShmapSetRectangleFlag(0.65, 1.9, 0.85, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
79 ShmapSetRectangleFlag(2.35, 1.9, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
81 /* Ignore other obstacles at edges */
82 ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
83 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
84 ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
85 ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
89 * Initializes the robot.
90 * Setup fields in robot structure, initializes FSMs and ORTE.
98 /* robot and competition configuration */
99 robot.mode = ROBO_TESTING;
100 robot.team_color = BLUE;
101 robot.beacon_color = BEACON_BLUE;
103 pthread_mutex_init(&robot.lock, NULL);
104 pthread_mutex_init(&robot.lock_ref_pos, NULL);
105 pthread_mutex_init(&robot.lock_est_pos, NULL);
106 pthread_mutex_init(&robot.lock_meas_angles, NULL);
107 pthread_mutex_init(&robot.lock_joy_data, NULL);
108 pthread_mutex_init(&robot.lock_disp, NULL);
110 /* FSM initialization */
111 for (i=0; i<FSM_CNT; i++)
112 fsm_init(&robot.fsm[i], fsm_name[i]);
114 /* FIXME: there should be team color initilization */
116 robot.team_color = RED;
119 robot.team_color = BLUE;
122 if (robot.team_color == RED) {
123 printf("We are RED!\n");
125 printf("We are BLUE!\n");
127 #ifdef CONFIG_OPEN_LOOP
128 printf("OPEN_LOOP enabled\n");
131 robot.map = ShmapInit(1);
132 fill_in_known_areas_in_map();
134 signal(SIGINT, int_handler);
138 robot.gorte.motion_speed.left = 0;
139 robot.gorte.motion_speed.right = 0;
141 robot.gorte.pwr_ctrl.voltage33 = 1;
142 robot.gorte.pwr_ctrl.voltage50 = 1;
143 robot.gorte.pwr_ctrl.voltage80 = 1;
145 robot.orte.servos.brush_left = BRUSH_LEFT_CLOSE;
146 robot.orte.servos.brush_right = BRUSH_RIGHT_CLOSE;
147 robot.orte.servos.door_bottom = BOTTOM_DOOR_CLOSE;
148 robot.orte.servos.door_top = TOP_DOOR_CLOSE;
149 robot.orte.servos.door_back = BACK_DOOR_CLOSE;
151 robot.orte.drives.brush_left = LEFT_BRUSH_DRIVE_OFF;
152 robot.orte.drives.brush_right = RIGHT_BRUSH_DRIVE_OFF;
153 robot.orte.drives.bagr = BAGR_DRIVE_OFF;
155 /* MCL initialization */
156 robot.laser.width = PLAYGROUND_WIDTH_M; /* in meters */
157 robot.laser.height = PLAYGROUND_WIDTH_M; /* in meters */
159 robot.laser.pred_dnoise = 0.001;
160 robot.laser.pred_anoise = DEG2RAD(2);
161 robot.laser.aeval_sigma = DEG2RAD(4);
162 robot.mcl = mcl_laser_init(&robot.laser, 1000);
164 robot.fsm[FSM_ID_MOTION].state = &fsm_state_motion_init;
165 robot.fsm[FSM_ID_LOC].state = &fsm_state_loc_init;
167 /* Only activate display if it is configured */
168 robot.sercom = uoled_init(serial_comm);
169 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
170 robot.fsm[FSM_ID_DISP].state = &fsm_state_disp_init;
172 robot.obstacle_avoidance_enabled = false;
173 robot.laser_enabled = true;
175 /* init ORTE domain, create publishers, subscribers, .. */
182 * Starts the robot threads.
183 * @todo Don't exit() in case of error.
190 /* start FSM threads */
191 for(i=0; i<FSM_CNT; i++) {
193 struct sched_param param;
195 pthread_attr_init(&attr);
197 param.sched_priority = thread_priorities[i];
198 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
199 if (pthread_attr_setschedparam(&attr, ¶m)) {
200 perror("pthread_attr_setschedparam");
205 if (fsm_start(&robot.fsm[i], &attr) != 0) {
211 pthread_attr_t tattr;
212 struct sched_param param;
213 pthread_t thr_obstacle_forgeting;
216 // Trajectory follower thread
217 pthread_attr_init (&tattr);
218 pthread_attr_getschedparam (&tattr, ¶m);
219 param.sched_priority = OBST_FORGETING_PRIO;
220 ret = pthread_attr_setschedparam (&tattr, ¶m);
222 perror("robot_start: pthread_attr_setschedparam()");
225 ret = pthread_create(&thr_obstacle_forgeting, &tattr, thread_obstacle_forgeting, NULL);
227 perror("robot_start: pthread_create");
235 * Signals all the robot threads to finish.
242 /* stop FSM threads */
243 for(i=0; i<FSM_CNT; i++) {
244 fsm_event e = { EV_EXIT, NULL };
245 __fsm_signal(&robot.fsm[i], e, true);
253 robot.gorte.motion_speed.right = 0;
254 robot.gorte.motion_speed.left = 0;
255 ORTEPublicationSend(robot.gorte.publication_motion_speed);
257 eb2008_roboorte_destroy(&robot.orte);
258 generic_roboorte_destroy(&robot.gorte);
264 * Wait for finishing the robot threads after calling robot_exit(). *
272 /* wait for threads */
273 for (i=0; i<FSM_CNT; i++) {
274 pthread_join(robot.fsm[i].threadid, NULL);
281 * Stops the robot. All resources alocated by robot_init() or
282 * robot_start() are dealocated here.
289 for (i=0; i<FSM_CNT; i++)
290 fsm_destroy(&robot.fsm[i]);
291 DBG("robofsm: stop.\n");