summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/guybrush/top.c
blob: 439ac1a63140bb6451dba0e183d042017546de8d (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
/* top.c */
/* guybrush - Eurobot 2012 AI. {{{
 *
 * Copyright (C) 2012 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 "io.h"

#include "playground_2012.h"
#include "asserv.h"

#define FSM_NAME AI
#include "fsm.h"

#include "move.h"
#include "chrono.h"
#include "contact.h"

#include "strat.h"
#include "path.h"

#include "output_defs.h"

/*
 * Here is the top FSM.  This FSM is suppose to give life to the robot with an
 * impression of intelligence... Well...
 */

FSM_INIT

FSM_STATES (
	    /* Initial state. */
	    TOP_START,

	    /* Going to a collect position above or below a totem. */
	    TOP_TOTEM_GOING,
	    /* Approaching a totem. */
	    TOP_TOTEM_APPROACHING,
	    /* Pushing until full contact. */
	    TOP_TOTEM_PUSHING,
	    /* Going back after totem has been emptied. */
	    TOP_TOTEM_GOING_BACK,

	    /* Going to an unload position. */
	    TOP_UNLOAD_GOING,
	    /* Unloading, waiting for elements to fall. */
	    TOP_UNLOADING)

FSM_START_WITH (TOP_START)

/** Top context. */
struct top_t
{
    /** Decision position. */
    vect_t decision_pos;
};

/** Global context. */
struct top_t top;

/** Go collect a totem. */
static void
top_go_totem (void)
{
    position_t pos;
    pos.v = top.decision_pos;
    pos.a = pos.v.y > PG_LENGTH / 2 ? POSITION_A_DEG (-90)
	: POSITION_A_DEG (90);
    move_start (pos, 0);
}

/** Go unload. */
static void
top_go_unload (void)
{
    position_t pos;
    pos.v = top.decision_pos;
    pos.a = PG_A_DEG (70);
    move_start (pos, 0);
}

/** Call strat to make a decision, apply it and return the decision to go to
 * the next state. */
static uint8_t
top_decision (void)
{
    uint8_t decision = strat_decision (&top.decision_pos);
    switch (decision)
      {
      case STRAT_DECISION_TOTEM:
	top_go_totem ();
	break;
      case STRAT_DECISION_UNLOAD:
	top_go_unload ();
	break;
      default:
	assert (0);
      }
    return decision;
}

#define RETURN_TOP_DECISION_SWITCH(state, event) do { \
    switch (top_decision ()) \
      { \
      default: assert (0); \
      case STRAT_DECISION_TOTEM: \
	return FSM_NEXT (state, event, totem); \
      case STRAT_DECISION_UNLOAD: \
	return FSM_NEXT (state, event, unload); \
      } \
} while (0)

FSM_TRANS (TOP_START, init_start_round,
	   totem, TOP_TOTEM_GOING,
	   unload, TOP_UNLOAD_GOING)
{
    strat_init ();
    RETURN_TOP_DECISION_SWITCH (TOP_START, init_start_round);
}

/** TOTEM */

FSM_TRANS (TOP_TOTEM_GOING, move_success, TOP_TOTEM_APPROACHING)
{
    asserv_move_linearly (PATH_GRID_CLEARANCE_MM - BOT_SIZE_FRONT - 30);
    return FSM_NEXT (TOP_TOTEM_GOING, move_success);
}

FSM_TRANS (TOP_TOTEM_APPROACHING, robot_move_success, TOP_TOTEM_PUSHING)
{
    asserv_push_the_wall (0, -1, -1, -1);
    return FSM_NEXT (TOP_TOTEM_APPROACHING, robot_move_success);
}

FSM_TRANS (TOP_TOTEM_PUSHING, robot_move_success, TOP_TOTEM_GOING_BACK)
{
    asserv_stop_motor ();
    strat_success ();
    move_start_noangle (top.decision_pos, ASSERV_BACKWARD, 0);
    return FSM_NEXT (TOP_TOTEM_PUSHING, robot_move_success);
}

FSM_TRANS (TOP_TOTEM_GOING_BACK, move_success,
	   totem, TOP_TOTEM_GOING,
	   unload, TOP_UNLOAD_GOING)
{
    RETURN_TOP_DECISION_SWITCH (TOP_TOTEM_GOING_BACK, move_success);
}

/** UNLOAD */

FSM_TRANS (TOP_UNLOAD_GOING, move_success, TOP_UNLOADING)
{
    IO_SET (OUTPUT_DOOR_OPEN);
    IO_CLR (OUTPUT_DOOR_CLOSE);
    return FSM_NEXT (TOP_UNLOAD_GOING, move_success);
}

FSM_TRANS_TIMEOUT (TOP_UNLOADING, 250,
		   totem, TOP_TOTEM_GOING,
		   unload, TOP_UNLOAD_GOING)
{
    strat_success ();
    IO_CLR (OUTPUT_DOOR_OPEN);
    IO_SET (OUTPUT_DOOR_CLOSE);
    RETURN_TOP_DECISION_SWITCH (TOP_UNLOADING, TOP_UNLOADING_TIMEOUT);
}