]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.cc
robofsm: Robot is now class with functions
[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 static void trans_callback(struct robo_fsm *fsm)
66 {
67 /*      if (fsm == &robot.fsm.main) {
68                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
69                 ORTEPublicationSend(robot.orte.publication_fsm_main);
70         } else if (fsm == &robot.fsm.motion) {
71                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
72                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
73         } else if (fsm == &robot.fsm.act) {
74                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
75                 ORTEPublicationSend(robot.orte.publication_fsm_act);
76         }
77 */
78 }
79
80 /**
81  * Initializes the robot.
82  * Setup fields in robot structure, initializes FSMs and ORTE.
83  *
84  * @return 0
85  */
86 int Robot::init()
87 {
88         int rv;
89         pthread_mutexattr_t mattr;
90
91         rv = pthread_mutexattr_init(&mattr);
92 #ifdef HAVE_PRIO_INHERIT
93         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
94 #endif
95         pthread_mutex_init(&robot.lock, &mattr);
96         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
97         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
98         pthread_mutex_init(&robot.lock_est_pos_indep_odo, &mattr);
99         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
100         pthread_mutex_init(&robot.lock_joy_data, &mattr);
101         pthread_mutex_init(&robot.lock_disp, &mattr);
102
103         MOTION = sched.create_processor<FSMMotion>();
104
105         robot.team_color = BLUE;
106
107         set_est_pos_trans(1, 1, DEG2RAD(0));
108
109         robot.ignore_hokuyo = false;
110         robot.map = ShmapInit(1);
111         fill_in_known_areas_in_map();
112
113         signal(SIGINT, int_handler);
114         signal(SIGTERM, int_handler);
115         block_signals();
116
117         /* initial values */
118         robot.orte.motion_speed.left = 0xFFFF;
119         robot.orte.motion_speed.right = 0xFFFF;
120
121         robot.orte.pwr_ctrl.voltage33 = 1;
122         robot.orte.pwr_ctrl.voltage50 = 1;
123         robot.orte.pwr_ctrl.voltage80 = 1;
124
125         robot.orte.camera_control.on = false;
126
127
128         /* Only activate display if it is configured */
129         /* FIXME: display
130         robot.sercom = uoled_init(serial_comm);
131         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
132                 robot.fsm.display.state = &fsm_state_disp_init;
133         */
134
135         robot.obstacle_avoidance_enabled = true;
136         robot.use_rear_bumper = true;
137         robot.use_left_bumper = true;
138         robot.use_right_bumper = true;
139         robot.start_state = POWER_ON;
140         robot.check_turn_safety = true;
141
142         /* init ORTE domain, create publishers, subscribers, .. */
143         rv = robot_init_orte();
144         act_init(&robot.orte);
145
146         return rv;
147 }
148
149 /**
150  * Starts the robot FSMs and threads.
151  *
152  * @return 0
153  */
154 int Robot::start()
155 {
156         int rv = 0;
157
158         pthread_attr_t tattr;
159         struct sched_param param;
160         pthread_t thr_obstacle_forgeting;
161         int ret;
162
163         ret = motion_control_init();
164         if(ret) {
165                 perror("motion_control_init");
166                 robot_exit();
167         }
168
169
170         /* Obstacle forgeting thread */
171         pthread_attr_init (&tattr);
172         pthread_attr_getschedparam (&tattr, &param);
173         pthread_attr_getschedparam (&tattr, &param);
174         pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
175         param.sched_priority = OBST_FORGETING_PRIO;
176         rv = pthread_attr_setschedparam (&tattr, &param);
177         if (rv) {
178                 perror("robot_start: pthread_attr_setschedparam()");
179                 goto err;
180         }
181         rv = pthread_create(&thr_obstacle_forgeting,
182                             &tattr, thread_obstacle_forgeting, NULL);
183         if (rv) {
184                 perror("robot_start: pthread_create");
185                 goto err;
186         }
187
188
189 err:
190         return rv;
191 }
192
193 /**
194  * Signals all the robot threads to finish.
195  */
196 void robot_exit()
197 {
198         /* stop FSMs */
199 }
200
201 /**
202  * Stops the robot. All resources alocated by robot_init() or
203  * robot_start() are dealocated here.
204  */
205 void Robot::destroy()
206 {
207         motion_control_done();
208
209         // FIXME: set actuators to well defined position (FJ)
210
211         robottype_roboorte_destroy(&robot.orte);
212
213         ShmapFree();
214         ul_logdeb("robofsm: stop.\n");
215 }
216
217 void Robot::get_est_pos_trans(double &x, double &y, double &phi)
218 {
219         get_est_pos(x, y, phi);
220         x = trans_x(x);
221         y = trans_y(y);
222         phi = trans_angle(phi);
223 }
224
225 void Robot::get_est_pos(double &x, double &y, double &phi)
226 {
227         if (robot.indep_odometry_works) {
228                 Guard g(robot.lock_est_pos_indep_odo);
229                 x = robot.est_pos_indep_odo.x;
230                 y = robot.est_pos_indep_odo.y;
231                 phi = robot.est_pos_indep_odo.phi;
232         } else if (robot.odometry_works) {
233                 Guard g(robot.lock_est_pos_odo);
234                 x = robot.est_pos_odo.x;
235                 y = robot.est_pos_odo.y;
236                 phi = robot.est_pos_odo.phi;
237         } else {
238                 Guard g(robot.lock_ref_pos);
239                 x = robot.ref_pos.x;
240                 y = robot.ref_pos.y;
241                 phi = robot.ref_pos.phi;
242         }
243 }
244
245 /** Sets actual position of the robot and with respoect to color of
246  * the team. Should be used for setting initial position of the
247  * robot. */
248 void Robot::set_est_pos(double x, double y, double phi) {
249         if (x<0) x=0;
250         if (x>PLAYGROUND_WIDTH_M) x=PLAYGROUND_WIDTH_M;
251         if (y<0) y=0;
252         if (y>PLAYGROUND_HEIGHT_M) y=PLAYGROUND_HEIGHT_M;
253
254         {
255                 Guard g(lock_est_pos_indep_odo);
256                 est_pos_indep_odo.x = x;
257                 est_pos_indep_odo.y = y;
258                 est_pos_indep_odo.phi = phi;
259         }
260         
261         {
262                 Guard g(lock_est_pos_odo);
263                 est_pos_odo.x = x;
264                 est_pos_odo.y = y;
265                 est_pos_odo.phi = phi;
266         }
267
268         {
269                 Guard g(lock_ref_pos);
270                 ref_pos.x = x;
271                 ref_pos.y = y;
272                 ref_pos.phi = phi;
273         }
274 }