cobboard: prepare state machine
[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
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 static void state_debug_wait_key_pressed(void)
82 {
83         if (!state_debug)
84                 return;
85         printf_P(PSTR("press a key\r\n"));
86         while(!cmdline_keypressed());
87 }
88
89 /* return true if cob is present */
90 static uint8_t state_cob_present(uint8_t side)
91 {
92         if (side == I2C_LEFT_SIDE)
93                 return sensor_get(S_LCOB);
94         else
95                 /* XXX */
96                 //              return sensor_get(S_LCOB);
97                 return 0;
98 }
99
100 /* return the detected color of the cob (only valid if present) */
101 static uint8_t state_cob_color(uint8_t side)
102 {
103         if (side == I2C_LEFT_SIDE)
104                 return I2C_COB_WHITE;
105         else
106                 /* XXX */
107                 //              return sensor_get(S_LCOB);
108                 return 0;
109 }
110
111 /* set a new state, return 0 on success */
112 int8_t state_set_mode(uint8_t mode)
113 {
114         state_mode = mode;
115         /* XXX synchrounous pack here */
116         STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d "
117                     "r_deploy=%d r_harvest=%d eject=%d",
118                     __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode),
119                     R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode));
120         return 0;
121 }
122
123 /* check that state is the one in parameter and that state did not
124  * changed */
125 static uint8_t state_want_exit(void)
126 {
127         int16_t c;
128
129         /* force quit when CTRL-C is typed */
130         c = cmdline_getchar();
131         if (c == -1)
132                 return 0;
133         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
134                 return 1;
135         return 0;
136 }
137
138 uint8_t state_get_mode(void)
139 {
140         return state_mode;
141 }
142
143 /* harvest cobs from area */
144 static void state_do_harvest(uint8_t side)
145 {
146         uint16_t delay;
147
148         /* if there is no cob, return */
149         if (state_cob_present(side))
150                 return;
151                 
152         /* if it is black, nothing to do */
153         if (state_cob_color(side) == I2C_COB_BLACK)
154                 return;
155
156         /* eat the cob */
157         spickle_pack(side);
158         state_debug_wait_key_pressed();
159         delay = spickle_get_pack_delay(side);
160         time_wait_ms(delay);
161
162         /* redeploy the spickle */
163         spickle_deploy(side);
164         state_debug_wait_key_pressed();
165
166         cob_count ++;
167
168         /* store it */
169         shovel_up();
170         wait_ms(200);
171         state_debug_wait_key_pressed();
172         shovel_down();
173         state_debug_wait_key_pressed();
174 }
175
176 /* eject cobs */
177 static void state_do_eject(void)
178 {
179         cob_count = 0;
180         shovel_mid();
181         time_wait_ms(2000);
182         shovel_down();
183 }
184
185 /* main state machine */
186 void state_machine(void)
187 {
188         while (state_want_exit() == 0) {
189
190                 /* pack spickles */
191                 if (!L_DEPLOY(state_mode))
192                         spickle_pack(I2C_LEFT_SIDE);
193                 if (!R_DEPLOY(state_mode))
194                         spickle_pack(I2C_RIGHT_SIDE);
195
196                 /* harvest */
197                 if (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
198                         state_do_harvest(I2C_LEFT_SIDE);
199                 if (R_DEPLOY(state_mode) && R_HARVEST(state_mode))
200                         state_do_harvest(I2C_RIGHT_SIDE);
201
202                 /* eject */
203                 if (EJECT(state_mode))
204                         state_do_eject();
205         }
206 }
207
208 void state_init(void)
209 {
210         vt100_init(&local_vt100);
211         shovel_down();
212         spickle_pack(I2C_LEFT_SIDE);
213         spickle_pack(I2C_RIGHT_SIDE);
214         state_mode = 0;
215         cob_count = 0;
216 }