summaryrefslogtreecommitdiff
path: root/digital/ai/src/fsm/init.c
blob: 51ba31ab47b814d53a5efaa9fa06b7372a922ead (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
/* init.c */
/* robospierre - Eurobot 2011 AI. {{{
 *
 * Copyright (C) 2011 Nicolas Schodet
 *
 * 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 "asserv.h"
#include "contact.h"
#include "chrono.h"

#define FSM_NAME AI
#include "fsm.h"
#include "fsm_queue.h"

#include "bot.h"
#include "init_defs.h"

/*
 * Initialise robot position with a calibration procedure.
 */

FSM_STATES (
	    /* Initial state, waiting for jack. */
	    INIT_START,
	    /* After first jack insertion, waiting for removal to initialise actuators. */
	    INIT_WAITING_FIRST_JACK_OUT,
	    /* Initialising actuators, then waiting for second jack insertion. */
	    INIT_INITIALISING_ACTUATORS,
	    /* After second jack insertion, waiting the operator remove its
	     * hand. */
	    INIT_WAITING_HANDS_OUT,
	    /* Finding the first wall. */
	    INIT_FINDING_FIRST_WALL,
	    /* Going away from the wall. */
	    INIT_GOING_AWAY_FIRST_WALL,
	    /* Turning to face the other wall. */
	    INIT_FACING_SECOND_WALL,
	    /* Waiting after rotation for robot to stabilize. */
	    INIT_WAITING_AFTER_FACING_SECOND_WALL,
	    /* Finding the second wall. */
	    INIT_FINDING_SECOND_WALL,
	    /* Going away from the wall. */
	    INIT_GOING_AWAY_SECOND_WALL,
#ifdef INIT_START_POSITION_ANGLE
	    /* Facing the start position. */
	    INIT_FACING_START_POSITION,
#endif
	    /* Going to start position. */
	    INIT_GOING_TO_START_POSITION,
	    /* Waiting for the round start (waiting for the jack). */
	    INIT_WAITING_SECOND_JACK_OUT,
	    /* Initialisation finished, nothing else to do. */
	    INIT_FINISHED)

FSM_EVENTS (
	    /* Jack is inserted. */
	    jack_inserted,
	    /* Jack is removed. */
	    jack_removed,
	    /* Sent to initialise actuators. */
	    init_actuators,
	    /* Sent to start round. */
	    init_start_round)

FSM_START_WITH (INIT_START)

FSM_TRANS (INIT_START, jack_inserted, INIT_WAITING_FIRST_JACK_OUT)
{
    return FSM_NEXT (INIT_START, jack_inserted);
}

FSM_TRANS (INIT_WAITING_FIRST_JACK_OUT, jack_removed,
	   INIT_INITIALISING_ACTUATORS)
{
    fsm_queue_post_event (FSM_EVENT (AI, init_actuators));
    return FSM_NEXT (INIT_WAITING_FIRST_JACK_OUT, jack_removed);
}

FSM_TRANS (INIT_INITIALISING_ACTUATORS, jack_inserted, INIT_WAITING_HANDS_OUT)
{
    team_color = contact_get_color ();
    return FSM_NEXT (INIT_INITIALISING_ACTUATORS, jack_inserted);
}

FSM_TRANS_TIMEOUT (INIT_WAITING_HANDS_OUT, 225, INIT_FINDING_FIRST_WALL)
{
    asserv_set_speed (BOT_SPEED_INIT);
    asserv_push_the_wall (INIT_FIRST_WALL_PUSH);
    return FSM_NEXT_TIMEOUT (INIT_WAITING_HANDS_OUT);
}

FSM_TRANS (INIT_FINDING_FIRST_WALL, robot_move_success,
	   INIT_GOING_AWAY_FIRST_WALL)
{
    asserv_move_linearly (INIT_FIRST_WALL_AWAY);
    return FSM_NEXT (INIT_FINDING_FIRST_WALL, robot_move_success);
}

FSM_TRANS (INIT_GOING_AWAY_FIRST_WALL, robot_move_success,
	   INIT_FACING_SECOND_WALL)
{
    asserv_goto_angle (INIT_SECOND_WALL_ANGLE);
    return FSM_NEXT (INIT_GOING_AWAY_FIRST_WALL, robot_move_success);
}

FSM_TRANS (INIT_FACING_SECOND_WALL, robot_move_success,
	   INIT_WAITING_AFTER_FACING_SECOND_WALL)
{
    return FSM_NEXT (INIT_FACING_SECOND_WALL, robot_move_success);
}

FSM_TRANS_TIMEOUT (INIT_WAITING_AFTER_FACING_SECOND_WALL, 225 / 2,
		   INIT_FINDING_SECOND_WALL)
{
    asserv_push_the_wall (INIT_SECOND_WALL_PUSH);
    return FSM_NEXT_TIMEOUT (INIT_WAITING_AFTER_FACING_SECOND_WALL);
}

FSM_TRANS (INIT_FINDING_SECOND_WALL, robot_move_success,
	   INIT_GOING_AWAY_SECOND_WALL)
{
    asserv_move_linearly (INIT_SECOND_WALL_AWAY);
    return FSM_NEXT (INIT_FINDING_SECOND_WALL, robot_move_success);
}

#ifdef INIT_START_POSITION_ANGLE
FSM_TRANS (INIT_GOING_AWAY_SECOND_WALL, robot_move_success,
	   INIT_FACING_START_POSITION)
{
    asserv_goto_angle (INIT_START_POSITION_ANGLE);
    return FSM_NEXT (INIT_GOING_AWAY_SECOND_WALL, robot_move_success);
}

FSM_TRANS (INIT_FACING_START_POSITION, robot_move_success,
	   INIT_GOING_TO_START_POSITION)
{
    asserv_goto_xya (INIT_START_POSITION);
    return FSM_NEXT (INIT_FACING_START_POSITION, robot_move_success);
}

#else

FSM_TRANS (INIT_GOING_AWAY_SECOND_WALL, robot_move_success,
	   INIT_GOING_TO_START_POSITION)
{
    asserv_goto_xya (INIT_START_POSITION);
    return FSM_NEXT (INIT_GOING_AWAY_SECOND_WALL, robot_move_success);
}

#endif

FSM_TRANS (INIT_GOING_TO_START_POSITION, robot_move_success,
	   INIT_WAITING_SECOND_JACK_OUT)
{
    asserv_set_speed (BOT_SPEED_NORMAL);
    return FSM_NEXT (INIT_GOING_TO_START_POSITION, robot_move_success);
}

FSM_TRANS (INIT_WAITING_SECOND_JACK_OUT, jack_removed, INIT_FINISHED)
{
    chrono_init ();
    fsm_queue_post_event (FSM_EVENT (AI, init_start_round));
    return FSM_NEXT (INIT_WAITING_SECOND_JACK_OUT, jack_removed);
}