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