]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/common-states.cc
Tune loading oranges
[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                                                 /*
129                                                 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.3, slope_approach_style_p->which_side),
130                                                 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05,
131                                                 ARRIVE_FROM(DEG2RAD(0),0.05),
132                                                 */
133                                                 x_coord(0.3, slope_approach_style_p->which_side),
134                                                 PLAYGROUND_HEIGHT_M - ROBOT_WIDTH_M/2 - 0.03,
135                                                 ARRIVE_FROM(DEG2RAD(0), 0.02),
136                                                 &tcFast);
137                                 }
138                                 break;
139                         }
140                 case EV_MOTION_DONE:
141                         FSM_TRANSITION(climb_the_slope);
142                         break;
143                 case EV_START:
144                 case EV_TIMER:
145                 case EV_RETURN:
146                 case EV_VIDLE_DONE:
147                 case EV_MOTION_ERROR:
148                 case EV_SWITCH_STRATEGY:
149                         DBG_PRINT_EVENT("unhandled event");
150                 case EV_EXIT:
151                         break;
152         }
153 }
154
155 void inline enable_switches(bool enabled)
156 {
157         robot.use_left_switch = enabled;
158         robot.use_right_switch = enabled;
159         robot.use_back_switch = enabled;
160 }
161
162 FSM_STATE(climb_the_slope)
163 {
164         switch(FSM_EVENT) {
165                 case EV_ENTRY: {
166                                 // disables using side switches on bumpers when going up
167                                 enable_switches(false);
168                                 act_vidle(VIDLE_LOAD_PREPARE, 5);
169                                 robot.ignore_hokuyo = true;
170                                 /* create the trajectory and go */
171                                 robot_trajectory_new_backward(&tcSlow);
172                                 if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
173                                         robot_trajectory_add_point_trans(
174                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
175                                                 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01);
176                                         robot_trajectory_add_final_point_trans(
177                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
178                                                 PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) +0.01,
179                                                 NO_TURN());
180                                 } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
181                                         robot_trajectory_add_point_trans(
182                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
183                                                 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85));
184                                         robot_trajectory_add_final_point_trans(
185                                                 x_coord(SLOPE_TO_RIM_M + SLOPE_LENGTH_M - ROBOT_AXIS_TO_BACK_M + 0.07, slope_approach_style_p->which_side),
186                                                 1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) + 0.01 - 1.85),
187                                                 NO_TURN());
188                                 }
189                                 break;
190                         }
191                 case EV_MOTION_DONE:
192                         SUBFSM_TRANSITION(load_oranges, NULL);
193                         break;
194                 case EV_RETURN:
195                         FSM_TRANSITION(sledge_down);
196                         break;
197                 case EV_TIMER:
198                 case EV_START:
199                 case EV_VIDLE_DONE:
200                 case EV_MOTION_ERROR:
201                 case EV_SWITCH_STRATEGY:
202                         DBG_PRINT_EVENT("unhandled event");
203                 case EV_EXIT:
204                         break;
205         }
206 }
207
208 /* subautomaton to load oranges in two stages */
209 FSM_STATE_DECL(load_oranges2);
210 FSM_STATE(load_oranges)
211 {
212         switch(FSM_EVENT) {
213                 case EV_ENTRY:
214                         FSM_TIMER(2000);
215                         act_vidle(VIDLE_MIDDLE, VIDLE_MEDIUM_SPEED);
216                         break;
217                 case EV_VIDLE_DONE:
218                         FSM_TIMER(1000);
219                         break;
220                 case EV_TIMER:
221                         FSM_TRANSITION(load_oranges2);
222                         break;
223                 case EV_MOTION_DONE:
224                 case EV_START:
225                 case EV_RETURN:
226                 case EV_MOTION_ERROR:
227                 case EV_SWITCH_STRATEGY:
228                         DBG_PRINT_EVENT("unhandled event");
229                 case EV_EXIT:
230                         break;
231         }
232 }
233
234 FSM_STATE(load_oranges2)
235 {
236         switch(FSM_EVENT) {
237                 case EV_ENTRY:
238                         act_vidle(VIDLE_UP, 5);
239                         FSM_TIMER(2000);
240                         break;
241                 case EV_VIDLE_DONE:
242                         SUBFSM_RET(NULL);
243                         break;
244                 case EV_TIMER:
245                         SUBFSM_RET(NULL);
246                         break;
247                 case EV_MOTION_DONE:
248                 case EV_START:
249                 case EV_RETURN:
250                 case EV_MOTION_ERROR:
251                 case EV_SWITCH_STRATEGY:
252                         DBG_PRINT_EVENT("unhandled event");
253                 case EV_EXIT:
254                         act_vidle(VIDLE_UP-1, VIDLE_FAST_SPEED);
255                         break;
256         }
257 }
258
259 FSM_STATE(sledge_down)
260 {       
261         struct TrajectoryConstraints tc;
262         switch(FSM_EVENT) {
263                 case EV_ENTRY:
264                         tc = tcFast;
265                         tc.maxe = 0.5;
266                         robot_trajectory_new(&tc);
267
268                         if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_BOUNDARY) {
269                                 robot_trajectory_add_point_trans(
270                                         x_coord(1.2 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
271                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01);
272                                 robot_trajectory_add_point_trans(
273                                         x_coord(1.0 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
274                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.01);
275                                 robot_trajectory_add_point_trans(
276                                         x_coord(0.8 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
277                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.05);
278                                 robot_trajectory_add_point_trans(
279                                         x_coord(0.6 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
280                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.10);
281                                 robot_trajectory_add_final_point_trans(
282                                         x_coord(0.5 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
283                                         PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.17,
284                                         NO_TURN());
285                         } else if (slope_approach_style_p->which_oranges == NEAR_PLAYGROUND_CENTER) {
286                                 robot_trajectory_add_point_trans(
287                                         x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
288                                         1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85));
289                                 robot_trajectory_add_final_point_trans(
290                                         x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.2, slope_approach_style_p->which_side),
291                                         1.85 - (PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01 - 1.85),
292                                         NO_TURN());
293                         }
294                         /* robot_trajectory_add_point_trans(
295                                 x_coord(1 - ROBOT_AXIS_TO_BACK_M, slope_approach_style_p->which_side),
296                                 y_coord(PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2)+0.01, slope_approach_style_p->which_oranges));
297                         robot_trajectory_add_point_trans(
298                                 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.20, slope_approach_style_p->which_side),
299                                 y_coord(PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.08, slope_approach_style_p->which_oranges));
300                         robot_trajectory_add_final_point_trans(
301                                 x_coord(SLOPE_TO_RIM_M - ROBOT_AXIS_TO_BACK_M - 0.26, slope_approach_style_p->which_side),
302                                 y_coord(PLAYGROUND_HEIGHT_M - (ROBOT_WIDTH_M/2) - 0.14, slope_approach_style_p->which_oranges),
303                                 NO_TURN()); */
304                         break;
305                 case EV_MOTION_DONE:
306                         /* just for sure, try to close it one more time */
307                         act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
308                         SUBFSM_RET(NULL);
309                         delete(slope_approach_style_p);
310                         break;
311                 case EV_START:
312                 case EV_TIMER:
313                 case EV_RETURN:
314                 case EV_VIDLE_DONE:
315                 case EV_MOTION_ERROR:
316                 case EV_SWITCH_STRATEGY:
317                         DBG_PRINT_EVENT("unhandled event");
318                         break;
319                 case EV_EXIT:
320                         // enables using side switches on bumpers
321                         enable_switches(true);
322                         robot.ignore_hokuyo = false;
323                         robot.check_turn_safety = true;
324
325                         break;
326         }
327 }
328
329 /************************************************************************
330  * The "unload our oranges" subautomaton
331  ************************************************************************/
332
333 FSM_STATE(to_cntainer_and_unld)
334 {
335         switch(FSM_EVENT) {
336                 case EV_ENTRY:
337                         /*
338                         if (slope_approach_style_p->which_side == MINE) {
339                                 robot_trajectory_new(&tcFast);
340                                 // face the rim with front of the robot
341                                 //robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.12, ARRIVE_FROM(DEG2RAD(-90), 0.10));
342                                 // face the rim with back of the robot
343                                 robot_trajectory_add_point_trans(PLAYGROUND_WIDTH_M-0.6, 0.35);
344                                 robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05));
345                         } else { */
346                                 robot_goto_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, ARRIVE_FROM(DEG2RAD(90),0.05), &tcFast);
347                         //}
348                         break;
349                 case EV_MOTION_DONE:
350                         FSM_TIMER(3000); // FIXME: test this
351                         act_vidle(VIDLE_DOWN, VIDLE_FAST_SPEED);
352                         break;
353                 case EV_TIMER:
354                         act_vidle(VIDLE_UP, VIDLE_FAST_SPEED);
355                         SUBFSM_RET(NULL);
356                         break;
357                 case EV_START:
358                 case EV_RETURN:
359                 case EV_VIDLE_DONE:
360                 case EV_MOTION_ERROR:
361                 case EV_SWITCH_STRATEGY:
362                         DBG_PRINT_EVENT("unhandled event");
363                 case EV_EXIT:
364                         break;
365         }
366 }
367
368 /************************************************************************
369  * The "collect corns" subautomaton
370  ************************************************************************/
371
372 static enum where_to_go {
373         CORN,
374         TURN_AROUND,
375         CONTAINER,
376         NO_MORE_CORN
377 } where_to_go = CORN;
378
379 static struct corn *corn_to_get;
380
381 FSM_STATE(rush_corns_decider)
382 {
383         switch(FSM_EVENT) {
384                 case EV_ENTRY:
385                         if (where_to_go == CORN) {
386                                 FSM_TRANSITION(approach_next_corn);
387                         } else if (where_to_go == CONTAINER) {
388                                 FSM_TRANSITION(rush_the_corn);
389                         } else if (where_to_go == TURN_AROUND) {
390                                 FSM_TRANSITION(turn_around);
391                         } else /* NO_MORE_CORN */ { 
392                         }
393                         break;
394                 case EV_START:
395                 case EV_TIMER:
396                 case EV_RETURN:
397                 case EV_VIDLE_DONE:
398                 case EV_MOTION_DONE:
399                 case EV_MOTION_ERROR:
400                 case EV_SWITCH_STRATEGY:
401                         DBG_PRINT_EVENT("unhandled event");
402                 case EV_EXIT:
403                         break;
404         }
405 }
406
407 static int cnt = 0;
408 FSM_STATE(approach_next_corn)
409 {
410         switch(FSM_EVENT) {
411                 case EV_ENTRY: {
412                                 double x, y, phi;
413                                 robot_get_est_pos(&x, &y, &phi);
414                                 printf("approach_next_corn: puck cnt: %d, est pos %.3f, %.3f, %.3f\n",
415                                         cnt, x, y, phi);
416
417                                 corn_to_get = choose_next_corn();
418                                 if (corn_to_get) {
419                                         Pos *p = get_corn_approach_position(corn_to_get);
420                                         corn_to_get->was_collected = true;
421                                         //robot_trajectory_new(&tcFast);
422                                         //robot_trajectory_add_final_point_trans(robot_trajectory_add_final_point_trans(p->x, p->y, TURN(p->phi));
423                                         robot_goto_trans(p->x, p->y, TURN(p->phi), &tcFast);
424                                         delete(p);
425                                         where_to_go = CONTAINER;
426                                 } else {
427                                         where_to_go = NO_MORE_CORN;
428                                 }
429                                 break;
430                         }
431                 case EV_MOTION_DONE:
432                         cnt++;
433                         FSM_TRANSITION(rush_corns_decider);
434                         break;
435                 case EV_START:
436                 case EV_TIMER:
437                 case EV_RETURN:
438                 case EV_VIDLE_DONE:
439                 case EV_MOTION_ERROR:
440                 case EV_SWITCH_STRATEGY:
441                         DBG_PRINT_EVENT("unhandled event");
442                 case EV_EXIT:
443                         break;
444         }
445 }
446
447 FSM_STATE(rush_the_corn)
448 {
449         switch(FSM_EVENT) {
450                 case EV_ENTRY:
451                         double x;
452                         if (robot.team_color == BLUE) {
453                                 x = corn_to_get->position.x;
454                         } else {
455                                 x = PLAYGROUND_WIDTH_M - corn_to_get->position.x;
456                         }
457                         remove_wall_around_corn(x, corn_to_get->position.y);
458                         robot_goto_trans(PLAYGROUND_WIDTH_M - 0.4, 0.15, ARRIVE_FROM(DEG2RAD(-90), 0.02), &tcFast);
459                         where_to_go = TURN_AROUND;
460                         break;
461                 case EV_MOTION_DONE:
462                         FSM_TRANSITION(rush_corns_decider);
463                         break;
464                 case EV_START:
465                 case EV_TIMER:
466                 case EV_RETURN:
467                 case EV_VIDLE_DONE:
468                 case EV_MOTION_ERROR:
469                 case EV_SWITCH_STRATEGY:
470                         DBG_PRINT_EVENT("unhandled event");
471                 case EV_EXIT:
472                         break;
473         }
474 }
475
476 // used to perform the maneuvre
477 FSM_STATE(turn_around)
478 {
479         switch(FSM_EVENT) {
480                 case EV_ENTRY:
481                         robot_trajectory_new_backward(&tcFast);
482                         robot_trajectory_add_final_point_trans(PLAYGROUND_WIDTH_M-0.35, 0.45, TURN_CCW(90));
483                         break;
484                 case EV_MOTION_DONE:
485                         where_to_go = CORN;
486                         FSM_TRANSITION(rush_corns_decider);
487                         break;
488                 case EV_START:
489                 case EV_TIMER:
490                 case EV_RETURN:
491                 case EV_VIDLE_DONE:
492                 case EV_MOTION_ERROR:
493                 case EV_SWITCH_STRATEGY:
494                         DBG_PRINT_EVENT("unhandled event");
495                 case EV_EXIT:
496                         break;
497         }
498 }