summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/init_cb.c
blob: d622cd68049759840bfb404af52328b9142993ef (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
248
249
250
251
252
/* 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.
 *
 * }}} */
#include "common.h"
#include "fsm.h"
#include "init_cb.h"
#include "asserv.h"
#include "init.h"
#include "playground.h"
#include "main.h"
#include "aquajim.h"
#include "switch.h"
#include "chrono.h"
#include "top.h"
#include "playground.h"
#include "modules/trace/trace.h"
#include "modules/path/path.h"

/*
 * IDLE =start=>
 *  => WAIT_FIRST_JACK_IN
 *   do nothing.
 */
fsm_branch_t
init__IDLE__start (void)
{
    return init_next (IDLE, start);
}

/*
 * WAIT_FIRST_JACK_IN =jack_inserted_into_bot=>
 *  => WAIT_FIRST_JACK_OUT
 *   do nothing.
 */
fsm_branch_t
init__WAIT_FIRST_JACK_IN__jack_inserted_into_bot (void)
{
    return init_next (WAIT_FIRST_JACK_IN, jack_inserted_into_bot);
}

/*
 * WAIT_FIRST_JACK_OUT =jack_removed_from_bot=>
 *  => WAIT_SECOND_JACK_IN
 *   start trace module.
 *   get and store the color of the bot.
 */
fsm_branch_t
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 ();
    /* Top initialization. */
    top_init ();
    return init_next (WAIT_FIRST_JACK_OUT, jack_removed_from_bot);
}

/*
 * WAIT_SECOND_JACK_IN =jack_inserted_into_bot=>
 *  => WAIT_FOR_HANDS_OUT
 *   do nothing.
 */
fsm_branch_t
init__WAIT_SECOND_JACK_IN__jack_inserted_into_bot (void)
{
    return init_next (WAIT_SECOND_JACK_IN, jack_inserted_into_bot);
}

/*
 * WAIT_FOR_HANDS_OUT =state_timeout=>
 *  => GO_TO_THE_WALL
 *   fuck the wall in front.
 */
fsm_branch_t
init__WAIT_FOR_HANDS_OUT__state_timeout (void)
{
    /* Go to the wall, no backward. */
    asserv_go_to_the_wall (0);
    return init_next (WAIT_FOR_HANDS_OUT, state_timeout);
}

/*
 * GO_TO_THE_WALL =bot_move_succeed=>
 *  => SET_Y_POSITION
 *   reset the Y position of the bot.
 */
fsm_branch_t
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 init_next (GO_TO_THE_WALL, bot_move_succeed);
}

/*
 * SET_Y_POSITION =asserv_last_cmd_ack=>
 *  => SET_ANGULAR_POSITION
 *   reset the angular position of the bot.
 */
fsm_branch_t
init__SET_Y_POSITION__asserv_last_cmd_ack (void)
{
    /* We are facing top border. */
    asserv_set_angle_position (90 * BOT_ANGLE_DEGREE);
    return init_next (SET_Y_POSITION, asserv_last_cmd_ack);
}

/*
 * SET_ANGULAR_POSITION =asserv_last_cmd_ack=>
 *  => GO_AWAY_FROM_THE_WALL
 *   move away from the wall (linear move).
 */
fsm_branch_t
init__SET_ANGULAR_POSITION__asserv_last_cmd_ack (void)
{
    /* Move away from the border. */
    asserv_move_linearly (- INIT_DIST);
    return init_next (SET_ANGULAR_POSITION, asserv_last_cmd_ack);
}

/*
 * GO_AWAY_FROM_THE_WALL =bot_move_succeed=>
 *  => FACE_OTHER_WALL
 *   get and store the color of the bot.
 *   turn to face the other wall.
 */
fsm_branch_t
init__GO_AWAY_FROM_THE_WALL__bot_move_succeed (void)
{
    /* Face the other wall. */
    asserv_goto_angle (PG_A_VALUE_COMPUTING (180 * BOT_ANGLE_DEGREE));
    return init_next (GO_AWAY_FROM_THE_WALL, bot_move_succeed);
}

/*
 * FACE_OTHER_WALL =bot_move_succeed=>
 *  => WAIT_AFTER_ROTATION
 *   nothing to do.
 */
fsm_branch_t
init__FACE_OTHER_WALL__bot_move_succeed (void)
{
    return init_next (FACE_OTHER_WALL, bot_move_succeed);
}

/*
 * WAIT_AFTER_ROTATION =state_timeout=>
 *  => GO_TO_THE_WALL_AGAIN
 *   fuck the wall in front.
 */
fsm_branch_t
init__WAIT_AFTER_ROTATION__state_timeout (void)
{
    /* Go to the wall, no backward. */
    asserv_go_to_the_wall (0);
    return init_next (WAIT_AFTER_ROTATION, state_timeout);
}

/*
 * GO_TO_THE_WALL_AGAIN =bot_move_succeed=>
 *  => SET_X_POSITION
 *   reset the X position of the bot.
 */
fsm_branch_t
init__GO_TO_THE_WALL_AGAIN__bot_move_succeed (void)
{
    asserv_set_x_position (PG_X_VALUE_COMPUTING (BOT_LENGTH / 2));
    return init_next (GO_TO_THE_WALL_AGAIN, bot_move_succeed);
}

/*
 * SET_X_POSITION =asserv_last_cmd_ack=>
 *  => GO_AWAY_FROM_THE_WALL_AGAIN
 *   move away from the wall (linear move).
 */
fsm_branch_t
init__SET_X_POSITION__asserv_last_cmd_ack (void)
{
    /* Move away from the border. */
    asserv_move_linearly (- INIT_DIST);
    return init_next (SET_X_POSITION, asserv_last_cmd_ack);
}

/*
 * GO_AWAY_FROM_THE_WALL_AGAIN =bot_move_succeed=>
 *  => GO_TO_START_POSITION
 *   go to the start position with a go to movement.
 */
fsm_branch_t
init__GO_AWAY_FROM_THE_WALL_AGAIN__bot_move_succeed (void)
{
    /* Move away from the border. */
    asserv_goto_xya (PG_X_VALUE_COMPUTING (PG_START_ZONE_WIDTH
                                           - BOT_WIDTH / 2
                                           - 50),
                     PG_LENGTH - PG_START_ZONE_LENGTH + BOT_LENGTH / 2 + 50,
		     PG_A_VALUE_COMPUTING (0 * BOT_ANGLE_DEGREE), 0);
    return init_next (GO_AWAY_FROM_THE_WALL_AGAIN, bot_move_succeed);
}

/*
 * GO_TO_START_POSITION =bot_move_succeed=>
 *  => WAIT_SECOND_JACK_OUT
 *   nothing to do, the bot is at the start position.
 */
fsm_branch_t
init__GO_TO_START_POSITION__bot_move_succeed (void)
{
    return init_next (GO_TO_START_POSITION, bot_move_succeed);
}

/*
 * WAIT_SECOND_JACK_OUT =jack_removed_from_bot=>
 *  => IDLE
 *   tell other FSM the match begins.
 */
fsm_branch_t
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 ();
    /* Set the central round to path module. */
    path_obstacle (0x01, PG_WIDTH / 2, PG_LENGTH / 2, 350, 0,
		   PATH_OBSTACLE_VALID_ALWAYS);
    return init_next (WAIT_SECOND_JACK_OUT, jack_removed_from_bot);
}