summaryrefslogtreecommitdiffhomepage
path: root/digital/io-hub/src/robospierre/top.c
blob: 5756d38ec8c541cf385887bbe4ff5a4be6e25b5e (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
/* top.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 "playground_2011.h"
#include "asserv.h"

#define FSM_NAME AI
#include "fsm.h"

#include "logistic.h"
#include "move.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 out of start area. */
	    TOP_GOING_OUT1,
	    /* Going out, first pawn emplacement. */
	    TOP_GOING_OUT2,

	    TOP_GOING_TO_DROP,
	    TOP_GOING_TO_ELEMENT,
	    /* Waiting clamp has finished its work. */
	    TOP_WAITING_CLAMP)

FSM_START_WITH (TOP_START)

/** Top context. */
struct top_t
{
    /** Target element. */
    uint8_t target_element_id;
};

/** Global context. */
struct top_t top_global;
#define ctx top_global

FSM_TRANS (TOP_START, init_start_round, TOP_GOING_OUT1)
{
    element_init ();
    asserv_goto (PG_X (PG_GREEN_WIDTH_MM + 100), PG_Y (PG_LENGTH - 200), 0);
    return FSM_NEXT (TOP_START, init_start_round);
}

FSM_TRANS (TOP_GOING_OUT1, robot_move_success, TOP_GOING_OUT2)
{
    asserv_goto (PG_X (1500 - 2 * 350), PG_Y (PG_LENGTH - 350), 0);
    return FSM_NEXT (TOP_GOING_OUT1, robot_move_success);
}

static uint8_t
top_go_element (void)
{
    position_t robot_pos;
    asserv_get_position (&robot_pos);
    ctx.target_element_id = element_best (robot_pos);
    vect_t element_pos = element_get_pos (ctx.target_element_id);
    uint8_t backward = logistic_global.collect_direction == DIRECTION_FORWARD
	? 0 : ASSERV_BACKWARD;
    move_start_noangle (element_pos, backward, 0);
    return 1;
}

static uint8_t
top_go_drop (void)
{
    position_t robot_pos;
    asserv_get_position (&robot_pos);
    uint8_t drop_pos_id = 37;
    vect_t drop_pos = element_get_pos (drop_pos_id);
    uint8_t backward = logistic_global.collect_direction == DIRECTION_FORWARD
	? 0 : ASSERV_BACKWARD;
    move_start_noangle (drop_pos, backward, 0);
    return 0;
}

static uint8_t
top_decision (void)
{
    /* If we can make a tower. */
    if (logistic_global.construct_possible == 1)
	return top_go_drop ();
    else
	return top_go_element ();
}

FSM_TRANS (TOP_GOING_OUT2, robot_move_success,
	   drop, TOP_GOING_TO_DROP,
	   element, TOP_GOING_TO_ELEMENT)
{
    switch (top_decision ())
      {
      default: return FSM_NEXT (TOP_GOING_OUT2, robot_move_success, drop);
      case 1: return FSM_NEXT (TOP_GOING_OUT2, robot_move_success, element);
      }
}

FSM_TRANS (TOP_GOING_TO_DROP, move_success,
	   drop, TOP_GOING_TO_DROP,
	   element, TOP_GOING_TO_ELEMENT)
{
    clamp_drop (logistic_global.collect_direction);
    switch (top_decision ())
      {
      default: return FSM_NEXT (TOP_GOING_TO_DROP, move_success, drop);
      case 1: return FSM_NEXT (TOP_GOING_TO_DROP, move_success, element);
      }
}

FSM_TRANS (TOP_GOING_TO_ELEMENT, move_success,
	   clamp_working, TOP_WAITING_CLAMP,
	   drop, TOP_GOING_TO_DROP,
	   element, TOP_GOING_TO_ELEMENT)
{
    element_taken (ctx.target_element_id, ELEMENT_PAWN);
    if (clamp_working ())
	return FSM_NEXT (TOP_GOING_TO_ELEMENT, move_success, clamp_working);
    switch (top_decision ())
      {
      default: return FSM_NEXT (TOP_GOING_TO_ELEMENT, move_success, drop);
      case 1: return FSM_NEXT (TOP_GOING_TO_ELEMENT, move_success, element);
      }
}

FSM_TRANS (TOP_WAITING_CLAMP, clamp_done,
	   drop, TOP_GOING_TO_DROP,
	   element, TOP_GOING_TO_ELEMENT)
{
    switch (top_decision ())
      {
      default: return FSM_NEXT (TOP_WAITING_CLAMP, clamp_done, drop);
      case 1: return FSM_NEXT (TOP_WAITING_CLAMP, clamp_done, element);
      }
}