]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
Obstacle avoidance enabled by default
[eurobot/public.git] / src / robofsm / robot.c
1 /*
2  * robot_eb2008.c                       08/04/20
3  *
4  * Robot's generic initialization and clean up functions.
5  *
6  * Copyright: (c) 2008 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * License: GNU GPL v.2
9  */
10
11 #include <map.h>
12 #include <movehelper.h>
13 #include <pthread.h>
14 #include <robomath.h>
15 #include <robot.h>
16 #include <robot_orte.h>
17 #include <servos.h>
18 #include <signal.h>
19 #include <sys/time.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include "map_handling.h"
23 #include <uoled.h>
24 #include <string.h>
25
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,
32 };
33
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",
40 };
41
42 struct robot_eb2008 robot;
43
44 #ifdef CONFIG_LOCK_CHECKING
45 struct lock_log robot_lock_log;
46 #endif
47
48 static void block_signals()
49 {
50         sigset_t sigset;
51         int i;
52
53         sigemptyset(&sigset);
54         for (i=0; i<7; i++)
55                 sigaddset(&sigset, SIGRTMIN+i);
56
57         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
58 }
59
60 static void int_handler(int sig)
61 {
62         robot_exit();
63 }
64
65 void fill_in_known_areas_in_map()
66 {
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); */
72
73         
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 */
76
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 */
79
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 */
85
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 */
88 }
89
90 /** 
91  * Initializes the robot.
92  * Setup fields in robot structure, initializes FSMs and ORTE.
93  * 
94  * @return 0
95  */
96 int robot_init()
97 {
98         int i;
99         int rv;
100
101         /* robot and competition configuration */
102         robot.mode = ROBO_TESTING;
103         robot.team_color = BLUE;
104         robot.beacon_color = BEACON_BLUE;
105
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);
112
113         /* FSM initialization */
114         for (i=0; i<FSM_CNT; i++)
115                 fsm_init(&robot.fsm[i], fsm_name[i]);
116
117         /* FIXME: there should be team color initilization */
118 #ifdef WE_ARE_RED
119         robot.team_color = RED;
120 #endif
121 #ifdef WE_ARE_BLUE
122         robot.team_color = BLUE;
123 #endif
124
125         if (robot.team_color == RED) {
126                 printf("We are RED!\n");
127         } else {
128                 printf("We are BLUE!\n");
129         }
130
131 #ifdef CONFIG_OPEN_LOOP
132         printf("OPEN_LOOP enabled\n");
133 #endif
134
135         robot.map = ShmapInit(1);
136         fill_in_known_areas_in_map();
137
138         signal(SIGINT, int_handler);
139         block_signals();
140
141         /* initial values */
142         robot.orte.motion_speed.left = 0;
143         robot.orte.motion_speed.right = 0;
144
145         robot.orte.pwr_ctrl.voltage33 = 1;
146         robot.orte.pwr_ctrl.voltage50 = 1;
147         robot.orte.pwr_ctrl.voltage80 = 1;
148
149         brushes_in();
150         open_bottom_door();
151         close_top_door();
152         close_back_door();
153         
154         off_brush_left();
155         off_brush_right();
156         off_bagr();
157         
158         /* MCL initialization */
159         robot.laser.width = PLAYGROUND_WIDTH_M; /* in meters */
160         robot.laser.height = PLAYGROUND_WIDTH_M; /* in meters */
161         /* the noises */
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);
166
167         robot.fsm[FSM_ID_MOTION].state = &fsm_state_motion_init;
168         robot.fsm[FSM_ID_LOC].state = &fsm_state_loc_init;
169
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;
174
175         robot.obstacle_avoidance_enabled = true;
176         robot.laser_enabled = true;
177         robot.state = JUST_STARTED;
178
179         robot.carousel_cnt = 0;
180         robot.carousel_pos = 0;
181
182         /* init ORTE domain, create publishers, subscribers, .. */
183         rv = robot_init_orte();
184
185         return rv;
186 }
187
188 /** 
189  * Starts the robot threads.
190  *
191  * @return 0
192  */
193 int robot_start()
194 {
195         int i;
196         int rv = 0;
197
198         /* start FSM threads */
199         for(i=0; i<FSM_CNT; i++) {
200                 pthread_attr_t attr;
201                 struct sched_param param;
202
203                 pthread_attr_init(&attr);
204
205                 param.sched_priority = thread_priorities[i];
206                 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
207                 rv = pthread_attr_setschedparam(&attr, &param);
208                 if (rv) {
209                         perror("pthread_attr_setschedparam");
210                         goto err;
211                 }
212
213                 rv = fsm_start(&robot.fsm[i], &attr);
214                 if (rv) {
215                         perror("fsm_start");
216                         goto err;
217                 }
218         }
219
220         pthread_attr_t tattr;
221         struct sched_param param;
222         pthread_t thr_obstacle_forgeting;
223
224         /* trajectory follower thread */
225         pthread_attr_init (&tattr);
226         pthread_attr_getschedparam (&tattr, &param);
227         param.sched_priority = OBST_FORGETING_PRIO;
228         rv = pthread_attr_setschedparam (&tattr, &param);
229         if (rv) {
230                 perror("robot_start: pthread_attr_setschedparam()");
231                 goto err;
232         }
233
234         rv = pthread_create(&thr_obstacle_forgeting, 
235                                 &tattr, thread_obstacle_forgeting, NULL);
236         if (rv) {
237                 perror("robot_start: pthread_create");
238                 goto err;
239         }
240
241 err:
242         return rv;
243 }
244
245 /** 
246  * Destroy ORTE stuffs.
247  *
248  * @return 0
249  */
250 int robot_orte_destroy()
251 {
252         robot.orte.laser_cmd.speed = LASER_DRIVE_OFF;
253         ORTEPublicationSend(robot.orte.publication_laser_cmd);
254         ORTEPublicationSend(robot.orte.publication_laser_cmd);
255
256         open_back_door();
257         open_bottom_door();
258         off_bagr();
259         off_brush_left();
260         off_brush_right();
261         robot.orte.motion_speed.right = 0;
262         robot.orte.motion_speed.left = 0;
263         ORTEPublicationSend(robot.orte.publication_motion_speed);                       
264         
265         robottype_roboorte_destroy(&robot.orte);
266
267         return 0;
268 }
269
270 /** 
271  * Signals all the robot threads to finish.
272  * @return 0
273  */
274 int robot_exit()
275 {
276         int i;
277
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);
282         }
283
284         robot_orte_destroy();
285
286         return 0;
287 }
288
289 /** 
290  * Wait for finishing the robot threads after calling robot_exit(). *
291  * 
292  * @return 0
293  */
294 int robot_wait()
295 {
296         int i;
297
298         /* wait for threads */
299         for (i=0; i<FSM_CNT; i++) {
300                 pthread_join(robot.fsm[i].threadid, NULL);
301         }
302
303         return 0;
304 }
305
306 /** 
307  * Stops the robot. All resources alocated by robot_init() or
308  * robot_start() are dealocated here.
309  * @return 0
310  */
311 int robot_destroy()
312 {
313         int i;
314
315         for (i=0; i<FSM_CNT; i++)
316                 fsm_destroy(&robot.fsm[i]);
317         DBG("robofsm: stop.\n");
318
319         return 0;
320 }