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"
25 #define MOTION_CONTROL_INIT_ONLY
26 #include "motion-control.h"
28 /* Global definition of robot structure */
31 #ifdef CONFIG_LOCK_CHECKING
32 struct lock_log robot_lock_log;
35 static void block_signals()
41 for (i=SIGRTMIN; i<=SIGRTMAX; i++)
42 sigaddset(&sigset, i);
44 pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
47 static void int_handler(int sig)
52 void fill_in_known_areas_in_map()
54 /* Do not plan path close to edges */
55 /* ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); */
56 /* ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
57 /* ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
58 /* ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
60 //ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
61 //ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
63 //ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
64 //ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
66 /* Ignore other obstacles at edges */
67 ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
68 ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
69 ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
70 ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
72 //ShmapSetRectangleFlag(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
73 //ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
75 /* Construction areas (Building zones) */
76 ShmapSetRectangleFlag(0.0, 0.0, 1.5, 2.1, MAP_FLAG_SIMULATED_WALL, 0); /* Central construction area */
80 * Initializes the robot.
81 * Setup fields in robot structure, initializes FSMs and ORTE.
89 pthread_mutex_init(&robot.lock, NULL);
90 pthread_mutex_init(&robot.lock_ref_pos, NULL);
91 pthread_mutex_init(&robot.lock_est_pos, NULL);
92 pthread_mutex_init(&robot.lock_meas_angles, NULL);
93 pthread_mutex_init(&robot.lock_joy_data, NULL);
94 pthread_mutex_init(&robot.lock_disp, NULL);
96 fsm_main_loop_init(&robot.main_loop);
98 /* FSM initialization */
99 fsm_init(&robot.fsm.main, "main", &robot.main_loop);
100 fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
101 fsm_init(&robot.fsm.display, "display", &robot.main_loop);
102 fsm_init(&robot.fsm.act, "actuators", &robot.main_loop);
104 /* FIXME: there should be team color initilization */
106 robot.team_color = RED;
109 robot.team_color = GREEN;
112 if (robot.team_color == RED) {
113 printf("We are RED!\n");
115 printf("We are GREEN!\n");
118 #ifdef CONFIG_OPEN_LOOP
119 printf("OPEN_LOOP enabled\n");
122 robot.map = ShmapInit(1);
123 fill_in_known_areas_in_map();
125 signal(SIGINT, int_handler);
126 signal(SIGTERM, int_handler);
130 robot.orte.motion_speed.left = 0;
131 robot.orte.motion_speed.right = 0;
133 robot.orte.pwr_ctrl.voltage33 = 1;
134 robot.orte.pwr_ctrl.voltage50 = 1;
135 robot.orte.pwr_ctrl.voltage80 = 1;
137 robot.fsm.motion.state = &fsm_state_motion_init;
139 robot.fsm.act.state = &fsm_state_act_wait_for_command; // puck handling actuators FSM's initial state
141 /* Only activate display if it is configured */
142 robot.sercom = uoled_init(serial_comm);
143 if (strcmp(robot.sercom->devname, "/dev/null") != 0)
144 robot.fsm.display.state = &fsm_state_disp_init;
146 robot.obstacle_avoidance_enabled = true;
147 robot.state = JUST_STARTED;
149 /* init ORTE domain, create publishers, subscribers, .. */
150 rv = robot_init_orte();
151 act_init(&robot.orte);
157 * Starts the robot FSMs and threads.
165 pthread_attr_t tattr;
166 struct sched_param param;
167 pthread_t thr_obstacle_forgeting;
170 ret = motion_control_init();
172 perror("motion_control_init");
177 /* Obstacle forgeting thread */
178 pthread_attr_init (&tattr);
179 pthread_attr_getschedparam (&tattr, ¶m);
180 param.sched_priority = OBST_FORGETING_PRIO;
181 rv = pthread_attr_setschedparam (&tattr, ¶m);
183 perror("robot_start: pthread_attr_setschedparam()");
186 rv = pthread_create(&thr_obstacle_forgeting,
187 &tattr, thread_obstacle_forgeting, NULL);
189 perror("robot_start: pthread_create");
193 fsm_main_loop(&robot.main_loop);
200 * Signals all the robot threads to finish.
205 fsm_exit(&robot.fsm.main);
206 fsm_exit(&robot.fsm.motion);
207 fsm_exit(&robot.fsm.display);
208 fsm_exit(&robot.fsm.act);
212 * Stops the robot. All resources alocated by robot_init() or
213 * robot_start() are dealocated here.
217 motion_control_done();
219 robottype_roboorte_destroy(&robot.orte);
221 fsm_destroy(&robot.fsm.main);
222 fsm_destroy(&robot.fsm.motion);
223 fsm_destroy(&robot.fsm.display);
224 fsm_destroy(&robot.fsm.act);
225 DBG("robofsm: stop.\n");