Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
dxl.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: libdxl
6 //
7 // File: dxl.h
8 //
9 /*! \file
10  *
11  * \brief Modified dynamixel SDK interface.
12  *
13  * Based on Robotis Inc. dxl_sdk-1.01
14  *
15  * \author Robotis
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  */
18 /*
19  * This file is a modified version of the file freely provided by Robotis Inc.
20  * No restrictions by Robotis Inc. nor RoadNarrows LLC are made.
21  */
22 ////////////////////////////////////////////////////////////////////////////////
23 
24 #ifndef _DXL_H
25 #define _DXL_H
26 
27 #include <sys/types.h>
28 #include <stdint.h>
29 
30 #include <string>
31 
32 namespace libdxl
33 {
34  //----------------------------------------------------------------------------
35  // Defines and Types
36  //----------------------------------------------------------------------------
37 
38  //
39  // Common baud number enumeration.
40  //
41 #define DXL_BAUDNUM_1000000 (1) ///< 1,000,000 bps at 0.0% tolerance
42 #define DXL_BAUDNUM_500000 (3) ///< 500,000 bps at 0.0% tolerance
43 #define DXL_BAUDNUM_400000 (4) ///< 500,000 bps at 0.0% tolerance
44 #define DXL_BAUDNUM_250000 (7) ///< 250,000 bps at 0.0% tolerance
45 #define DXL_BAUDNUM_200000 (9) ///< 200,000 bps at 0.0% tolerance
46 #define DXL_BAUDNUM_115200 (16) ///< 115,200 bps at -2.124% tolerance
47 #define DXL_BAUDNUM_57600 (34) ///< 57,600 bps at 0.794 % tolerance
48 #define DXL_BAUDNUM_19200 (103) ///< 19,200 bps at -0.160% tolerance
49 #define DXL_BAUDNUM_9600 (207) ///< 9,600 bps at -0.160% tolerance
50 
51  //
52  // Extended baud number enumeration.
53  //
54 #define DXL_BAUDNUM_EXT_2250000 (250) ///< 2,250,000 bps at 0.0% tolerance
55 #define DXL_BAUDNUM_EXT_2500000 (251) ///< 2,500,000 bps at 0.0% tolerance
56 #define DXL_BAUDNUM_EXT_3000000 (252) ///< 3,000,000 bps at 0.0% tolerance
57 
58  // Dynamixel default rate.
59 #define DXL_DEFAULT_BAUDNUMBER (34) ///< default baud number
60 
61  //
62  // Packet field indices.
63  //
64 #define DXL_ID (2) ///< servo id field packet index
65 #define DXL_LENGTH (3) ///< packet length field packet index
66 #define DXL_INSTRUCTION (4) ///< instruction field packet index
67 #define DXL_ERRBIT (4) ///< error bits field packet index
68 #define DXL_PARAMETER (5) ///< start of parameter fields packet index
69 
70  //
71  // Maximum transmit/receive lengths.
72  //
73 #define DXL_MAXNUM_TXPARAM (150) ///< maximum number of tx parameters
74 #define DXL_MAXNUM_RXPARAM (60) ///< maximum number of rx parameters
75 
76  //
77  // Instructions (commands).
78  //
79 #define DXL_INST_PING (1) ///< ping servo
80 #define DXL_INST_READ (2) ///< read servo control table data
81 #define DXL_INST_WRITE (3) ///< write servo control table data
82 #define DXL_INST_REG_WRITE (4) ///< register write
83 #define DXL_INST_ACTION (5) ///< action
84 #define DXL_INST_RESET (6) ///< reset servo defaults
85 #define DXL_INST_SYNC_WRITE (131) ///< synchronous write servo(s) data
86 
87  //
88  // Servo Ids
89  //
90 #define DXL_MIN_ID (0) ///< minimum servo id
91 #define DXL_MAX_ID (253) ///< maximum servo id
92 #define DXL_BROADCAST_ID (254) ///< broadcast "servo" id
93 
94  //
95  // Servo error bits.
96  //
97 #define DXL_ERRBIT_VOLTAGE (1) ///< voltage out-of-range error
98 #define DXL_ERRBIT_ANGLE (2) ///< servo angle out-of-range error
99 #define DXL_ERRBIT_OVERHEAT (4) ///< temperature out-of-range error
100 #define DXL_ERRBIT_RANGE (8) ///< parameter? out-of-range error
101 #define DXL_ERRBIT_CHECKSUM (16) ///< bad checksum error
102 #define DXL_ERRBIT_OVERLOAD (32) ///< load out-of-range error
103 #define DXL_ERRBIT_INSTRUCTION (64) ///< bad instruction error
104 
105  //
106  // Communication result codes.
107  //
108 #define DXL_COMM_TXSUCCESS (0) ///< transmit success
109 #define DXL_COMM_RXSUCCESS (1) ///< receive success
110 #define DXL_COMM_TXFAIL (2) ///< transmit failure error
111 #define DXL_COMM_RXFAIL (3) ///< receive failure error
112 #define DXL_COMM_TXERROR (4) ///< packed transmit packet format error
113 #define DXL_COMM_RXWAITING (5) ///< waiting for complete receive packet
114 #define DXL_COMM_RXTIMEOUT (6) ///< receive timeout error
115 #define DXL_COMM_RXCORRUPT (7) ///< receive corrupted packet
116 
117  /*!
118  * \brief half-duplex control transmit callback type.
119  *
120  * \param arg Callback user argument.
121  */
122  typedef void (*dxl_hdctl_tx_cb_t)(void *arg);
123 
124  /*!
125  * \brief half-duplex control receive callback type.
126  *
127  * \param arg Callback user argument.
128  * \param arg Transmit packet length.
129  */
130  typedef void (*dxl_hdctl_rx_cb_t)(void *arg, size_t txlen);
131 
132  //
133  // Foward declarations.
134  //
135  class dxlhal;
136 
137  //----------------------------------------------------------------------------
138  // Class dxl
139  //----------------------------------------------------------------------------
140 
141  class dxl
142  {
143  public:
144  /*!
145  * \brief Default constructor.
146  */
147  dxl();
148 
149  /*!
150  * \brief Destructor.
151  */
152  ~dxl();
153 
154  /*!
155  * \brief Open Dynamixel Bus serial interface.
156  *
157  * The serial device is opened and attributes set.
158  *
159  * \param deviceName Serial device name.
160  * \param baudrate Dynamixel baud rate.
161  *
162  * \return Returns 1 on success, 0 on failure.
163  */
164  int open(const char *deviceName, int baudrate);
165 
166  /*!
167  * \brief Initialize Dynamixel Bus USB serial interface by index.
168  *
169  * The serial device is opened and attributes set.
170  *
171  * \note This method is deprecated. The preferred method is open().
172  *
173  * \param deviceIndex Device number of /dev/ttyUSBn, n == deviceIndex.
174  * \param baudnum Dynamixel baud number.
175  *
176  * \return Returns 1 on success, 0 on failure.
177  */
178  int dxl_initialize(int deviceIndex, int baudnum);
179 
180  /*!
181  * \brief Close the Dynamixel Bus serial interface.
182  *
183  * The serial device is closed.
184  */
185  void close();
186 
187  /*!
188  * \brief Set Dynamixel Bus serial device baud rate.
189  *
190  * \param baudrate Baud rate.
191  *
192  * \return Returns 1 on success, 0 on failure.
193  */
194  int setBaudRate(int baudrate);
195 
196  /*!
197  * \brief Set serial half-duplex callbacks.
198  *
199  * The relavent callback is made just prior to transmitting or receiving a
200  * packet.
201  *
202  * \param enable_tx_cb Enable transmit callback.
203  * \param enable_rx_cb Enable receive callback.
204  * \param arg Tx/Rx callback parameter.
205  *
206  * \return Returns 1 on success, 0 on failure.
207  */
208  int setHalfDuplexCallbacks(dxl_hdctl_tx_cb_t enable_tx_cb,
209  dxl_hdctl_rx_cb_t enable_rx_cb,
210  void *arg);
211 
212  /*!
213  * \brief Get the Dynamixel Bus serial device file descriptor.
214  *
215  * \return Returns >= 0 if the device is open, -1 otherwise.
216  */
217  int getFd();
218 
219  /*!
220  * \brief Set transmit packet servo id.
221  *
222  * \param id Servo id or broadcast id.
223  */
224  void setTxPacketId(int id);
225 
226  /*!
227  * \brief Set transmit packet instruction.
228  *
229  * \param instruction Request instruction id.
230  */
231  void setTxPacketInstruction(int instruction);
232 
233  /*!
234  * \brief Set transmit packet parameter value
235  *
236  * \param index Zero offset parameter index.
237  * \param value Parameter byte value.
238  */
239  void setTxPacketParameter(int index, int value);
240 
241  /*!
242  * \brief Set transmit packet length.
243  *
244  * \param length Length in bytes.
245  */
246  void setTxPacketLength(int length);
247 
248  /*!
249  * \brief Get receive packet error bits field.
250  *
251  * \return Error bits.
252  */
253  unsigned int getRxPacketErrBits();
254 
255  /*!
256  * \brief Test if error bit is set on received packet.
257  *
258  * \param errbit Error bit to test.
259  *
260  * \return Returns true or false.
261  */
262  bool getRxPacketError(unsigned int errbit);
263 
264 
265  /*!
266  * \brief Get receive packet length field.
267  *
268  * \return Receive packet expected length.
269  */
270  int getRxPacketLength();
271 
272  /*!
273  * \brief Get receive packet parameter value
274  *
275  * \param index Zero offset parameter index.
276  *
277  * \return Paramter byte value.
278  */
279  int getRxPacketParameter(int index);
280 
281  /*!
282  * \brief Make 16-bit word.
283  *
284  * \param lowbyte Least significant byte.
285  * \param highbyte Most significant byte.
286  *
287  * \return Packed word.
288  */
289  int makeWord(int lowbyte, int highbyte);
290 
291  /*!
292  * \brief Get low byte from word.
293  *
294  * \param word 16-bit word.
295  *
296  * \return Unpacked least significant byte.
297  */
298  int getLowByte(int word);
299 
300  /*!
301  * \brief Get high byte from word.
302  *
303  * \param word 16-bit word.
304  *
305  * \return Unpacked most significant byte.
306  */
307  int getHighByte(int word);
308 
309  /*!
310  * \brief Transmit a previously packed instruction packet.
311  *
312  * \par Transmit Packet
313  * 0xff 0xff ID LENGTH INSTRUCTION paramters CHECKSUM
314  */
315  void txPacket();
316 
317  /*!
318  * \brief Receive status packet.
319  *
320  * \par Non-Broadcast Receive Packet
321  * 0xff 0xff ID LENGTH ERRBIT paramters CHECKSUM
322  */
323  void rxPacket();
324 
325  /*!
326  * \brief Transmit instruction packet and receive status response packet.
327  *
328  * Use dxl_get_result() to check for success or failure.
329  *
330  * Use the tranmit packing functions to build the instruction packet and the
331  * receive unpacking function to get any read data.
332  */
333  void txrxPacket();
334 
335  /*!
336  * \brief Get last transmit/receive result.
337  *
338  * \return Result code.
339  */
340  int getResult();
341 
342  /*!
343  * \brief Ping servo.
344  *
345  * Use dxl_get_result() to check for success or failure.
346  *
347  * \param id Servo id.
348  */
349  void ping(int id);
350 
351  /*!
352  * \brief Read byte from servo's control table.
353  *
354  * Use dxl_get_result() to check for success or failure.
355  *
356  * \param id Servo id.
357  * \param address Control table address.
358  *
359  * \return Read 8-bit byte.
360  */
361  int readByte(int id, int address);
362 
363  /*!
364  * \brief Write byte to servo's control table.
365  *
366  * Use dxl_get_result() to check for success or failure.
367  *
368  * \param id Servo id.
369  * \param address Control table address.
370  * \param value 8-bit value to write.
371  */
372  void writeByte(int id, int address, int value);
373 
374  /*!
375  * \brief Read word from servo's control table.
376  *
377  * Use dxl_get_result() to check for success or failure.
378  *
379  * \param id Servo id.
380  * \param address Control table address.
381  *
382  * \return Read 16-bit word.
383  */
384  int readWord(int id, int address);
385 
386  /*!
387  * \brief Write word to servo's control table.
388  *
389  * Use dxl_get_result() to check for success or failure.
390  *
391  * \param id Servo id.
392  * \param address Control table address.
393  * \param value 16-bit value to write.
394  */
395  void writeWord(int id, int address, int value);
396 
397  protected:
398  // packets
400  ///< instruction transmit packet buffer
402  ///< status receive packet buffer
403 
404  // communication
405  int m_nRxPacketLength; ///< expected received packet length
406  int m_nRxGetLength; ///< current received packet length
407  int m_nCommStatus; ///< communication status
408  bool m_bBusUsing; ///< bus is [not] busy
409 
410  // half-duplex control callbacks.
411  dxl_hdctl_tx_cb_t m_fnEnableTx; ///< enable tx callback
412  dxl_hdctl_rx_cb_t m_fnEnableRx; ///< enable rx callback
413  void *m_pTxRxArg; ///< tx/rx argument passback
414 
415  // implementaion hardware abstraction layer
416  dxlhal *m_pDxlHal;
417 
418  /*!
419  * \brief Convert Dynamixel baud number to baud rate.
420  *
421  * \note This method is deprecated. The preferred method is open().
422  *
423  * \param baudnum Dynamixel baud number.
424  *
425  * \return Baud rate.
426  */
427  float dxl_baudnum_to_baudrate(int baudnum);
428  };
429 
430 } // namespace libdxl
431 
432 
433 #endif // _DXL_H
void writeWord(int id, int address, int value)
Write word to servo&#39;s control table.
Definition: dynamixel.cxx:505
int getHighByte(int word)
Get high byte from word.
Definition: dynamixel.cxx:173
void(* dxl_hdctl_tx_cb_t)(void *arg)
half-duplex control transmit callback type.
Definition: dxl.h:122
void * m_pTxRxArg
tx/rx argument passback
Definition: dxl.h:413
#define DXL_MAXNUM_TXPARAM
maximum number of tx parameters
Definition: dxl.h:73
int getLowByte(int word)
Get low byte from word.
Definition: dynamixel.cxx:165
dxl_hdctl_rx_cb_t m_fnEnableRx
enable rx callback
Definition: dxl.h:412
void setTxPacketParameter(int index, int value)
Set transmit packet parameter value.
Definition: dynamixel.cxx:125
float dxl_baudnum_to_baudrate(int baudnum)
Convert Dynamixel baud number to baud rate.
Definition: dynamixel.cxx:519
int getResult()
Get last transmit/receive result.
Definition: dynamixel.cxx:446
int m_nRxPacketLength
expected received packet length
Definition: dxl.h:405
void txrxPacket()
Transmit instruction packet and receive status response packet.
Definition: dynamixel.cxx:431
int getRxPacketParameter(int index)
Get receive packet parameter value.
Definition: dynamixel.cxx:150
void setTxPacketId(int id)
Set transmit packet servo id.
Definition: dynamixel.cxx:115
void writeByte(int id, int address, int value)
Write byte to servo&#39;s control table.
Definition: dynamixel.cxx:477
int setHalfDuplexCallbacks(dxl_hdctl_tx_cb_t enable_tx_cb, dxl_hdctl_rx_cb_t enable_rx_cb, void *arg)
Set serial half-duplex callbacks.
Definition: dynamixel.cxx:99
Definition: dxl.h:32
dxl()
Default constructor.
Definition: dynamixel.cxx:37
~dxl()
Destructor.
Definition: dynamixel.cxx:54
void setTxPacketInstruction(int instruction)
Set transmit packet instruction.
Definition: dynamixel.cxx:120
void(* dxl_hdctl_rx_cb_t)(void *arg, size_t txlen)
half-duplex control receive callback type.
Definition: dxl.h:130
void rxPacket()
Receive status packet.
Definition: dynamixel.cxx:271
int makeWord(int lowbyte, int highbyte)
Make 16-bit word.
Definition: dynamixel.cxx:155
dxl_hdctl_tx_cb_t m_fnEnableTx
enable tx callback
Definition: dxl.h:411
void txPacket()
Transmit a previously packed instruction packet.
Definition: dynamixel.cxx:182
int open(const char *deviceName, int baudrate)
Open Dynamixel Bus serial interface.
Definition: dynamixel.cxx:59
unsigned int getRxPacketErrBits()
Get receive packet error bits field.
Definition: dynamixel.cxx:135
uint8_t m_bufInstructionPacket[(150)+10]
instruction transmit packet buffer
Definition: dxl.h:399
int getRxPacketLength()
Get receive packet length field.
Definition: dynamixel.cxx:145
uint8_t m_bufStatusPacket[(60)+10]
status receive packet buffer
Definition: dxl.h:401
bool m_bBusUsing
bus is [not] busy
Definition: dxl.h:408
int setBaudRate(int baudrate)
Set Dynamixel Bus serial device baud rate.
Definition: dynamixel.cxx:94
void close()
Close the Dynamixel Bus serial interface.
Definition: dynamixel.cxx:89
#define DXL_MAXNUM_RXPARAM
maximum number of rx parameters
Definition: dxl.h:74
int getFd()
Get the Dynamixel Bus serial device file descriptor.
Definition: dynamixel.cxx:110
int m_nCommStatus
communication status
Definition: dxl.h:407
void ping(int id)
Ping servo.
Definition: dynamixel.cxx:451
bool getRxPacketError(unsigned int errbit)
Test if error bit is set on received packet.
Definition: dynamixel.cxx:140
int dxl_initialize(int deviceIndex, int baudnum)
Initialize Dynamixel Bus USB serial interface by index.
Definition: dynamixel.cxx:72
void setTxPacketLength(int length)
Set transmit packet length.
Definition: dynamixel.cxx:130
int readByte(int id, int address)
Read byte from servo&#39;s control table.
Definition: dynamixel.cxx:462
int readWord(int id, int address)
Read word from servo&#39;s control table.
Definition: dynamixel.cxx:490
int m_nRxGetLength
current received packet length
Definition: dxl.h:406