2 * Copyright Droids Corporation, Microb Technology, Eirbot (2008)
3 * JD Brossillon <jean.damien.brossillon@gmail.com> (main job)
4 * Olivier Matz <zer0@droids-corp.org> (clean-up, fixes)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Revision : $Id: ax12.h,v 1.1.4.4 2009-04-07 20:00:57 zer0 Exp $
27 * @brief This module provides functions for using a Robotis Dynamixel
28 * AX-12 numeric actuator
31 * - Manage the "Status Return Level"
33 * AX12 servos uses a half duplex uart. This means that there is only
34 * one line that is used for both transmitting and receiving. Refer to
35 * the AX12 documentation for more informations.
37 * This module doesn't need ATmega's uart, user have to provide
38 * 3 functions to control the half duplex uart:
43 * These functions may use (or not) the AVR uart. Have a look to the
44 * test program in test/ directory for an example.
53 #include "ax12_address.h"
54 #include "ax12_config.h"
56 /*__________________________________________*/
58 /* Half duplex uart switch */
59 #define AX12_STATE_READ 0
60 #define AX12_STATE_WRITE 1
63 #define AX12_ERROR_BIT_VOLTAGE 0
64 #define AX12_ERROR_BIT_ANGLE_LIMIT 1
65 #define AX12_ERROR_BIT_OVERHEAT 2
66 #define AX12_ERROR_BIT_RANGE 3
67 #define AX12_ERROR_BIT_CHECKSUM 4
68 #define AX12_ERROR_BIT_OVERLOAD 5
69 #define AX12_ERROR_BIT_INSTRUCTION 6
71 /* Other error types when bit 7 id 1 */
72 #define AX12_ERROR_TYPE_NO_ANSWER 0xff
73 #define AX12_ERROR_TYPE_TIMEOUT 0xfe
74 #define AX12_ERROR_TYPE_INVALID_PACKET 0xfd
75 #define AX12_ERROR_TYPE_XMIT_FAILED 0xfc
76 #define AX12_ERROR_TYPE_BAD_CKSUM 0xfb
78 /* Address (see ax12_address.h) */
79 #define AX12_BROADCAST_ID 0xFE
81 /* AX-12 instruction set */
82 #define AX12_PING 0x01
83 #define AX12_READ 0x02
84 #define AX12_WRITE 0x03
85 #define AX12_REG_WRITE 0x04
86 #define AX12_ACTION 0x05
87 #define AX12_RESET 0x06
88 #define AX12_SYNC_WRITE 0x83
90 /*__________________________________________*/
94 /* function to send a byte to the serial line. Return 0 on
96 int8_t (*hardware_send)(uint8_t);
98 /* synchronously receive a byte from ax12. Return -1 on
99 * timeout, or the received byte. */
100 int16_t (*hardware_recv)(void);
102 /* switch the line state (read or write) */
103 void (*hardware_switch)(uint8_t);
106 typedef struct AX12_Packet
111 /* Size of AX12_Packet.params */
113 uint8_t params[AX12_MAX_PARAMS];
115 /* Used in status packet */
120 /*___________ Interface ____________*/
122 void AX12_init(AX12 *ax12);
124 /*___________ Hardware layer _____________*/
126 /** @brief Set the function called when writing a character. */
127 void AX12_set_hardware_send(AX12 *ax12, int8_t(*)(uint8_t));
129 /** @brief Set the function called when reading a character. */
130 void AX12_set_hardware_recv(AX12 *ax12, int16_t(*)(void));
132 /** @brief Set the function called when switching line direction */
133 void AX12_set_hardware_switch(AX12 *ax12, void(*)(uint8_t));
135 /*___________ Low level layer ____________*/
137 /** @brief Compute AX12 packet checksum */
138 uint8_t AX12_checksum(AX12_Packet *packet);
140 /** @brief Send a formated AX12 instruction packet. Return 0 on
142 uint8_t AX12_send(AX12 *ax12, AX12_Packet *packet);
144 /* @brief Receive a formated AX12 status packet. Return 0 on
146 uint8_t AX12_recv(AX12 *ax12, AX12_Packet *packet);
148 /*___________ Medium level layer _________*/
151 * ////////////////// WARNING /////////////////////////
152 * The following functions assume that AX12 always
153 * answer with a status packet
154 * (Status Return Level = 0x02 in AX12 EEPROM area)
155 * ////////////////// WARNING /////////////////////////
158 /** @brief Write byte in AX-12 memory
159 * @return Error code from AX-12 (0 means okay) */
160 uint8_t AX12_write_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
163 /** @brief Write integer (2 bytes) in AX-12 memory
164 * @return Error code from AX-12 (0 means okay)
167 * address+1 : data high
169 uint8_t AX12_write_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
172 /** @brief Read byte from AX-12 memory
173 * @return Error code from AX-12 (0 means okay) */
174 uint8_t AX12_read_byte(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
177 /** @brief Write integer (2 bytes) from AX-12 memory
178 * @return Error code from AX-12 (0 means okay) */
179 uint8_t AX12_read_int(AX12 *ax12, uint8_t id, AX12_ADDRESS address,
182 /*___________ High level layer _________*/
184 /** @brief Set AX12 position */
185 uint8_t AX12_set_position(AX12 *ax12,uint8_t id, uint16_t position);
187 /** @brief Set AX12 position and moving speed */
188 uint8_t AX12_set_position2(AX12 *ax12, uint8_t id, uint16_t position,
191 /** @brief Set AX12 position, moving speed and torque */
192 uint8_t AX12_set_position3(AX12 *ax12, uint8_t id, uint16_t position,
193 uint16_t speed, uint16_t torque);
195 /** @brief Read AX12 position */
196 uint8_t AX12_get_position(AX12 *ax12, uint8_t id, uint16_t *pos);
198 /** @brief Read AX12 speed */
199 uint8_t AX12_get_speed(AX12 *ax12, uint8_t id, uint16_t *speed);
201 /** @brief Read AX12 load */
202 uint8_t AX12_get_load(AX12 *ax12, uint8_t id, uint16_t *load);
205 /** @brief Ping an AX12 and return error register */
206 uint8_t AX12_ping(AX12 *ax12, uint8_t id);
208 /** @brief Reset AX12 back to factory settings */
209 uint8_t AX12_reset(AX12 *ax12, uint8_t id);