]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
robofsm: Robot est. position initialized to starting position.
[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         /* opponent start area */
83         if (robot.team_color == RED) {
84                 ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M - 0.4 - 0.05 - 0.35, PLAYGROUND_HEIGHT_M - 0.4, MAP_FLAG_WALL, 0);
85         } else {
86                 ShmapSetRectangleFlag(0, PLAYGROUND_HEIGHT_M, 0.8, PLAYGROUND_HEIGHT_M - 0.4, MAP_FLAG_WALL, 0);
87         }
88
89         /* protected bloc left */
90         ShmapSetRectangleFlag(0, 0, 1.15, 0.35, MAP_FLAG_WALL, 0);
91
92         /* protected bloc right */
93         ShmapSetRectangleFlag(1.85, 0, PLAYGROUND_WIDTH_M, 0.35, MAP_FLAG_WALL, 0);
94
95         /* plan trajectory on */
96
97         /* playing area walls */
98 //      ShmapSetRectangleFlag(0, 0, PLAYGROUND_WIDTH_M, 0.1, MAP_FLAG_IGNORE_OBST, 0);
99 //      ShmapSetRectangleFlag(PLAYGROUND_WIDTH_M, 0, PLAYGROUND_WIDTH_M - 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
100 //      ShmapSetRectangleFlag(0 ,PLAYGROUND_HEIGHT_M, PLAYGROUND_WIDTH_M, PLAYGROUND_HEIGHT_M - 0.1, MAP_FLAG_IGNORE_OBST, 0);
101 //      ShmapSetRectangleFlag(0 , 0, 0.1, PLAYGROUND_HEIGHT_M, MAP_FLAG_IGNORE_OBST, 0);
102 }
103
104 static void trans_callback(struct robo_fsm *fsm)
105 {
106         if (fsm == &robot.fsm.main) {
107                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
108                 ORTEPublicationSend(robot.orte.publication_fsm_main);
109         } else if (fsm == &robot.fsm.motion) {
110                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
111                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
112         } else if (fsm == &robot.fsm.act) {
113                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
114                 ORTEPublicationSend(robot.orte.publication_fsm_act);
115         }
116
117 }
118
119 /**
120  * Initializes the robot.
121  * Setup fields in robot structure, initializes FSMs and ORTE.
122  *
123  * @return 0
124  */
125 int robot_init()
126 {
127         int rv;
128         pthread_mutexattr_t mattr;
129
130         rv = pthread_mutexattr_init(&mattr);
131 #ifdef HAVE_PRIO_INHERIT
132         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
133 #endif
134         pthread_mutex_init(&robot.lock, &mattr);
135         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
136         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
137         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
138         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
139         pthread_mutex_init(&robot.lock_joy_data, &mattr);
140         pthread_mutex_init(&robot.lock_disp, &mattr);
141
142         fsm_main_loop_init(&robot.main_loop);
143
144         /* FSM initialization */
145         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
146         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
147         fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
148         robot.fsm.main.transition_callback = trans_callback;
149         robot.fsm.act.transition_callback = trans_callback;
150         robot.fsm.motion.transition_callback = trans_callback;
151
152         robot.team_color = RED;
153
154         if (robot.team_color == RED) {
155                 ul_loginf("We are RED!\n");
156         } else {
157                 ul_loginf("We are BLUE!\n");
158         }
159
160         robot_set_est_pos_trans(ROBOT_START_X_M, ROBOT_START_Y_M, DEG2RAD(ROBOT_START_ANGLE_DEG));
161
162         robot.ignore_hokuyo = false;
163         robot.map = ShmapInit(1);
164         fill_in_known_areas_in_map();
165
166         signal(SIGINT, int_handler);
167         signal(SIGTERM, int_handler);
168         block_signals();
169
170         /* initial values */
171         robot.orte.motion_speed.left = 0;
172         robot.orte.motion_speed.right = 0;
173
174         robot.orte.pwr_ctrl.voltage33 = 1;
175         robot.orte.pwr_ctrl.voltage50 = 1;
176         robot.orte.pwr_ctrl.voltage80 = 1;
177
178         robot.orte.camera_control.on = true;
179
180         robot.fsm.motion.state = &fsm_state_motion_init;
181
182         /* Only activate display if it is configured */
183         /* FIXME: display
184         robot.sercom = uoled_init(serial_comm);
185         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
186                 robot.fsm.display.state = &fsm_state_disp_init;
187         */
188
189         robot.obstacle_avoidance_enabled = true;
190         robot.use_back_bumpers = true;
191         robot.use_left_bumper = true;
192         robot.use_right_bumper = true;
193         robot.start_state = POWER_ON;
194         robot.check_turn_safety = true;
195
196         /* init ORTE domain, create publishers, subscribers, .. */
197         rv = robot_init_orte();
198         act_init(&robot.orte);
199
200         return rv;
201 }
202
203 /**
204  * Starts the robot FSMs and threads.
205  *
206  * @return 0
207  */
208 int robot_start()
209 {
210         int rv = 0;
211
212         pthread_attr_t tattr;
213         struct sched_param param;
214         pthread_t thr_obstacle_forgeting;
215         int ret;
216
217         ret = motion_control_init();
218         if(ret) {
219                 perror("motion_control_init");
220                 robot_exit();
221         }
222
223
224         /* Obstacle forgeting thread */
225         pthread_attr_init (&tattr);
226         pthread_attr_getschedparam (&tattr, &param);
227         pthread_attr_getschedparam (&tattr, &param);
228         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
229         param.sched_priority = OBST_FORGETING_PRIO;
230         rv = pthread_attr_setschedparam (&tattr, &param);
231         if (rv) {
232                 perror("robot_start: pthread_attr_setschedparam()");
233                 goto err;
234         }
235         rv = pthread_create(&thr_obstacle_forgeting,
236                             &tattr, thread_obstacle_forgeting, NULL);
237         if (rv) {
238                 perror("robot_start: pthread_create");
239                 goto err;
240         }
241
242         sem_init(&robot.start, 0, 0);
243
244         fsm_main_loop(&robot.main_loop);
245
246 err:
247         return rv;
248 }
249
250 /**
251  * Signals all the robot threads to finish.
252  */
253 void robot_exit()
254 {
255         /* stop FSMs */
256         fsm_exit(&robot.fsm.main);
257         fsm_exit(&robot.fsm.motion);
258         fsm_exit(&robot.fsm.act);
259 }
260
261 /**
262  * Stops the robot. All resources alocated by robot_init() or
263  * robot_start() are dealocated here.
264  */
265 void robot_destroy()
266 {
267         motion_control_done();
268
269         // FIXME: set actuators to well defined position (FJ)
270
271         robottype_roboorte_destroy(&robot.orte);
272
273         fsm_destroy(&robot.fsm.main);
274         fsm_destroy(&robot.fsm.motion);
275         fsm_destroy(&robot.fsm.act);
276         ShmapFree();
277         ul_logdeb("robofsm: stop.\n");
278 }
279
280 void robot_get_est_pos_trans(double *x, double *y, double *phi)
281 {
282         robot_get_est_pos(x, y, phi);
283         *x = __trans_x(*x);
284         *y = __trans_y(*y);
285         *phi = __trans_ang(*phi);
286 }
287
288 void robot_get_est_pos(double *x, double *y, double *phi)
289 {
290         if (robot.indep_odometry_works) {
291                 ROBOT_LOCK(est_pos_indep_odo);
292                 *x = robot.est_pos_indep_odo.x;
293                 *y = robot.est_pos_indep_odo.y;
294                 *phi = robot.est_pos_indep_odo.phi;
295                 ROBOT_UNLOCK(est_pos_indep_odo);
296         } else if (robot.odometry_works) {
297                 ROBOT_LOCK(est_pos_odo);
298                 *x = robot.est_pos_odo.x;
299                 *y = robot.est_pos_odo.y;
300                 *phi = robot.est_pos_odo.phi;
301                 ROBOT_UNLOCK(est_pos_odo);
302         } else {
303                 ROBOT_LOCK(ref_pos);
304                 *x = robot.ref_pos.x;
305                 *y = robot.ref_pos.y;
306                 *phi = robot.ref_pos.phi;
307                 ROBOT_UNLOCK(ref_pos);
308         }
309 }