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