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