70ae08427171e37d5a379141bac646b5188fdff5
[aversive.git] / projects / microb2010 / ballboard / commands_ballboard.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: commands_ballboard.c,v 1.2 2009-04-24 19:30:42 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
29
30 #include <ax12.h>
31 #include <uart.h>
32 #include <pwm_ng.h>
33 #include <clock_time.h>
34 #include <spi.h>
35
36 #include <pid.h>
37 #include <quadramp.h>
38 #include <control_system_manager.h>
39 #include <blocking_detection_manager.h>
40
41 #include <rdline.h>
42 #include <parse.h>
43 #include <parse_string.h>
44 #include <parse_num.h>
45
46 #include "main.h"
47 #include "state.h"
48 #include "cmdline.h"
49 #include "../common/i2c_commands.h"
50 #include "i2c_protocol.h"
51 #include "actuator.h"
52
53 struct cmd_event_result {
54         fixed_string_t arg0;
55         fixed_string_t arg1;
56         fixed_string_t arg2;
57 };
58
59
60 /* function called when cmd_event is parsed successfully */
61 static void cmd_event_parsed(void *parsed_result, void *data)
62 {
63         u08 bit=0;
64
65         struct cmd_event_result * res = parsed_result;
66
67         if (!strcmp_P(res->arg1, PSTR("all"))) {
68                 bit = 0xFF;
69                 if (!strcmp_P(res->arg2, PSTR("on")))
70                         ballboard.flags |= bit;
71                 else if (!strcmp_P(res->arg2, PSTR("off")))
72                         ballboard.flags &= bit;
73                 else { /* show */
74                         printf_P(PSTR("encoders is %s\r\n"),
75                                  (DO_ENCODERS & ballboard.flags) ? "on":"off");
76                         printf_P(PSTR("cs is %s\r\n"),
77                                  (DO_CS & ballboard.flags) ? "on":"off");
78                         printf_P(PSTR("bd is %s\r\n"),
79                                  (DO_BD & ballboard.flags) ? "on":"off");
80                         printf_P(PSTR("power is %s\r\n"),
81                                  (DO_POWER & ballboard.flags) ? "on":"off");
82                         printf_P(PSTR("errblock is %s\r\n"),
83                                  (DO_ERRBLOCKING & ballboard.flags) ? "on":"off");
84                 }
85                 return;
86         }
87
88         if (!strcmp_P(res->arg1, PSTR("encoders")))
89                 bit = DO_ENCODERS;
90         else if (!strcmp_P(res->arg1, PSTR("cs"))) {
91                 bit = DO_CS;
92         }
93         else if (!strcmp_P(res->arg1, PSTR("bd")))
94                 bit = DO_BD;
95         else if (!strcmp_P(res->arg1, PSTR("power")))
96                 bit = DO_POWER;
97         else if (!strcmp_P(res->arg1, PSTR("errblock")))
98                 bit = DO_ERRBLOCKING;
99
100
101         if (!strcmp_P(res->arg2, PSTR("on")))
102                 ballboard.flags |= bit;
103         else if (!strcmp_P(res->arg2, PSTR("off"))) {
104                 if (!strcmp_P(res->arg1, PSTR("cs"))) {
105                         pwm_ng_set(ROLLER_PWM, 0);
106                         pwm_ng_set(FORKTRANS_PWM, 0);
107                         pwm_ng_set(FORKROT_PWM, 0);
108                 }
109                 ballboard.flags &= (~bit);
110         }
111         printf_P(PSTR("%s is %s\r\n"), res->arg1,
112                       (bit & ballboard.flags) ? "on":"off");
113 }
114
115 prog_char str_event_arg0[] = "event";
116 parse_pgm_token_string_t cmd_event_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg0, str_event_arg0);
117 prog_char str_event_arg1[] = "all#encoders#cs#bd#power#errblock";
118 parse_pgm_token_string_t cmd_event_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg1, str_event_arg1);
119 prog_char str_event_arg2[] = "on#off#show";
120 parse_pgm_token_string_t cmd_event_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg2, str_event_arg2);
121
122 prog_char help_event[] = "Enable/disable events";
123 parse_pgm_inst_t cmd_event = {
124         .f = cmd_event_parsed,  /* function to call */
125         .data = NULL,      /* 2nd arg of func */
126         .help_str = help_event,
127         .tokens = {        /* token list, NULL terminated */
128                 (prog_void *)&cmd_event_arg0,
129                 (prog_void *)&cmd_event_arg1,
130                 (prog_void *)&cmd_event_arg2,
131                 NULL,
132         },
133 };
134
135 /**********************************************************/
136 /* Color */
137
138 /* this structure is filled when cmd_color is parsed successfully */
139 struct cmd_color_result {
140         fixed_string_t arg0;
141         fixed_string_t color;
142 };
143
144 /* function called when cmd_color is parsed successfully */
145 static void cmd_color_parsed(void *parsed_result, void *data)
146 {
147         struct cmd_color_result *res = (struct cmd_color_result *) parsed_result;
148         if (!strcmp_P(res->color, PSTR("yellow"))) {
149                 ballboard.our_color = I2C_COLOR_YELLOW;
150         }
151         else if (!strcmp_P(res->color, PSTR("blue"))) {
152                 ballboard.our_color = I2C_COLOR_BLUE;
153         }
154         printf_P(PSTR("Done\r\n"));
155 }
156
157 prog_char str_color_arg0[] = "color";
158 parse_pgm_token_string_t cmd_color_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_color_result, arg0, str_color_arg0);
159 prog_char str_color_color[] = "blue#yellow";
160 parse_pgm_token_string_t cmd_color_color = TOKEN_STRING_INITIALIZER(struct cmd_color_result, color, str_color_color);
161
162 prog_char help_color[] = "Set our color";
163 parse_pgm_inst_t cmd_color = {
164         .f = cmd_color_parsed,  /* function to call */
165         .data = NULL,      /* 2nd arg of func */
166         .help_str = help_color,
167         .tokens = {        /* token list, NULL terminated */
168                 (prog_void *)&cmd_color_arg0,
169                 (prog_void *)&cmd_color_color,
170                 NULL,
171         },
172 };
173
174
175 /**********************************************************/
176 /* State1 */
177
178 /* this structure is filled when cmd_state1 is parsed successfully */
179 struct cmd_state1_result {
180         fixed_string_t arg0;
181         fixed_string_t arg1;
182 };
183
184 /* function called when cmd_state1 is parsed successfully */
185 static void cmd_state1_parsed(void *parsed_result,
186                               __attribute__((unused)) void *data)
187 {
188         struct cmd_state1_result *res = parsed_result;
189
190         if (!strcmp_P(res->arg1, PSTR("init")))
191                 state_init();
192         else if (!strcmp_P(res->arg1, PSTR("off")))
193                 state_set_mode(I2C_BALLBOARD_MODE_OFF);
194         else if (!strcmp_P(res->arg1, PSTR("eject")))
195                 state_set_mode(I2C_BALLBOARD_MODE_EJECT);
196         else if (!strcmp_P(res->arg1, PSTR("harvest")))
197                 state_set_mode(I2C_BALLBOARD_MODE_HARVEST);
198         else if (!strcmp_P(res->arg1, PSTR("prepare")))
199                 state_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
200         else if (!strcmp_P(res->arg1, PSTR("take")))
201                 state_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
202
203         /* other commands */
204 }
205
206 prog_char str_state1_arg0[] = "ballboard";
207 parse_pgm_token_string_t cmd_state1_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg0, str_state1_arg0);
208 prog_char str_state1_arg1[] = "init#eject#harvest#off#prepare#take";
209 parse_pgm_token_string_t cmd_state1_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg1, str_state1_arg1);
210
211 prog_char help_state1[] = "set ballboard mode";
212 parse_pgm_inst_t cmd_state1 = {
213         .f = cmd_state1_parsed,  /* function to call */
214         .data = NULL,      /* 2nd arg of func */
215         .help_str = help_state1,
216         .tokens = {        /* token list, NULL terminated */
217                 (prog_void *)&cmd_state1_arg0,
218                 (prog_void *)&cmd_state1_arg1,
219                 NULL,
220         },
221 };
222
223 /**********************************************************/
224 /* State2 */
225
226 /* this structure is filled when cmd_state2 is parsed successfully */
227 struct cmd_state2_result {
228         fixed_string_t arg0;
229         fixed_string_t arg1;
230         fixed_string_t arg2;
231 };
232
233 /* function called when cmd_state2 is parsed successfully */
234 static void cmd_state2_parsed(void *parsed_result,
235                               __attribute__((unused)) void *data)
236 {
237 }
238
239 prog_char str_state2_arg0[] = "ballboard";
240 parse_pgm_token_string_t cmd_state2_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg0, str_state2_arg0);
241 prog_char str_state2_arg1[] = "xxx";
242 parse_pgm_token_string_t cmd_state2_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg1, str_state2_arg1);
243 prog_char str_state2_arg2[] = "left#right";
244 parse_pgm_token_string_t cmd_state2_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg2, str_state2_arg2);
245
246 prog_char help_state2[] = "set ballboard mode";
247 parse_pgm_inst_t cmd_state2 = {
248         .f = cmd_state2_parsed,  /* function to call */
249         .data = NULL,      /* 2nd arg of func */
250         .help_str = help_state2,
251         .tokens = {        /* token list, NULL terminated */
252                 (prog_void *)&cmd_state2_arg0,
253                 (prog_void *)&cmd_state2_arg1,
254                 (prog_void *)&cmd_state2_arg2,
255                 NULL,
256         },
257 };
258
259 /**********************************************************/
260 /* State3 */
261
262 /* this structure is filled when cmd_state3 is parsed successfully */
263 struct cmd_state3_result {
264         fixed_string_t arg0;
265         fixed_string_t arg1;
266         uint8_t arg2;
267 };
268
269 /* function called when cmd_state3 is parsed successfully */
270 static void cmd_state3_parsed(void *parsed_result,
271                               __attribute__((unused)) void *data)
272 {
273         struct cmd_state3_result *res = parsed_result;
274
275         if (!strcmp_P(res->arg1, PSTR("xxx"))) {
276                 /* xxx = res->arg2 */
277         }
278         else if (!strcmp_P(res->arg1, PSTR("yyy"))) {
279         }
280         state_set_mode(0);
281 }
282
283 prog_char str_state3_arg0[] = "ballboard";
284 parse_pgm_token_string_t cmd_state3_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg0, str_state3_arg0);
285 prog_char str_state3_arg1[] = "xxx";
286 parse_pgm_token_string_t cmd_state3_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg1, str_state3_arg1);
287 parse_pgm_token_num_t cmd_state3_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_state3_result, arg2, UINT8);
288
289 prog_char help_state3[] = "set ballboard mode";
290 parse_pgm_inst_t cmd_state3 = {
291         .f = cmd_state3_parsed,  /* function to call */
292         .data = NULL,      /* 2nd arg of func */
293         .help_str = help_state3,
294         .tokens = {        /* token list, NULL terminated */
295                 (prog_void *)&cmd_state3_arg0,
296                 (prog_void *)&cmd_state3_arg1,
297                 (prog_void *)&cmd_state3_arg2,
298                 NULL,
299         },
300 };
301
302 /**********************************************************/
303 /* State_Machine */
304
305 /* this structure is filled when cmd_state_machine is parsed successfully */
306 struct cmd_state_machine_result {
307         fixed_string_t arg0;
308 };
309
310 /* function called when cmd_state_machine is parsed successfully */
311 static void cmd_state_machine_parsed(__attribute__((unused)) void *parsed_result,
312                                      __attribute__((unused)) void *data)
313 {
314         state_machine();
315 }
316
317 prog_char str_state_machine_arg0[] = "state_machine";
318 parse_pgm_token_string_t cmd_state_machine_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_machine_result, arg0, str_state_machine_arg0);
319
320 prog_char help_state_machine[] = "launch state machine";
321 parse_pgm_inst_t cmd_state_machine = {
322         .f = cmd_state_machine_parsed,  /* function to call */
323         .data = NULL,      /* 2nd arg of func */
324         .help_str = help_state_machine,
325         .tokens = {        /* token list, NULL terminated */
326                 (prog_void *)&cmd_state_machine_arg0,
327                 NULL,
328         },
329 };
330
331 /**********************************************************/
332 /* State_Debug */
333
334 /* this structure is filled when cmd_state_debug is parsed successfully */
335 struct cmd_state_debug_result {
336         fixed_string_t arg0;
337         uint8_t on;
338 };
339
340 /* function called when cmd_state_debug is parsed successfully */
341 static void cmd_state_debug_parsed(void *parsed_result,
342                                    __attribute__((unused)) void *data)
343 {
344         struct cmd_state_debug_result *res = parsed_result;
345         state_debug = res->on;
346 }
347
348 prog_char str_state_debug_arg0[] = "state_debug";
349 parse_pgm_token_string_t cmd_state_debug_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_debug_result, arg0, str_state_debug_arg0);
350 parse_pgm_token_num_t cmd_state_debug_on = TOKEN_NUM_INITIALIZER(struct cmd_state_debug_result, on, UINT8);
351
352 prog_char help_state_debug[] = "Set debug for state machine";
353 parse_pgm_inst_t cmd_state_debug = {
354         .f = cmd_state_debug_parsed,  /* function to call */
355         .data = NULL,      /* 2nd arg of func */
356         .help_str = help_state_debug,
357         .tokens = {        /* token list, NULL terminated */
358                 (prog_void *)&cmd_state_debug_arg0,
359                 (prog_void *)&cmd_state_debug_on,
360                 NULL,
361         },
362 };
363
364 /**********************************************************/
365 /* Fork */
366
367 /* this structure is filled when cmd_fork is parsed successfully */
368 struct cmd_fork_result {
369         fixed_string_t arg0;
370         fixed_string_t arg1;
371 };
372
373 /* function called when cmd_fork is parsed successfully */
374 static void cmd_fork_parsed(void *parsed_result,
375                                     __attribute__((unused)) void *data)
376 {
377         struct cmd_fork_result *res = parsed_result;
378         if (!strcmp_P(res->arg1, PSTR("deploy")))
379                 fork_deploy();
380         else if (!strcmp_P(res->arg1, PSTR("pack")))
381                 fork_pack();
382         else if (!strcmp_P(res->arg1, PSTR("middle")))
383                 fork_mid();
384 }
385
386 prog_char str_fork_arg0[] = "fork";
387 parse_pgm_token_string_t cmd_fork_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_fork_result, arg0, str_fork_arg0);
388 prog_char str_fork_arg1[] = "deploy#pack#middle";
389 parse_pgm_token_string_t cmd_fork_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_fork_result, arg1, str_fork_arg1);
390
391 prog_char help_fork[] = "move fork";
392 parse_pgm_inst_t cmd_fork = {
393         .f = cmd_fork_parsed,  /* function to call */
394         .data = NULL,      /* 2nd arg of func */
395         .help_str = help_fork,
396         .tokens = {        /* token list, NULL terminated */
397                 (prog_void *)&cmd_fork_arg0,
398                 (prog_void *)&cmd_fork_arg1,
399                 NULL,
400         },
401 };
402
403 /**********************************************************/
404 /* Roller */
405
406 /* this structure is filled when cmd_roller is parsed successfully */
407 struct cmd_roller_result {
408         fixed_string_t arg0;
409         fixed_string_t arg1;
410 };
411
412 /* function called when cmd_roller is parsed successfully */
413 static void cmd_roller_parsed(void *parsed_result,
414                                     __attribute__((unused)) void *data)
415 {
416         struct cmd_roller_result *res = parsed_result;
417         if (!strcmp_P(res->arg1, PSTR("on")))
418                 roller_on();
419         else if (!strcmp_P(res->arg1, PSTR("off")))
420                 roller_off();
421         else if (!strcmp_P(res->arg1, PSTR("reverse")))
422                 roller_reverse();
423 }
424
425 prog_char str_roller_arg0[] = "roller";
426 parse_pgm_token_string_t cmd_roller_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_roller_result, arg0, str_roller_arg0);
427 prog_char str_roller_arg1[] = "on#off#reverse";
428 parse_pgm_token_string_t cmd_roller_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_roller_result, arg1, str_roller_arg1);
429
430 prog_char help_roller[] = "Servo door function";
431 parse_pgm_inst_t cmd_roller = {
432         .f = cmd_roller_parsed,  /* function to call */
433         .data = NULL,      /* 2nd arg of func */
434         .help_str = help_roller,
435         .tokens = {        /* token list, NULL terminated */
436                 (prog_void *)&cmd_roller_arg0,
437                 (prog_void *)&cmd_roller_arg1,
438                 NULL,
439         },
440 };
441
442 /**********************************************************/
443 /* Test */
444
445 /* this structure is filled when cmd_test is parsed successfully */
446 struct cmd_test_result {
447         fixed_string_t arg0;
448 };
449
450 /* function called when cmd_test is parsed successfully */
451 static void cmd_test_parsed(void *parsed_result, void *data)
452 {
453         //struct cmd_test_result *res = parsed_result;
454 }
455
456 prog_char str_test_arg0[] = "test";
457 parse_pgm_token_string_t cmd_test_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_test_result, arg0, str_test_arg0);
458
459 prog_char help_test[] = "Test function";
460 parse_pgm_inst_t cmd_test = {
461         .f = cmd_test_parsed,  /* function to call */
462         .data = NULL,      /* 2nd arg of func */
463         .help_str = help_test,
464         .tokens = {        /* token list, NULL terminated */
465                 (prog_void *)&cmd_test_arg0,
466                 NULL,
467         },
468 };