]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.cc
robofsm: fix problems when transfered from FSM to boost statechart
[eurobot/public.git] / src / robofsm / robot.cc
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 <pthread.h>
15 #include <robomath.h>
16 #include <robot.h>
17 #include <robot_orte.h>
18 #include <signal.h>
19 #include <sys/time.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include "map_handling.h"
23 #include <string.h>
24 #include "actuators.h"
25 #include <robodim.h>
26 #include <ul_log.h>
27 #include "guard.hpp"
28 #include "fsmmove.cc"
29
30 //UL_LOG_CUST(ulogd_robot); /* Log domain name = ulogd + name of the file */
31
32
33 #define MOTION_CONTROL_INIT_ONLY
34 #include "motion-control.h"
35
36 /* Global definition of robot structure */
37 Robot robot;
38
39 static void block_signals()
40 {
41         sigset_t sigset;
42         int i;
43
44         sigemptyset(&sigset);
45         for (i=SIGRTMIN; i<=SIGRTMAX; i++)
46                 sigaddset(&sigset, i);
47
48         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
49 }
50
51 static void int_handler(int sig)
52 {
53         robot.robot_exit();
54 }
55
56 void fill_in_known_areas_in_map()
57 {
58         /* Do not plan path close to edges */
59         //ShmapSetRectangleFlag(0.0, 0.0, 0.26, 2.1, MAP_FLAG_WALL, 0); /* left */
60         //ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.26, MAP_FLAG_WALL, 0); /* bottom */
61         //ShmapSetRectangleFlag(0.0, 1.84, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
62         //ShmapSetRectangleFlag(2.74, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */
63 }
64
65 void trans_callback()
66 {
67         if (robot.MAIN == robot.sched.getActualHandle()) {
68                 strncpy(robot.orte.fsm_main.state_name, robot.MAIN->state_name, sizeof(robot.orte.fsm_main.state_name));
69                 ORTEPublicationSend(robot.orte.publication_fsm_main);
70         } else if (robot.MOTION == robot.sched.getActualHandle()) {
71                 strncpy(robot.orte.fsm_motion.state_name, robot.MOTION->state_name, sizeof(robot.orte.fsm_motion.state_name));
72                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
73         }
74 }
75
76 /**
77  * Initializes the robot.
78  * Setup fields in robot structure, initializes FSMs and ORTE.
79  *
80  * @return 0
81  */
82 int Robot::init()
83 {
84         int rv;
85         pthread_mutexattr_t mattr;
86
87         rv = pthread_mutexattr_init(&mattr);
88 #ifdef HAVE_PRIO_INHERIT
89         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
90 #endif
91         pthread_mutex_init(&robot.lock, &mattr);
92         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
93         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
94         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
95         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
96         pthread_mutex_init(&robot.lock_joy_data, &mattr);
97         pthread_mutex_init(&robot.lock_disp, &mattr);
98
99         MOTION = sched.create_processor<FSMMotion>();
100
101         robot.team_color = BLUE;
102
103         set_est_pos_trans(1, 1, DEG2RAD(0));
104
105         robot.ignore_hokuyo = false;
106         robot.map = ShmapInit(1);
107         fill_in_known_areas_in_map();
108
109         signal(SIGINT, int_handler);
110         signal(SIGTERM, int_handler);
111         block_signals();
112
113         /* initial values */
114         robot.orte.motion_speed.left = 0xFFFF;
115         robot.orte.motion_speed.right = 0xFFFF;
116
117         robot.orte.pwr_ctrl.voltage33 = 1;
118         robot.orte.pwr_ctrl.voltage50 = 1;
119         robot.orte.pwr_ctrl.voltage80 = 1;
120
121         robot.orte.camera_control.on = false;
122
123
124         /* Only activate display if it is configured */
125         /* FIXME: display
126         robot.sercom = uoled_init(serial_comm);
127         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
128                 robot.fsm.display.state = &fsm_state_disp_init;
129         */
130
131         robot.obstacle_avoidance_enabled = true;
132         robot.use_rear_bumper = true;
133         robot.use_left_bumper = true;
134         robot.use_right_bumper = true;
135         robot.start_state = POWER_ON;
136         robot.check_turn_safety = true;
137
138         /* init ORTE domain, create publishers, subscribers, .. */
139         rv = robot_init_orte();
140         act_init(&robot.orte);
141
142         return rv;
143 }
144
145 /**
146  * Starts the robot FSMs and threads.
147  *
148  * @return 0
149  */
150 int Robot::start()
151 {
152         int rv = 0;
153
154         pthread_attr_t tattr;
155         struct sched_param param;
156         pthread_t thr_obstacle_forgeting;
157         int ret;
158
159         ret = motion_control_init();
160         if(ret) {
161                 perror("motion_control_init");
162                 robot_exit();
163         }
164
165
166         /* Obstacle forgeting thread */
167         pthread_attr_init (&tattr);
168         pthread_attr_getschedparam (&tattr, &param);
169         pthread_attr_getschedparam (&tattr, &param);
170         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
171         param.sched_priority = OBST_FORGETING_PRIO;
172         rv = pthread_attr_setschedparam (&tattr, &param);
173         if (rv) {
174                 perror("robot_start: pthread_attr_setschedparam()");
175                 goto err;
176         }
177         rv = pthread_create(&thr_obstacle_forgeting,
178                             &tattr, thread_obstacle_forgeting, NULL);
179         if (rv) {
180                 perror("robot_start: pthread_create");
181                 goto err;
182         }
183         sched();
184
185 err:
186         return rv;
187 }
188
189 /**
190  * Signals all the robot threads to finish.
191  */
192 void Robot::robot_exit()
193 {
194         sched.terminate();
195         /* stop FSMs */
196 }
197
198 /**
199  * Stops the robot. All resources alocated by robot_init() or
200  * robot_start() are dealocated here.
201  */
202 void Robot::destroy()
203 {
204         motion_control_done();
205
206         // FIXME: set actuators to well defined position (FJ)
207
208         robottype_roboorte_destroy(&robot.orte);
209
210         ShmapFree();
211         ul_logdeb("robofsm: stop.\n");
212 }
213
214 void Robot::get_est_pos_trans(double &x, double &y, double &phi)
215 {
216         get_est_pos(x, y, phi);
217         x = trans_x(x);
218         y = trans_y(y);
219         phi = trans_angle(phi);
220 }
221
222 void Robot::get_est_pos(double &x, double &y, double &phi)
223 {
224         if (robot.indep_odometry_works) {
225                 Guard g(robot.lock_est_pos_indep_odo);
226                 x = robot.est_pos_indep_odo.x;
227                 y = robot.est_pos_indep_odo.y;
228                 phi = robot.est_pos_indep_odo.phi;
229         } else if (robot.odometry_works) {
230                 Guard g(robot.lock_est_pos_odo);
231                 x = robot.est_pos_odo.x;
232                 y = robot.est_pos_odo.y;
233                 phi = robot.est_pos_odo.phi;
234         } else {
235                 Guard g(robot.lock_ref_pos);
236                 x = robot.ref_pos.x;
237                 y = robot.ref_pos.y;
238                 phi = robot.ref_pos.phi;
239         }
240 }
241
242 /** Sets actual position of the robot and with respoect to color of
243  * the team. Should be used for setting initial position of the
244  * robot. */
245 void Robot::set_est_pos(double x, double y, double phi) {
246         if (x<0) x=0;
247         if (x>PLAYGROUND_WIDTH_M) x=PLAYGROUND_WIDTH_M;
248         if (y<0) y=0;
249         if (y>PLAYGROUND_HEIGHT_M) y=PLAYGROUND_HEIGHT_M;
250
251         {
252                 Guard g(lock_est_pos_indep_odo);
253                 est_pos_indep_odo.x = x;
254                 est_pos_indep_odo.y = y;
255                 est_pos_indep_odo.phi = phi;
256         }
257         
258         {
259                 Guard g(lock_est_pos_odo);
260                 est_pos_odo.x = x;
261                 est_pos_odo.y = y;
262                 est_pos_odo.phi = phi;
263         }
264
265         {
266                 Guard g(lock_ref_pos);
267                 ref_pos.x = x;
268                 ref_pos.y = y;
269                 ref_pos.phi = phi;
270         }
271 }
272
273 void Robot::set_state_name(const char* name)
274 {
275         sched.getActualHandle()->state_name = name;
276         trans_callback();
277 }