]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
225744044b9a871145c1fe31f74e0ff515ba0b55
[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); */
65 /*      ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
66 /*      ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
67 /*      ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
68
69         //ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
70         //ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
71
72         //ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
73         //ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
74
75         /* Ignore other obstacles at edges */
76         ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
77         ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
78         ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
79         ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
80
81         //ShmapSetRectangleFlag(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
82         //ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
83
84         /* Construction areas (Building zones) */
85         //ShmapSetRectangleFlag(ACROPOLIS_CENTER_X - ACROPOLIS_RADIUS, ACROPOLIS_CENTER_Y - ACROPOLIS_RADIUS, ACROPOLIS_CENTER_X + ACROPOLIS_RADIUS, ACROPOLIS_CENTER_Y + ACROPOLIS_RADIUS, MAP_FLAG_WALL, 0);           /* Central construction area */
86         //ShmapSetRectangleFlag(1.35, 0.9, 1.65, 1.2, MAP_FLAG_WALL, 0);                 /* Central construction area */
87
88         // Enclosure wall
89         ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); /* left */
90         ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); /* bottom */
91         ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); /* top */
92         ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); /* right */      
93         // Bottom-middle unloading area
94         ShmapSetRectangleFlag(1.0, 0.0, 2.0, 0.3, MAP_FLAG_WALL, 0); /* bottom */
95         // Unloading area - dwarf wall
96         ShmapSetRectangleFlag(0.4, 0.0, 0.8, 0.3, MAP_FLAG_WALL, 0); 
97         ShmapSetRectangleFlag(2.2, 0.0, 2.6, 0.3, MAP_FLAG_WALL, 0); 
98         // Acropolis wall
99         ShmapSetCircleFlag(ACROPOLIS_CENTER_X, ACROPOLIS_CENTER_Y, ACROPOLIS_DIAMETER + 0.09, MAP_FLAG_WALL, 0); 
100
101 }
102
103 static void trans_callback(struct robo_fsm *fsm)
104 {
105         if (fsm == &robot.fsm.main) {
106                 strncpy(robot.orte.fsm_main.state_name, fsm->state_name, sizeof(robot.orte.fsm_main.state_name));
107                 ORTEPublicationSend(robot.orte.publication_fsm_main);
108         } else if (fsm == &robot.fsm.motion) {
109                 strncpy(robot.orte.fsm_motion.state_name, fsm->state_name, sizeof(robot.orte.fsm_motion.state_name));
110                 ORTEPublicationSend(robot.orte.publication_fsm_motion);
111         } else if (fsm == &robot.fsm.act) {
112                 strncpy(robot.orte.fsm_act.state_name, fsm->state_name, sizeof(robot.orte.fsm_act.state_name));
113                 ORTEPublicationSend(robot.orte.publication_fsm_act);
114         }
115         
116 }
117
118 /** 
119  * Initializes the robot.
120  * Setup fields in robot structure, initializes FSMs and ORTE.
121  * 
122  * @return 0
123  */
124 int robot_init()
125 {
126         int rv;
127         pthread_mutexattr_t mattr;
128
129         rv = pthread_mutexattr_init(&mattr);
130 #ifdef HAVE_PRIO_INHERIT
131         rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
132 #endif
133         pthread_mutex_init(&robot.lock, &mattr);
134         pthread_mutex_init(&robot.lock_ref_pos, &mattr);
135         pthread_mutex_init(&robot.lock_est_pos_uzv, &mattr);
136         pthread_mutex_init(&robot.lock_est_pos_odo, &mattr);
137         pthread_mutex_init(&robot.lock_meas_angles, &mattr);
138         pthread_mutex_init(&robot.lock_joy_data, &mattr);
139         pthread_mutex_init(&robot.lock_disp, &mattr);
140
141         fsm_main_loop_init(&robot.main_loop);
142         
143         /* FSM initialization */
144         /* fsm_init(&robot.fsm.main, "main", &robot.main_loop);
145         fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
146         fsm_init(&robot.fsm.display, "display", &robot.main_loop);
147         fsm_init(&robot.fsm.act, "actuators", &robot.main_loop); */
148         fsm_init(&robot.fsm.main, "MAIN", &robot.main_loop);
149         fsm_init(&robot.fsm.motion, "\tmot", &robot.main_loop);
150         fsm_init(&robot.fsm.display, "\tdisp", &robot.main_loop);
151         fsm_init(&robot.fsm.act, "\tACT", &robot.main_loop);
152         robot.fsm.main.transition_callback = trans_callback;
153         robot.fsm.act.transition_callback = trans_callback;
154         robot.fsm.motion.transition_callback = trans_callback;
155
156         robot.team_color = GREEN;
157
158         if (robot.team_color == RED) {
159                 printf("We are RED!\n");
160         } else {
161                 printf("We are GREEN!\n");
162         }
163
164         robot_set_est_pos_trans(0.16, PLAYGROUND_HEIGHT_M - 0.16, DEG2RAD(-45));
165         
166         robot.map = ShmapInit(1);
167         fill_in_known_areas_in_map();
168
169         signal(SIGINT, int_handler);
170         signal(SIGTERM, int_handler);
171         block_signals();
172
173         /* initial values */
174         robot.orte.motion_speed.left = 0;
175         robot.orte.motion_speed.right = 0;
176
177         robot.orte.pwr_ctrl.voltage33 = 1;
178         robot.orte.pwr_ctrl.voltage50 = 1;
179         robot.orte.pwr_ctrl.voltage80 = 1;
180
181         robot.orte.camera_control.on = true;
182        
183         robot.fsm.motion.state = &fsm_state_motion_init;
184
185         robot.fsm.act.state = &fsm_state_act_wait_for_command; // puck handling actuators FSM's initial state
186
187         /* Only activate display if it is configured */
188         robot.sercom = uoled_init(serial_comm);
189         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
190                 robot.fsm.display.state = &fsm_state_disp_init;
191
192         robot.obstacle_avoidance_enabled = true;
193         robot.use_back_switch = false; /* Switched on sime time after start */
194         robot.state = POWER_ON;
195
196         /* init ORTE domain, create publishers, subscribers, .. */
197         rv = robot_init_orte();
198         act_init(&robot.orte);
199
200         return rv;
201 }
202
203 /** 
204  * Starts the robot FSMs and threads.
205  *
206  * @return 0
207  */
208 int robot_start()
209 {
210         int rv = 0;
211
212         pthread_attr_t tattr;
213         struct sched_param param;
214         pthread_t thr_obstacle_forgeting;
215         int ret;
216
217         ret = motion_control_init();
218         if(ret) {
219                 perror("motion_control_init");
220                 robot_exit();
221         }
222
223
224         /* Obstacle forgeting thread */
225         pthread_attr_init (&tattr);
226         pthread_attr_getschedparam (&tattr, &param);
227         param.sched_priority = OBST_FORGETING_PRIO;
228         rv = pthread_attr_setschedparam (&tattr, &param);
229         if (rv) {
230                 perror("robot_start: pthread_attr_setschedparam()");
231                 goto err;
232         }
233         rv = pthread_create(&thr_obstacle_forgeting, 
234                             &tattr, thread_obstacle_forgeting, NULL);
235         if (rv) {
236                 perror("robot_start: pthread_create");
237                 goto err;
238         }
239
240         fsm_main_loop(&robot.main_loop);
241
242 err:
243         return rv;
244 }
245
246 /** 
247  * Signals all the robot threads to finish.
248  */
249 void robot_exit()
250 {
251         /* stop FSMs */
252         fsm_exit(&robot.fsm.main);
253         fsm_exit(&robot.fsm.motion);
254         fsm_exit(&robot.fsm.display);
255         fsm_exit(&robot.fsm.act);
256 }
257
258 /** 
259  * Stops the robot. All resources alocated by robot_init() or
260  * robot_start() are dealocated here.
261  */
262 void robot_destroy()
263 {
264         motion_control_done();
265
266         act_chelae(CHELA_OPEN, CHELA_OPEN);
267         act_holder(HOLDER_OPENED);
268         
269         robottype_roboorte_destroy(&robot.orte);
270
271         fsm_destroy(&robot.fsm.main);
272         fsm_destroy(&robot.fsm.motion);
273         fsm_destroy(&robot.fsm.display);
274         fsm_destroy(&robot.fsm.act);
275         ShmapFree();
276         DBG("robofsm: stop.\n");
277 }