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.h>
16 #include <robot_orte.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;
48 static void block_signals()
55 sigaddset(&sigset, SIGRTMIN+i);
57 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
60 static void int_handler(int sig)
65 void fill_in_known_areas_in_map()
67 /* Do not plan path close to edges */
68 /* ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); */
69 /* ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
70 /* ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
71 /* ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
74 ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
75 ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
77 ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
78 ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
80 /* Ignore other obstacles at edges */
81 ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
82 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
83 ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
84 ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
86 ShmapSetRectangleFlag(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
87 ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
91 * Initializes the robot.
92 * Setup fields in robot structure, initializes FSMs and ORTE.
101 /* robot and competition configuration */
102 robot.mode = ROBO_TESTING;
103 robot.team_color = BLUE;
104 robot.beacon_color = BEACON_BLUE;
106 pthread_mutex_init(&robot.lock, NULL);
107 pthread_mutex_init(&robot.lock_ref_pos, NULL);
108 pthread_mutex_init(&robot.lock_est_pos, NULL);
109 pthread_mutex_init(&robot.lock_meas_angles, NULL);
110 pthread_mutex_init(&robot.lock_joy_data, NULL);
111 pthread_mutex_init(&robot.lock_disp, NULL);
113 /* FSM initialization */
114 for (i=0; i<FSM_CNT; i++)
115 fsm_init(&robot.fsm[i], fsm_name[i]);
117 /* FIXME: there should be team color initilization */
119 robot.team_color = RED;
122 robot.team_color = BLUE;
125 if (robot.team_color == RED) {
126 printf("We are RED!\n");
128 printf("We are BLUE!\n");
131 #ifdef CONFIG_OPEN_LOOP
132 printf("OPEN_LOOP enabled\n");
135 robot.map = ShmapInit(1);
136 fill_in_known_areas_in_map();
138 signal(SIGINT, int_handler);
142 robot.orte.motion_speed.left = 0;
143 robot.orte.motion_speed.right = 0;
145 robot.orte.pwr_ctrl.voltage33 = 1;
146 robot.orte.pwr_ctrl.voltage50 = 1;
147 robot.orte.pwr_ctrl.voltage80 = 1;
158 /* MCL initialization */
159 robot.laser.width = PLAYGROUND_WIDTH_M; /* in meters */
160 robot.laser.height = PLAYGROUND_WIDTH_M; /* in meters */
162 robot.laser.pred_dnoise = 0.001;
163 robot.laser.pred_anoise = DEG2RAD(2);
164 robot.laser.aeval_sigma = DEG2RAD(4);
165 robot.mcl = mcl_laser_init(&robot.laser, 1000);
167 robot.fsm[FSM_ID_MOTION].state = &fsm_state_motion_init;
168 robot.fsm[FSM_ID_LOC].state = &fsm_state_loc_init;
170 /* Only activate display if it is configured */
171 robot.sercom = uoled_init(serial_comm);
172 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
173 robot.fsm[FSM_ID_DISP].state = &fsm_state_disp_init;
175 robot.obstacle_avoidance_enabled = true;
176 robot.laser_enabled = true;
177 robot.state = JUST_STARTED;
179 robot.carousel_cnt = 0;
180 robot.carousel_pos = 0;
182 /* init ORTE domain, create publishers, subscribers, .. */
183 rv = robot_init_orte();
189 * Starts the robot threads.
198 /* start FSM threads */
199 for(i=0; i<FSM_CNT; i++) {
201 struct sched_param param;
203 pthread_attr_init(&attr);
205 param.sched_priority = thread_priorities[i];
206 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
207 rv = pthread_attr_setschedparam(&attr, ¶m);
209 perror("pthread_attr_setschedparam");
213 rv = fsm_start(&robot.fsm[i], &attr);
220 pthread_attr_t tattr;
221 struct sched_param param;
222 pthread_t thr_obstacle_forgeting;
224 /* trajectory follower thread */
225 pthread_attr_init (&tattr);
226 pthread_attr_getschedparam (&tattr, ¶m);
227 param.sched_priority = OBST_FORGETING_PRIO;
228 rv = pthread_attr_setschedparam (&tattr, ¶m);
230 perror("robot_start: pthread_attr_setschedparam()");
234 rv = pthread_create(&thr_obstacle_forgeting,
235 &tattr, thread_obstacle_forgeting, NULL);
237 perror("robot_start: pthread_create");
246 * Destroy ORTE stuffs.
250 int robot_orte_destroy()
252 robot.orte.laser_cmd.speed = LASER_DRIVE_OFF;
253 ORTEPublicationSend(robot.orte.publication_laser_cmd);
254 ORTEPublicationSend(robot.orte.publication_laser_cmd);
261 robot.orte.motion_speed.right = 0;
262 robot.orte.motion_speed.left = 0;
263 ORTEPublicationSend(robot.orte.publication_motion_speed);
265 robottype_roboorte_destroy(&robot.orte);
271 * Signals all the robot threads to finish.
278 /* stop FSM threads */
279 for(i=0; i<FSM_CNT; i++) {
280 fsm_event e = { EV_EXIT, NULL };
281 __fsm_signal(&robot.fsm[i], e, true);
284 robot_orte_destroy();
290 * Wait for finishing the robot threads after calling robot_exit(). *
298 /* wait for threads */
299 for (i=0; i<FSM_CNT; i++) {
300 pthread_join(robot.fsm[i].threadid, NULL);
307 * Stops the robot. All resources alocated by robot_init() or
308 * robot_start() are dealocated here.
315 for (i=0; i<FSM_CNT; i++)
316 fsm_destroy(&robot.fsm[i]);
317 DBG("robofsm: stop.\n");