ballboard blocking detection
[aversive.git] / projects / microb2010 / ballboard / 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 "state.h"
52
53 #define STMCH_DEBUG(args...) DEBUG(E_USER_ST_MACH, args)
54 #define STMCH_NOTICE(args...) NOTICE(E_USER_ST_MACH, args)
55 #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args)
56
57 static struct vt100 local_vt100;
58 static volatile uint8_t state_mode;
59 static volatile uint8_t state_status;
60 static volatile uint8_t ball_count;
61
62 /* short aliases */
63 #define INIT I2C_BALLBOARD_MODE_INIT
64 #define OFF I2C_BALLBOARD_MODE_OFF
65 #define HARVEST I2C_BALLBOARD_MODE_HARVEST
66 #define EJECT I2C_BALLBOARD_MODE_EJECT
67 #define PREP_L_FORK I2C_BALLBOARD_MODE_PREP_L_FORK
68 #define TAKE_L_FORK I2C_BALLBOARD_MODE_TAKE_L_FORK
69 #define PREP_R_FORK I2C_BALLBOARD_MODE_PREP_R_FORK
70 #define TAKE_R_FORK I2C_BALLBOARD_MODE_TAKE_R_FORK
71
72 uint8_t state_debug = 0;
73
74 #if 0
75 static void state_dump_sensors(void)
76 {
77         STMCH_DEBUG("TODO\n");
78 }
79 #endif
80
81 uint8_t state_get_ball_count(void)
82 {
83         return ball_count;
84 }
85
86 #if 0
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 #endif
95
96 /* set a new state, return 0 on success */
97 int8_t state_set_mode(uint8_t mode)
98 {
99         state_mode = mode;
100         STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode);
101
102 /*      STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
103 /*                  "r_deploy=%d r_harvest=%d eject=%d", */
104 /*                  __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
105 /*                  R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
106
107         return 0;
108 }
109
110 /* check that state is the one in parameter and that state did not
111  * changed */
112 static uint8_t state_want_exit(void)
113 {
114         int16_t c;
115
116         /* force quit when CTRL-C is typed */
117         c = cmdline_getchar();
118         if (c == -1)
119                 return 0;
120         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
121                 return 1;
122         printf_P(PSTR("CTRL-C\r\n"));
123         return 0;
124 }
125
126 uint8_t state_get_status(void)
127 {
128         return state_status;
129 }
130
131 /* harvest balls from area */
132 static void state_do_harvest(void)
133 {
134         //state_debug_wait_key_pressed();
135
136         if (bd_get(&ballboard.roller.bd)) {
137                 STMCH_DEBUG("%s(): roller blocked", __FUNCTION__);
138                 roller_reverse();
139                 time_wait_ms(500);
140                 bd_reset(&ballboard.roller.bd);
141                 STMCH_DEBUG("%s(): roller restart", __FUNCTION__);
142                 return;
143         }
144
145         /* deduct ball count */
146         if (sensor_get(S_LOW_BARRIER) && !sensor_get(S_HIGH_BARRIER))
147                 ball_count = 1;
148         else if (sensor_get(S_LOW_BARRIER) && sensor_get(S_HIGH_BARRIER))
149                 ball_count = 3;
150
151         if (sensor_get(S_HIGH_BARRIER))
152                 roller_off();
153         else
154                 roller_on();
155 }
156
157 /* eject balls */
158 static void state_do_eject(void)
159 {
160         uint8_t i, blocked;
161         microseconds us;
162
163         for (i = 0; i < 3; i ++) {
164
165                 roller_reverse();
166
167                 us = time_get_us2();
168                 blocked = 0;
169
170                 while (1) {
171
172                         /* no more balls (sensor is heavily filtered) */
173                         if (!sensor_get(S_LOW_BARRIER)) {
174                                 STMCH_DEBUG("%s(): no more balls", __FUNCTION__);
175                                 break;
176                         }
177
178                         /* timeout */
179                         if ((time_get_us2() - us) > 1000UL * 1000UL) {
180                                 STMCH_DEBUG("%s(): eject timeout", __FUNCTION__);
181                                 blocked = 1;
182                                 break;
183                         }
184
185                         /* blocking ! */
186                         if (bd_get(&ballboard.roller.bd)) {
187                                 blocked = 1;
188                                 break;
189                         }
190                 }
191
192                 if (!blocked)
193                         break;
194
195                 STMCH_DEBUG("%s(): roller blocked", __FUNCTION__);
196                 roller_on();
197                 time_wait_ms(500);
198                 bd_reset(&ballboard.roller.bd);
199                 STMCH_DEBUG("%s(): roller restart", __FUNCTION__);
200         }
201 }
202
203 /* main state machine */
204 void state_machine(void)
205 {
206         uint8_t mode = 0;
207
208         while (state_want_exit() == 0) {
209
210                 if (state_mode != mode) {
211                         mode = state_mode;
212                         STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode);
213                 }
214
215                 switch (state_mode) {
216
217                 case INIT:
218                         state_init();
219                         state_mode = OFF;
220                         state_status = I2C_BALLBOARD_STATUS_F_READY;
221                         break;
222
223                 case OFF:
224                         state_status = I2C_BALLBOARD_STATUS_F_READY;
225                         roller_off();
226                         break;
227
228                 case HARVEST:
229                         state_status = I2C_BALLBOARD_STATUS_F_READY;
230                         state_do_harvest();
231                         break;
232
233                 case EJECT:
234                         state_status = I2C_BALLBOARD_STATUS_F_BUSY;
235                         state_do_eject();
236                         state_status = I2C_BALLBOARD_STATUS_F_READY;
237                         state_mode = HARVEST;
238                         break;
239
240                 default:
241                         break;
242                 }
243         }
244 }
245
246 void state_init(void)
247 {
248         vt100_init(&local_vt100);
249         state_mode = 0;
250         state_status = I2C_BALLBOARD_STATUS_F_READY;
251         ball_count = 0;
252 }