]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
60c4b722508aabca1c80e0fa8bbde3cb39494912
[eurobot/public.git] / src / robofsm / robot.c
1 /*
2  * robot_demo2011.c
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 #define _XOPEN_SOURCE 500       /* For PTHREAD_PRIO_INHERIT etc. */
12
13 #include <map.h>
14 #include <movehelper.h>
15 #include <pthread.h>
16 #include <robomath.h>
17 #include <robot.h>
18 #include <robot_orte.h>
19 #include <signal.h>
20 #include <sys/time.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include "map_handling.h"
24 #include <string.h>
25 #include "actuators.h"
26 #include <robodim.h>
27 #include <ul_log.h>
28
29 UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
30
31
32 #define MOTION_CONTROL_INIT_ONLY
33 #include "motion-control.h"
34 #include "robot.h"
35
36 /* Global definition of robot structure */
37 struct robot robot;
38
39 #ifdef CONFIG_LOCK_CHECKING
40 struct lock_log robot_lock_log;
41 #endif
42
43 static void block_signals()
44 {
45         sigset_t sigset;
46         int i;
47
48         sigemptyset(&sigset);
49         for (i=SIGRTMIN; i<=SIGRTMAX; i++)
50                 sigaddset(&sigset, i);
51
52         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
53 }
54
55 static void int_handler(int sig)
56 {
57         robot_exit();
58 }
59
60 void fill_in_known_areas_in_map()
61 {
62         /*----- playground width 3.0 m and playground height 2.0 m -----*/
63         /* Ignore other obstacles at edges */
64         ShmapSetRectangleFlag(0.0, 0.0, 0.40, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* left */
65         ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
66         ShmapSetRectangleFlag(0.0, 1.91, 3.0, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* top */
67         ShmapSetRectangleFlag(2.6, 0.0, 3.0, 2.0, MAP_FLAG_IGNORE_OBST, 0); /* right */
68
69         /* Small walls that cannot be detected by hokuyo */
70         ShmapSetRectangleFlag(0.0, 1.49, 0.39, 1.432, MAP_FLAG_WALL, 0);
71         ShmapSetRectangleFlag(3.0, 1.49, 2.61, 1.432, MAP_FLAG_WALL, 0);
72         ShmapSetRectangleFlag(0.325, 0.0, 0.38, 0.74, MAP_FLAG_WALL, 0);
73         ShmapSetRectangleFlag(2.675, 0.0, 2.62, 0.74, MAP_FLAG_WALL, 0);
74         
75         /* Palm tree */
76         //ShmapSetCircleFlag(1.5, 1.0, 0.075, MAP_FLAG_WALL, 0);
77         
78         /* Totems and palm tree */
79         ShmapSetRectangleFlag(0.975, 0.875, 2.025, 1.125, MAP_FLAG_WALL, 0);
80         ShmapSetCircleFlag(1.1, 1.0, 0.3, MAP_FLAG_WALL, 0);
81         ShmapSetCircleFlag(2.0, 1.0, 0.3, MAP_FLAG_WALL, 0);
82         
83         //ShmapSetRectangleFlag(1.775, 0.875, 2.025, 1.125, MAP_FLAG_WALL, 0);
84 }
85
86 static void trans_callback(struct robo_fsm *fsm)
87 {
88         if (fsm == &robot.fsm.main) {
89                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
90                 ORTEPublicationSend(robot.orte.publication_fsm_main);
91         } else if (fsm == &robot.fsm.motion) {
92                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
93                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
94         } else if (fsm == &robot.fsm.act) {
95                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
96                 ORTEPublicationSend(robot.orte.publication_fsm_act);
97         }
98
99 }
100
101 /**
102  * Initializes the robot.
103  * Setup fields in robot structure, initializes FSMs and ORTE.
104  *
105  * @return 0
106  */
107 int robot_init()
108 {
109         int rv;
110         pthread_mutexattr_t mattr;
111         robot_calibrate_odometry();
112         rv = pthread_mutexattr_init(&mattr);
113 #ifdef HAVE_PRIO_INHERIT
114         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
115 #endif
116         pthread_mutex_init(&robot.lock, &mattr);
117         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
118         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
119         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
120         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
121         pthread_mutex_init(&robot.lock_joy_data, &mattr);
122         pthread_mutex_init(&robot.lock_disp, &mattr);
123
124         fsm_main_loop_init(&robot.main_loop);
125
126         /* FSM initialization */
127         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
128         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
129         fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
130         robot.fsm.main.transition_callback = trans_callback;
131         robot.fsm.act.transition_callback = trans_callback;
132         robot.fsm.motion.transition_callback = trans_callback;
133
134         robot.team_color = VIOLET;
135
136         if (robot.team_color == VIOLET) {
137                 ul_loginf("We are VIOLET!\n");
138         } else {
139                 ul_loginf("We are RED!\n");
140         }
141
142         robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
143
144         robot.ignore_hokuyo = false;
145         robot.map = ShmapInit(1);
146         fill_in_known_areas_in_map();
147
148         signal(SIGINT, int_handler);
149         signal(SIGTERM, int_handler);
150         block_signals();
151
152         /* initial values */
153         robot.orte.motion_speed.left = 0;
154         robot.orte.motion_speed.right = 0;
155
156         robot.orte.pwr_ctrl.voltage33 = 1;
157         robot.orte.pwr_ctrl.voltage50 = 1;
158         robot.orte.pwr_ctrl.voltage80 = 1;
159
160         robot.orte.camera_control.on = true;
161
162         robot.fsm.motion.state = &fsm_state_motion_init;
163
164         /* Only activate display if it is configured */
165         /* FIXME: display
166         robot.sercom = uoled_init(serial_comm);
167         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
168                 robot.fsm.display.state = &fsm_state_disp_init;
169         */
170
171         robot.obstacle_avoidance_enabled = true;
172         robot.use_back_bumpers = true;
173         robot.use_left_bumper = true;
174         robot.use_right_bumper = true;
175         robot.start_state = POWER_ON;
176         robot.check_turn_safety = true;
177
178         /* init ORTE domain, create publishers, subscribers, .. */
179         rv = robot_init_orte();
180         act_init(&robot.orte);
181
182         return rv;
183 }
184
185 /**
186  * Starts the robot FSMs and threads.
187  *
188  * @return 0
189  */
190 int robot_start()
191 {
192         int rv = 0;
193
194         pthread_attr_t tattr;
195         struct sched_param param;
196         pthread_t thr_obstacle_forgeting;
197         int ret;
198
199         ret = motion_control_init();
200         if(ret) {
201                 perror("motion_control_init");
202                 robot_exit();
203         }
204
205
206         /* Obstacle forgeting thread */
207         pthread_attr_init (&tattr);
208         pthread_attr_getschedparam (&tattr, &param);
209         pthread_attr_getschedparam (&tattr, &param);
210         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
211         param.sched_priority = OBST_FORGETING_PRIO;
212         rv = pthread_attr_setschedparam (&tattr, &param);
213         if (rv) {
214                 perror("robot_start: pthread_attr_setschedparam()");
215                 goto err;
216         }
217         rv = pthread_create(&thr_obstacle_forgeting,
218                             &tattr, thread_obstacle_forgeting, NULL);
219         if (rv) {
220                 perror("robot_start: pthread_create");
221                 goto err;
222         }
223
224         sem_init(&robot.start, 0, 0);
225
226         fsm_main_loop(&robot.main_loop);
227
228 err:
229         return rv;
230 }
231
232 /**
233  * Signals all the robot threads to finish.
234  */
235 void robot_exit()
236 {
237         /* stop FSMs */
238         fsm_exit(&robot.fsm.main);
239         fsm_exit(&robot.fsm.motion);
240         fsm_exit(&robot.fsm.act);
241 }
242
243 /**
244  * Stops the robot. All resources alocated by robot_init() or
245  * robot_start() are dealocated here.
246  */
247 void robot_destroy()
248 {
249         motion_control_done();
250
251         // FIXME: set actuators to well defined position (FJ)
252
253         robottype_roboorte_destroy(&robot.orte);
254
255         fsm_destroy(&robot.fsm.main);
256         fsm_destroy(&robot.fsm.motion);
257         fsm_destroy(&robot.fsm.act);
258         ShmapFree();
259         ul_logdeb("robofsm: stop.\n");
260 }
261
262 void robot_get_est_pos_trans(double *x, double *y, double *phi)
263 {
264         robot_get_est_pos(x, y, phi);
265         *x = __trans_x(*x);
266         *y = __trans_y(*y);
267         *phi = __trans_ang(*phi);
268 }
269
270 void robot_get_est_pos(double *x, double *y, double *phi)
271 {
272         if (robot.indep_odometry_works) {
273                 ROBOT_LOCK(est_pos_indep_odo);
274                 *x = robot.est_pos_indep_odo.x;
275                 *y = robot.est_pos_indep_odo.y;
276                 *phi = robot.est_pos_indep_odo.phi;
277                 ROBOT_UNLOCK(est_pos_indep_odo);
278         } else if (robot.odometry_works) {
279                 ROBOT_LOCK(est_pos_odo);
280                 *x = robot.est_pos_odo.x;
281                 *y = robot.est_pos_odo.y;
282                 *phi = robot.est_pos_odo.phi;
283                 ROBOT_UNLOCK(est_pos_odo);
284         } else {
285                 ROBOT_LOCK(ref_pos);
286                 *x = robot.ref_pos.x;
287                 *y = robot.ref_pos.y;
288                 *phi = robot.ref_pos.phi;
289                 ROBOT_UNLOCK(ref_pos);
290         }
291 }
292
293 void robot_calibrate_odometry()
294 {
295         robot.odo_distance_a = 0;
296         robot.odo_distance_b = 0;
297         FILE *file;     
298         file = fopen ("odometry_cal_data", "r");
299         if (file == NULL)
300         { 
301                 robot.odo_cal_a = 1;
302                 robot.odo_cal_b = 1;
303                 fprintf(stderr, "ODO calibration file not found! \n\n");
304                 return;
305         }
306         char line [15];
307         float a = 0;
308         float b = 0;
309         int num = 0;
310         while (fgets (line, 15 , file)) {
311                 a += atof(strtok(line," "));
312                 fgets (line, 15 , file);
313                 b += atof(strtok(NULL," "));
314                 num ++;
315         }
316         fclose (file);
317         if(a == 0 || b == 0) {
318                 robot.odo_cal_a = 1;
319                 robot.odo_cal_b = 1;
320                 return;
321         }
322         robot.odo_cal_a = a / num;
323         robot.odo_cal_b = b / num;
324         printf("ODO calibrated value A: %f\n",robot.odo_cal_a);
325         printf("ODO calibrated value B: %f\n",robot.odo_cal_b);
326 }