summaryrefslogtreecommitdiff
path: root/digital/io/src/ai_init_cb.c
blob: 1fb41b640c876cb54b34cb7facd83de0b3c51231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* init_cb.c - init FSM callbacks. */
/*  {{{
 *
 * Copyright (C) 2009 Nicolas Haller
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}}
 * init:
 * place the bot in the start position for a match.
 */
#include "common.h"
#include "fsm.h"
#include "ai_cb.h"
#include "asserv.h"
#include "init.h"
#include "playground.h"
#include "main.h"
#include "bot.h"
#include "switch.h"
#include "chrono.h"
#include "playground.h"
#include "modules/trace/trace.h"
#include "modules/path/path.h"

/*
 * INIT_IDLE =start=>
 *  => INIT_WAIT_FIRST_JACK_IN
 *   do nothing.
 */
fsm_branch_t
ai__INIT_IDLE__start (void)
{
    return ai_next (INIT_IDLE, start);
}

/*
 * INIT_WAIT_FIRST_JACK_IN =jack_inserted_into_bot=>
 *  => INIT_WAIT_FIRST_JACK_OUT
 *   do nothing.
 */
fsm_branch_t
ai__INIT_WAIT_FIRST_JACK_IN__jack_inserted_into_bot (void)
{
    return ai_next (INIT_WAIT_FIRST_JACK_IN, jack_inserted_into_bot);
}

/*
 * INIT_WAIT_FIRST_JACK_OUT =jack_removed_from_bot=>
 *  => INIT_WAIT_SECOND_JACK_IN
 *   start trace module.
 *   get and store the color of the bot.
 */
fsm_branch_t
ai__INIT_WAIT_FIRST_JACK_OUT__jack_removed_from_bot (void)
{
    /* Initialize trace module (erase the flash). */
    trace_init ();
    /* Get the color. */
    bot_color = switch_get_color ();
    return ai_next (INIT_WAIT_FIRST_JACK_OUT, jack_removed_from_bot);
}

/*
 * INIT_WAIT_SECOND_JACK_IN =jack_inserted_into_bot=>
 *  => INIT_WAIT_FOR_HANDS_OUT
 *   do nothing.
 */
fsm_branch_t
ai__INIT_WAIT_SECOND_JACK_IN__jack_inserted_into_bot (void)
{
    return ai_next (INIT_WAIT_SECOND_JACK_IN, jack_inserted_into_bot);
}

/*
 * INIT_WAIT_FOR_HANDS_OUT =state_timeout=>
 *  => INIT_GO_TO_THE_WALL
 *   fuck the wall in front.
 */
fsm_branch_t
ai__INIT_WAIT_FOR_HANDS_OUT__state_timeout (void)
{
    /* Go to the wall, no backward. */
    asserv_go_to_the_wall (0);
    return ai_next (INIT_WAIT_FOR_HANDS_OUT, state_timeout);
}

/*
 * INIT_GO_TO_THE_WALL =bot_move_succeed=>
 *  => INIT_SET_Y_POSITION
 *   reset the Y position of the bot.
 */
fsm_branch_t
ai__INIT_GO_TO_THE_WALL__bot_move_succeed (void)
{
    /* We are against the border of absys Y set to PG_LENGTH. */
    asserv_set_y_position (PG_LENGTH - (BOT_LENGTH / 2));
    return ai_next (INIT_GO_TO_THE_WALL, bot_move_succeed);
}

/*
 * INIT_SET_Y_POSITION =asserv_last_cmd_ack=>
 *  => INIT_SET_ANGULAR_POSITION
 *   reset the angular position of the bot.
 */
fsm_branch_t
ai__INIT_SET_Y_POSITION__asserv_last_cmd_ack (void)
{
    /* We are facing top border. */
    asserv_set_angle_position (POSITION_A_DEG (90));
    return ai_next (INIT_SET_Y_POSITION, asserv_last_cmd_ack);
}

/*
 * INIT_SET_ANGULAR_POSITION =asserv_last_cmd_ack=>
 *  => INIT_GO_AWAY_FROM_THE_WALL
 *   move away from the wall (linear move).
 */
fsm_branch_t
ai__INIT_SET_ANGULAR_POSITION__asserv_last_cmd_ack (void)
{
    /* Move away from the border. */
    asserv_move_linearly (- INIT_DIST);
    return ai_next (INIT_SET_ANGULAR_POSITION, asserv_last_cmd_ack);
}

/*
 * INIT_GO_AWAY_FROM_THE_WALL =bot_move_succeed=>
 *  => INIT_FACE_OTHER_WALL
 *   turn to face the other wall.
 */
fsm_branch_t
ai__INIT_GO_AWAY_FROM_THE_WALL__bot_move_succeed (void)
{
    /* Face the other wall. */
    asserv_goto_angle (PG_A_DEG (180));
    return ai_next (INIT_GO_AWAY_FROM_THE_WALL, bot_move_succeed);
}

/*
 * INIT_FACE_OTHER_WALL =bot_move_succeed=>
 *  => INIT_WAIT_AFTER_ROTATION
 *   nothing to do.
 */
fsm_branch_t
ai__INIT_FACE_OTHER_WALL__bot_move_succeed (void)
{
    return ai_next (INIT_FACE_OTHER_WALL, bot_move_succeed);
}

/*
 * INIT_WAIT_AFTER_ROTATION =state_timeout=>
 *  => INIT_GO_TO_THE_WALL_AGAIN
 *   fuck the wall in front.
 */
fsm_branch_t
ai__INIT_WAIT_AFTER_ROTATION__state_timeout (void)
{
    /* Go to the wall, no backward. */
    asserv_go_to_the_wall (0);
    return ai_next (INIT_WAIT_AFTER_ROTATION, state_timeout);
}

/*
 * INIT_GO_TO_THE_WALL_AGAIN =bot_move_succeed=>
 *  => INIT_SET_X_POSITION
 *   reset the X position of the bot.
 */
fsm_branch_t
ai__INIT_GO_TO_THE_WALL_AGAIN__bot_move_succeed (void)
{
    asserv_set_x_position (PG_X (BOT_LENGTH / 2));
    return ai_next (INIT_GO_TO_THE_WALL_AGAIN, bot_move_succeed);
}

/*
 * INIT_SET_X_POSITION =asserv_last_cmd_ack=>
 *  => INIT_GO_AWAY_FROM_THE_WALL_AGAIN
 *   move away from the wall (linear move).
 */
fsm_branch_t
ai__INIT_SET_X_POSITION__asserv_last_cmd_ack (void)
{
    /* Move away from the border. */
    asserv_move_linearly (- INIT_DIST);
    return ai_next (INIT_SET_X_POSITION, asserv_last_cmd_ack);
}

/*
 * INIT_GO_AWAY_FROM_THE_WALL_AGAIN =bot_move_succeed=>
 *  => INIT_GO_TO_START_POSITION
 *   go to the start position with a go to movement.
 */
fsm_branch_t
ai__INIT_GO_AWAY_FROM_THE_WALL_AGAIN__bot_move_succeed (void)
{
    /* Move away from the border. */
    asserv_goto_xya (PG_X (PG_START_ZONE_WIDTH - BOT_WIDTH / 2 - 50),
		     PG_Y (PG_LENGTH - PG_START_ZONE_LENGTH + BOT_LENGTH / 2 + 50),
		     PG_A_DEG (0), 0);
    return ai_next (INIT_GO_AWAY_FROM_THE_WALL_AGAIN, bot_move_succeed);
}

/*
 * INIT_GO_TO_START_POSITION =bot_move_succeed=>
 *  => INIT_WAIT_SECOND_JACK_OUT
 *   nothing to do, the bot is at the start position.
 */
fsm_branch_t
ai__INIT_GO_TO_START_POSITION__bot_move_succeed (void)
{
    return ai_next (INIT_GO_TO_START_POSITION, bot_move_succeed);
}

/*
 * INIT_WAIT_SECOND_JACK_OUT =jack_removed_from_bot=>
 *  => INIT_IDLE
 *   tell other FSM the match begins.
 *   start the chrono.
 */
fsm_branch_t
ai__INIT_WAIT_SECOND_JACK_OUT__jack_removed_from_bot (void)
{
    /* Set the flag to transmit to other FSM. */
    init_match_is_started = 1;
    /* Start the chrono. */
    chrono_init ();
    return ai_next (INIT_WAIT_SECOND_JACK_OUT, jack_removed_from_bot);
}