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