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 char *fsm_name[] = {
27 [FSM_ID_MAIN] = "main",
28 [FSM_ID_MOTION] = "motion",
29 [FSM_ID_DISP] = "display",
32 struct robot_eb2008 robot;
34 #ifdef CONFIG_LOCK_CHECKING
35 struct lock_log robot_lock_log;
38 static void block_signals()
45 sigaddset(&sigset, SIGRTMIN+i);
47 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
50 static void int_handler(int sig)
55 void fill_in_known_areas_in_map()
57 /* Do not plan path close to edges */
58 /* ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); */
59 /* ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
60 /* ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
61 /* ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
64 ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
65 ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
67 ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
68 ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
70 /* Ignore other obstacles at edges */
71 ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
72 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
73 ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
74 ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
76 ShmapSetRectangleFlag(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
77 ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
81 * Initializes the robot.
82 * Setup fields in robot structure, initializes FSMs and ORTE.
91 /* robot and competition configuration */
92 robot.mode = ROBO_TESTING;
93 robot.team_color = BLUE;
94 robot.beacon_color = BEACON_BLUE;
96 pthread_mutex_init(&robot.lock, NULL);
97 pthread_mutex_init(&robot.lock_ref_pos, NULL);
98 pthread_mutex_init(&robot.lock_est_pos, NULL);
99 pthread_mutex_init(&robot.lock_meas_angles, NULL);
100 pthread_mutex_init(&robot.lock_joy_data, NULL);
101 pthread_mutex_init(&robot.lock_disp, NULL);
103 fsm_main_loop_init(&robot.main_loop);
105 /* FSM initialization */
106 for (i=0; i<FSM_CNT; i++)
107 fsm_init(&robot.fsm[i], fsm_name[i], &robot.main_loop);
109 /* FIXME: there should be team color initilization */
111 robot.team_color = RED;
114 robot.team_color = BLUE;
117 if (robot.team_color == RED) {
118 printf("We are RED!\n");
120 printf("We are BLUE!\n");
123 #ifdef CONFIG_OPEN_LOOP
124 printf("OPEN_LOOP enabled\n");
127 robot.map = ShmapInit(1);
128 fill_in_known_areas_in_map();
130 signal(SIGINT, int_handler);
131 signal(SIGTERM, int_handler);
135 robot.orte.motion_speed.left = 0;
136 robot.orte.motion_speed.right = 0;
138 robot.orte.pwr_ctrl.voltage33 = 1;
139 robot.orte.pwr_ctrl.voltage50 = 1;
140 robot.orte.pwr_ctrl.voltage80 = 1;
147 /* MCL initialization */
148 robot.laser.width = PLAYGROUND_WIDTH_M; /* in meters */
149 robot.laser.height = PLAYGROUND_WIDTH_M; /* in meters */
151 robot.laser.pred_dnoise = 0.001;
152 robot.laser.pred_anoise = DEG2RAD(2);
153 robot.laser.aeval_sigma = DEG2RAD(4);
154 robot.mcl = mcl_laser_init(&robot.laser, 1000);
156 robot.fsm[FSM_ID_MOTION].state = &fsm_state_motion_init;
158 /* Only activate display if it is configured */
159 robot.sercom = uoled_init(serial_comm);
160 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
161 robot.fsm[FSM_ID_DISP].state = &fsm_state_disp_init;
163 robot.obstacle_avoidance_enabled = true;
164 robot.laser_enabled = true;
165 robot.state = JUST_STARTED;
167 robot.carousel_cnt = 0;
168 robot.carousel_pos = 0;
170 /* init ORTE domain, create publishers, subscribers, .. */
171 rv = robot_init_orte();
177 * Starts the robot threads.
185 pthread_attr_t tattr;
186 struct sched_param param;
187 pthread_t thr_obstacle_forgeting;
189 /* Obstacle forgeting thread */
190 pthread_attr_init (&tattr);
191 pthread_attr_getschedparam (&tattr, ¶m);
192 param.sched_priority = OBST_FORGETING_PRIO;
193 rv = pthread_attr_setschedparam (&tattr, ¶m);
195 perror("robot_start: pthread_attr_setschedparam()");
198 rv = pthread_create(&thr_obstacle_forgeting,
199 &tattr, thread_obstacle_forgeting, NULL);
201 perror("robot_start: pthread_create");
205 fsm_main_loop(&robot.main_loop);
212 * Signals all the robot threads to finish.
218 /* stop FSM threads */
219 for(i=0; i<FSM_CNT; i++) {
220 fsm_exit(&robot.fsm[i]);
227 * Stops the robot. All resources alocated by robot_init() or
228 * robot_start() are dealocated here.
234 robot.orte.laser_cmd.speed = LASER_DRIVE_OFF;
235 ORTEPublicationSend(robot.orte.publication_laser_cmd);
236 ORTEPublicationSend(robot.orte.publication_laser_cmd);
240 robot.orte.motion_speed.right = 0;
241 robot.orte.motion_speed.left = 0;
242 ORTEPublicationSend(robot.orte.publication_motion_speed);
244 robottype_roboorte_destroy(&robot.orte);
246 for (i=0; i<FSM_CNT; i++)
247 fsm_destroy(&robot.fsm[i]);
248 DBG("robofsm: stop.\n");