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.1 2009-03-29 18:44:54 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 <blocking_detection_manager.h>
42 #include <parse_string.h>
43 #include <parse_num.h>
47 uint8_t addr_from_string(const char *s)
50 if (!strcmp_P(s, PSTR("cw_angle_limit")))
51 return AA_CW_ANGLE_LIMIT_L;
52 if (!strcmp_P(s, PSTR("ccw_angle_limit")))
53 return AA_CCW_ANGLE_LIMIT_L;
54 if (!strcmp_P(s, PSTR("max_torque")))
55 return AA_MAX_TORQUE_L;
56 if (!strcmp_P(s, PSTR("down_calibration")))
57 return AA_DOWN_CALIBRATION_L;
58 if (!strcmp_P(s, PSTR("up_calibration")))
59 return AA_UP_CALIBRATION_L;
60 if (!strcmp_P(s, PSTR("torque_limit")))
61 return AA_TORQUE_LIMIT_L;
62 if (!strcmp_P(s, PSTR("position")))
63 return AA_PRESENT_POSITION_L;
64 if (!strcmp_P(s, PSTR("speed")))
65 return AA_PRESENT_SPEED_L;
66 if (!strcmp_P(s, PSTR("load")))
67 return AA_PRESENT_LOAD_L;
68 if (!strcmp_P(s, PSTR("moving_speed")))
69 return AA_MOVING_SPEED_L;
70 if (!strcmp_P(s, PSTR("model")))
71 return AA_MODEL_NUMBER_L;
72 if (!strcmp_P(s, PSTR("goal_pos")))
73 return AA_GOAL_POSITION_L;
74 if (!strcmp_P(s, PSTR("punch")))
78 if (!strcmp_P(s, PSTR("firmware")))
80 if (!strcmp_P(s, PSTR("id")))
82 if (!strcmp_P(s, PSTR("baudrate")))
84 if (!strcmp_P(s, PSTR("delay")))
86 if (!strcmp_P(s, PSTR("high_lim_temp")))
87 return AA_HIGHEST_LIMIT_TEMP;
88 if (!strcmp_P(s, PSTR("low_lim_volt")))
89 return AA_LOWEST_LIMIT_VOLTAGE;
90 if (!strcmp_P(s, PSTR("high_lim_volt")))
91 return AA_HIGHEST_LIMIT_VOLTAGE;
92 if (!strcmp_P(s, PSTR("status_return")))
93 return AA_STATUS_RETURN_LEVEL;
94 if (!strcmp_P(s, PSTR("alarm_led")))
96 if (!strcmp_P(s, PSTR("alarm_shutdown")))
97 return AA_ALARM_SHUTDOWN;
98 if (!strcmp_P(s, PSTR("torque_enable")))
99 return AA_TORQUE_ENABLE;
100 if (!strcmp_P(s, PSTR("led")))
102 if (!strcmp_P(s, PSTR("cw_comp_margin")))
103 return AA_CW_COMPLIANCE_MARGIN;
104 if (!strcmp_P(s, PSTR("ccw_comp_margin")))
105 return AA_CCW_COMPLIANCE_MARGIN;
106 if (!strcmp_P(s, PSTR("cw_comp_slope")))
107 return AA_CW_COMPLIANCE_SLOPE;
108 if (!strcmp_P(s, PSTR("ccw_comp_slope")))
109 return AA_CCW_COMPLIANCE_SLOPE;
110 if (!strcmp_P(s, PSTR("voltage")))
111 return AA_PRESENT_VOLTAGE;
112 if (!strcmp_P(s, PSTR("temp")))
113 return AA_PRESENT_TEMP;
114 if (!strcmp_P(s, PSTR("reginst")))
115 return AA_PRESENT_REGINST;
116 if (!strcmp_P(s, PSTR("moving")))
118 if (!strcmp_P(s, PSTR("lock")))
124 /**********************************************************/
127 /* this structure is filled when cmd_ax12_stress is parsed successfully */
128 struct cmd_ax12_stress_result {
133 /* function called when cmd_ax12_stress is parsed successfully */
134 static void cmd_ax12_stress_parsed(void *parsed_result, void *data)
136 struct cmd_ax12_stress_result *res = parsed_result;
139 microseconds t = time_get_us2();
141 for (i=0; i<1000; i++) {
142 if (AX12_read_byte(&gen.ax12, res->id, AA_ID, &val) != 0)
146 printf_P(PSTR("%d errors / 1000\r\n"), nb_errs);
147 t = (time_get_us2() - t) / 1000;
148 printf_P(PSTR("Test done in %d ms\r\n"), (int)t);
151 prog_char str_ax12_stress_arg0[] = "ax12_stress";
152 parse_pgm_token_string_t cmd_ax12_stress_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_ax12_stress_result, arg0, str_ax12_stress_arg0);
153 parse_pgm_token_num_t cmd_ax12_stress_id = TOKEN_NUM_INITIALIZER(struct cmd_ax12_stress_result, id, UINT8);
155 prog_char help_ax12_stress[] = "Stress an AX12 with 1000 'read id' commands";
156 parse_pgm_inst_t cmd_ax12_stress = {
157 .f = cmd_ax12_stress_parsed, /* function to call */
158 .data = NULL, /* 2nd arg of func */
159 .help_str = help_ax12_stress,
160 .tokens = { /* token list, NULL terminated */
161 (prog_void *)&cmd_ax12_stress_arg0,
162 (prog_void *)&cmd_ax12_stress_id,
167 /**********************************************************/
169 /* this structure is filled when cmd_baudrate is parsed successfully */
170 struct cmd_baudrate_result {
175 /* function called when cmd_baudrate is parsed successfully */
176 static void cmd_baudrate_parsed(void * parsed_result, void * data)
178 struct cmd_baudrate_result *res = parsed_result;
179 struct uart_config c;
181 printf_P(PSTR("%d %d\r\n"), UBRR1H, UBRR1L);
183 c.baudrate = res->arg1;
185 printf_P(PSTR("%d %d\r\n"), UBRR1H, UBRR1L);
188 prog_char str_baudrate_arg0[] = "baudrate";
189 parse_pgm_token_string_t cmd_baudrate_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_baudrate_result, arg0, str_baudrate_arg0);
190 parse_pgm_token_num_t cmd_baudrate_arg1 = TOKEN_NUM_INITIALIZER(struct cmd_baudrate_result, arg1, UINT32);
192 prog_char help_baudrate[] = "Change ax12 baudrate";
193 parse_pgm_inst_t cmd_baudrate = {
194 .f = cmd_baudrate_parsed, /* function to call */
195 .data = NULL, /* 2nd arg of func */
196 .help_str = help_baudrate,
197 .tokens = { /* token list, NULL terminated */
198 (prog_void *)&cmd_baudrate_arg0,
199 (prog_void *)&cmd_baudrate_arg1,
204 /**********************************************************/
208 /* this structure is filled when cmd_uint16_read is parsed successfully */
209 struct cmd_uint16_result {
216 /* function called when cmd_uint16_read is parsed successfully */
217 static void cmd_uint16_read_parsed(void * parsed_result, void * data)
219 struct cmd_uint16_result *res = parsed_result;
222 uint8_t addr = addr_from_string(res->arg1);
223 ret = AX12_read_int(&gen.ax12, res->num, addr, &val);
225 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
226 printf_P(PSTR("%s: %d [0x%.4x]\r\n"), res->arg1, val, val);
229 prog_char str_uint16_arg0[] = "read";
230 parse_pgm_token_string_t cmd_uint16_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg0, str_uint16_arg0);
231 prog_char str_uint16_arg1[] = "moving_speed#model#goal_pos#cw_angle_limit#ccw_angle_limit#"
232 "max_torque#down_calibration#up_calibration#torque_limit#"
233 "position#speed#load#punch";
234 parse_pgm_token_string_t cmd_uint16_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg1, str_uint16_arg1);
235 parse_pgm_token_num_t cmd_uint16_num = TOKEN_NUM_INITIALIZER(struct cmd_uint16_result, num, UINT8);
237 prog_char help_uint16_read[] = "Read uint16 value (type, num)";
238 parse_pgm_inst_t cmd_uint16_read = {
239 .f = cmd_uint16_read_parsed, /* function to call */
240 .data = NULL, /* 2nd arg of func */
241 .help_str = help_uint16_read,
242 .tokens = { /* token list, NULL terminated */
243 (prog_void *)&cmd_uint16_arg0,
244 (prog_void *)&cmd_uint16_arg1,
245 (prog_void *)&cmd_uint16_num,
250 /* function called when cmd_uint16_write is parsed successfully */
251 static void cmd_uint16_write_parsed(void * parsed_result, void * data)
253 struct cmd_uint16_result *res = parsed_result;
255 uint8_t addr = addr_from_string(res->arg1);
256 printf_P(PSTR("writing %s: %d [0x%.4x]\r\n"), res->arg1,
258 ret = AX12_write_int(&gen.ax12, res->num, addr, res->val);
260 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
263 prog_char str_uint16_arg0_w[] = "write";
264 parse_pgm_token_string_t cmd_uint16_arg0_w = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg0, str_uint16_arg0_w);
265 prog_char str_uint16_arg1_w[] = "moving_speed#goal_pos#cw_angle_limit#ccw_angle_limit#"
266 "max_torque#torque_limit#punch";
267 parse_pgm_token_string_t cmd_uint16_arg1_w = TOKEN_STRING_INITIALIZER(struct cmd_uint16_result, arg1, str_uint16_arg1_w);
268 parse_pgm_token_num_t cmd_uint16_val = TOKEN_NUM_INITIALIZER(struct cmd_uint16_result, val, UINT16);
270 prog_char help_uint16_write[] = "Write uint16 value (write, num, val)";
271 parse_pgm_inst_t cmd_uint16_write = {
272 .f = cmd_uint16_write_parsed, /* function to call */
273 .data = NULL, /* 2nd arg of func */
274 .help_str = help_uint16_write,
275 .tokens = { /* token list, NULL terminated */
276 (prog_void *)&cmd_uint16_arg0_w,
277 (prog_void *)&cmd_uint16_arg1_w,
278 (prog_void *)&cmd_uint16_num,
279 (prog_void *)&cmd_uint16_val,
284 /**********************************************************/
288 /* this structure is filled when cmd_uint8_read is parsed successfully */
289 struct cmd_uint8_result {
296 /* function called when cmd_uint8_read is parsed successfully */
297 static void cmd_uint8_read_parsed(void * parsed_result, void * data)
299 struct cmd_uint8_result *res = parsed_result;
302 uint8_t addr = addr_from_string(res->arg1);
304 ret = AX12_read_byte(&gen.ax12, res->num, addr, &val);
306 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
307 printf_P(PSTR("%s: %d [0x%.2x]\r\n"), res->arg1, val, val);
310 prog_char str_uint8_arg0[] = "read";
311 parse_pgm_token_string_t cmd_uint8_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg0, str_uint8_arg0);
312 prog_char str_uint8_arg1[] = "id#firmware#baudrate#delay#high_lim_temp#"
313 "low_lim_volt#high_lim_volt#status_return#alarm_led#"
314 "alarm_shutdown#torque_enable#led#cw_comp_margin#"
315 "ccw_comp_margin#cw_comp_slope#ccw_comp_slope#"
316 "voltage#temp#reginst#moving#lock";
317 parse_pgm_token_string_t cmd_uint8_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg1, str_uint8_arg1);
318 parse_pgm_token_num_t cmd_uint8_num = TOKEN_NUM_INITIALIZER(struct cmd_uint8_result, num, UINT8);
320 prog_char help_uint8_read[] = "Read uint8 value (type, num)";
321 parse_pgm_inst_t cmd_uint8_read = {
322 .f = cmd_uint8_read_parsed, /* function to call */
323 .data = NULL, /* 2nd arg of func */
324 .help_str = help_uint8_read,
325 .tokens = { /* token list, NULL terminated */
326 (prog_void *)&cmd_uint8_arg0,
327 (prog_void *)&cmd_uint8_arg1,
328 (prog_void *)&cmd_uint8_num,
333 /* function called when cmd_uint8_write is parsed successfully */
334 static void cmd_uint8_write_parsed(void * parsed_result, void * data)
336 struct cmd_uint8_result *res = parsed_result;
337 uint8_t addr = addr_from_string(res->arg1);
339 printf_P(PSTR("writing %s: %d [0x%.2x]\r\n"), res->arg1,
341 ret = AX12_write_byte(&gen.ax12, res->num, addr, res->val);
343 printf_P(PSTR("AX12 error %.2x!\r\n"), ret);
346 prog_char str_uint8_arg0_w[] = "write";
347 parse_pgm_token_string_t cmd_uint8_arg0_w = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg0, str_uint8_arg0_w);
348 prog_char str_uint8_arg1_w[] = "id#baudrate#delay#high_lim_temp#"
349 "low_lim_volt#high_lim_volt#status_return#alarm_led#"
350 "alarm_shutdown#torque_enable#led#cw_comp_margin#"
351 "ccw_comp_margin#cw_comp_slope#ccw_comp_slope#"
353 parse_pgm_token_string_t cmd_uint8_arg1_w = TOKEN_STRING_INITIALIZER(struct cmd_uint8_result, arg1, str_uint8_arg1_w);
354 parse_pgm_token_num_t cmd_uint8_val = TOKEN_NUM_INITIALIZER(struct cmd_uint8_result, val, UINT8);
356 prog_char help_uint8_write[] = "Write uint8 value (write, num, val)";
357 parse_pgm_inst_t cmd_uint8_write = {
358 .f = cmd_uint8_write_parsed, /* function to call */
359 .data = NULL, /* 2nd arg of func */
360 .help_str = help_uint8_write,
361 .tokens = { /* token list, NULL terminated */
362 (prog_void *)&cmd_uint8_arg0_w,
363 (prog_void *)&cmd_uint8_arg1_w,
364 (prog_void *)&cmd_uint8_num,
365 (prog_void *)&cmd_uint8_val,