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>
21 #include "map_handling.h"
24 #include "actuators.h"
27 #define DBG(format, ...) printf(format, ##__VA_ARGS__)
29 #define DBG(format, ...)
32 #define MOTION_CONTROL_INIT_ONLY
33 #include "motion-control.h"
35 /* Global definition of robot structure */
38 #ifdef CONFIG_LOCK_CHECKING
39 struct lock_log robot_lock_log;
42 static void block_signals()
48 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
49 sigaddset(&sigset, i);
51 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
54 static void int_handler(int sig)
59 void fill_in_known_areas_in_map()
61 /* Do not plan path close to edges */
62 /* ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); */
63 /* ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
64 /* ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
65 /* ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
67 //ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
68 //ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
70 //ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
71 //ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
73 /* Ignore other obstacles at edges */
74 ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
75 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
76 ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
77 ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
79 //ShmapSetRectangleFlag(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
80 //ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
82 /* Construction areas (Building zones) */
83 double x_center = (PLAYGROUND_WIDTH_M/2.0);
84 double y_center = (PLAYGROUND_HEIGHT_M/2.0);
85 //ShmapSetRectangleFlag(x_center - ACROPOLIS_RADIUS, y_center - ACROPOLIS_RADIUS, x_center + ACROPOLIS_RADIUS, y_center + ACROPOLIS_RADIUS, MAP_FLAG_WALL, 0); /* Central construction area */
86 //ShmapSetRectangleFlag(1.35, 0.9, 1.65, 1.2, MAP_FLAG_WALL, 0); /* Central construction area */
89 ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); /* left */
90 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
91 ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
92 ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
93 // Bottom-middle unloading area
94 ShmapSetRectangleFlag(1.0, 0.0, 2.0, 0.3, MAP_FLAG_WALL, 0); /* bottom */
95 // Unloading area - dwarf wall
96 ShmapSetRectangleFlag(0.4, 0.0, 0.8, 0.3, MAP_FLAG_WALL, 0);
97 ShmapSetRectangleFlag(2.2, 0.0, 2.6, 0.3, MAP_FLAG_WALL, 0);
99 ShmapSetCircleFlag(x_center, y_center, ACROPOLIS_RADIUS + 0.04, MAP_FLAG_WALL, 0);
103 static void trans_callback(struct robo_fsm *fsm)
105 if (fsm == &robot.fsm.main) {
106 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
107 ORTEPublicationSend(robot.orte.publication_fsm_main);
108 } else if (fsm == &robot.fsm.motion) {
109 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
110 ORTEPublicationSend(robot.orte.publication_fsm_motion);
111 } else if (fsm == &robot.fsm.act) {
112 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
113 ORTEPublicationSend(robot.orte.publication_fsm_act);
119 * Initializes the robot.
120 * Setup fields in robot structure, initializes FSMs and ORTE.
128 pthread_mutex_init(&robot.lock, NULL);
129 pthread_mutex_init(&robot.lock_ref_pos, NULL);
130 pthread_mutex_init(&robot.lock_est_pos, NULL);
131 pthread_mutex_init(&robot.lock_meas_angles, NULL);
132 pthread_mutex_init(&robot.lock_joy_data, NULL);
133 pthread_mutex_init(&robot.lock_disp, NULL);
135 fsm_main_loop_init(&robot.main_loop);
137 /* FSM initialization */
138 /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
139 fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
140 fsm_init(&robot.fsm.display, "display", &robot.main_loop);
141 fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
142 fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
143 fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
144 fsm_init(&robot.fsm.display, "\tdisp", &robot.main_loop);
145 fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
146 robot.fsm.main.transition_callback = trans_callback;
147 robot.fsm.act.transition_callback = trans_callback;
148 robot.fsm.motion.transition_callback = trans_callback;
153 robot.ref_pos.phi = 0;
154 ROBOT_UNLOCK(ref_pos);
159 robot.est_pos.phi = 0;
160 ROBOT_UNLOCK(est_pos);
162 robot.team_color = GREEN;
164 if (robot.team_color == RED) {
165 printf("We are RED!\n");
167 printf("We are GREEN!\n");
170 robot.map = ShmapInit(1);
171 fill_in_known_areas_in_map();
173 signal(SIGINT, int_handler);
174 signal(SIGTERM, int_handler);
178 robot.orte.motion_speed.left = 0;
179 robot.orte.motion_speed.right = 0;
181 robot.orte.pwr_ctrl.voltage33 = 1;
182 robot.orte.pwr_ctrl.voltage50 = 1;
183 robot.orte.pwr_ctrl.voltage80 = 1;
185 robot.orte.camera_control.on = true;
187 robot.fsm.motion.state = &fsm_state_motion_init;
189 robot.fsm.act.state = &fsm_state_act_wait_for_command; // puck handling actuators FSM's initial state
191 /* Only activate display if it is configured */
192 robot.sercom = uoled_init(serial_comm);
193 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
194 robot.fsm.display.state = &fsm_state_disp_init;
196 robot.obstacle_avoidance_enabled = true;
197 robot.use_back_switch = false; /* Switched on sime time after start */
198 robot.state = POWER_ON;
200 /* init ORTE domain, create publishers, subscribers, .. */
201 rv = robot_init_orte();
202 act_init(&robot.orte);
208 * Starts the robot FSMs and threads.
216 pthread_attr_t tattr;
217 struct sched_param param;
218 pthread_t thr_obstacle_forgeting;
221 ret = motion_control_init();
223 perror("motion_control_init");
228 /* Obstacle forgeting thread */
229 pthread_attr_init (&tattr);
230 pthread_attr_getschedparam (&tattr, ¶m);
231 param.sched_priority = OBST_FORGETING_PRIO;
232 rv = pthread_attr_setschedparam (&tattr, ¶m);
234 perror("robot_start: pthread_attr_setschedparam()");
237 rv = pthread_create(&thr_obstacle_forgeting,
238 &tattr, thread_obstacle_forgeting, NULL);
240 perror("robot_start: pthread_create");
244 fsm_main_loop(&robot.main_loop);
251 * Signals all the robot threads to finish.
256 fsm_exit(&robot.fsm.main);
257 fsm_exit(&robot.fsm.motion);
258 fsm_exit(&robot.fsm.display);
259 fsm_exit(&robot.fsm.act);
263 * Stops the robot. All resources alocated by robot_init() or
264 * robot_start() are dealocated here.
268 motion_control_done();
270 act_chelae(CHELA_OPEN, CHELA_OPEN);
271 act_holder(HOLDER_OPENED);
273 robottype_roboorte_destroy(&robot.orte);
275 fsm_destroy(&robot.fsm.main);
276 fsm_destroy(&robot.fsm.motion);
277 fsm_destroy(&robot.fsm.display);
278 fsm_destroy(&robot.fsm.act);
279 DBG("robofsm: stop.\n");