]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
87028b1fa2f005c45df07e03064a5e3a88c94af0
[eurobot/public.git] / src / robofsm / robot.c
1 /*
2  * robot_eb2008.c                       08/04/20
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 "corns_configs.h"
27 #include <robodim.h>
28 #include <ul_log.h>
29
30 UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
31
32
33 #define MOTION_CONTROL_INIT_ONLY
34 #include "motion-control.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         /* Do not plan path close to edges */
63         //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
64         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
65         //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
66         //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
67
68         /* Container surroundings */
69         ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
70         ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
71
72         /* Ignore other obstacles at edges */
73         ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
74         ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
75         ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
76         ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
77         ShmapSetRectangleFlag(1.0, 1.5, 1.95, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* rised area */
78
79         ShmapSetRectangleFlag(0.6, 1.45, 2.45, 1.55, MAP_FLAG_WALL, 0); /* plateau, slopes */
80         ShmapSetRectangleFlag(1.46, PLAYGROUND_HEIGHT_M - 0.5, 1.57, PLAYGROUND_HEIGHT_M, MAP_FLAG_WALL, 0); /* plateau, slopes */
81         
82         /* corns */
83         struct corns_group *corns = get_all_corns(0, 0);
84         struct corn * corn;
85         for(corn = corns->corns; corn < &corns->corns[corns->corns_count]; corn++) {
86                 ShmapSetCircleFlag(corn->position.x, corn->position.y, CORN_NEIGHBOURHOOD_RADIUS_M, MAP_FLAG_WALL, 0);
87         }
88         dispose_corns_group(corns); // robot.corns will be set later, when the corns' configuration is known
89
90         ShmapSetRectangleFlag(0.32, 0.25, 0.38, 0.55, 0, MAP_FLAG_WALL); /* destination position near blue container */
91         ShmapSetRectangleFlag(2.62, 0.25, 2.68, 0.55, 0, MAP_FLAG_WALL); /* destination position near yellow container */
92 }
93
94 static void trans_callback(struct robo_fsm *fsm)
95 {
96         if (fsm == &robot.fsm.main) {
97                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
98                 ORTEPublicationSend(robot.orte.publication_fsm_main);
99         } else if (fsm == &robot.fsm.motion) {
100                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
101                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
102         } else if (fsm == &robot.fsm.act) {
103                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
104                 ORTEPublicationSend(robot.orte.publication_fsm_act);
105         }
106         
107 }
108
109 /** 
110  * Initializes the robot.
111  * Setup fields in robot structure, initializes FSMs and ORTE.
112  * 
113  * @return 0
114  */
115 int robot_init()
116 {
117         int rv;
118         pthread_mutexattr_t mattr;
119
120         rv = pthread_mutexattr_init(&mattr);
121 #ifdef HAVE_PRIO_INHERIT
122         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
123 #endif
124         pthread_mutex_init(&robot.lock, &mattr);
125         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
126         pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
127         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
128         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
129         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
130         pthread_mutex_init(&robot.lock_joy_data, &mattr);
131         pthread_mutex_init(&robot.lock_disp, &mattr);
132
133         fsm_main_loop_init(&robot.main_loop);
134         
135         /* FSM initialization */
136         /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
137         fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
138         fsm_init(&robot.fsm.display, "display", &robot.main_loop);
139         fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
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 = BLUE;
148
149         if (robot.team_color == YELLOW) {
150                 ul_loginf("We are YELLOW!\n");
151         } else {
152                 ul_loginf("We are BLUE!\n");
153         }
154
155         robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
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_switch = true;
186         robot.use_left_switch = true;
187         robot.use_right_switch = 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 }