summaryrefslogtreecommitdiff
path: root/digital/io/src/init.c
blob: 7915e96171d7d6123955d0cb60fc7da33dbc9b94 (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
/* init.c */
/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
 *
 * 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.
 *
 * }}} */
#define ANGFSM_NAME AI

#include "common.h"
#include "angfsm.h"
#include "asserv.h"
#include "init.h"
#include "playground_2010.h"
#include "main.h"
#include "bot.h"
#include "switch.h"
#include "chrono.h"
#include "modules/trace/trace.h"
#include "modules/path/path.h"

uint8_t init_match_is_started = 0;

FSM_STATES (
	    /* waiting for the beginning of the top FSM. */
	    INIT_IDLE,
	    /* wait for the jack to be inserted into the bot for the first time. */
	    INIT_WAIT_FIRST_JACK_IN,
	    /* wait the jack to be removed from the bot for the first time. */
	    INIT_WAIT_FIRST_JACK_OUT,
	    /* wait for the jack to be inserted into the bot for the second time. */
	    INIT_WAIT_SECOND_JACK_IN,
	    /* just wait for operator hands to be removed from the jack. */
	    INIT_WAIT_FOR_HANDS_OUT,
	    /* go to the wall for the first time. */
	    INIT_GO_TO_THE_WALL,
	    /* reset the Y position of the bot.
	     * reset the angular position of the bot.
	     * go backward from the wall for a specific distance. */
	    INIT_GO_AWAY_FROM_THE_WALL,
	    /* make the bot face the other wall. */
	    INIT_FACE_OTHER_WALL,
	    /* make sure rotation is really finished before continuing. */
	    INIT_WAIT_AFTER_ROTATION,
	    /* go to the wall for the second time. */
	    INIT_GO_TO_THE_WALL_AGAIN,
	    /* reset the X position of the bot.
	     * go backward from the wall for a specific distance. */
	    INIT_GO_AWAY_FROM_THE_WALL_AGAIN,
	    /* go to the start position. */
	    INIT_GO_TO_START_POSITION,
	    /* wait the jack to be removed from the bot for the second time. */
	    INIT_WAIT_SECOND_JACK_OUT)

FSM_EVENTS (
	    /* the jack is inserted into the bot. */
	    jack_inserted_into_bot,
	    /* the jack is removed from the bot. */
	    jack_removed_from_bot)

FSM_START_WITH (INIT_IDLE)

/*
 * do nothing.
 */
FSM_TRANS (INIT_IDLE, start, INIT_WAIT_FIRST_JACK_IN)
{
}

/*
 * do nothing.
 */
FSM_TRANS (INIT_WAIT_FIRST_JACK_IN,
	   jack_inserted_into_bot,
	   INIT_WAIT_FIRST_JACK_OUT)
{
}

/*
 * start trace module.
 * get and store the color of the bot.
 */
FSM_TRANS (INIT_WAIT_FIRST_JACK_OUT,
	   jack_removed_from_bot,
	   INIT_WAIT_SECOND_JACK_IN)
{
    /* Initialize trace module (erase the flash). */
    trace_init ();
    /* Get the color. */
    team_color = switch_get_color ();
}

/*
 * do nothing.
 */
FSM_TRANS (INIT_WAIT_SECOND_JACK_IN,
	   jack_inserted_into_bot,
	   INIT_WAIT_FOR_HANDS_OUT)
{
}

/*
 * fuck the wall in front.
 */
FSM_TRANS_TIMEOUT (INIT_WAIT_FOR_HANDS_OUT, 225,
		   INIT_GO_TO_THE_WALL)
{
    /* Go to the wall, no backward. */
    asserv_go_to_the_wall (0);
}

/*
 * reset the Y position of the bot.
 * reset the angular position of the bot.
 * move away from the wall (linear move).
 */
FSM_TRANS (INIT_GO_TO_THE_WALL,
	   bot_move_succeed,
	   INIT_GO_AWAY_FROM_THE_WALL)
{
    asserv_set_y_position (PG_Y (PG_LENGTH - BOT_SIZE_FRONT));
    asserv_set_angle_position (PG_A_DEG (90));
    /* Move away from the border. */
    asserv_move_linearly (- INIT_DIST);
}

/*
 * turn to face the other wall.
 */
FSM_TRANS (INIT_GO_AWAY_FROM_THE_WALL,
	   bot_move_succeed,
	   INIT_FACE_OTHER_WALL)
{
    /* Face the other wall. */
    asserv_goto_angle (PG_A_DEG (180));
}

/*
 * nothing to do.
 */
FSM_TRANS (INIT_FACE_OTHER_WALL,
	   bot_move_succeed,
	   INIT_WAIT_AFTER_ROTATION)
{
}

/*
 * fuck the wall in front.
 */
FSM_TRANS_TIMEOUT (INIT_WAIT_AFTER_ROTATION, 100,
		   INIT_GO_TO_THE_WALL_AGAIN)
{
    /* Go to the wall, no backward. */
    asserv_go_to_the_wall (0);
}

/*
 * reset the X position of the bot.
 * move away from the wall (linear move).
 */
FSM_TRANS (INIT_GO_TO_THE_WALL_AGAIN,
	   bot_move_succeed,
	   INIT_GO_AWAY_FROM_THE_WALL_AGAIN)
{
    asserv_set_x_position (PG_X (BOT_SIZE_FRONT));
    /* Move away from the border. */
    asserv_move_linearly (- INIT_DIST);
}

/*
 * go to the start position with a go to movement.
 */
FSM_TRANS (INIT_GO_AWAY_FROM_THE_WALL_AGAIN,
	   bot_move_succeed,
	   INIT_GO_TO_START_POSITION)
{
    /* Move away from the border. */
    asserv_goto_xya (BOT_INIT_XYA, 0);
}

/*
 * nothing to do, the bot is at the start position.
 */
FSM_TRANS (INIT_GO_TO_START_POSITION,
	   bot_move_succeed,
	   INIT_WAIT_SECOND_JACK_OUT)
{
}

/*
 * tell other FSM the match begins.
 * start the chrono.
 */
FSM_TRANS (INIT_WAIT_SECOND_JACK_OUT,
	   jack_removed_from_bot,
	   INIT_IDLE)
{
    /* Set the flag to transmit to other FSM. */
    init_match_is_started = 1;
    /* Start the chrono. */
    chrono_start ();
}