9d082bacf7aabb9b606b654d00fd635bac4e0e12
[aversive.git] / projects / microb2010 / cobboard / state.c
1 /*
2  *  Copyright Droids Corporation (2009)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: state.c,v 1.5 2009-11-08 17:25:00 zer0 Exp $
19  *
20  */
21
22 #include <math.h>
23 #include <string.h>
24
25 #include <aversive.h>
26 #include <aversive/wait.h>
27 #include <aversive/error.h>
28
29 #include <ax12.h>
30 #include <uart.h>
31 #include <spi.h>
32 #include <encoders_spi.h>
33 #include <pwm_ng.h>
34 #include <timer.h>
35 #include <scheduler.h>
36 #include <clock_time.h>
37
38 #include <pid.h>
39 #include <quadramp.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
42
43 #include <rdline.h>
44 #include <vt100.h>
45
46 #include "../common/i2c_commands.h"
47 #include "main.h"
48 #include "cmdline.h"
49 #include "sensor.h"
50 #include "actuator.h"
51 #include "spickle.h"
52 #include "shovel.h"
53 #include "state.h"
54
55 #define STMCH_DEBUG(args...) DEBUG(E_USER_ST_MACH, args)
56 #define STMCH_NOTICE(args...) NOTICE(E_USER_ST_MACH, args)
57 #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args)
58
59 static struct vt100 local_vt100;
60 static volatile uint8_t state_mode;
61 static uint8_t cob_count;
62
63 /* short aliases */
64 #define L_DEPLOY(mode)   (!!((mode) & I2C_COBBOARD_MODE_L_DEPLOY))
65 #define R_DEPLOY(mode)   (!!((mode) & I2C_COBBOARD_MODE_R_DEPLOY))
66 #define DEPLOY(side, mode) ((side) == I2C_LEFT_SIDE ? L_DEPLOY(mode) : R_DEPLOY(mode))
67 #define L_HARVEST(mode)  (!!((mode) & I2C_COBBOARD_MODE_L_HARVEST))
68 #define R_HARVEST(mode)  (!!((mode) & I2C_COBBOARD_MODE_R_HARVEST))
69 #define HARVEST(side, mode) ((side) == I2C_LEFT_SIDE ? L_HARVEST(mode) : R_HARVEST(mode))
70 #define EJECT(mode)      (!!((mode) & I2C_COBBOARD_MODE_EJECT))
71 #define INIT(mode)       (!!((mode) & I2C_COBBOARD_MODE_INIT))
72
73 uint8_t state_debug = 0;
74
75 #if 0
76 static void state_dump_sensors(void)
77 {
78         STMCH_DEBUG("TODO\n");
79 }
80 #endif
81
82 uint8_t state_get_cob_count(void)
83 {
84         return cob_count;
85 }
86
87 static void state_debug_wait_key_pressed(void)
88 {
89         if (!state_debug)
90                 return;
91         printf_P(PSTR("press a key\r\n"));
92         while(!cmdline_keypressed());
93 }
94
95 /* return true if cob is present */
96 static uint8_t state_cob_present(uint8_t side)
97 {
98         if (side == I2C_LEFT_SIDE)
99                 return sensor_get(S_LCOB);
100         else
101                 return sensor_get(S_RCOB);
102 }
103
104 /* return the detected color of the cob (only valid if present) */
105 static uint8_t state_cob_color(uint8_t side)
106 {
107         /* XXX no color sensor for now */
108         if (side == I2C_LEFT_SIDE)
109                 return I2C_COB_WHITE;
110         else
111                 return I2C_COB_WHITE;
112 }
113
114 /* return true if the cob is correctly inside */
115 static uint8_t state_cob_inside(void)
116 {
117         return sensor_get(S_COB_INSIDE_L) &&
118                 sensor_get(S_COB_INSIDE_R);
119 }
120
121 /* return true if there is no cob correctly inside */
122 static uint8_t state_no_cob_inside(void)
123 {
124         return !sensor_get(S_COB_INSIDE_L) &&
125                 !sensor_get(S_COB_INSIDE_R);
126 }
127
128 /* set a new state, return 0 on success */
129 int8_t state_set_mode(uint8_t mode)
130 {
131         state_mode = mode;
132
133 /*      STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
134 /*                  "r_deploy=%d r_harvest=%d eject=%d", */
135 /*                  __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
136 /*                  R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
137
138         return 0;
139 }
140
141 /* check that state is the one in parameter and that state did not
142  * changed */
143 static uint8_t state_want_exit(void)
144 {
145         int16_t c;
146
147         /* force quit when CTRL-C is typed */
148         c = cmdline_getchar();
149         if (c == -1)
150                 return 0;
151         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
152                 return 1;
153         printf_P(PSTR("CTRL-C\r\n"));
154         return 0;
155 }
156
157 uint8_t state_get_mode(void)
158 {
159         return state_mode;
160 }
161
162 /* harvest cobs from area */
163 static void state_do_harvest(uint8_t side)
164 {
165         /* if there is no cob, return */
166         if (state_cob_present(side))
167                 return;
168
169         /* if it is black, nothing to do */
170         if (state_cob_color(side) == I2C_COB_BLACK)
171                 return;
172
173         STMCH_DEBUG("start");
174
175         /* eat the cob */
176         spickle_pack(side);
177
178         time_wait_ms(250);
179         cobroller_on(side);
180
181         if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
182                 if (state_no_cob_inside()) {
183                         printf_P(PSTR("NO COB\r\n"));
184                         return;
185                 }
186                 printf_P(PSTR("BAD COB - press a key\r\n"));
187                 while(!cmdline_keypressed());
188         }
189
190         /* cob is inside, switch off roller */
191         cobroller_off(side);
192         cob_count ++;
193
194         /* last cob, nothing to do */
195         if (cob_count == 5)
196                 return;
197
198         /* redeploy the spickle */
199         spickle_deploy(side);
200         state_debug_wait_key_pressed();
201
202         /* let the loaded cobs go */
203         servo_door_block();
204         servo_carry_open();
205         time_wait_ms(100);
206         state_debug_wait_key_pressed();
207
208         /* store it */
209         shovel_up();
210
211         if (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 400) == 0) {
212                 BRAKE_ON();
213                 printf_P(PSTR("BLOCKED\r\n"));
214                 while(!cmdline_keypressed());
215         }
216
217         state_debug_wait_key_pressed();
218
219         /* close the carry servos */
220         servo_carry_close();
221         servo_door_close();
222         time_wait_ms(200);
223         state_debug_wait_key_pressed();
224
225         shovel_down();
226
227         if (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
228                 BRAKE_ON();
229                 printf_P(PSTR("BLOCKED\r\n"));
230                 while(!cmdline_keypressed());
231         }
232
233         STMCH_DEBUG("end");
234 }
235
236 /* eject cobs */
237 static void state_do_eject(void)
238 {
239         cob_count = 0;
240         shovel_mid();
241         servo_carry_open();
242         servo_door_open();
243         time_wait_ms(2000);
244         shovel_down();
245         servo_door_close();
246         servo_carry_close();
247 }
248
249 /* main state machine */
250 void state_machine(void)
251 {
252         while (state_want_exit() == 0) {
253
254                 /* init */
255                 if (INIT(state_mode)) {
256                         state_mode &= (~I2C_COBBOARD_MODE_INIT);
257                         state_init();
258                 }
259
260                 cobroller_off(I2C_LEFT_SIDE);
261                 cobroller_off(I2C_RIGHT_SIDE);
262
263                 /* pack/deply spickles, enable/disable roller */
264                 if (cob_count >= 5)
265                         spickle_pack(I2C_LEFT_SIDE);
266                 else if (L_DEPLOY(state_mode) && !L_HARVEST(state_mode))
267                         spickle_mid(I2C_LEFT_SIDE);
268                 else if (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
269                         spickle_deploy(I2C_LEFT_SIDE);
270                 else
271                         spickle_pack(I2C_LEFT_SIDE);
272
273                 if (cob_count >= 5)
274                         spickle_pack(I2C_RIGHT_SIDE);
275                 else if (R_DEPLOY(state_mode) && !R_HARVEST(state_mode))
276                         spickle_mid(I2C_RIGHT_SIDE);
277                 else if (R_DEPLOY(state_mode) && R_HARVEST(state_mode))
278                         spickle_deploy(I2C_RIGHT_SIDE);
279                 else
280                         spickle_pack(I2C_RIGHT_SIDE);
281
282                 /* harvest */
283                 if (cob_count < 5) {
284                         if (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
285                                 state_do_harvest(I2C_LEFT_SIDE);
286                         if (R_DEPLOY(state_mode) && R_HARVEST(state_mode))
287                                 state_do_harvest(I2C_RIGHT_SIDE);
288                 }
289
290                 /* eject */
291                 if (EJECT(state_mode)) {
292                         state_mode &= (~I2C_COBBOARD_MODE_EJECT);
293                         state_do_eject();
294                 }
295         }
296 }
297
298 void state_init(void)
299 {
300         vt100_init(&local_vt100);
301         shovel_down();
302         servo_door_close();
303         servo_carry_close();
304         spickle_pack(I2C_LEFT_SIDE);
305         spickle_pack(I2C_RIGHT_SIDE);
306         state_mode = 0;
307         cob_count = 0;
308 }