]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
robofsm: Added map flag ignore obstacle inside the table around the walls.
[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
35 /* Global definition of robot structure */
36 struct robot robot;
37
38 #ifdef CONFIG_LOCK_CHECKING
39 struct lock_log robot_lock_log;
40 #endif
41
42 static void block_signals()
43 {
44         sigset_t sigset;
45         int i;
46
47         sigemptyset(&sigset);
48         for (i=SIGRTMIN; i<=SIGRTMAX; i++)
49                 sigaddset(&sigset, i);
50
51         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
52 }
53
54 static void int_handler(int sig)
55 {
56         robot_exit();
57 }
58
59 void fill_in_known_areas_in_map()
60 {
61         /* Do not plan path close to edges */
62         //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
63         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
64         //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
65         //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
66
67 //      /* Container surroundings */
68 //      ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
69 //      ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
70
71 //      /* Ignore other obstacles at edges */
72 //      ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
73 //      ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
74 //      ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
75 //      ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
76 //      ShmapSetRectangleFlag(1.0, 1.5, 1.95, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* rised area */
77
78         /* start blocs */
79         ShmapSetRectangleFlag(0, 1.7, 0.4, 1.678, MAP_FLAG_WALL, 0);
80         ShmapSetRectangleFlag(2.55, 1.7, 3, 1.678, MAP_FLAG_WALL, 0);
81
82         /* protected bloc left */
83         ShmapSetRectangleFlag(0.45, 0, 1.15, 0.12, MAP_FLAG_WALL, 0);
84         ShmapSetRectangleFlag(0.45, 0.12, 0.472, 0.25, MAP_FLAG_WALL, 0);
85         ShmapSetRectangleFlag(1.128, 0.12, 1.15, 0.25, MAP_FLAG_WALL, 0);
86
87         /* protected bloc right */
88         ShmapSetRectangleFlag(1.85, 0, 2.55, 0.12, MAP_FLAG_WALL, 0);
89         ShmapSetRectangleFlag(1.85, 0.12, 1.872, 0.25, MAP_FLAG_WALL, 0);
90         ShmapSetRectangleFlag(2.528, 0.12, 2.55, 0.25, MAP_FLAG_WALL, 0);
91
92         /* playing area walls */
93         ShmapSetRectangleFlag(0, 0, PLAYGROUND_WIDTH_M, 0.1, MAP_FLAG_IGNORE_OBST, 0);
94         ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, 0, PLAYGROUND_WIDTH_M - 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
95         ShmapSetRectangleFlag(0 ,PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M - 0.1, MAP_FLAG_IGNORE_OBST, 0);
96         ShmapSetRectangleFlag(0 , 0, 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
97 }
98
99 static void trans_callback(struct robo_fsm *fsm)
100 {
101         if (fsm == &robot.fsm.main) {
102                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
103                 ORTEPublicationSend(robot.orte.publication_fsm_main);
104         } else if (fsm == &robot.fsm.motion) {
105                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
106                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
107         } else if (fsm == &robot.fsm.act) {
108                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
109                 ORTEPublicationSend(robot.orte.publication_fsm_act);
110         }
111
112 }
113
114 /**
115  * Initializes the robot.
116  * Setup fields in robot structure, initializes FSMs and ORTE.
117  *
118  * @return 0
119  */
120 int robot_init()
121 {
122         int rv;
123         pthread_mutexattr_t mattr;
124
125         rv = pthread_mutexattr_init(&mattr);
126 #ifdef HAVE_PRIO_INHERIT
127         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
128 #endif
129         pthread_mutex_init(&robot.lock, &mattr);
130         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
131         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
132         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
133         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
134         pthread_mutex_init(&robot.lock_joy_data, &mattr);
135         pthread_mutex_init(&robot.lock_disp, &mattr);
136
137         fsm_main_loop_init(&robot.main_loop);
138
139         /* FSM initialization */
140         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
141         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
142         fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
143         robot.fsm.main.transition_callback = trans_callback;
144         robot.fsm.act.transition_callback = trans_callback;
145         robot.fsm.motion.transition_callback = trans_callback;
146
147         robot.team_color = RED;
148
149         if (robot.team_color == RED) {
150                 ul_loginf("We are RED!\n");
151         } else {
152                 ul_loginf("We are BLUE!\n");
153         }
154
155         robot_set_est_pos_trans(1, 1, DEG2RAD(0));
156
157         robot.ignore_hokuyo = false;
158         robot.map = ShmapInit(1);
159         fill_in_known_areas_in_map();
160
161         signal(SIGINT, int_handler);
162         signal(SIGTERM, int_handler);
163         block_signals();
164
165         /* initial values */
166         robot.orte.motion_speed.left = 0;
167         robot.orte.motion_speed.right = 0;
168
169         robot.orte.pwr_ctrl.voltage33 = 1;
170         robot.orte.pwr_ctrl.voltage50 = 1;
171         robot.orte.pwr_ctrl.voltage80 = 1;
172
173         robot.orte.camera_control.on = true;
174
175         robot.fsm.motion.state = &fsm_state_motion_init;
176
177         /* Only activate display if it is configured */
178         /* FIXME: display
179         robot.sercom = uoled_init(serial_comm);
180         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
181                 robot.fsm.display.state = &fsm_state_disp_init;
182         */
183
184         robot.obstacle_avoidance_enabled = true;
185         robot.use_back_bumpers = true;
186         robot.use_left_bumper = true;
187         robot.use_right_bumper = true;
188         robot.start_state = POWER_ON;
189         robot.check_turn_safety = true;
190
191         /* init ORTE domain, create publishers, subscribers, .. */
192         rv = robot_init_orte();
193         act_init(&robot.orte);
194
195         return rv;
196 }
197
198 /**
199  * Starts the robot FSMs and threads.
200  *
201  * @return 0
202  */
203 int robot_start()
204 {
205         int rv = 0;
206
207         pthread_attr_t tattr;
208         struct sched_param param;
209         pthread_t thr_obstacle_forgeting;
210         int ret;
211
212         ret = motion_control_init();
213         if(ret) {
214                 perror("motion_control_init");
215                 robot_exit();
216         }
217
218
219         /* Obstacle forgeting thread */
220         pthread_attr_init (&tattr);
221         pthread_attr_getschedparam (&tattr, &param);
222         pthread_attr_getschedparam (&tattr, &param);
223         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
224         param.sched_priority = OBST_FORGETING_PRIO;
225         rv = pthread_attr_setschedparam (&tattr, &param);
226         if (rv) {
227                 perror("robot_start: pthread_attr_setschedparam()");
228                 goto err;
229         }
230         rv = pthread_create(&thr_obstacle_forgeting,
231                             &tattr, thread_obstacle_forgeting, NULL);
232         if (rv) {
233                 perror("robot_start: pthread_create");
234                 goto err;
235         }
236
237         sem_init(&robot.start, 0, 0);
238
239         fsm_main_loop(&robot.main_loop);
240
241 err:
242         return rv;
243 }
244
245 /**
246  * Signals all the robot threads to finish.
247  */
248 void robot_exit()
249 {
250         /* stop FSMs */
251         fsm_exit(&robot.fsm.main);
252         fsm_exit(&robot.fsm.motion);
253         fsm_exit(&robot.fsm.act);
254 }
255
256 /**
257  * Stops the robot. All resources alocated by robot_init() or
258  * robot_start() are dealocated here.
259  */
260 void robot_destroy()
261 {
262         motion_control_done();
263
264         // FIXME: set actuators to well defined position (FJ)
265
266         robottype_roboorte_destroy(&robot.orte);
267
268         fsm_destroy(&robot.fsm.main);
269         fsm_destroy(&robot.fsm.motion);
270         fsm_destroy(&robot.fsm.act);
271         ShmapFree();
272         ul_logdeb("robofsm: stop.\n");
273 }
274
275 void robot_get_est_pos_trans(double *x, double *y, double *phi)
276 {
277         robot_get_est_pos(x, y, phi);
278         *x = __trans_x(*x);
279         *y = __trans_y(*y);
280         *phi = __trans_ang(*phi);
281 }
282
283 void robot_get_est_pos(double *x, double *y, double *phi)
284 {
285         if (robot.indep_odometry_works) {
286                 ROBOT_LOCK(est_pos_indep_odo);
287                 *x = robot.est_pos_indep_odo.x;
288                 *y = robot.est_pos_indep_odo.y;
289                 *phi = robot.est_pos_indep_odo.phi;
290                 ROBOT_UNLOCK(est_pos_indep_odo);
291         } else if (robot.odometry_works) {
292                 ROBOT_LOCK(est_pos_odo);
293                 *x = robot.est_pos_odo.x;
294                 *y = robot.est_pos_odo.y;
295                 *phi = robot.est_pos_odo.phi;
296                 ROBOT_UNLOCK(est_pos_odo);
297         } else {
298                 ROBOT_LOCK(ref_pos);
299                 *x = robot.ref_pos.x;
300                 *y = robot.ref_pos.y;
301                 *phi = robot.ref_pos.phi;
302                 ROBOT_UNLOCK(ref_pos);
303         }
304 }