2 * Copyright Droids Corporation (2009)
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.
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.
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
18 * Revision : $Id: commands_ballboard.c,v 1.2 2009-04-24 19:30:42 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
33 #include <clock_time.h>
38 #include <control_system_manager.h>
39 #include <blocking_detection_manager.h>
43 #include <parse_string.h>
44 #include <parse_num.h>
49 #include "../common/i2c_commands.h"
50 #include "i2c_protocol.h"
53 struct cmd_event_result {
60 /* function called when cmd_event is parsed successfully */
61 static void cmd_event_parsed(void *parsed_result, void *data)
65 struct cmd_event_result * res = parsed_result;
67 if (!strcmp_P(res->arg1, PSTR("all"))) {
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;
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");
88 if (!strcmp_P(res->arg1, PSTR("encoders")))
90 else if (!strcmp_P(res->arg1, PSTR("cs"))) {
93 else if (!strcmp_P(res->arg1, PSTR("bd")))
95 else if (!strcmp_P(res->arg1, PSTR("power")))
97 else if (!strcmp_P(res->arg1, PSTR("errblock")))
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);
109 ballboard.flags &= (~bit);
111 printf_P(PSTR("%s is %s\r\n"), res->arg1,
112 (bit & ballboard.flags) ? "on":"off");
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);
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,
135 /**********************************************************/
138 /* this structure is filled when cmd_color is parsed successfully */
139 struct cmd_color_result {
141 fixed_string_t color;
144 /* function called when cmd_color is parsed successfully */
145 static void cmd_color_parsed(void *parsed_result, void *data)
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;
151 else if (!strcmp_P(res->color, PSTR("blue"))) {
152 ballboard.our_color = I2C_COLOR_BLUE;
154 printf_P(PSTR("Done\r\n"));
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);
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,
175 /**********************************************************/
178 /* this structure is filled when cmd_state1 is parsed successfully */
179 struct cmd_state1_result {
184 /* function called when cmd_state1 is parsed successfully */
185 static void cmd_state1_parsed(void *parsed_result,
186 __attribute__((unused)) void *data)
188 struct cmd_state1_result *res = parsed_result;
190 if (!strcmp_P(res->arg1, PSTR("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);
202 prog_char str_state1_arg0[] = "ballboard";
203 parse_pgm_token_string_t cmd_state1_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg0, str_state1_arg0);
204 prog_char str_state1_arg1[] = "init#eject#harvest#off";
205 parse_pgm_token_string_t cmd_state1_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg1, str_state1_arg1);
207 prog_char help_state1[] = "set ballboard mode";
208 parse_pgm_inst_t cmd_state1 = {
209 .f = cmd_state1_parsed, /* function to call */
210 .data = NULL, /* 2nd arg of func */
211 .help_str = help_state1,
212 .tokens = { /* token list, NULL terminated */
213 (prog_void *)&cmd_state1_arg0,
214 (prog_void *)&cmd_state1_arg1,
219 /**********************************************************/
222 /* this structure is filled when cmd_state2 is parsed successfully */
223 struct cmd_state2_result {
229 /* function called when cmd_state2 is parsed successfully */
230 static void cmd_state2_parsed(void *parsed_result,
231 __attribute__((unused)) void *data)
233 struct cmd_state2_result *res = parsed_result;
236 if (!strcmp_P(res->arg2, PSTR("left"))) {
237 if (!strcmp_P(res->arg1, PSTR("prepare")))
238 mode = I2C_BALLBOARD_MODE_PREP_L_FORK;
239 else if (!strcmp_P(res->arg1, PSTR("take")))
240 mode = I2C_BALLBOARD_MODE_TAKE_L_FORK;
243 if (!strcmp_P(res->arg1, PSTR("prepare")))
244 mode = I2C_BALLBOARD_MODE_PREP_R_FORK;
245 else if (!strcmp_P(res->arg1, PSTR("take")))
246 mode = I2C_BALLBOARD_MODE_TAKE_R_FORK;
248 //state_set_mode(mode);
251 prog_char str_state2_arg0[] = "ballboard";
252 parse_pgm_token_string_t cmd_state2_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg0, str_state2_arg0);
253 prog_char str_state2_arg1[] = "prepare#take";
254 parse_pgm_token_string_t cmd_state2_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg1, str_state2_arg1);
255 prog_char str_state2_arg2[] = "left#right";
256 parse_pgm_token_string_t cmd_state2_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg2, str_state2_arg2);
258 prog_char help_state2[] = "set ballboard mode";
259 parse_pgm_inst_t cmd_state2 = {
260 .f = cmd_state2_parsed, /* function to call */
261 .data = NULL, /* 2nd arg of func */
262 .help_str = help_state2,
263 .tokens = { /* token list, NULL terminated */
264 (prog_void *)&cmd_state2_arg0,
265 (prog_void *)&cmd_state2_arg1,
266 (prog_void *)&cmd_state2_arg2,
271 /**********************************************************/
274 /* this structure is filled when cmd_state3 is parsed successfully */
275 struct cmd_state3_result {
281 /* function called when cmd_state3 is parsed successfully */
282 static void cmd_state3_parsed(void *parsed_result,
283 __attribute__((unused)) void *data)
285 struct cmd_state3_result *res = parsed_result;
287 if (!strcmp_P(res->arg1, PSTR("xxx"))) {
288 /* xxx = res->arg2 */
290 else if (!strcmp_P(res->arg1, PSTR("yyy"))) {
295 prog_char str_state3_arg0[] = "ballboard";
296 parse_pgm_token_string_t cmd_state3_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg0, str_state3_arg0);
297 prog_char str_state3_arg1[] = "xxx";
298 parse_pgm_token_string_t cmd_state3_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg1, str_state3_arg1);
299 parse_pgm_token_num_t cmd_state3_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_state3_result, arg2, UINT8);
301 prog_char help_state3[] = "set ballboard mode";
302 parse_pgm_inst_t cmd_state3 = {
303 .f = cmd_state3_parsed, /* function to call */
304 .data = NULL, /* 2nd arg of func */
305 .help_str = help_state3,
306 .tokens = { /* token list, NULL terminated */
307 (prog_void *)&cmd_state3_arg0,
308 (prog_void *)&cmd_state3_arg1,
309 (prog_void *)&cmd_state3_arg2,
314 /**********************************************************/
317 /* this structure is filled when cmd_state_machine is parsed successfully */
318 struct cmd_state_machine_result {
322 /* function called when cmd_state_machine is parsed successfully */
323 static void cmd_state_machine_parsed(__attribute__((unused)) void *parsed_result,
324 __attribute__((unused)) void *data)
329 prog_char str_state_machine_arg0[] = "state_machine";
330 parse_pgm_token_string_t cmd_state_machine_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_machine_result, arg0, str_state_machine_arg0);
332 prog_char help_state_machine[] = "launch state machine";
333 parse_pgm_inst_t cmd_state_machine = {
334 .f = cmd_state_machine_parsed, /* function to call */
335 .data = NULL, /* 2nd arg of func */
336 .help_str = help_state_machine,
337 .tokens = { /* token list, NULL terminated */
338 (prog_void *)&cmd_state_machine_arg0,
343 /**********************************************************/
346 /* this structure is filled when cmd_state_debug is parsed successfully */
347 struct cmd_state_debug_result {
352 /* function called when cmd_state_debug is parsed successfully */
353 static void cmd_state_debug_parsed(void *parsed_result,
354 __attribute__((unused)) void *data)
356 struct cmd_state_debug_result *res = parsed_result;
357 state_debug = res->on;
360 prog_char str_state_debug_arg0[] = "state_debug";
361 parse_pgm_token_string_t cmd_state_debug_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_debug_result, arg0, str_state_debug_arg0);
362 parse_pgm_token_num_t cmd_state_debug_on = TOKEN_NUM_INITIALIZER(struct cmd_state_debug_result, on, UINT8);
364 prog_char help_state_debug[] = "Set debug for state machine";
365 parse_pgm_inst_t cmd_state_debug = {
366 .f = cmd_state_debug_parsed, /* function to call */
367 .data = NULL, /* 2nd arg of func */
368 .help_str = help_state_debug,
369 .tokens = { /* token list, NULL terminated */
370 (prog_void *)&cmd_state_debug_arg0,
371 (prog_void *)&cmd_state_debug_on,
376 /**********************************************************/
379 /* this structure is filled when cmd_fork is parsed successfully */
380 struct cmd_fork_result {
385 /* function called when cmd_fork is parsed successfully */
386 static void cmd_fork_parsed(void *parsed_result,
387 __attribute__((unused)) void *data)
389 struct cmd_fork_result *res = parsed_result;
390 if (!strcmp_P(res->arg1, PSTR("deploy")))
392 else if (!strcmp_P(res->arg1, PSTR("pack")))
394 else if (!strcmp_P(res->arg1, PSTR("left")))
396 else if (!strcmp_P(res->arg1, PSTR("right")))
398 else if (!strcmp_P(res->arg1, PSTR("middle")))
402 prog_char str_fork_arg0[] = "fork";
403 parse_pgm_token_string_t cmd_fork_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_fork_result, arg0, str_fork_arg0);
404 prog_char str_fork_arg1[] = "deploy#pack#left#right#middle";
405 parse_pgm_token_string_t cmd_fork_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_fork_result, arg1, str_fork_arg1);
407 prog_char help_fork[] = "move fork";
408 parse_pgm_inst_t cmd_fork = {
409 .f = cmd_fork_parsed, /* function to call */
410 .data = NULL, /* 2nd arg of func */
411 .help_str = help_fork,
412 .tokens = { /* token list, NULL terminated */
413 (prog_void *)&cmd_fork_arg0,
414 (prog_void *)&cmd_fork_arg1,
419 /**********************************************************/
422 /* this structure is filled when cmd_roller is parsed successfully */
423 struct cmd_roller_result {
428 /* function called when cmd_roller is parsed successfully */
429 static void cmd_roller_parsed(void *parsed_result,
430 __attribute__((unused)) void *data)
432 struct cmd_roller_result *res = parsed_result;
433 if (!strcmp_P(res->arg1, PSTR("on")))
435 else if (!strcmp_P(res->arg1, PSTR("off")))
437 else if (!strcmp_P(res->arg1, PSTR("reverse")))
441 prog_char str_roller_arg0[] = "roller";
442 parse_pgm_token_string_t cmd_roller_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_roller_result, arg0, str_roller_arg0);
443 prog_char str_roller_arg1[] = "on#off#reverse";
444 parse_pgm_token_string_t cmd_roller_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_roller_result, arg1, str_roller_arg1);
446 prog_char help_roller[] = "Servo door function";
447 parse_pgm_inst_t cmd_roller = {
448 .f = cmd_roller_parsed, /* function to call */
449 .data = NULL, /* 2nd arg of func */
450 .help_str = help_roller,
451 .tokens = { /* token list, NULL terminated */
452 (prog_void *)&cmd_roller_arg0,
453 (prog_void *)&cmd_roller_arg1,
458 /**********************************************************/
461 /* this structure is filled when cmd_test is parsed successfully */
462 struct cmd_test_result {
466 /* function called when cmd_test is parsed successfully */
467 static void cmd_test_parsed(void *parsed_result, void *data)
469 //struct cmd_test_result *res = parsed_result;
472 prog_char str_test_arg0[] = "test";
473 parse_pgm_token_string_t cmd_test_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_test_result, arg0, str_test_arg0);
475 prog_char help_test[] = "Test function";
476 parse_pgm_inst_t cmd_test = {
477 .f = cmd_test_parsed, /* function to call */
478 .data = NULL, /* 2nd arg of func */
479 .help_str = help_test,
480 .tokens = { /* token list, NULL terminated */
481 (prog_void *)&cmd_test_arg0,