]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
Merge branch 'master' of /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
27 #ifdef ROBOT_DEBUG
28    #define DBG(format, ...) printf(format, ##__VA_ARGS__)
29 #else
30    #define DBG(format, ...)
31 #endif
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.199, 2.1, MAP_FLAG_WALL, 0); /* left */
64         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
65         //ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
66         //ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
67
68         /* Ignore other obstacles at edges */
69         //ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
70         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
71         //ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
72         //ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
73
74         ShmapSetRectangleFlag(0.74, 1.6, 2.259, 2.1, MAP_FLAG_WALL, 0); /* plateau, slopes */
75
76 }
77
78 static void trans_callback(struct robo_fsm *fsm)
79 {
80         if (fsm == &robot.fsm.main) {
81                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
82                 ORTEPublicationSend(robot.orte.publication_fsm_main);
83         } else if (fsm == &robot.fsm.motion) {
84                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
85                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
86         } else if (fsm == &robot.fsm.act) {
87                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
88                 ORTEPublicationSend(robot.orte.publication_fsm_act);
89         }
90         
91 }
92
93 /** 
94  * Initializes the robot.
95  * Setup fields in robot structure, initializes FSMs and ORTE.
96  * 
97  * @return 0
98  */
99 int robot_init()
100 {
101         int rv;
102         pthread_mutexattr_t mattr;
103
104         rv = pthread_mutexattr_init(&mattr);
105 #ifdef HAVE_PRIO_INHERIT
106         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
107 #endif
108         pthread_mutex_init(&robot.lock, &mattr);
109         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
110         pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
111         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
112         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
113         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
114         pthread_mutex_init(&robot.lock_joy_data, &mattr);
115         pthread_mutex_init(&robot.lock_disp, &mattr);
116
117         fsm_main_loop_init(&robot.main_loop);
118         
119         /* FSM initialization */
120         /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
121         fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
122         fsm_init(&robot.fsm.display, "display", &robot.main_loop);
123         fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
124         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
125         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
126         fsm_init(&robot.fsm.display, "\tdisp", &robot.main_loop);
127         fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
128         robot.fsm.main.transition_callback = trans_callback;
129         robot.fsm.act.transition_callback = trans_callback;
130         robot.fsm.motion.transition_callback = trans_callback;
131
132         robot.team_color = BLUE;
133
134         if (robot.team_color == YELLOW) {
135                 printf("We are YELLOW!\n");
136         } else {
137                 printf("We are BLUE!\n");
138         }
139
140         robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
141         
142         robot.map = ShmapInit(1);
143         fill_in_known_areas_in_map();
144
145         signal(SIGINT, int_handler);
146         signal(SIGTERM, int_handler);
147         block_signals();
148
149         /* initial values */
150         robot.orte.motion_speed.left = 0;
151         robot.orte.motion_speed.right = 0;
152
153         robot.orte.pwr_ctrl.voltage33 = 1;
154         robot.orte.pwr_ctrl.voltage50 = 1;
155         robot.orte.pwr_ctrl.voltage80 = 1;
156
157         robot.orte.camera_control.on = true;
158        
159         robot.fsm.motion.state = &fsm_state_motion_init;
160
161         robot.fsm.act.state = &fsm_state_act_wait_for_command; // actuator FSM's initial state
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 = false; /* Switched on sime time after start */
172         robot.state = POWER_ON;
173
174         /* init ORTE domain, create publishers, subscribers, .. */
175         rv = robot_init_orte();
176         act_init(&robot.orte);
177
178         return rv;
179 }
180
181 /** 
182  * Starts the robot FSMs and threads.
183  *
184  * @return 0
185  */
186 int robot_start()
187 {
188         int rv = 0;
189
190         pthread_attr_t tattr;
191         struct sched_param param;
192         pthread_t thr_obstacle_forgeting;
193         int ret;
194
195         ret = motion_control_init();
196         if(ret) {
197                 perror("motion_control_init");
198                 robot_exit();
199         }
200
201
202         /* Obstacle forgeting thread */
203         pthread_attr_init (&tattr);
204         pthread_attr_getschedparam (&tattr, &param);
205         pthread_attr_getschedparam (&tattr, &param);
206         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
207         param.sched_priority = OBST_FORGETING_PRIO;
208         rv = pthread_attr_setschedparam (&tattr, &param);
209         if (rv) {
210                 perror("robot_start: pthread_attr_setschedparam()");
211                 goto err;
212         }
213         rv = pthread_create(&thr_obstacle_forgeting, 
214                             &tattr, thread_obstacle_forgeting, NULL);
215         if (rv) {
216                 perror("robot_start: pthread_create");
217                 goto err;
218         }
219
220         fsm_main_loop(&robot.main_loop);
221
222 err:
223         return rv;
224 }
225
226 /** 
227  * Signals all the robot threads to finish.
228  */
229 void robot_exit()
230 {
231         /* stop FSMs */
232         fsm_exit(&robot.fsm.main);
233         fsm_exit(&robot.fsm.motion);
234         fsm_exit(&robot.fsm.display);
235         fsm_exit(&robot.fsm.act);
236 }
237
238 /** 
239  * Stops the robot. All resources alocated by robot_init() or
240  * robot_start() are dealocated here.
241  */
242 void robot_destroy()
243 {
244         motion_control_done();
245
246         // FIXME: set actuators to well defined position (FJ)
247         
248         robottype_roboorte_destroy(&robot.orte);
249
250         fsm_destroy(&robot.fsm.main);
251         fsm_destroy(&robot.fsm.motion);
252         fsm_destroy(&robot.fsm.display);
253         fsm_destroy(&robot.fsm.act);
254         ShmapFree();
255         DBG("robofsm: stop.\n");
256 }
257
258 void robot_get_est_pos(double *x, double *y, double *phi)
259 {
260         if (robot.indep_odometry_works) {
261                 ROBOT_LOCK(est_pos_indep_odo);
262                 *x = robot.est_pos_indep_odo.x;
263                 *y = robot.est_pos_indep_odo.y;
264                 *phi = robot.est_pos_indep_odo.phi;
265                 ROBOT_UNLOCK(est_pos_indep_odo);
266         } else if (robot.odometry_works) {
267                 ROBOT_LOCK(est_pos_odo);
268                 *x = robot.est_pos_odo.x;
269                 *y = robot.est_pos_odo.y;
270                 *phi = robot.est_pos_odo.phi;
271                 ROBOT_UNLOCK(est_pos_odo);
272         } else {
273                 ROBOT_LOCK(ref_pos);
274                 *x = robot.ref_pos.x;
275                 *y = robot.ref_pos.y;
276                 *phi = robot.ref_pos.phi;
277                 ROBOT_UNLOCK(ref_pos);
278         }
279 }