]> 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 <uoled.h>
25 #include <string.h>
26 #include "actuators.h"
27
28 #ifdef ROBOT_DEBUG
29    #define DBG(format, ...) printf(format, ##__VA_ARGS__)
30 #else
31    #define DBG(format, ...)
32 #endif
33
34 #define MOTION_CONTROL_INIT_ONLY
35 #include "motion-control.h"
36
37 /* Global definition of robot structure */
38 struct robot robot;
39
40 #ifdef CONFIG_LOCK_CHECKING
41 struct lock_log robot_lock_log;
42 #endif
43
44 static void block_signals()
45 {
46         sigset_t sigset;
47         int i;
48
49         sigemptyset(&sigset);
50         for (i=SIGRTMIN; i<=SIGRTMAX; i++)
51                 sigaddset(&sigset, i);
52
53         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
54 }
55
56 static void int_handler(int sig)
57 {
58         robot_exit();
59 }
60
61 void fill_in_known_areas_in_map()
62 {
63         /* Do not plan path close to edges */
64         //ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); /* left */
65         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
66         //ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
67         //ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
68
69         /* Ignore other obstacles at edges */
70         //ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
71         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
72         //ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
73         //ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
74
75         ShmapSetRectangleFlag(0.74, 1.6, 2.259, 2.1, MAP_FLAG_WALL, 0); /* plateau, slopes */
76
77 }
78
79 static void trans_callback(struct robo_fsm *fsm)
80 {
81         if (fsm == &robot.fsm.main) {
82                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
83                 ORTEPublicationSend(robot.orte.publication_fsm_main);
84         } else if (fsm == &robot.fsm.motion) {
85                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
86                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
87         } else if (fsm == &robot.fsm.act) {
88                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
89                 ORTEPublicationSend(robot.orte.publication_fsm_act);
90         }
91         
92 }
93
94 /** 
95  * Initializes the robot.
96  * Setup fields in robot structure, initializes FSMs and ORTE.
97  * 
98  * @return 0
99  */
100 int robot_init()
101 {
102         int rv;
103         pthread_mutexattr_t mattr;
104
105         rv = pthread_mutexattr_init(&mattr);
106 #ifdef HAVE_PRIO_INHERIT
107         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
108 #endif
109         pthread_mutex_init(&robot.lock, &mattr);
110         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
111         pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
112         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
113         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
114         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
115         pthread_mutex_init(&robot.lock_joy_data, &mattr);
116         pthread_mutex_init(&robot.lock_disp, &mattr);
117
118         fsm_main_loop_init(&robot.main_loop);
119         
120         /* FSM initialization */
121         /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
122         fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
123         fsm_init(&robot.fsm.display, "display", &robot.main_loop);
124         fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
125         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
126         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
127         fsm_init(&robot.fsm.display, "\tdisp", &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                 printf("We are YELLOW!\n");
137         } else {
138                 printf("We are BLUE!\n");
139         }
140
141         robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
142         
143         robot.map = ShmapInit(1);
144         fill_in_known_areas_in_map();
145
146         signal(SIGINT, int_handler);
147         signal(SIGTERM, int_handler);
148         block_signals();
149
150         /* initial values */
151         robot.orte.motion_speed.left = 0;
152         robot.orte.motion_speed.right = 0;
153
154         robot.orte.pwr_ctrl.voltage33 = 1;
155         robot.orte.pwr_ctrl.voltage50 = 1;
156         robot.orte.pwr_ctrl.voltage80 = 1;
157
158         robot.orte.camera_control.on = true;
159        
160         robot.fsm.motion.state = &fsm_state_motion_init;
161
162         robot.fsm.act.state = &fsm_state_act_wait_for_command; // actuator FSM's initial state
163
164         /* Only activate display if it is configured */
165         /* FIXME: display
166         robot.sercom = uoled_init(serial_comm);
167         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
168                 robot.fsm.display.state = &fsm_state_disp_init;
169         */
170
171         robot.obstacle_avoidance_enabled = true;
172         robot.use_back_switch = false; /* Switched on sime time after start */
173         robot.state = POWER_ON;
174
175         /* init ORTE domain, create publishers, subscribers, .. */
176         rv = robot_init_orte();
177         act_init(&robot.orte);
178
179         return rv;
180 }
181
182 /** 
183  * Starts the robot FSMs and threads.
184  *
185  * @return 0
186  */
187 int robot_start()
188 {
189         int rv = 0;
190
191         pthread_attr_t tattr;
192         struct sched_param param;
193         pthread_t thr_obstacle_forgeting;
194         int ret;
195
196         ret = motion_control_init();
197         if(ret) {
198                 perror("motion_control_init");
199                 robot_exit();
200         }
201
202
203         /* Obstacle forgeting thread */
204         pthread_attr_init (&tattr);
205         pthread_attr_getschedparam (&tattr, &param);
206         pthread_attr_getschedparam (&tattr, &param);
207         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
208         param.sched_priority = OBST_FORGETING_PRIO;
209         rv = pthread_attr_setschedparam (&tattr, &param);
210         if (rv) {
211                 perror("robot_start: pthread_attr_setschedparam()");
212                 goto err;
213         }
214         rv = pthread_create(&thr_obstacle_forgeting, 
215                             &tattr, thread_obstacle_forgeting, NULL);
216         if (rv) {
217                 perror("robot_start: pthread_create");
218                 goto err;
219         }
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.display);
236         fsm_exit(&robot.fsm.act);
237 }
238
239 /** 
240  * Stops the robot. All resources alocated by robot_init() or
241  * robot_start() are dealocated here.
242  */
243 void robot_destroy()
244 {
245         motion_control_done();
246
247         // FIXME: set actuators to well defined position (FJ)
248         
249         robottype_roboorte_destroy(&robot.orte);
250
251         fsm_destroy(&robot.fsm.main);
252         fsm_destroy(&robot.fsm.motion);
253         fsm_destroy(&robot.fsm.display);
254         fsm_destroy(&robot.fsm.act);
255         ShmapFree();
256         DBG("robofsm: stop.\n");
257 }
258
259 void robot_get_est_pos(double *x, double *y, double *phi)
260 {
261         if (robot.indep_odometry_works) {
262                 ROBOT_LOCK(est_pos_indep_odo);
263                 *x = robot.est_pos_indep_odo.x;
264                 *y = robot.est_pos_indep_odo.y;
265                 *phi = robot.est_pos_indep_odo.phi;
266                 ROBOT_UNLOCK(est_pos_indep_odo);
267         } else if (robot.odometry_works) {
268                 ROBOT_LOCK(est_pos_odo);
269                 *x = robot.est_pos_odo.x;
270                 *y = robot.est_pos_odo.y;
271                 *phi = robot.est_pos_odo.phi;
272                 ROBOT_UNLOCK(est_pos_odo);
273         } else {
274                 ROBOT_LOCK(ref_pos);
275                 *x = robot.ref_pos.x;
276                 *y = robot.ref_pos.y;
277                 *phi = robot.ref_pos.phi;
278                 ROBOT_UNLOCK(ref_pos);
279         }
280 }