2 * Copyright Droids Corporation (2008)
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_gen.c,v 1.8 2009-11-08 17:24:33 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>
34 #include <encoders_spi.h>
37 #include <scheduler.h>
38 #include <scheduler_stats.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <vect_base.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
53 #include <parse_string.h>
54 #include <parse_num.h>
56 #include <diagnostic.h>
62 /**********************************************************/
65 /* this structure is filled when cmd_reset is parsed successfully */
66 struct cmd_reset_result {
70 /* function called when cmd_reset is parsed successfully */
71 static void cmd_reset_parsed(void * parsed_result, void * data)
76 prog_char str_reset_arg0[] = "reset";
77 parse_pgm_token_string_t cmd_reset_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_reset_result, arg0, str_reset_arg0);
79 prog_char help_reset[] = "Reset the board";
80 parse_pgm_inst_t cmd_reset = {
81 .f = cmd_reset_parsed, /* function to call */
82 .data = NULL, /* 2nd arg of func */
83 .help_str = help_reset,
84 .tokens = { /* token list, NULL terminated */
85 (prog_void *)&cmd_reset_arg0,
90 /**********************************************************/
93 /* this structure is filled when cmd_bootloader is parsed successfully */
94 struct cmd_bootloader_result {
98 /* function called when cmd_bootloader is parsed successfully */
99 static void cmd_bootloader_parsed(void *parsed_result, void *data)
104 prog_char str_bootloader_arg0[] = "bootloader";
105 parse_pgm_token_string_t cmd_bootloader_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_bootloader_result, arg0, str_bootloader_arg0);
107 prog_char help_bootloader[] = "Launch the bootloader";
108 parse_pgm_inst_t cmd_bootloader = {
109 .f = cmd_bootloader_parsed, /* function to call */
110 .data = NULL, /* 2nd arg of func */
111 .help_str = help_bootloader,
112 .tokens = { /* token list, NULL terminated */
113 (prog_void *)&cmd_bootloader_arg0,
118 /**********************************************************/
121 /* this structure is filled when cmd_encoders is parsed successfully */
122 struct cmd_encoders_result {
127 /* function called when cmd_encoders is parsed successfully */
128 static void cmd_encoders_parsed(void *parsed_result, void *data)
130 struct cmd_encoders_result *res = parsed_result;
132 if (!strcmp_P(res->arg1, PSTR("reset"))) {
133 encoders_spi_set_value((void *)0, 0);
134 encoders_spi_set_value((void *)1, 0);
135 encoders_spi_set_value((void *)2, 0);
136 encoders_spi_set_value((void *)3, 0);
141 while(!cmdline_keypressed()) {
142 printf_P(PSTR("% .8ld % .8ld % .8ld % .8ld\r\n"),
143 encoders_spi_get_value((void *)0),
144 encoders_spi_get_value((void *)1),
145 encoders_spi_get_value((void *)2),
146 encoders_spi_get_value((void *)3));
151 prog_char str_encoders_arg0[] = "encoders";
152 parse_pgm_token_string_t cmd_encoders_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_encoders_result, arg0, str_encoders_arg0);
153 prog_char str_encoders_arg1[] = "show#reset";
154 parse_pgm_token_string_t cmd_encoders_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_encoders_result, arg1, str_encoders_arg1);
156 prog_char help_encoders[] = "Show encoders values";
157 parse_pgm_inst_t cmd_encoders = {
158 .f = cmd_encoders_parsed, /* function to call */
159 .data = NULL, /* 2nd arg of func */
160 .help_str = help_encoders,
161 .tokens = { /* token list, NULL terminated */
162 (prog_void *)&cmd_encoders_arg0,
163 (prog_void *)&cmd_encoders_arg1,
168 /**********************************************************/
171 /* this structure is filled when cmd_scheduler is parsed successfully */
172 struct cmd_scheduler_result {
177 /* function called when cmd_scheduler is parsed successfully */
178 static void cmd_scheduler_parsed(void *parsed_result, void *data)
180 scheduler_dump_events();
181 scheduler_stats_dump();
184 prog_char str_scheduler_arg0[] = "scheduler";
185 parse_pgm_token_string_t cmd_scheduler_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_scheduler_result, arg0, str_scheduler_arg0);
186 prog_char str_scheduler_arg1[] = "show";
187 parse_pgm_token_string_t cmd_scheduler_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_scheduler_result, arg1, str_scheduler_arg1);
189 prog_char help_scheduler[] = "Show scheduler events";
190 parse_pgm_inst_t cmd_scheduler = {
191 .f = cmd_scheduler_parsed, /* function to call */
192 .data = NULL, /* 2nd arg of func */
193 .help_str = help_scheduler,
194 .tokens = { /* token list, NULL terminated */
195 (prog_void *)&cmd_scheduler_arg0,
196 (prog_void *)&cmd_scheduler_arg1,
201 /**********************************************************/
204 /* this structure is filled when cmd_pwm is parsed successfully */
205 struct cmd_pwm_result {
211 /* function called when cmd_pwm is parsed successfully */
212 static void cmd_pwm_parsed(void * parsed_result, void * data)
214 void * pwm_ptr = NULL;
215 struct cmd_pwm_result * res = parsed_result;
217 if (!strcmp_P(res->arg1, PSTR("1(4A)")))
218 pwm_ptr = &gen.pwm1_4A;
219 else if (!strcmp_P(res->arg1, PSTR("2(4B)")))
220 pwm_ptr = &gen.pwm2_4B;
221 else if (!strcmp_P(res->arg1, PSTR("3(1A)")))
222 pwm_ptr = &gen.pwm3_1A;
223 else if (!strcmp_P(res->arg1, PSTR("4(1B)")))
224 pwm_ptr = &gen.pwm4_1B;
226 else if (!strcmp_P(res->arg1, PSTR("s1(3C)")))
227 pwm_ptr = &gen.servo1;
228 else if (!strcmp_P(res->arg1, PSTR("s2(5A)")))
229 pwm_ptr = &gen.servo2;
230 else if (!strcmp_P(res->arg1, PSTR("s3(5B)")))
231 pwm_ptr = &gen.servo3;
232 else if (!strcmp_P(res->arg1, PSTR("s3(5C)")))
233 pwm_ptr = &gen.servo4;
236 pwm_ng_set(pwm_ptr, res->arg2);
238 printf_P(PSTR("done\r\n"));
241 prog_char str_pwm_arg0[] = "pwm";
242 parse_pgm_token_string_t cmd_pwm_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_pwm_result, arg0, str_pwm_arg0);
243 prog_char str_pwm_arg1[] = "1(4A)#2(4B)#3(1A)#4(1B)#s1(3C)#s2(5A)#s3(5B)#s4(5C)";
244 parse_pgm_token_string_t cmd_pwm_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_pwm_result, arg1, str_pwm_arg1);
245 parse_pgm_token_num_t cmd_pwm_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_pwm_result, arg2, INT16);
247 prog_char help_pwm[] = "Set pwm values [-4096 ; 4095]";
248 parse_pgm_inst_t cmd_pwm = {
249 .f = cmd_pwm_parsed, /* function to call */
250 .data = NULL, /* 2nd arg of func */
251 .help_str = help_pwm,
252 .tokens = { /* token list, NULL terminated */
253 (prog_void *)&cmd_pwm_arg0,
254 (prog_void *)&cmd_pwm_arg1,
255 (prog_void *)&cmd_pwm_arg2,
260 /**********************************************************/
263 /* this structure is filled when cmd_adc is parsed successfully */
264 struct cmd_adc_result {
269 /* function called when cmd_adc is parsed successfully */
270 static void cmd_adc_parsed(void *parsed_result, void *data)
272 struct cmd_adc_result *res = parsed_result;
275 if (!strcmp_P(res->arg1, PSTR("loop_show")))
279 printf_P(PSTR("ADC values: "));
280 for (i=0; i<ADC_MAX; i++) {
281 printf_P(PSTR("%.4d "), sensor_get_adc(i));
283 printf_P(PSTR("\r\n"));
285 } while (loop && !cmdline_keypressed());
288 prog_char str_adc_arg0[] = "adc";
289 parse_pgm_token_string_t cmd_adc_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_adc_result, arg0, str_adc_arg0);
290 prog_char str_adc_arg1[] = "show#loop_show";
291 parse_pgm_token_string_t cmd_adc_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_adc_result, arg1, str_adc_arg1);
293 prog_char help_adc[] = "Show adc values";
294 parse_pgm_inst_t cmd_adc = {
295 .f = cmd_adc_parsed, /* function to call */
296 .data = NULL, /* 2nd arg of func */
297 .help_str = help_adc,
298 .tokens = { /* token list, NULL terminated */
299 (prog_void *)&cmd_adc_arg0,
300 (prog_void *)&cmd_adc_arg1,
306 /**********************************************************/
309 /* this structure is filled when cmd_sensor is parsed successfully */
310 struct cmd_sensor_result {
315 /* function called when cmd_sensor is parsed successfully */
316 static void cmd_sensor_parsed(void *parsed_result, void *data)
318 struct cmd_sensor_result *res = parsed_result;
321 if (!strcmp_P(res->arg1, PSTR("loop_show")))
325 printf_P(PSTR("SENSOR values: "));
326 for (i=0; i<SENSOR_MAX; i++) {
327 printf_P(PSTR("%d "), !!sensor_get(i));
329 printf_P(PSTR("\r\n"));
331 } while (loop && !cmdline_keypressed());
334 prog_char str_sensor_arg0[] = "sensor";
335 parse_pgm_token_string_t cmd_sensor_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_sensor_result, arg0, str_sensor_arg0);
336 prog_char str_sensor_arg1[] = "show#loop_show";
337 parse_pgm_token_string_t cmd_sensor_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_sensor_result, arg1, str_sensor_arg1);
339 prog_char help_sensor[] = "Show sensor values";
340 parse_pgm_inst_t cmd_sensor = {
341 .f = cmd_sensor_parsed, /* function to call */
342 .data = NULL, /* 2nd arg of func */
343 .help_str = help_sensor,
344 .tokens = { /* token list, NULL terminated */
345 (prog_void *)&cmd_sensor_arg0,
346 (prog_void *)&cmd_sensor_arg1,
352 /**********************************************************/
355 /* this structure is filled when cmd_log is parsed successfully */
356 struct cmd_log_result {
363 /* keep it sync with string choice */
364 static const prog_char uart_log[] = "uart";
365 static const prog_char rs_log[] = "rs";
366 static const prog_char traj_log[] = "traj";
367 static const prog_char i2c_log[] = "i2c";
368 static const prog_char oa_log[] = "oa";
369 static const prog_char strat_log[] = "strat";
370 static const prog_char i2cproto_log[] = "i2cproto";
371 static const prog_char sensor_log[] = "sensor";
372 static const prog_char block_log[] = "bd";
373 static const prog_char cs_log[] = "cs";
375 struct log_name_and_num {
376 const prog_char * name;
380 static const struct log_name_and_num log_name_and_num[] = {
381 { uart_log, E_UART },
382 { rs_log, E_ROBOT_SYSTEM },
383 { traj_log, E_TRAJECTORY },
386 { strat_log, E_USER_STRAT },
387 { i2cproto_log, E_USER_I2C_PROTO },
388 { sensor_log, E_USER_SENSOR },
389 { block_log, E_BLOCKING_DETECTION_MANAGER },
390 { cs_log, E_USER_CS },
394 log_name2num(const char * s)
398 for (i=0; i<sizeof(log_name_and_num)/sizeof(struct log_name_and_num); i++) {
399 if (!strcmp_P(s, log_name_and_num[i].name)) {
400 return log_name_and_num[i].num;
407 log_num2name(uint8_t num)
411 for (i=0; i<sizeof(log_name_and_num)/sizeof(struct log_name_and_num); i++) {
412 if (num == log_name_and_num[i].num) {
413 return log_name_and_num[i].name;
419 /* function called when cmd_log is parsed successfully */
420 static void cmd_log_do_show(void)
423 const prog_char * name;
425 printf_P(PSTR("log level is %d\r\n"), gen.log_level);
426 for (i=0; i<NB_LOGS; i++) {
427 name = log_num2name(gen.logs[i]);
429 printf_P(PSTR("log type %S is on\r\n"), name);
434 printf_P(PSTR("no log configured\r\n"), gen.logs[i]);
437 /* function called when cmd_log is parsed successfully */
438 static void cmd_log_parsed(void * parsed_result, void * data)
440 struct cmd_log_result *res = (struct cmd_log_result *) parsed_result;
442 if (!strcmp_P(res->arg1, PSTR("level"))) {
443 gen.log_level = res->arg2;
446 /* else it is a show */
450 prog_char str_log_arg0[] = "log";
451 parse_pgm_token_string_t cmd_log_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg0, str_log_arg0);
452 prog_char str_log_arg1[] = "level";
453 parse_pgm_token_string_t cmd_log_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg1, str_log_arg1);
454 parse_pgm_token_num_t cmd_log_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_log_result, arg2, INT32);
456 prog_char help_log[] = "Set log options: level (0 -> 5)";
457 parse_pgm_inst_t cmd_log = {
458 .f = cmd_log_parsed, /* function to call */
459 .data = NULL, /* 2nd arg of func */
460 .help_str = help_log,
461 .tokens = { /* token list, NULL terminated */
462 (prog_void *)&cmd_log_arg0,
463 (prog_void *)&cmd_log_arg1,
464 (prog_void *)&cmd_log_arg2,
469 prog_char str_log_arg1_show[] = "show";
470 parse_pgm_token_string_t cmd_log_arg1_show = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg1, str_log_arg1_show);
472 prog_char help_log_show[] = "Show configured logs";
473 parse_pgm_inst_t cmd_log_show = {
474 .f = cmd_log_parsed, /* function to call */
475 .data = NULL, /* 2nd arg of func */
476 .help_str = help_log_show,
477 .tokens = { /* token list, NULL terminated */
478 (prog_void *)&cmd_log_arg0,
479 (prog_void *)&cmd_log_arg1_show,
484 /* this structure is filled when cmd_log is parsed successfully */
485 struct cmd_log_type_result {
492 /* function called when cmd_log is parsed successfully */
493 static void cmd_log_type_parsed(void * parsed_result, void * data)
495 struct cmd_log_type_result *res = (struct cmd_log_type_result *) parsed_result;
499 lognum = log_name2num(res->arg2);
501 printf_P(PSTR("Cannot find log num\r\n"));
505 if (!strcmp_P(res->arg3, PSTR("on"))) {
506 for (i=0; i<NB_LOGS; i++) {
507 if (gen.logs[i] == lognum) {
508 printf_P(PSTR("Already on\r\n"));
512 for (i=0; i<NB_LOGS; i++) {
513 if (gen.logs[i] == 0) {
514 gen.logs[i] = lognum;
519 printf_P(PSTR("no more room\r\n"));
522 else if (!strcmp_P(res->arg3, PSTR("off"))) {
523 for (i=0; i<NB_LOGS; i++) {
524 if (gen.logs[i] == lognum) {
530 printf_P(PSTR("already off\r\n"));
536 prog_char str_log_arg1_type[] = "type";
537 parse_pgm_token_string_t cmd_log_arg1_type = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg1, str_log_arg1_type);
538 /* keep it sync with log_name_and_num above */
539 prog_char str_log_arg2_type[] = "uart#rs#servo#traj#i2c#oa#strat#i2cproto#ext#sensor#bd#cs";
540 parse_pgm_token_string_t cmd_log_arg2_type = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg2, str_log_arg2_type);
541 prog_char str_log_arg3[] = "on#off";
542 parse_pgm_token_string_t cmd_log_arg3 = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg3, str_log_arg3);
544 prog_char help_log_type[] = "Set log type";
545 parse_pgm_inst_t cmd_log_type = {
546 .f = cmd_log_type_parsed, /* function to call */
547 .data = NULL, /* 2nd arg of func */
548 .help_str = help_log_type,
549 .tokens = { /* token list, NULL terminated */
550 (prog_void *)&cmd_log_arg0,
551 (prog_void *)&cmd_log_arg1_type,
552 (prog_void *)&cmd_log_arg2_type,
553 (prog_void *)&cmd_log_arg3,
559 /**********************************************************/
562 /* this structure is filled when cmd_stack_space is parsed successfully */
563 struct cmd_stack_space_result {
567 /* function called when cmd_stack_space is parsed successfully */
568 static void cmd_stack_space_parsed(void *parsed_result, void *data)
570 printf("res stack: %d\r\n", min_stack_space_available());
574 prog_char str_stack_space_arg0[] = "stack_space";
575 parse_pgm_token_string_t cmd_stack_space_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_stack_space_result, arg0, str_stack_space_arg0);
577 prog_char help_stack_space[] = "Display remaining stack space";
578 parse_pgm_inst_t cmd_stack_space = {
579 .f = cmd_stack_space_parsed, /* function to call */
580 .data = NULL, /* 2nd arg of func */
581 .help_str = help_stack_space,
582 .tokens = { /* token list, NULL terminated */
583 (prog_void *)&cmd_stack_space_arg0,