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