]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
Merge branch 'master' of rtime.felk.cvut.cz:/var/git/eurobot
[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
29 #ifdef ROBOT_DEBUG
30    #define DBG(format, ...) printf(format, ##__VA_ARGS__)
31 #else
32    #define DBG(format, ...)
33 #endif
34
35 #define MOTION_CONTROL_INIT_ONLY
36 #include "motion-control.h"
37
38 /* Global definition of robot structure */
39 struct robot robot;
40
41 #ifdef CONFIG_LOCK_CHECKING
42 struct lock_log robot_lock_log;
43 #endif
44
45 static void block_signals()
46 {
47         sigset_t sigset;
48         int i;
49
50         sigemptyset(&sigset);
51         for (i=SIGRTMIN; i<=SIGRTMAX; i++)
52                 sigaddset(&sigset, i);
53
54         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
55 }
56
57 static void int_handler(int sig)
58 {
59         robot_exit();
60 }
61
62 void fill_in_known_areas_in_map()
63 {
64         /* Do not plan path close to edges */
65         //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
66         ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
67         ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
68         //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
69
70         /* Container surroundings */
71         ShmapSetRectangleFlag(0.0, 0.0, 0.4, 0.2, 0, MAP_FLAG_WALL); /* left container */
72         ShmapSetRectangleFlag(3.0, 0.0, 2.6, 0.2, 0, MAP_FLAG_WALL); /* right container */
73
74         /* Ignore other obstacles at edges */
75         //ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
76         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
77         //ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
78         //ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
79
80         ShmapSetRectangleFlag(0.6, 1.45, 2.45, 2.1, 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
91 static void trans_callback(struct robo_fsm *fsm)
92 {
93         if (fsm == &robot.fsm.main) {
94                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
95                 ORTEPublicationSend(robot.orte.publication_fsm_main);
96         } else if (fsm == &robot.fsm.motion) {
97                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
98                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
99         } else if (fsm == &robot.fsm.act) {
100                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
101                 ORTEPublicationSend(robot.orte.publication_fsm_act);
102         }
103         
104 }
105
106 /** 
107  * Initializes the robot.
108  * Setup fields in robot structure, initializes FSMs and ORTE.
109  * 
110  * @return 0
111  */
112 int robot_init()
113 {
114         int rv;
115         pthread_mutexattr_t mattr;
116
117         rv = pthread_mutexattr_init(&mattr);
118 #ifdef HAVE_PRIO_INHERIT
119         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
120 #endif
121         pthread_mutex_init(&robot.lock, &mattr);
122         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
123         pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
124         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
125         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
126         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
127         pthread_mutex_init(&robot.lock_joy_data, &mattr);
128         pthread_mutex_init(&robot.lock_disp, &mattr);
129
130         fsm_main_loop_init(&robot.main_loop);
131         
132         /* FSM initialization */
133         /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
134         fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
135         fsm_init(&robot.fsm.display, "display", &robot.main_loop);
136         fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
137         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
138         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
139         fsm_init(&robot.fsm.display, "\tdisp", &robot.main_loop);
140         fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
141         robot.fsm.main.transition_callback = trans_callback;
142         robot.fsm.act.transition_callback = trans_callback;
143         robot.fsm.motion.transition_callback = trans_callback;
144
145         robot.team_color = BLUE;
146
147         if (robot.team_color == YELLOW) {
148                 printf("We are YELLOW!\n");
149         } else {
150                 printf("We are BLUE!\n");
151         }
152
153         robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
154         
155         robot.ignore_hokuyo = false;
156         robot.map = ShmapInit(1);
157         fill_in_known_areas_in_map();
158
159         signal(SIGINT, int_handler);
160         signal(SIGTERM, int_handler);
161         block_signals();
162
163         /* initial values */
164         robot.orte.motion_speed.left = 0;
165         robot.orte.motion_speed.right = 0;
166
167         robot.orte.pwr_ctrl.voltage33 = 1;
168         robot.orte.pwr_ctrl.voltage50 = 1;
169         robot.orte.pwr_ctrl.voltage80 = 1;
170
171         robot.orte.camera_control.on = true;
172        
173         robot.fsm.motion.state = &fsm_state_motion_init;
174
175         robot.fsm.act.state = &fsm_state_act_wait_for_command; // actuator FSM's initial state
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 = false; /* Switched on sime time after start */
186         robot.state = POWER_ON;
187
188         /* init ORTE domain, create publishers, subscribers, .. */
189         rv = robot_init_orte();
190         act_init(&robot.orte);
191
192         return rv;
193 }
194
195 /** 
196  * Starts the robot FSMs and threads.
197  *
198  * @return 0
199  */
200 int robot_start()
201 {
202         int rv = 0;
203
204         pthread_attr_t tattr;
205         struct sched_param param;
206         pthread_t thr_obstacle_forgeting;
207         int ret;
208
209         ret = motion_control_init();
210         if(ret) {
211                 perror("motion_control_init");
212                 robot_exit();
213         }
214
215
216         /* Obstacle forgeting thread */
217         pthread_attr_init (&tattr);
218         pthread_attr_getschedparam (&tattr, &param);
219         pthread_attr_getschedparam (&tattr, &param);
220         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
221         param.sched_priority = OBST_FORGETING_PRIO;
222         rv = pthread_attr_setschedparam (&tattr, &param);
223         if (rv) {
224                 perror("robot_start: pthread_attr_setschedparam()");
225                 goto err;
226         }
227         rv = pthread_create(&thr_obstacle_forgeting, 
228                             &tattr, thread_obstacle_forgeting, NULL);
229         if (rv) {
230                 perror("robot_start: pthread_create");
231                 goto err;
232         }
233
234         fsm_main_loop(&robot.main_loop);
235
236 err:
237         return rv;
238 }
239
240 /** 
241  * Signals all the robot threads to finish.
242  */
243 void robot_exit()
244 {
245         /* stop FSMs */
246         fsm_exit(&robot.fsm.main);
247         fsm_exit(&robot.fsm.motion);
248         fsm_exit(&robot.fsm.display);
249         fsm_exit(&robot.fsm.act);
250 }
251
252 /** 
253  * Stops the robot. All resources alocated by robot_init() or
254  * robot_start() are dealocated here.
255  */
256 void robot_destroy()
257 {
258         motion_control_done();
259
260         // FIXME: set actuators to well defined position (FJ)
261         
262         robottype_roboorte_destroy(&robot.orte);
263
264         fsm_destroy(&robot.fsm.main);
265         fsm_destroy(&robot.fsm.motion);
266         fsm_destroy(&robot.fsm.display);
267         fsm_destroy(&robot.fsm.act);
268         ShmapFree();
269         DBG("robofsm: stop.\n");
270 }
271
272 void robot_get_est_pos(double *x, double *y, double *phi)
273 {
274         if (robot.indep_odometry_works) {
275                 ROBOT_LOCK(est_pos_indep_odo);
276                 *x = robot.est_pos_indep_odo.x;
277                 *y = robot.est_pos_indep_odo.y;
278                 *phi = robot.est_pos_indep_odo.phi;
279                 ROBOT_UNLOCK(est_pos_indep_odo);
280         } else if (robot.odometry_works) {
281                 ROBOT_LOCK(est_pos_odo);
282                 *x = robot.est_pos_odo.x;
283                 *y = robot.est_pos_odo.y;
284                 *phi = robot.est_pos_odo.phi;
285                 ROBOT_UNLOCK(est_pos_odo);
286         } else {
287                 ROBOT_LOCK(ref_pos);
288                 *x = robot.ref_pos.x;
289                 *y = robot.ref_pos.y;
290                 *phi = robot.ref_pos.phi;
291                 ROBOT_UNLOCK(ref_pos);
292         }
293 }