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