]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
Merge branch 'master' of sachj1@rtime.felk.cvut.cz:/var/git/eurobot
[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 char *fsm_name[] = {
27         [FSM_ID_MAIN] = "main",
28         [FSM_ID_MOTION] = "motion",
29         [FSM_ID_DISP] = "display",
30 };
31
32 struct robot_eb2008 robot;
33
34 #ifdef CONFIG_LOCK_CHECKING
35 struct lock_log robot_lock_log;
36 #endif
37
38 static void block_signals()
39 {
40         sigset_t sigset;
41         int i;
42
43         sigemptyset(&sigset);
44         for (i=0; i<7; i++)
45                 sigaddset(&sigset, SIGRTMIN+i);
46
47         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
48 }
49
50 static void int_handler(int sig)
51 {
52         robot_exit();
53 }
54
55 void fill_in_known_areas_in_map()
56 {
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); */
62
63         
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 */
66
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 */
69
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 */
75
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 */
78 }
79
80 /** 
81  * Initializes the robot.
82  * Setup fields in robot structure, initializes FSMs and ORTE.
83  * 
84  * @return 0
85  */
86 int robot_init()
87 {
88         int i;
89         int rv;
90
91         /* robot and competition configuration */
92         robot.mode = ROBO_TESTING;
93         robot.team_color = BLUE;
94         robot.beacon_color = BEACON_BLUE;
95
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);
102
103         fsm_main_loop_init(&robot.main_loop);
104         
105         /* FSM initialization */
106         for (i=0; i<FSM_CNT; i++)
107                 fsm_init(&robot.fsm[i], fsm_name[i], &robot.main_loop);
108
109         /* FIXME: there should be team color initilization */
110 #ifdef WE_ARE_RED
111         robot.team_color = RED;
112 #endif
113 #ifdef WE_ARE_BLUE
114         robot.team_color = BLUE;
115 #endif
116
117         if (robot.team_color == RED) {
118                 printf("We are RED!\n");
119         } else {
120                 printf("We are BLUE!\n");
121         }
122
123 #ifdef CONFIG_OPEN_LOOP
124         printf("OPEN_LOOP enabled\n");
125 #endif
126
127         robot.map = ShmapInit(1);
128         fill_in_known_areas_in_map();
129
130         signal(SIGINT, int_handler);
131         signal(SIGTERM, int_handler);
132         block_signals();
133
134         /* initial values */
135         robot.orte.motion_speed.left = 0;
136         robot.orte.motion_speed.right = 0;
137
138         robot.orte.pwr_ctrl.voltage33 = 1;
139         robot.orte.pwr_ctrl.voltage50 = 1;
140         robot.orte.pwr_ctrl.voltage80 = 1;
141
142         brushes_in();
143         
144         off_brush_left();
145         off_brush_right();
146         
147         /* MCL initialization */
148         robot.laser.width = PLAYGROUND_WIDTH_M; /* in meters */
149         robot.laser.height = PLAYGROUND_WIDTH_M; /* in meters */
150         /* the noises */
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);
155
156         robot.fsm[FSM_ID_MOTION].state = &fsm_state_motion_init;
157
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;
162
163         robot.obstacle_avoidance_enabled = true;
164         robot.laser_enabled = true;
165         robot.state = JUST_STARTED;
166
167         robot.carousel_cnt = 0;
168         robot.carousel_pos = 0;
169
170         /* init ORTE domain, create publishers, subscribers, .. */
171         rv = robot_init_orte();
172
173         return rv;
174 }
175
176 /** 
177  * Starts the robot threads.
178  *
179  * @return 0
180  */
181 int robot_start()
182 {
183         int rv = 0;
184
185         pthread_attr_t tattr;
186         struct sched_param param;
187         pthread_t thr_obstacle_forgeting;
188
189         /* Obstacle forgeting thread */
190         pthread_attr_init (&tattr);
191         pthread_attr_getschedparam (&tattr, &param);
192         param.sched_priority = OBST_FORGETING_PRIO;
193         rv = pthread_attr_setschedparam (&tattr, &param);
194         if (rv) {
195                 perror("robot_start: pthread_attr_setschedparam()");
196                 goto err;
197         }
198         rv = pthread_create(&thr_obstacle_forgeting, 
199                             &tattr, thread_obstacle_forgeting, NULL);
200         if (rv) {
201                 perror("robot_start: pthread_create");
202                 goto err;
203         }
204
205         fsm_main_loop(&robot.main_loop);
206
207 err:
208         return rv;
209 }
210
211 /** 
212  * Signals all the robot threads to finish.
213  */
214 void robot_exit()
215 {
216         int i;
217
218         /* stop FSM threads */
219         for(i=0; i<FSM_CNT; i++) {
220                 fsm_exit(&robot.fsm[i]);
221         }
222
223         return 0;
224 }
225
226 /** 
227  * Stops the robot. All resources alocated by robot_init() or
228  * robot_start() are dealocated here.
229  */
230 void robot_destroy()
231 {
232         int i;
233
234         robot.orte.laser_cmd.speed = LASER_DRIVE_OFF;
235         ORTEPublicationSend(robot.orte.publication_laser_cmd);
236         ORTEPublicationSend(robot.orte.publication_laser_cmd);
237
238         off_brush_left();
239         off_brush_right();
240         robot.orte.motion_speed.right = 0;
241         robot.orte.motion_speed.left = 0;
242         ORTEPublicationSend(robot.orte.publication_motion_speed);                       
243         
244         robottype_roboorte_destroy(&robot.orte);
245
246         for (i=0; i<FSM_CNT; i++)
247                 fsm_destroy(&robot.fsm[i]);
248         DBG("robofsm: stop.\n");
249
250         return 0;
251 }