]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/common-states.cc
9296065f000583eeb90a6c7bc22fcee0ae983324
[eurobot/public.git] / src / robofsm / common-states.cc
1 #define FSM_MAIN
2 #include <robodata.h>
3 #include <robot.h>
4 #include <fsm.h>
5 #include <unistd.h>
6 #include <math.h>
7 #include <movehelper.h>
8 #include <map.h>
9 #include <sharp.h>
10 #include <robomath.h>
11 #include <string.h>
12 #include <robodim.h>
13 #include <error.h>
14 #include "corns_configs.h"
15 #include "actuators.h"
16 #include <trgen.h>
17 #include "match-timing.h"
18 #include "eb2010misc.h"
19 #include <stdbool.h>
20
21 #include "common-states.h"
22
23 /************************************************************************
24  * Functions used in and called from all the (almost identical)
25  * "wait for start" states in particular strategies.
26  ************************************************************************/
27
28 #undef DBG_FSM_STATE
29 #define DBG_FSM_STATE(name)     do { if (fsm->debug_states) printf("fsm %s %.1f: %s(%s)\n", \
30                                                                    fsm->debug_name, robot_current_time(), \
31                                                                    name, fsm_event_str(fsm->events[fsm->ev_head])); } while(0)
32
33
34 static void set_initial_position()
35 {
36         robot_set_est_pos_trans(ROBOT_AXIS_TO_FRONT_M,
37                                 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2),
38                                 DEG2RAD(180));
39 }
40
41 static void actuators_home()
42 {
43         static int tmp = 0;
44         act_vidle(VIDLE_UP - tmp, VIDLE_FAST_SPEED);
45         tmp = 1 - tmp;          // Force movement (we need to change the target position)
46 }
47
48 void start_entry()
49 {
50         pthread_t thid;
51         robot.check_turn_safety = false;
52         pthread_create(&thid, NULL, timing_thread, NULL);
53         start_timer();
54 }
55
56 // We set initial position periodically in order for it to be updated
57 // on the display if the team color is changed during waiting for
58 // start.
59 void start_timer()
60 {
61         set_initial_position();
62         if (robot.start_state == START_PLUGGED_IN)
63                 actuators_home();
64 }
65
66 void start_go()
67 {
68         sem_post(&robot.start);
69         actuators_home();
70         set_initial_position();
71 }
72
73 void start_exit()
74 {
75         robot.corns = get_all_corns(robot.corns_conf_side, robot.corns_conf_center);
76         act_camera_off();
77 }
78
79 /************************************************************************
80  * Trajectory constraints used; They are  initialized in the main() function in competition.cc
81  ************************************************************************/
82
83 struct TrajectoryConstraints tcFast, tcSlow, tcVerySlow;
84
85 #define VIDLE_TIMEOUT 2000
86
87 /************************************************************************
88  * States that form the "collect some oranges" subautomaton. Calling automaton
89  * SHOULD ALWAYS call the "approach_the_slope" state.
90  ************************************************************************/
91
92 bool inline is_ready_to_climb_the_slope(enum which_side which_slope, double x, double y) {
93         bool ret;
94         if (which_slope == MINE) {
95                 ret = x < 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
96         } else if (which_slope == OPPONENTS) {
97                 ret = x > 0.5 && y > PLAYGROUND_HEIGHT_M - 0.5;
98         } else {
99                 printf("ERROR: unknown side;");
100 #warning Remove the next line
101                 robot_exit();
102         }
103         return ret;
104 }
105
106 static struct slope_approach_style *slope_approach_style_p;
107
108 /* assures that the robot is near the slope rim; if outside our starting area, approach the slope first */
109 FSM_STATE(approach_the_slope)
110 {
111         switch(FSM_EVENT) {
112                 case EV_ENTRY: {
113                                 slope_approach_style_p = (struct slope_approach_style *) FSM_EVENT_PTR;
114                                 if (slope_approach_style_p == NULL) {
115                                         printf("\n\nit is not allowed to call the approach_the_slope state  with NULL data!!\n\n");
116 #warning remove the next line
117                                         robot_exit();
118                                 }
119                                 double x, y, phi;
120                                 robot_get_est_pos_trans(&x, &y, &phi);
121                                 /* decide */
122                                 bool ready_to_climb_the_slope = is_ready_to_climb_the_slope(slope_approach_style_p->which_side, x, y);
123                                 /* if necessary, approach the slope */
124                                 if (ready_to_climb_the_slope) {
125                                         FSM_TRANSITION(climb_the_slope);
126                                 } else {
127                                         robot_goto_trans(
128                                                 x_coord(0.3, slope_approach_style_p->which_side),
129                                                 PLAYGROUND_HEIGHT_M - ROBOT_WIDTH_M/2 - 0.03,
130                                                 ARRIVE_FROM(DEG2RAD(0), 0.02),
131                                                 &tcFast);
132                                 }
133                                 break;
134                         }
135                 case EV_MOTION_DONE:
136                         FSM_TRANSITION(climb_the_slope);
137                         break;
138                 case EV_START:
139                 case EV_TIMER:
140                 case EV_RETURN:
141                 case EV_VIDLE_DONE:
142                 case EV_MOTION_ERROR:
143                 case EV_SWITCH_STRATEGY:
144                         DBG_PRINT_EVENT("unhandled event");
145                 case EV_EXIT:
146                         break;
147         }
148 }
149
150 void inline enable_switches(bool enabled)
151 {
152         robot.use_left_switch = enabled;
153         robot.use_right_switch = enabled;
154         robot.use_back_switch = enabled;
155 }
156
157 FSM_STATE(climb_the_slope)
158 {
159         struct TrajectoryConstraints tc;
160         switch(FSM_EVENT) {
161                 case EV_ENTRY: {
162                                 // disables using side switches on bumpers when going up
163                                 enable_switches(false);
164                                 robot.ignore_hokuyo = true;
165                                 /* create the trajectory and go */
166                                 tc = tcSlow;
167                                 tc.maxacc = 0.4;
168                                 robot_trajectory_new_backward(&tc);
169                                 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
170                                         act_vidle(VIDLE_LOAD_PREPARE, 5);
171                                         robot_trajectory_add_point_trans(
172                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
173                                                 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
174                                         robot_trajectory_add_final_point_trans(
175                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
176                                                 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
177                                                 NO_TURN());
178                                 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
179                                         FSM_TIMER(3500);
180                                         robot_trajectory_add_point_trans(
181                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
182                                                 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85));
183                                         robot_trajectory_add_final_point_trans(
184                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
185                                                 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85),
186                                                 NO_TURN());
187                                 }
188                                 break;
189                         }
190                 case EV_MOTION_DONE:
191                         SUBFSM_TRANSITION(load_oranges, NULL);
192                         break;
193                 case EV_RETURN:
194                         FSM_TRANSITION(sledge_down);
195                         break;
196                 case EV_TIMER:
197                         act_vidle(VIDLE_LOAD_PREPARE, 10);
198                         break;
199                 case EV_START:
200                 case EV_VIDLE_DONE:
201                 case EV_MOTION_ERROR:
202                 case EV_SWITCH_STRATEGY:
203                         DBG_PRINT_EVENT("unhandled event");
204                 case EV_EXIT:
205                         break;
206         }
207 }
208
209 /* subautomaton to load oranges in two stages */
210 FSM_STATE_DECL(load_oranges2);
211 FSM_STATE_DECL(load_oranges3);
212 FSM_STATE(load_oranges)
213 {
214         switch(FSM_EVENT) {
215                 case EV_ENTRY:
216                         FSM_TIMER(1000);
217                         act_vidle(VIDLE_MIDDLE, VIDLE_MEDIUM_SPEED);
218                         break;
219                 case EV_VIDLE_DONE:
220                         FSM_TIMER(1000);
221                         break;
222                 case EV_TIMER:
223                         FSM_TRANSITION(load_oranges2);
224                         break;
225                 case EV_MOTION_DONE:
226                 case EV_START:
227                 case EV_RETURN:
228                 case EV_MOTION_ERROR:
229                 case EV_SWITCH_STRATEGY:
230                         DBG_PRINT_EVENT("unhandled event");
231                 case EV_EXIT:
232                         break;
233         }
234 }
235
236 FSM_STATE(load_oranges2)
237 {
238         switch(FSM_EVENT) {
239                 case EV_ENTRY:
240                         act_vidle(VIDLE_UP, VIDLE_MEDIUM_SPEED);
241                         FSM_TIMER(1000);
242                         break;
243                 case EV_VIDLE_DONE:
244                         FSM_TRANSITION(load_oranges3);
245                         //SUBFSM_RET(NULL);
246                         break;
247                 case EV_TIMER:
248                         FSM_TRANSITION(load_oranges3);
249                         //SUBFSM_RET(NULL);
250                         break;
251                 case EV_MOTION_DONE:
252                 case EV_START:
253                 case EV_RETURN:
254                 case EV_MOTION_ERROR:
255                 case EV_SWITCH_STRATEGY:
256                         DBG_PRINT_EVENT("unhandled event");
257                 case EV_EXIT:
258                         act_vidle(VIDLE_UP-1, VIDLE_FAST_SPEED);
259                         break;
260         }
261 }
262
263 FSM_STATE(load_oranges3)
264 {
265         switch(FSM_EVENT) {
266                 case EV_ENTRY:
267                         act_vidle(VIDLE_MIDDLE+50, 0);
268                         FSM_TIMER(500);
269                         break;
270                 case EV_VIDLE_DONE:
271                         SUBFSM_RET(NULL);
272                         break;
273                 case EV_TIMER:
274                         SUBFSM_RET(NULL);
275                         break;
276                 case EV_MOTION_DONE:
277                 case EV_START:
278                 case EV_RETURN:
279                 case EV_MOTION_ERROR:
280                 case EV_SWITCH_STRATEGY:
281                         DBG_PRINT_EVENT("unhandled event");
282                 case EV_EXIT:
283                         act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
284                         break;
285         }
286 }
287
288 FSM_STATE(sledge_down)
289 {       
290         struct TrajectoryConstraints tc;
291         switch(FSM_EVENT) {
292                 case EV_ENTRY:
293                         tc = tcFast;
294                         tc.maxe = 0.5;
295                         robot_trajectory_new(&tc);
296
297                         if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
298                                 robot_trajectory_add_point_trans(
299                                         x_coord(1.2 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
300                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
301                                 robot_trajectory_add_point_trans(
302                                         x_coord(1.0 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
303                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.01);
304                                 robot_trajectory_add_point_trans(
305                                         x_coord(0.8 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
306                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05);
307                                 robot_trajectory_add_point_trans(
308                                         x_coord(0.6 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
309                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
310                                 robot_trajectory_add_final_point_trans(
311                                         x_coord(0.5 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
312                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.17,
313                                         NO_TURN());
314                         } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
315                                 robot_trajectory_add_point_trans(
316                                         x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
317                                         1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85));
318                                 robot_trajectory_add_final_point_trans(
319                                         x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.2, slope_approach_style_p->which_side),
320                                         1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85),
321                                         NO_TURN());
322                         }
323                         break;
324                 case EV_MOTION_DONE:
325                         /* just for sure, try to close it one more time */
326                         act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
327                         SUBFSM_RET(NULL);
328                         delete(slope_approach_style_p);
329                         break;
330                 case EV_START:
331                 case EV_TIMER:
332                 case EV_RETURN:
333                 case EV_VIDLE_DONE:
334                 case EV_MOTION_ERROR:
335                 case EV_SWITCH_STRATEGY:
336                         DBG_PRINT_EVENT("unhandled event");
337                         break;
338                 case EV_EXIT:
339                         // enables using side switches on bumpers
340                         enable_switches(true);
341                         robot.ignore_hokuyo = false;
342                         robot.check_turn_safety = true;
343
344                         break;
345         }
346 }
347
348 /************************************************************************
349  * The "unload our oranges" subautomaton
350  ************************************************************************/
351
352 FSM_STATE(to_cntainer_and_unld)
353 {
354         switch(FSM_EVENT) {
355                 case EV_ENTRY:
356                         robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
357                         break;
358                 case EV_MOTION_DONE:
359                         FSM_TIMER(3000); // FIXME: test this
360                         act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
361                         break;
362                 case EV_TIMER:
363                         act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
364                         SUBFSM_RET(NULL);
365                         break;
366                 case EV_START:
367                 case EV_RETURN:
368                 case EV_VIDLE_DONE:
369                 case EV_MOTION_ERROR:
370                 case EV_SWITCH_STRATEGY:
371                         DBG_PRINT_EVENT("unhandled event");
372                 case EV_EXIT:
373                         break;
374         }
375 }
376
377 /************************************************************************
378  * The "collect corns" subautomaton
379  ************************************************************************/
380
381 static enum where_to_go {
382         CORN,
383         TURN_AROUND,
384         CONTAINER,
385         NO_MORE_CORN
386 } where_to_go = CORN;
387
388 static struct corn *corn_to_get;
389
390 FSM_STATE(rush_corns_decider)
391 {
392         switch(FSM_EVENT) {
393                 case EV_ENTRY:
394                         if (where_to_go == CORN) {
395                                 FSM_TRANSITION(approach_next_corn);
396                         } else if (where_to_go == CONTAINER) {
397                                 FSM_TRANSITION(rush_the_corn);
398                         } else if (where_to_go == TURN_AROUND) {
399                                 FSM_TRANSITION(turn_around);
400                         } else /* NO_MORE_CORN */ { 
401                         }
402                         break;
403                 case EV_START:
404                 case EV_TIMER:
405                 case EV_RETURN:
406                 case EV_VIDLE_DONE:
407                 case EV_MOTION_DONE:
408                 case EV_MOTION_ERROR:
409                 case EV_SWITCH_STRATEGY:
410                         DBG_PRINT_EVENT("unhandled event");
411                 case EV_EXIT:
412                         break;
413         }
414 }
415
416 static int cnt = 0;
417 FSM_STATE(approach_next_corn)
418 {
419         switch(FSM_EVENT) {
420                 case EV_ENTRY: {
421                                 double x, y, phi;
422                                 robot_get_est_pos(&x, &y, &phi);
423                                 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
424                                         cnt, x, y, phi);
425
426                                 corn_to_get = choose_next_corn();
427                                 if (corn_to_get) {
428                                         Pos *p = get_corn_approach_position(corn_to_get);
429                                         corn_to_get->was_collected = true;
430                                         robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
431                                         delete(p);
432                                         where_to_go = CONTAINER;
433                                 } else {
434                                         where_to_go = NO_MORE_CORN;
435                                 }
436                                 break;
437                         }
438                 case EV_MOTION_DONE:
439                         cnt++;
440                         FSM_TRANSITION(rush_corns_decider);
441                         break;
442                 case EV_START:
443                 case EV_TIMER:
444                 case EV_RETURN:
445                 case EV_VIDLE_DONE:
446                 case EV_MOTION_ERROR:
447                 case EV_SWITCH_STRATEGY:
448                         DBG_PRINT_EVENT("unhandled event");
449                 case EV_EXIT:
450                         break;
451         }
452 }
453
454 FSM_STATE(rush_the_corn)
455 {
456         switch(FSM_EVENT) {
457                 case EV_ENTRY:
458                         double x;
459                         if (robot.team_color == BLUE) {
460                                 x = corn_to_get->position.x;
461                         } else {
462                                 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
463                         }
464                         remove_wall_around_corn(x, corn_to_get->position.y);
465                         robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcSlow);
466                         where_to_go = TURN_AROUND;
467                         break;
468                 case EV_MOTION_DONE:
469                         FSM_TRANSITION(rush_corns_decider);
470                         break;
471                 case EV_START:
472                 case EV_TIMER:
473                 case EV_RETURN:
474                 case EV_VIDLE_DONE:
475                 case EV_MOTION_ERROR:
476                 case EV_SWITCH_STRATEGY:
477                         DBG_PRINT_EVENT("unhandled event");
478                 case EV_EXIT:
479                         break;
480         }
481 }
482
483 // used to perform the maneuvre
484 FSM_STATE(turn_around)
485 {
486         switch(FSM_EVENT) {
487                 case EV_ENTRY:
488                         robot_trajectory_new_backward(&tcFast);
489                         robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
490                         break;
491                 case EV_MOTION_DONE:
492                         where_to_go = CORN;
493                         FSM_TRANSITION(rush_corns_decider);
494                         break;
495                 case EV_START:
496                 case EV_TIMER:
497                 case EV_RETURN:
498                 case EV_VIDLE_DONE:
499                 case EV_MOTION_ERROR:
500                 case EV_SWITCH_STRATEGY:
501                         DBG_PRINT_EVENT("unhandled event");
502                 case EV_EXIT:
503                         break;
504         }
505 }