Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaCommSerial.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: librnr_dynamixel
6 //
7 // File: DynaCommSerial.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-03-13 13:28:02 -0600 (Fri, 13 Mar 2015) $
12  * $Rev: 3890 $
13  *
14  * \ingroup dyna_lib_hdrs
15  *
16  * \brief RoadNarrows Dynamixel Bus Communication over Serial Interface Class
17  * Interface.
18  *
19  * \author Robin Knight (robin.knight@roadnarrows.com)
20  *
21  * \copyright
22  * \h_copy 2011-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Unless otherwise stated explicitly, all materials contained are copyrighted
30  * and may not be used without RoadNarrows LLC's written consent,
31  * except as provided in these terms and conditions or in the copyright
32  * notice (documents and software) or other proprietary notice provided with
33  * the relevant materials.
34  *
35  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
36  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
37  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
38  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
39  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
40  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
43  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
44  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
45  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
46  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
47  *
48  * @EulaEnd@
49  */
50 ////////////////////////////////////////////////////////////////////////////////
51 
52 #ifndef _DYNA_COMM_SERIAL_H
53 #define _DYNA_COMM_SERIAL_H
54 
55 #include "rnr/rnrconfig.h"
56 #include "rnr/log.h"
57 
58 #include "Dynamixel/dxl/dxl.h"
59 
60 #include "Dynamixel/Dynamixel.h"
61 #include "Dynamixel/DynaError.h"
62 #include "Dynamixel/DynaComm.h"
63 
64 
65 // ---------------------------------------------------------------------------
66 // Dynamixel Serial Bus Communications Class
67 // ---------------------------------------------------------------------------
68 
69 /*!
70  * \ingroup dyna_lib_classes
71  *
72  * \brief Dynamixel Serial Bus Communications Class
73  */
74 class DynaCommSerial : public DynaComm
75 {
76 public:
77  static const int RetryMax = 5; ///< maximum number of I/O retries
78  static const int RetryWait = 20000; ///< microsecond wait between retries
79 
80  /*!
81  * \brief Half-duplex control signals.
82  *
83  * \note Keep out of any potential non-modem signal space
84  * (e.g. GPIO 123, etc).
85  */
86  enum CtlSignal
87  {
88  // GPIO: [-999, 999]
89  CtlSignalMinGPIO = -999, ///< gpio signal is inverted (tx = low, rx = high)
90  CtlSignalMaxGPIO = 999, ///< gpio signal (tx = high, rx = low)
91 
92  // modem
93  CtlSignalModemRTS = 1000, ///< request to send
94  CtlSignalModemCTS = 1001, ///< clear to send
95 
96  // other
97  CtlSignalCustom = 1010, ///< custom interface (requires callbacks)
98  CtlSignalNone = 1011 ///< not a signal, but a value to clear signal
99  };
100 
101  /*!
102  * \brief Default constructor.
103  */
104  DynaCommSerial();
105 
106  /*!
107  * \brief Initialization constructor.
108  *
109  * The given serial device is opened at the baud rate.
110  *
111  * \param sSerialDevName Serial device name.
112  * \param nBaudRate Serial device baud rate.
113  */
114  DynaCommSerial(const char *sSerialDevName, int nBaudRate);
115 
116  /*!
117  * \brief Destructor.
118  */
119  virtual ~DynaCommSerial();
120 
121  /*!
122  * Get the Dynamixel Bus serial device name.
123  *
124  * \return Returns name.
125  */
126  const char *GetSerialDeviceName() const
127  {
128  return m_sSerialDevName;
129  }
130 
131  /*!
132  * \brief Get system-unique resource identifier.
133  *
134  * \return Resource id.
135  */
136  virtual int GetResourceId() const
137  {
138  return m_fd;
139  }
140 
141  /*!
142  * \brief Open serial communication to dynamixel bus.
143  *
144  * The given serial device is opened at the given baud rate.
145  *
146  * \param sSerialDevName Serial device name.
147  * \param nBaudRate Serial device baud rate.
148  *
149  * \copydoc doc_std_return
150  */
151  virtual int Open(const char *sSerialDevName, int nBaudRate);
152 
153  /*!
154  * \brief (Re)Open serial communication to dynamixel bus.
155  *
156  * \copydoc doc_std_return
157  */
158  virtual int Open();
159 
160  /*!
161  * \brief Close serial communication to dynamixel bus.
162  *
163  * \copydoc doc_std_return
164  */
165  virtual int Close();
166 
167  /*!
168  * \brief Set the Dynamixel Bus new baud rate.
169  *
170  * \param nNewBaudRate New baud rate.
171  *
172  * \copydoc doc_std_return
173  */
174  virtual int SetBaudRate(int nNewBaudRate);
175 
176  /*!
177  * \breif Set Dynamixel Bus half-duplex software control.
178  *
179  * The Dynamixel 3-wire bus is half-duplex. Hardware may automatically
180  * control toggling between transmit and receive (e.g. RoadNarrows
181  * DynaUSB dongle). If there is no hardware support, then software must
182  * provide the tx/rx toggle functions.
183  *
184  * \param nSignal Signal assign to toggle.
185  * \param fnEnableTx Enable transmit function.
186  * \param fnEnableRx Enable receive function.
187  *
188  * \copydoc doc_std_return
189  */
190  virtual int SetHalfDuplexCtl(int nSignal,
191  HalfDuplexTxFunc_T fnEnableTx = NULL,
192  HalfDuplexRxFunc_T fnEnableRx = NULL);
193 
194  /*!
195  * \brief Read an 8-bit value from Dynamixel servo control table.
196  *
197  * \param nServoId Servo id.
198  * \param uAddr Servo control table address.
199  * \param [out] pVal Value read.
200  *
201  * \copydoc doc_std_return
202  */
203  virtual int Read8(int nServoId, uint_t uAddr, byte_t *pVal);
204 
205  /*!
206  * \brief Write an 8-bit value to Dynamixel servo control table.
207  *
208  * \param nServoId Servo id.
209  * \param uAddr Servo control table address.
210  * \param byVal Value written.
211  *
212  * \copydoc doc_std_return
213  */
214  virtual int Write8(int nServoId, uint_t uAddr, byte_t byVal);
215 
216  /*!
217  * \brief Read a 16-bit value from Dynamixel servo control table.
218  *
219  * \param nServoId Servo id.
220  * \param uAddr Servo control table address.
221  * \param [out] pVal Value read.
222  *
223  * \copydoc doc_std_return
224  */
225  virtual int Read16(int nServoId, uint_t uAddr, ushort_t *pVal);
226 
227  /*!
228  * \brief Write a 16-bit value to Dynamixel servo control table.
229  *
230  * \param nServoId Servo id.
231  * \param uAddr Servo control table address.
232  * \param uhVal Value written.
233  *
234  * \copydoc doc_std_return
235  */
236  virtual int Write16(int nServoId, uint_t uAddr, ushort_t uhVal);
237 
238  /*!
239  * \brief Synchronous Write 8/16-bit values to a list of Dynamixel servos.
240  *
241  * \param uAddr Servo control table write address.
242  * \param uValSize Value storage size at addtess. 1 or 2 bytes.
243  * \param tuples Array of servo id, write value 2-tuples.
244  * \param uCount Number of tuples.
245  *
246  * \copydoc doc_std_return
247  */
248  virtual int SyncWrite(uint_t uAddr,
249  uint_t uValSize,
250  DynaSyncWriteTuple_T tuples[],
251  uint_t uCount);
252 
253  /*!
254  * \brief Ping the servo.
255  *
256  * \param nServoId Servo id.
257  *
258  * \return Returns true if the servo responded, else false.
259  */
260  virtual bool Ping(int nServoId);
261 
262  /*!
263  * \brief Reset a servo back to default values.
264  *
265  * \warning All configuration is lost.
266  *
267  * \param nServoId Servo id.
268  *
269  * \copydoc doc_std_return
270  */
271  virtual int Reset(int nServoId);
272 
273 protected:
274  char *m_sSerialDevName; ///< serial device name
275  int m_fd; ///< serial file descriptor
276  int m_nGpioNum; ///< gpio number
277  int m_fdGpio; ///< gpio file descriptor
278  int m_nGpioVal; ///< gpio shadow'ed value
279  libdxl::dxl m_dxl; ///< dxl low-level interface
280 
281  /*!
282  * \brief Make serial index from serial device name.
283  *
284  * \par Examples:
285  * /dev/ttyUSB0 \h_leftdblarrow 0\n
286  * COM3 \h_leftdblarrow 3
287  *
288  * \param sSeriaDevName Serial device name.
289  *
290  * \return Returns serial index on success.\n
291  * \copydoc doc_std_ecode
292  */
293  int MakeSerialIndex(const char *sSerialDevName);
294 
295  /*!
296  * \brief Intialize RTS signalling.
297  */
298  void InitRTS();
299 
300  /*!
301  * \brief Enable transmit via RTS signal.
302  *
303  * \param pArg Pointer to this.
304  */
305  static void EnableTxRTS(void *pArg);
306 
307  /*!
308  * \brief Enable receive via RTS signal.
309  *
310  * \param pArg Pointer to this.
311  * \param uNumTxBytes Number of transmit bytes (being) transmitted.
312  */
313  static void EnableRxRTS(void *pArg, size_t uNumTxBytes);
314 
315  /*!
316  * \brief Intialize CTS signalling.
317  */
318  void InitCTS();
319 
320  /*!
321  * \brief Enable transmit via CTS signal.
322  *
323  * \param pArg Pointer to this.
324  */
325  static void EnableTxCTS(void *pArg);
326 
327  /*!
328  * \brief Enable receive via CTS signal.
329  *
330  * \param pArg Pointer to this.
331  * \param uNumTxBytes Number of transmit bytes (being) transmitted.
332  */
333  static void EnableRxCTS(void *pArg, size_t uNumTxBytes);
334 
335  /*!
336  * \brief Intialize GPIO signalling.
337  *
338  * \param nGpioNum GPIO number.
339  */
340  void InitGPIO(int nGpioNum);
341 
342  /*!
343  * \brief Enable transmit via GPIO signal.
344  *
345  * \param pArg Pointer to this.
346  */
347  static void EnableTxGPIO(void *pArg);
348 
349  /*!
350  * \brief Enable receive via GPIO signal.
351  *
352  * \param pArg Pointer to this.
353  * \param uNumTxBytes Number of transmit bytes (being) transmitted.
354  */
355  static void EnableRxGPIO(void *pArg, size_t uNumTxBytes);
356 };
357 
358 
359 #endif // _DYNA_COMM_SERIAL_H
RoadNarrows Dynamixel Bus Communications Abstract Base Class Interface.
Modified dynamixel SDK interface.
virtual int Write16(int nServoId, uint_t uAddr, ushort_t uhVal)
Write a 16-bit value to Dynamixel servo control table.
virtual int Read16(int nServoId, uint_t uAddr, ushort_t *pVal)
Read a 16-bit value from Dynamixel servo control table.
Dynamixel Serial Bus Communications Class.
void InitCTS()
Intialize CTS signalling.
virtual int Write8(int nServoId, uint_t uAddr, byte_t byVal)
Write an 8-bit value to Dynamixel servo control table.
virtual int SetHalfDuplexCtl(int nSignal, HalfDuplexTxFunc_T fnEnableTx=NULL, HalfDuplexRxFunc_T fnEnableRx=NULL)
gpio signal (tx = high, rx = low)
virtual int Read8(int nServoId, uint_t uAddr, byte_t *pVal)
Read an 8-bit value from Dynamixel servo control table.
not a signal, but a value to clear signal
virtual int SetBaudRate(int nNewBaudRate)
Set the Dynamixel Bus new baud rate.
static void EnableRxRTS(void *pArg, size_t uNumTxBytes)
Enable receive via RTS signal.
int m_nGpioVal
gpio shadow&#39;ed value
char * m_sSerialDevName
serial device name
virtual int Reset(int nServoId)
Reset a servo back to default values.
int MakeSerialIndex(const char *sSerialDevName)
Make serial index from serial device name.
gpio signal is inverted (tx = low, rx = high)
static void EnableRxGPIO(void *pArg, size_t uNumTxBytes)
Enable receive via GPIO signal.
static void EnableRxCTS(void *pArg, size_t uNumTxBytes)
Enable receive via CTS signal.
libdxl::dxl m_dxl
dxl low-level interface
virtual int Open()
(Re)Open serial communication to dynamixel bus.
const char * GetSerialDeviceName() const
void(* HalfDuplexRxFunc_T)(void *pArg, size_t uNumTxBytes)
Half-duplex control receive function type.
Definition: DynaComm.h:87
void(* HalfDuplexTxFunc_T)(void *pArg)
Half-duplex control transmit function type.
Definition: DynaComm.h:84
static const int RetryMax
maximum number of I/O retries
void InitRTS()
Intialize RTS signalling.
void InitGPIO(int nGpioNum)
Intialize GPIO signalling.
virtual ~DynaCommSerial()
Destructor.
virtual int Close()
Close serial communication to dynamixel bus.
virtual int GetResourceId() const
Get system-unique resource identifier.
custom interface (requires callbacks)
RoadNarrows Dynamixel Top-Level Package Header File.
int m_fd
serial file descriptor
virtual bool Ping(int nServoId)
Ping the servo.
virtual int SyncWrite(uint_t uAddr, uint_t uValSize, DynaSyncWriteTuple_T tuples[], uint_t uCount)
Synchronous Write 8/16-bit values to a list of Dynamixel servos.
DynaCommSerial()
Default constructor.
int m_fdGpio
gpio file descriptor
static void EnableTxGPIO(void *pArg)
Enable transmit via GPIO signal.
static void EnableTxRTS(void *pArg)
Enable transmit via RTS signal.
CtlSignal
Half-duplex control signals.
static const int RetryWait
microsecond wait between retries
RoadNarrows Dynamixel Library Error and Logging Routines.
static void EnableTxCTS(void *pArg)
Enable transmit via CTS signal.
int m_nGpioNum
gpio number
Dynamixel Bus Communications Abstract Base Class.
Definition: DynaComm.h:80