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_ax12.c,v 1.3 2009-05-02 10:08:09 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
37 #include <control_system_manager.h>
38 #include <trajectory_manager.h>
39 #include <vect_base.h>
42 #include <obstacle_avoidance.h>
43 #include <blocking_detection_manager.h>
44 #include <robot_system.h>
45 #include <position_manager.h>
49 #include <parse_string.h>
50 #include <parse_num.h>
54 uint8_t addr_from_string(const char *s)
57 if (!strcmp_P(s, PSTR("cw_angle_limit")))
58 return AA_CW_ANGLE_LIMIT_L;
59 if (!strcmp_P(s, PSTR("ccw_angle_limit")))
60 return AA_CCW_ANGLE_LIMIT_L;
61 if (!strcmp_P(s, PSTR("max_torque")))
62 return AA_MAX_TORQUE_L;
63 if (!strcmp_P(s, PSTR("down_calibration")))
64 return AA_DOWN_CALIBRATION_L;
65 if (!strcmp_P(s, PSTR("up_calibration")))
66 return AA_UP_CALIBRATION_L;
67 if (!strcmp_P(s, PSTR("torque_limit")))
68 return AA_TORQUE_LIMIT_L;
69 if (!strcmp_P(s, PSTR("position")))
70 return AA_PRESENT_POSITION_L;
71 if (!strcmp_P(s, PSTR("speed")))
72 return AA_PRESENT_SPEED_L;
73 if (!strcmp_P(s, PSTR("load")))
74 return AA_PRESENT_LOAD_L;
75 if (!strcmp_P(s, PSTR("moving_speed")))
76 return AA_MOVING_SPEED_L;
77 if (!strcmp_P(s, PSTR("model")))
78 return AA_MODEL_NUMBER_L;
79 if (!strcmp_P(s, PSTR("goal_pos")))
80 return AA_GOAL_POSITION_L;
81 if (!strcmp_P(s, PSTR("punch")))
85 if (!strcmp_P(s, PSTR("firmware")))
87 if (!strcmp_P(s, PSTR("id")))
89 if (!strcmp_P(s, PSTR("baudrate")))
91 if (!strcmp_P(s, PSTR("delay")))
93 if (!strcmp_P(s, PSTR("high_lim_temp")))
94 return AA_HIGHEST_LIMIT_TEMP;
95 if (!strcmp_P(s, PSTR("low_lim_volt")))
96 return AA_LOWEST_LIMIT_VOLTAGE;
97 if (!strcmp_P(s, PSTR("high_lim_volt")))
98 return AA_HIGHEST_LIMIT_VOLTAGE;
99 if (!strcmp_P(s, PSTR("status_return")))
100 return AA_STATUS_RETURN_LEVEL;
101 if (!strcmp_P(s, PSTR("alarm_led")))
103 if (!strcmp_P(s, PSTR("alarm_shutdown")))
104 return AA_ALARM_SHUTDOWN;
105 if (!strcmp_P(s, PSTR("torque_enable")))
106 return AA_TORQUE_ENABLE;
107 if (!strcmp_P(s, PSTR("led")))
109 if (!strcmp_P(s, PSTR("cw_comp_margin")))
110 return AA_CW_COMPLIANCE_MARGIN;
111 if (!strcmp_P(s, PSTR("ccw_comp_margin")))
112 return AA_CCW_COMPLIANCE_MARGIN;
113 if (!strcmp_P(s, PSTR("cw_comp_slope")))
114 return AA_CW_COMPLIANCE_SLOPE;
115 if (!strcmp_P(s, PSTR("ccw_comp_slope")))
116 return AA_CCW_COMPLIANCE_SLOPE;
117 if (!strcmp_P(s, PSTR("voltage")))
118 return AA_PRESENT_VOLTAGE;
119 if (!strcmp_P(s, PSTR("temp")))
120 return AA_PRESENT_TEMP;
121 if (!strcmp_P(s, PSTR("reginst")))
122 return AA_PRESENT_REGINST;
123 if (!strcmp_P(s, PSTR("moving")))
125 if (!strcmp_P(s, PSTR("lock")))
131 /**********************************************************/
134 /* this structure is filled when cmd_ax12_stress is parsed successfully */
135 struct cmd_ax12_stress_result {
140 /* function called when cmd_ax12_stress is parsed successfully */
141 static void cmd_ax12_stress_parsed(void *parsed_result, void *data)
143 struct cmd_ax12_stress_result *res = parsed_result;
146 microseconds t = time_get_us2();
148 for (i=0; i<1000; i++) {
149 if (AX12_read_byte(&gen.ax12, res->id, AA_ID, &val) != 0)
153 printf_P(PSTR("%d errors / 1000\r\n"), nb_errs);
154 t = (time_get_us2() - t) / 1000;
155 printf_P(PSTR("Test done in %d ms\r\n"), (int)t);
158 prog_char str_ax12_stress_arg0[] = "ax12_stress";
159 parse_pgm_token_string_t cmd_ax12_stress_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_ax12_stress_result, arg0, str_ax12_stress_arg0);
160 parse_pgm_token_num_t cmd_ax12_stress_id = TOKEN_NUM_INITIALIZER(struct cmd_ax12_stress_result, id, UINT8);
162 prog_char help_ax12_stress[] = "Stress an AX12 with 1000 'read id' commands";
163 parse_pgm_inst_t cmd_ax12_stress = {
164 .f = cmd_ax12_stress_parsed, /* function to call */
165 .data = NULL, /* 2nd arg of func */
166 .help_str = help_ax12_stress,
167 .tokens = { /* token list, NULL terminated */
168 (prog_void *)&cmd_ax12_stress_arg0,
169 (prog_void *)&cmd_ax12_stress_id,
174 /**********************************************************/
176 /* this structure is filled when cmd_baudrate is parsed successfully */
177 struct cmd_baudrate_result {
182 /* function called when cmd_baudrate is parsed successfully */
183 static void cmd_baudrate_parsed(void * parsed_result, void * data)
185 struct cmd_baudrate_result *res = parsed_result;
186 struct uart_config c;
188 printf_P(PSTR("%d %d\r\n"), UBRR1H, UBRR1L);
190 c.baudrate = res->arg1;
192 printf_P(PSTR("%d %d\r\n"), UBRR1H, UBRR1L);
195 prog_char str_baudrate_arg0[] = "baudrate";
196 parse_pgm_token_string_t cmd_baudrate_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_baudrate_result, arg0, str_baudrate_arg0);
197 parse_pgm_token_num_t cmd_baudrate_arg1 = TOKEN_NUM_INITIALIZER(struct cmd_baudrate_result, arg1, UINT32);
199 prog_char help_baudrate[] = "Change ax12 baudrate";
200 parse_pgm_inst_t cmd_baudrate = {
201 .f = cmd_baudrate_parsed, /* function to call */
202 .data = NULL, /* 2nd arg of func */
203 .help_str = help_baudrate,
204 .tokens = { /* token list, NULL terminated */
205 (prog_void *)&cmd_baudrate_arg0,
206 (prog_void *)&cmd_baudrate_arg1,
211 /**********************************************************/
215 /* this structure is filled when cmd_uint16_read is parsed successfully */
216 struct cmd_uint16_result {
223 /* function called when cmd_uint16_read is parsed successfully */
224 static void cmd_uint16_read_parsed(void * parsed_result, void * data)
226 struct cmd_uint16_result *res = parsed_result;
229 uint8_t addr = addr_from_string(res->arg1);
230 ret = AX12_read_int(&gen.ax12, res->num, addr, &val);
232 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
233 printf_P(PSTR("%s: %d [0x%.4x]\r\n"), res->arg1, val, val);
236 prog_char str_uint16_arg0[] = "read";
237 parse_pgm_token_string_t cmd_uint16_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg0, str_uint16_arg0);
238 prog_char str_uint16_arg1[] = "moving_speed#model#goal_pos#cw_angle_limit#ccw_angle_limit#"
239 "max_torque#down_calibration#up_calibration#torque_limit#"
240 "position#speed#load#punch";
241 parse_pgm_token_string_t cmd_uint16_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg1, str_uint16_arg1);
242 parse_pgm_token_num_t cmd_uint16_num = TOKEN_NUM_INITIALIZER(struct cmd_uint16_result, num, UINT8);
244 prog_char help_uint16_read[] = "Read uint16 value (type, num)";
245 parse_pgm_inst_t cmd_uint16_read = {
246 .f = cmd_uint16_read_parsed, /* function to call */
247 .data = NULL, /* 2nd arg of func */
248 .help_str = help_uint16_read,
249 .tokens = { /* token list, NULL terminated */
250 (prog_void *)&cmd_uint16_arg0,
251 (prog_void *)&cmd_uint16_arg1,
252 (prog_void *)&cmd_uint16_num,
257 /* function called when cmd_uint16_write is parsed successfully */
258 static void cmd_uint16_write_parsed(void * parsed_result, void * data)
260 struct cmd_uint16_result *res = parsed_result;
262 uint8_t addr = addr_from_string(res->arg1);
263 printf_P(PSTR("writing %s: %d [0x%.4x]\r\n"), res->arg1,
265 ret = AX12_write_int(&gen.ax12, res->num, addr, res->val);
267 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
270 prog_char str_uint16_arg0_w[] = "write";
271 parse_pgm_token_string_t cmd_uint16_arg0_w = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg0, str_uint16_arg0_w);
272 prog_char str_uint16_arg1_w[] = "moving_speed#goal_pos#cw_angle_limit#ccw_angle_limit#"
273 "max_torque#torque_limit#punch";
274 parse_pgm_token_string_t cmd_uint16_arg1_w = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg1, str_uint16_arg1_w);
275 parse_pgm_token_num_t cmd_uint16_val = TOKEN_NUM_INITIALIZER(struct cmd_uint16_result, val, UINT16);
277 prog_char help_uint16_write[] = "Write uint16 value (write, num, val)";
278 parse_pgm_inst_t cmd_uint16_write = {
279 .f = cmd_uint16_write_parsed, /* function to call */
280 .data = NULL, /* 2nd arg of func */
281 .help_str = help_uint16_write,
282 .tokens = { /* token list, NULL terminated */
283 (prog_void *)&cmd_uint16_arg0_w,
284 (prog_void *)&cmd_uint16_arg1_w,
285 (prog_void *)&cmd_uint16_num,
286 (prog_void *)&cmd_uint16_val,
291 /**********************************************************/
295 /* this structure is filled when cmd_uint8_read is parsed successfully */
296 struct cmd_uint8_result {
303 /* function called when cmd_uint8_read is parsed successfully */
304 static void cmd_uint8_read_parsed(void * parsed_result, void * data)
306 struct cmd_uint8_result *res = parsed_result;
309 uint8_t addr = addr_from_string(res->arg1);
311 ret = AX12_read_byte(&gen.ax12, res->num, addr, &val);
313 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
314 printf_P(PSTR("%s: %d [0x%.2x]\r\n"), res->arg1, val, val);
317 prog_char str_uint8_arg0[] = "read";
318 parse_pgm_token_string_t cmd_uint8_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg0, str_uint8_arg0);
319 prog_char str_uint8_arg1[] = "id#firmware#baudrate#delay#high_lim_temp#"
320 "low_lim_volt#high_lim_volt#status_return#alarm_led#"
321 "alarm_shutdown#torque_enable#led#cw_comp_margin#"
322 "ccw_comp_margin#cw_comp_slope#ccw_comp_slope#"
323 "voltage#temp#reginst#moving#lock";
324 parse_pgm_token_string_t cmd_uint8_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg1, str_uint8_arg1);
325 parse_pgm_token_num_t cmd_uint8_num = TOKEN_NUM_INITIALIZER(struct cmd_uint8_result, num, UINT8);
327 prog_char help_uint8_read[] = "Read uint8 value (type, num)";
328 parse_pgm_inst_t cmd_uint8_read = {
329 .f = cmd_uint8_read_parsed, /* function to call */
330 .data = NULL, /* 2nd arg of func */
331 .help_str = help_uint8_read,
332 .tokens = { /* token list, NULL terminated */
333 (prog_void *)&cmd_uint8_arg0,
334 (prog_void *)&cmd_uint8_arg1,
335 (prog_void *)&cmd_uint8_num,
340 /* function called when cmd_uint8_write is parsed successfully */
341 static void cmd_uint8_write_parsed(void * parsed_result, void * data)
343 struct cmd_uint8_result *res = parsed_result;
344 uint8_t addr = addr_from_string(res->arg1);
346 printf_P(PSTR("writing %s: %d [0x%.2x]\r\n"), res->arg1,
348 ret = AX12_write_byte(&gen.ax12, res->num, addr, res->val);
350 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
353 prog_char str_uint8_arg0_w[] = "write";
354 parse_pgm_token_string_t cmd_uint8_arg0_w = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg0, str_uint8_arg0_w);
355 prog_char str_uint8_arg1_w[] = "id#baudrate#delay#high_lim_temp#"
356 "low_lim_volt#high_lim_volt#status_return#alarm_led#"
357 "alarm_shutdown#torque_enable#led#cw_comp_margin#"
358 "ccw_comp_margin#cw_comp_slope#ccw_comp_slope#"
360 parse_pgm_token_string_t cmd_uint8_arg1_w = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg1, str_uint8_arg1_w);
361 parse_pgm_token_num_t cmd_uint8_val = TOKEN_NUM_INITIALIZER(struct cmd_uint8_result, val, UINT8);
363 prog_char help_uint8_write[] = "Write uint8 value (write, num, val)";
364 parse_pgm_inst_t cmd_uint8_write = {
365 .f = cmd_uint8_write_parsed, /* function to call */
366 .data = NULL, /* 2nd arg of func */
367 .help_str = help_uint8_write,
368 .tokens = { /* token list, NULL terminated */
369 (prog_void *)&cmd_uint8_arg0_w,
370 (prog_void *)&cmd_uint8_arg1_w,
371 (prog_void *)&cmd_uint8_num,
372 (prog_void *)&cmd_uint8_val,