d9e6533c8f8f47bf379b379615c09b7eeb231c1e
[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, lspickle, rspickle;
61 static volatile uint8_t state_status;
62 static volatile uint8_t state_i2c_ignore = 0;
63 static uint8_t cob_count;
64
65 /* short aliases */
66 #define INIT(mode)       ((mode) == I2C_COBBOARD_MODE_INIT)
67 #define HARVEST(mode)    ((mode) == I2C_COBBOARD_MODE_HARVEST)
68 #define EJECT(mode)      ((mode) == I2C_COBBOARD_MODE_EJECT)
69 #define KICKSTAND(mode)  ((mode) == I2C_COBBOARD_MODE_KICKSTAND)
70
71 uint8_t state_debug = 0;
72
73 uint8_t state_get_cob_count(void)
74 {
75         return cob_count;
76 }
77
78 static void state_debug_wait_key_pressed(void)
79 {
80         if (!state_debug)
81                 return;
82         printf_P(PSTR("press a key\r\n"));
83         while(!cmdline_keypressed());
84 }
85
86 /* return true if the cob is correctly inside */
87 static uint8_t state_cob_inside(void)
88 {
89         return sensor_get(S_COB_INSIDE_L) &&
90                 sensor_get(S_COB_INSIDE_R);
91 }
92
93 /* return true if there is no cob correctly inside */
94 static uint8_t state_no_cob_inside(void)
95 {
96         return !sensor_get(S_COB_INSIDE_L) &&
97                 !sensor_get(S_COB_INSIDE_R);
98 }
99
100 static uint8_t state_spicklemode_deployed(uint8_t side)
101 {
102         if (side == I2C_LEFT_SIDE)
103                 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
104         else
105                 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
106 }
107
108 static uint8_t state_spicklemode_autoharvest(uint8_t side)
109 {
110         if (side == I2C_LEFT_SIDE)
111                 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
112         else
113                 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
114 }
115
116 static uint8_t state_spicklemode_nomove(uint8_t side)
117 {
118         if (side == I2C_LEFT_SIDE)
119                 return lspickle & I2C_COBBOARD_SPK_NO_MOVE;
120         else
121                 return rspickle & I2C_COBBOARD_SPK_NO_MOVE;
122 }
123
124 uint8_t state_spicklemode_weak(uint8_t side)
125 {
126         if (side == I2C_LEFT_SIDE)
127                 return lspickle & I2C_COBBOARD_SPK_WEAK;
128         else
129                 return rspickle & I2C_COBBOARD_SPK_WEAK;
130 }
131
132 /* pack/deploy spickles depending on mode */
133 static void spickle_prepare(uint8_t side)
134 {
135         /* pack spickle if we are not in harvest mode */
136         if (!HARVEST(state_mode))
137                 spickle_pack(side);
138
139         /* we do nothing in mode no out */
140         if (state_spicklemode_nomove(side))
141                 return;
142
143         if (state_spicklemode_deployed(side))
144                 spickle_deploy(side);
145         else
146                 spickle_pack(side);
147 }
148
149 void state_set_spickle(uint8_t side, uint8_t flags)
150 {
151         if (side == I2C_LEFT_SIDE) {
152                 /* the PACK command preempts the current action if the
153                  * arm is not busy */
154                 if (lspickle != 0 && flags == 0 &&
155                     state_status != I2C_COBBOARD_STATUS_LBUSY)
156                         spickle_prepare(I2C_LEFT_SIDE);
157                 lspickle = flags;
158         }
159         else {
160                 /* the PACK command preempts the current action if the
161                  * arm is not busy */
162                 if (rspickle != 0 && flags == 0 &&
163                     state_status != I2C_COBBOARD_STATUS_RBUSY)
164                         spickle_prepare(I2C_RIGHT_SIDE);
165                 rspickle = flags;
166         }
167 }
168
169 /* set a new state, return 0 on success */
170 int8_t state_set_mode(uint8_t mode)
171 {
172         state_mode = mode;
173         STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
174         return 0;
175 }
176
177 void state_set_i2c_ignore(uint8_t val)
178 {
179         state_i2c_ignore = val;
180 }
181
182 uint8_t state_get_i2c_ignore(void)
183 {
184         return state_i2c_ignore;
185 }
186
187 /* check that state is the one in parameter and that state did not
188  * changed */
189 static uint8_t state_want_exit(void)
190 {
191         int16_t c;
192
193         /* force quit when CTRL-C is typed */
194         c = cmdline_getchar();
195         if (c == -1)
196                 return 0;
197         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
198                 return 1;
199         printf_P(PSTR("CTRL-C\r\n"));
200         return 0;
201 }
202
203 uint8_t state_get_mode(void)
204 {
205         return state_mode;
206 }
207
208 uint8_t state_get_status(void)
209 {
210         return state_status;
211 }
212
213 /* harvest cobs from area */
214 static void state_do_harvest(uint8_t side)
215 {
216         /* if there is no cob, return */
217         if (cob_falling_edge(side) == 0)
218                 return;
219
220         if (side == I2C_LEFT_SIDE)
221                 state_status = I2C_COBBOARD_STATUS_LBUSY;
222         else
223                 state_status = I2C_COBBOARD_STATUS_RBUSY;
224
225         STMCH_DEBUG("start");
226
227         /* eat the cob */
228         spickle_pack(side);
229
230         time_wait_ms(200);
231         cobroller_on(side);
232
233         /* check that cob is correctly in place */
234         if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
235                 if (state_no_cob_inside()) {
236                         STMCH_DEBUG("no cob");
237                         return;
238                 }
239                 STMCH_DEBUG("bad cob state");
240
241                 /* while cob is not correctly placed try to extract
242                  * it as much as we can */
243                 while (state_cob_inside() == 0 &&
244                        state_no_cob_inside() == 0) {
245                         uint8_t cpt = 0;
246                         if (cpt == 0)
247                                 cobroller_reverse(side);
248                         else if (cpt == 1)
249                                 cobroller_off(side);
250                         else if (cpt == 2)
251                                 cobroller_on(side);
252                         cpt ++;
253                         cpt %= 3;
254                         shovel_mid();
255                         time_wait_ms(250);
256                         shovel_down();
257                         time_wait_ms(250);
258                 }
259
260                 STMCH_DEBUG("cob removed");
261                 if (state_no_cob_inside()) {
262                         STMCH_DEBUG("no cob");
263                         return;
264                 }
265         }
266
267         /* cob is inside, switch off roller */
268         cobroller_off(side);
269         cob_count ++;
270
271         state_debug_wait_key_pressed();
272
273         /* last cob, nothing to do */
274         if (cob_count == 5)
275                 return;
276
277         /* redeploy the spickle if there is a black cob */
278         if (!state_spicklemode_nomove(side))
279                 spickle_deploy(side);
280         state_debug_wait_key_pressed();
281
282         /* let the loaded cobs go */
283         servo_door_block();
284         servo_carry_open();
285         time_wait_ms(100);
286         state_debug_wait_key_pressed();
287
288         /* store it */
289         shovel_up();
290
291         while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
292                 STMCH_DEBUG("shovel blocked");
293                 shovel_down();
294                 time_wait_ms(250);
295                 shovel_up();
296         }
297
298         state_debug_wait_key_pressed();
299
300         /* close the carry servos */
301         servo_carry_close();
302         servo_door_close();
303         time_wait_ms(200);
304         state_debug_wait_key_pressed();
305
306         shovel_down();
307
308         while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
309                 STMCH_DEBUG("shovel blocked");
310                 shovel_up();
311                 time_wait_ms(250);
312                 shovel_down();
313         }
314
315         STMCH_DEBUG("end");
316 }
317
318 /* eject cobs */
319 static void state_do_eject(void)
320 {
321         state_status = I2C_COBBOARD_STATUS_EJECT;
322         cob_count = 0;
323         shovel_mid();
324         servo_carry_open();
325         servo_door_open();
326         time_wait_ms(2000);
327         shovel_down();
328         servo_door_close();
329         servo_carry_close();
330 }
331
332 /* main state machine */
333 void state_machine(void)
334 {
335         while (state_want_exit() == 0) {
336
337                 state_status = I2C_COBBOARD_STATUS_READY;
338
339                 /* init */
340                 if (INIT(state_mode)) {
341                         state_init();
342                         state_mode = I2C_COBBOARD_MODE_HARVEST;
343                 }
344
345                 if (HARVEST(state_mode)) {
346                         /* init for each loop */
347                         shovel_down();
348                         servo_carry_close();
349                         servo_door_close();
350                         shovel_current_limit_enable(0);
351
352                         /* pack/deply spickles, enable/disable roller */
353                         cobroller_off(I2C_LEFT_SIDE);
354                         cobroller_off(I2C_RIGHT_SIDE);
355                         spickle_prepare(I2C_LEFT_SIDE);
356                         spickle_prepare(I2C_RIGHT_SIDE);
357
358                         /* harvest if not many cobs */
359                         if (cob_count < 5) {
360                                 if (state_spicklemode_deployed(I2C_LEFT_SIDE) &&
361                                     state_spicklemode_autoharvest(I2C_LEFT_SIDE))
362                                         state_do_harvest(I2C_LEFT_SIDE);
363                                 if (state_spicklemode_deployed(I2C_RIGHT_SIDE) &&
364                                     state_spicklemode_autoharvest(I2C_RIGHT_SIDE))
365                                         state_do_harvest(I2C_RIGHT_SIDE);
366                         }
367                 }
368
369                 /* help to climb the hill */
370                 if (KICKSTAND(state_mode)) {
371                         servo_carry_open();
372                         servo_door_open();
373                         shovel_current_limit_enable(1);
374                         shovel_kickstand();
375                 }
376
377                 /* eject */
378                 if (EJECT(state_mode)) {
379                         state_mode = I2C_COBBOARD_MODE_HARVEST;
380                         state_do_eject();
381                 }
382         }
383         state_status = I2C_COBBOARD_STATUS_OFF;
384 }
385
386 void state_init(void)
387 {
388         vt100_init(&local_vt100);
389         shovel_down();
390         servo_door_close();
391         servo_carry_close();
392         spickle_pack(I2C_LEFT_SIDE);
393         spickle_pack(I2C_RIGHT_SIDE);
394         state_mode = 0;
395         cob_count = 0;
396         state_status = I2C_COBBOARD_STATUS_OFF;
397 }