0492e1855991c61e94fb972aa1bb6b0e76a50151
[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 uint8_t ball_count;
60
61 /* short aliases */
62 #define INIT I2C_BALLBOARD_MODE_INIT
63 #define OFF I2C_BALLBOARD_MODE_OFF
64 #define HARVEST I2C_BALLBOARD_MODE_HARVEST
65 #define EJECT I2C_BALLBOARD_MODE_EJECT
66 #define PREP_L_FORK I2C_BALLBOARD_MODE_PREP_L_FORK
67 #define TAKE_L_FORK I2C_BALLBOARD_MODE_TAKE_L_FORK
68 #define PREP_R_FORK I2C_BALLBOARD_MODE_PREP_R_FORK
69 #define TAKE_R_FORK I2C_BALLBOARD_MODE_TAKE_R_FORK
70
71 uint8_t state_debug = 0;
72
73 #if 0
74 static void state_dump_sensors(void)
75 {
76         STMCH_DEBUG("TODO\n");
77 }
78 #endif
79
80 uint8_t state_get_ball_count(void)
81 {
82         return ball_count;
83 }
84
85 static void state_debug_wait_key_pressed(void)
86 {
87         if (!state_debug)
88                 return;
89         printf_P(PSTR("press a key\r\n"));
90         while (!cmdline_keypressed());
91 }
92
93 /* set a new state, return 0 on success */
94 int8_t state_set_mode(uint8_t mode)
95 {
96         state_mode = mode;
97         STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode);
98
99 /*      STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
100 /*                  "r_deploy=%d r_harvest=%d eject=%d", */
101 /*                  __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
102 /*                  R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
103
104         return 0;
105 }
106
107 /* check that state is the one in parameter and that state did not
108  * changed */
109 static uint8_t state_want_exit(void)
110 {
111         int16_t c;
112
113         /* force quit when CTRL-C is typed */
114         c = cmdline_getchar();
115         if (c == -1)
116                 return 0;
117         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
118                 return 1;
119         printf_P(PSTR("CTRL-C\r\n"));
120         return 0;
121 }
122
123 uint8_t state_get_mode(void)
124 {
125         return state_mode;
126 }
127
128 /* harvest balls from area */
129 static void state_do_harvest(void)
130 {
131         state_debug_wait_key_pressed();
132         roller_on();
133 }
134
135 /* eject balls */
136 static void state_do_eject(void)
137 {
138         roller_reverse();
139         time_wait_ms(2000);
140 }
141
142 /* main state machine */
143 void state_machine(void)
144 {
145         while (state_want_exit() == 0) {
146
147                 switch (state_mode) {
148
149                 case INIT:
150                         state_mode = OFF;
151                         state_init();
152                         break;
153
154                 case OFF:
155                         roller_off();
156                         break;
157
158                 case HARVEST:
159                         state_do_harvest();
160                         break;
161
162                 case EJECT:
163                         state_mode = HARVEST;
164                         state_do_eject();
165                         break;
166
167                 default:
168                         break;
169                 }
170         }
171 }
172
173 void state_init(void)
174 {
175         vt100_init(&local_vt100);
176         state_mode = 0;
177         ball_count = 0;
178 }