Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
dxlhal.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: libdxl
6 //
7 // File: dxl_hal.h
8 //
9 /*! \file
10  *
11  * \brief Dynamixel Hardware Abstraction Layer implementation 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 _DXLHAL_H
25 #define _DXLHAL_H
26 
27 #include <sys/types.h>
28 #include <stdint.h>
29 
30 #include <string>
31 
32 namespace libdxl
33 {
34 #define MAX_RTD 254 ///< max return time delay value for servos
35 #define SEC_PER_RTD (0.000002) ///< seconds per RTD value (2 usec)
36 #define MAX_T_RTD ((double)MAX_RTD * SEC_PER_RTD)
37  ///< maximum time delay at maximum RTD
38 
39  class dxlhal
40  {
41  public:
42  /*!
43  * \brief Default constructor.
44  */
45  dxlhal();
46 
47  /*!
48  * \brief Destructor.
49  */
50  ~dxlhal();
51 
52  /*!
53  * \brief Open Dynamixel Bus USB serial device by name.
54  *
55  * The serial device is opened and attributes set.
56  *
57  * \param deviceName Name of serial device.
58  * \param baudrate Serial baud rate.
59  *
60  * \return Returns 1 on success, 0 on failure.
61  */
62  int open(const char *deviceName, int baudrate);
63 
64  /*!
65  * \brief Open Dynamixel Bus USB serial device by index.
66  *
67  * The serial device is opened and attributes set.
68  *
69  * \note This method is deprecated. The preferred method is open().
70  *
71  * \param deviceIndex Device number of /dev/ttyUSBn, n == deviceIndex.
72  * \param baudrate Serial baud rate.
73  *
74  * \return Returns 1 on success, 0 on failure.
75  */
76  int dxl_hal_open(int deviceIndex, float baudrate);
77 
78  /*!
79  * \brief Close Dynamixel Bus serial interface.
80  */
81  void close();
82 
83  /*!
84  * \brief Get the Dynamixel Bus serial device name.
85  *
86  * \return String.
87  */
88  std::string getDeviceName();
89 
90  /*!
91  * \brief Get the Dynamixel Bus serial device baud rate.
92  *
93  * \return Returns baud rate.
94  */
95  int getBaudRate();
96 
97  /*!
98  * \brief Get the Dynamixel Bus serial device file descriptor.
99  *
100  * \return Returns >= 0 if the device is open, -1 otherwise.
101  */
102  int getFd();
103 
104  /*!
105  * \brief Set opened Dynamixel Bus serial interface baud rate.
106  *
107  * \param baudrate Baud rate.
108  *
109  * \return Returns 1 on success, 0 on failure.
110  */
111  int setBaudRate(int baudrate);
112 
113  /*!
114  * \brief Discard any pending recieve data.
115  */
116  void clear();
117 
118  /*!
119  * \brief Transmit Dynamixel instruction packet.
120  *
121  * \param [in] pPacket Packed packet.
122  * \param numPacket Number of bytes to transmit.
123  *
124  * \return Returns 1 on success, 0 on failure.
125  */
126  int tx(unsigned char *pPacket, int numPacket);
127 
128  /*!
129  * \brief Receive Dynamixel status packet.
130  *
131  * \param [out] pPacket Packed packet.
132  * \param numPacket Number of bytes to read.
133  *
134  * \return On success, number of bytes received. -1 on error.
135  */
136  int rx(unsigned char *pPacket, int numPacket);
137 
138  /*!
139  * \brief Set receive timeout.
140  *
141  * \param NumRcvByte Number of expected receive bytes
142  */
143  void setTimeout(int NumRcvByte);
144 
145  /*!
146  * \brief Test if receive timeout has expired.
147  *
148  * \return Returns 1 if true, false otherwise.
149  */
150  bool hasTimedOut();
151 
152  protected:
153  std::string m_strDeviceName; ///< device name
154  int m_nBaudRate; ///< device baud rate
155  int m_fd; ///< device file descriptor
156  double m_fSecPerByte; ///< seconds per byte transfer time
157  double m_fStartTime; ///< event start time
158  double m_fRcvWaitTime; ///< receive max elapse wait time
159 
160  /*!
161  * \brief Calculate seconds/byte transmit/recieve time in seconds.
162  *
163  * \note Over serial, START_BIT byte STOP_BIT = 10 bits.
164  *
165  * \param baudrate Serial baud rate.
166  *
167  * \return Seconds per byte.
168  */
169  double calcSecPerByte(int baudrate);
170 
171  /*!
172  * \brief Mark time.
173  *
174  * \return Return marked time in seconds.
175  */
176  double now();
177  };
178 
179 
180 } // namespace libdxl
181 
182 #endif // _DXLHAL_H
int rx(unsigned char *pPacket, int numPacket)
Receive Dynamixel status packet.
Definition: dxlhal.cxx:229
void close()
Close Dynamixel Bus serial interface.
Definition: dxlhal.cxx:146
int m_fd
device file descriptor
Definition: dxlhal.h:155
double m_fSecPerByte
seconds per byte transfer time
Definition: dxlhal.h:156
dxlhal()
Default constructor.
Definition: dxlhal.cxx:46
Definition: dxl.h:32
int m_nBaudRate
device baud rate
Definition: dxlhal.h:154
~dxlhal()
Destructor.
Definition: dxlhal.cxx:55
double m_fRcvWaitTime
receive max elapse wait time
Definition: dxlhal.h:158
int dxl_hal_open(int deviceIndex, float baudrate)
Open Dynamixel Bus USB serial device by index.
Definition: dxlhal.cxx:86
double calcSecPerByte(int baudrate)
Calculate seconds/byte transmit/recieve time in seconds.
Definition: dxlhal.cxx:289
void setTimeout(int NumRcvByte)
Set receive timeout.
Definition: dxlhal.cxx:248
bool hasTimedOut()
Test if receive timeout has expired.
Definition: dxlhal.cxx:260
int open(const char *deviceName, int baudrate)
Open Dynamixel Bus USB serial device by name.
Definition: dxlhal.cxx:60
std::string getDeviceName()
Get the Dynamixel Bus serial device name.
Definition: dxlhal.cxx:158
std::string m_strDeviceName
device name
Definition: dxlhal.h:153
int setBaudRate(int baudrate)
Set opened Dynamixel Bus serial interface baud rate.
Definition: dxlhal.cxx:173
double now()
Mark time.
Definition: dxlhal.cxx:280
double m_fStartTime
event start time
Definition: dxlhal.h:157
void clear()
Discard any pending recieve data.
Definition: dxlhal.cxx:207
int tx(unsigned char *pPacket, int numPacket)
Transmit Dynamixel instruction packet.
Definition: dxlhal.cxx:212
int getFd()
Get the Dynamixel Bus serial device file descriptor.
Definition: dxlhal.cxx:168
int getBaudRate()
Get the Dynamixel Bus serial device baud rate.
Definition: dxlhal.cxx:163