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