Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaComm.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: librnr_dynamixel
6 //
7 // File: DynaComm.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-03-04 12:38:36 -0700 (Wed, 04 Mar 2015) $
12  * $Rev: 3873 $
13  *
14  * \ingroup dyna_lib_hdrs
15  *
16  * \brief RoadNarrows Dynamixel Bus Communications Abstract Base 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_H
53 #define _DYNA_COMM_H
54 
55 #include <pthread.h>
56 
57 #include <string>
58 
59 #include "rnr/rnrconfig.h"
60 #include "rnr/shm.h"
61 #include "rnr/log.h"
62 
63 #include "Dynamixel/Dynamixel.h"
64 #include "Dynamixel/DynaTypes.h"
65 #include "Dynamixel/DynaError.h"
66 
67 
68 // ---------------------------------------------------------------------------
69 // Dynamixel Bus Communications Abstract Base Class
70 // ---------------------------------------------------------------------------
71 
72 /*!
73  * \ingroup dyna_lib_classes
74  *
75  * \brief Dynamixel Bus Communications Abstract Base Class
76  *
77  * The DynaComm abstract class provides the I/O template to communicate
78  * with Dynamixel servos on a Dynamixel bus.
79  */
80 class DynaComm
81 {
82 public:
83  /*! \brief Half-duplex control transmit function type. */
84  typedef void (*HalfDuplexTxFunc_T)(void *pArg);
85 
86  /*! \brief Half-duplex control receive function type. */
87  typedef void (*HalfDuplexRxFunc_T)(void *pArg, size_t uNumTxBytes);
88 
89  /*!
90  * \breif Default constructor.
91  */
92  DynaComm();
93 
94  /*!
95  * \breif Initialization constructor.
96  *
97  * \param sUri Dynamixel Bus device Uniform Resource Identifier string.
98  * \param nBaudRate Device baud rate.
99  */
100  DynaComm(const char *sUri, int nBaudRate);
101 
102  /*!
103  * \brief Destructor.
104  */
105  virtual ~DynaComm();
106 
107  /*!
108  * \brief Archetype constructor to create a new Dynamixel bus communication
109  * derived instance.
110  *
111  * The specific DynaComm object created depends on the URI specified.
112  * Currently two derived objects are supported:
113  * \termblock
114  * \term DynaCommSerial \termdata Direct connected serial device. \endterm
115  * \term DynaCommBotSense \termdata BotSense proxied serial device. \endterm
116  * \endtermblock
117  *
118  * \par Supported URIs:
119  * <tt>/devicepath</tt>\n
120  * <tt>botsense://[hostname][:port]/devicepath</tt>\n
121  *
122  * \param sUri Uniform Resource Identifier string.
123  * \param nBaudRate Dynamixel bus supported baud rate.
124  *
125  * \return On succes, returns pointer to the allocated DynaComm derived
126  * object.\n
127  * On failure, NULL is returned.
128  */
129  static DynaComm *New(const char *sUri, int nBaudRate);
130 
131  /*!
132  * Get the Dynamixel Bus Uniform Resource Identifier.
133  *
134  * \return Returns URI.
135  */
136  const char *GetDeviceUri() const
137  {
138  return m_sDevUri;
139  }
140 
141  /*!
142  * Get the current baud rate.
143  *
144  * \return Return buad rate.
145  */
146  const int GetBaudRate() const
147  {
148  return m_nBaudRate;
149  }
150 
151  /*!
152  * \brief Get system-unique resource identifier.
153  *
154  * \return Resource id.
155  */
156  virtual int GetResourceId() const = 0;
157 
158  /*!
159  * \brief Open communication to dynamixel bus.
160  *
161  * The given serial device is opened at the given baud rate.
162  *
163  * \param sDevUri Device Uniform Resource Identifier.
164  * \param nBaudRate Device baud rate.
165  *
166  * \copydoc doc_std_return
167  */
168  virtual int Open(const char *sDevUri, int nBaudRate) = 0;
169 
170  /*!
171  * \brief (Re)Open communication to dynamixel bus.
172  *
173  * \copydoc doc_std_return
174  */
175  virtual int Open() = 0;
176 
177  /*!
178  * \brief Close communication to dynamixel bus.
179  *
180  * \copydoc doc_std_return
181  */
182  virtual int Close() = 0;
183 
184  /*!
185  * \brief Set the Dynamixel Bus new baud rate.
186  *
187  * \param nNewBaudRate New baud rate.
188  *
189  * \copydoc doc_std_return
190  */
191  virtual int SetBaudRate(int nNewBaudRate) = 0;
192 
193  /*!
194  * \breif Set Dynamixel Bus half-duplex software control.
195  *
196  * The Dynamixel 3-wire bus is half-duplex. Hardware may automatically
197  * control toggling between transmit and receive (e.g. RoadNarrows
198  * DynaUSB dongle). If there is no hardware support, then software must
199  * provide the tx/rx toggle functions.
200  *
201  * \param nSignal Signal assign to toggle.
202  * \param fnEnableTx Enable transmit function.
203  * \param fnEnableRx Enable receive function.
204  *
205  * \copydoc doc_std_return
206  */
207  virtual int SetHalfDuplexCtl(int nSignal,
208  HalfDuplexTxFunc_T fnEnableTx = NULL,
209  HalfDuplexRxFunc_T fnEnableRx = NULL) = 0;
210 
211  /*!
212  * \brief Read an 8-bit value from Dynamixel servo control table.
213  *
214  * \param nServoId Servo id.
215  * \param uAddr Servo control table address.
216  * \param [out] pVal Value read.
217  *
218  * \copydoc doc_std_return
219  */
220  virtual int Read8(int nServoId, uint_t uAddr, byte_t *pVal) = 0;
221 
222  /*!
223  * \brief Read an 8-bit value from Dynamixel servo control table.
224  *
225  * \param nServoId Servo id.
226  * \param uAddr Servo control table address.
227  * \param [out] pVal Value read.
228  *
229  * \copydoc doc_std_return
230  */
231  virtual int Read8(int nServoId, uint_t uAddr, uint_t *pVal)
232  {
233  byte_t val;
234  int rc;
235 
236  if( (rc = Read8(nServoId, uAddr, &val)) == DYNA_OK )
237  {
238  *pVal = (uint_t)val;
239  }
240  return rc;
241  }
242 
243  /*!
244  * \brief Write an 8-bit value to Dynamixel servo control table.
245  *
246  * \param nServoId Servo id.
247  * \param uAddr Servo control table address.
248  * \param byVal Value written.
249  *
250  * \copydoc doc_std_return
251  */
252  virtual int Write8(int nServoId, uint_t uAddr, byte_t byVal) = 0;
253 
254  /*!
255  * \brief Write an 8-bit value to Dynamixel servo control table.
256  *
257  * \param nServoId Servo id.
258  * \param uAddr Servo control table address.
259  * \param uVal Value written.
260  *
261  * \copydoc doc_std_return
262  */
263  virtual int Write8(int nServoId, uint_t uAddr, uint_t uVal)
264  {
265  byte_t val = (byte_t)uVal;
266 
267  return Write8(nServoId, uAddr, val);
268  }
269 
270  /*!
271  * \brief Read a 16-bit value from Dynamixel servo control table.
272  *
273  * \param nServoId Servo id.
274  * \param uAddr Servo control table address.
275  * \param [out] pVal Value read.
276  *
277  * \copydoc doc_std_return
278  */
279  virtual int Read16(int nServoId, uint_t uAddr, ushort_t *pVal) = 0;
280 
281  /*!
282  * \brief Read a 16-bit value from Dynamixel servo control table.
283  *
284  * \param nServoId Servo id.
285  * \param uAddr Servo control table address.
286  * \param [out] pVal Value read.
287  *
288  * \copydoc doc_std_return
289  */
290  virtual int Read16(int nServoId, uint_t uAddr, uint_t *pVal)
291  {
292  ushort_t val;
293  int rc;
294 
295  if( (rc = Read16(nServoId, uAddr, &val)) == DYNA_OK )
296  {
297  *pVal = (uint_t)val;
298  }
299  return rc;
300  }
301 
302  /*!
303  * \brief Write a 16-bit value to Dynamixel servo control table.
304  *
305  * \param nServoId Servo id.
306  * \param uAddr Servo control table address.
307  * \param uhVal Value written.
308  *
309  * \copydoc doc_std_return
310  */
311  virtual int Write16(int nServoId, uint_t uAddr, ushort_t uhVal) = 0;
312 
313  /*!
314  * \brief Write a 16-bit value to Dynamixel servo control table.
315  *
316  * \param nServoId Servo id.
317  * \param uAddr Servo control table address.
318  * \param uVal Value written.
319  *
320  * \copydoc doc_std_return
321  */
322  virtual int Write16(int nServoId, uint_t uAddr, uint_t uVal)
323  {
324  ushort_t val = (ushort_t)uVal;
325 
326  return Write16(nServoId, uAddr, val);
327  }
328 
329  /*!
330  * \brief Synchronous write 8/16-bit values to a list of Dynamixel servos.
331  *
332  * \param uAddr Servo control table write address.
333  * \param uValSize Value storage size of field at addtess. 1 or 2 bytes.
334  * \param uCount Number of tuples.
335  * \param ... A variable argument list of uCount 2-tuples of type
336  * (int,uint_t) specifying the servo id and the packed raw
337  * field value.
338  *
339  * \copydoc doc_std_return
340  */
341  virtual int vSyncWrite(uint_t uAddr, uint_t uValSize, uint_t uCount, ...);
342 
343  /*!
344  * \brief Synchronous Write 8/16-bit values to a list of Dynamixel servos.
345  *
346  * \param uAddr Servo control table write address.
347  * \param uValSize Value storage size at addtess. 1 or 2 bytes.
348  * \param tuples Array of servo id, write value 2-tuples.
349  * \param uCount Number of tuples.
350  *
351  * \copydoc doc_std_return
352  */
353  virtual int SyncWrite(uint_t uAddr,
354  uint_t uValSize,
355  DynaSyncWriteTuple_T tuples[],
356  uint_t uCount) = 0;
357 
358  /*!
359  * \brief Ping the servo.
360  *
361  * \param nServoId Servo id.
362  *
363  * \return Returns true if the servo responded, else false.
364  */
365  virtual bool Ping(int nServoId) = 0;
366 
367  /*!
368  * \brief Reset a servo back to default values.
369  *
370  * \warning All configuration is lost.
371  *
372  * \param nServoId Servo id.
373  *
374  * \copydoc doc_std_return
375  */
376  virtual int Reset(int nServoId) = 0;
377 
378  /*!
379  * \brief Test if Dynamixel Bus is open.
380  *
381  * \return Returns true or false.
382  */
383  virtual bool IsOpen()
384  {
385  return m_bIsOpen;
386  }
387 
388  /*!
389  * \brief Get current alarms.
390  *
391  * \return Returns alarm bits.
392  */
393  virtual uint_t GetAlarms()
394  {
395  return m_uAlarms;
396  }
397 
398  /*!
399  * \brief Clear current alarms.
400  */
401  virtual void ClearAlarms()
402  {
404  }
405 
406  /*!
407  * \brief Get a formatted servo alarms string associated with the alarms.
408  *
409  * \param uAlarms Dynamixel servo alarm bits.
410  * \param strSep Separator string between alarm substrings.
411  *
412  * \return Returns string.
413  */
414  static std::string GetAlarmsString(const uint_t uAlarms,
415  const std::string &strSep="; ");
416 
417  /*!
418  * \brief Get a formatted servo alarms short string associated with the
419  * alarms.
420  *
421  * \param uAlarms Dynamixel servo alarm bits.
422  * \param strSep Separator string between alarm substrings.
423  *
424  * \return Returns string.
425  */
426  static std::string GetAlarmsShortString(const uint_t uAlarms,
427  const std::string &strSep=",");
428 
429  /*!
430  * \brief Get the Dynamixel Bus status.
431  *
432  * \return Status.
433  */
434  virtual uint_t GetBusStatus()
435  {
436  return m_uBusStatus;
437  }
438 
439  /*!
440  * \brief Get the string describing the Dynamixel servo communication status.
441  *
442  * \param uBusStatus Dynamixel bus communication status.
443  *
444  * \return Returns the appropriate status string.
445  */
446  static const char *GetBusStatusString(uint_t uBusStatus);
447 
448  /*!
449  * \brief Map baud rate to Dynamixel baud number.
450  *
451  * \param nBaudRate Baud rate.
452  *
453  * \return Returns baud number on success.\n
454  * \copydoc doc_std_ecode
455  */
456  static int BaudRateToNum(int nBaudRate);
457 
458  /*!
459  * \brief Map baud number to Dynamixel baud rate.
460  *
461  * \param nBaudNum Baud number.
462  *
463  * \return Returns baud rate on success.\n
464  * \copydoc doc_std_ecode
465  */
466  static int BaudNumToRate(int nBaudRate);
467 
468  /*!
469  * \brief Get the baud rate associated with the given index.
470  *
471  * This function can be used to iterate of all supported baud rates.
472  *
473  * \param nIndex Zero based index.
474  *
475  * \return
476  * If the index is in range, returns the baud rate at the index.\n
477  * Otherwise 0 is returned.
478  *
479  */
480  static int BaudRateAt(int nIndex);
481 
482  /*!
483  * \brief Get the baud number associated with the given index.
484  *
485  * This function can be used to iterate of all supported baud rates.
486  *
487  * \param nIndex Zero based index.
488  *
489  * \return
490  * If the index is in range, returns the baud number at the index.\n
491  * Otherwise 0 is returned.
492  *
493  */
494  static int BaudNumAt(int nIndex);
495 
496 protected:
497  static const key_t ShmKey; ///< shared memory key
498 
499  char *m_sDevUri; ///< dynamixel bus device URI
500  int m_nBaudRate; ///< baud rate
501  bool m_bIsOpen; ///< dynamixel bus communication is [not] open
502  uint_t m_uBusStatus; ///< bus comminication status
503  uint_t m_uAlarms; ///< servo alarms from last I/O operation
504  shm_mutex_t m_mutexComm; ///< synchonization mutex
505 };
506 
507 
508 #endif // _DYNA_COMM_H
uint_t m_uAlarms
servo alarms from last I/O operation
Definition: DynaComm.h:503
static int BaudNumToRate(int nBaudRate)
Map baud number to Dynamixel baud rate.
Definition: DynaComm.cxx:382
char * m_sDevUri
dynamixel bus device URI
Definition: DynaComm.h:499
#define DYNA_OK
not an error, success
Definition: Dynamixel.h:78
int m_nBaudRate
baud rate
Definition: DynaComm.h:500
virtual int vSyncWrite(uint_t uAddr, uint_t uValSize, uint_t uCount,...)
Synchronous write 8/16-bit values to a list of Dynamixel servos.
Definition: DynaComm.cxx:420
static int BaudNumAt(int nIndex)
Get the baud number associated with the given index.
Definition: DynaComm.cxx:408
virtual ~DynaComm()
Destructor.
Definition: DynaComm.cxx:169
static int BaudRateAt(int nIndex)
Get the baud rate associated with the given index.
Definition: DynaComm.cxx:396
virtual int Reset(int nServoId)=0
Reset a servo back to default values.
virtual bool IsOpen()
Test if Dynamixel Bus is open.
Definition: DynaComm.h:383
const char * GetDeviceUri() const
Definition: DynaComm.h:136
#define DYNA_ALARM_NONE
no alarms
Definition: Dynamixel.h:646
static const key_t ShmKey
shared memory key
Definition: DynaComm.h:497
static const char * GetBusStatusString(uint_t uBusStatus)
Get the string describing the Dynamixel servo communication status.
Definition: DynaComm.cxx:298
virtual int Read8(int nServoId, uint_t uAddr, byte_t *pVal)=0
Read an 8-bit value from Dynamixel servo control table.
virtual int SetBaudRate(int nNewBaudRate)=0
Set the Dynamixel Bus new baud rate.
virtual int Read16(int nServoId, uint_t uAddr, ushort_t *pVal)=0
Read a 16-bit value from Dynamixel servo control table.
shm_mutex_t m_mutexComm
synchonization mutex
Definition: DynaComm.h:504
virtual bool Ping(int nServoId)=0
Ping the servo.
bool m_bIsOpen
dynamixel bus communication is [not] open
Definition: DynaComm.h:501
static std::string GetAlarmsShortString(const uint_t uAlarms, const std::string &strSep=",")
Get a formatted servo alarms short string associated with the alarms.
Definition: DynaComm.cxx:340
virtual int Write8(int nServoId, uint_t uAddr, uint_t uVal)
Write an 8-bit value to Dynamixel servo control table.
Definition: DynaComm.h:263
virtual int Write8(int nServoId, uint_t uAddr, byte_t byVal)=0
Write an 8-bit value to Dynamixel servo control table.
virtual int Read16(int nServoId, uint_t uAddr, uint_t *pVal)
Read a 16-bit value from Dynamixel servo control table.
Definition: DynaComm.h:290
static DynaComm * New(const char *sUri, int nBaudRate)
Archetype constructor to create a new Dynamixel bus communication derived instance.
Definition: DynaComm.cxx:179
virtual uint_t GetAlarms()
Get current alarms.
Definition: DynaComm.h:393
virtual int Write16(int nServoId, uint_t uAddr, ushort_t uhVal)=0
Write a 16-bit value to Dynamixel servo control table.
virtual int GetResourceId() const =0
Get system-unique resource identifier.
virtual int SetHalfDuplexCtl(int nSignal, HalfDuplexTxFunc_T fnEnableTx=NULL, HalfDuplexRxFunc_T fnEnableRx=NULL)=0
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
uint_t m_uBusStatus
bus comminication status
Definition: DynaComm.h:502
virtual void ClearAlarms()
Clear current alarms.
Definition: DynaComm.h:401
virtual int Open()=0
(Re)Open communication to dynamixel bus.
RoadNarrows Dynamixel Fundatmental Types.
static std::string GetAlarmsString(const uint_t uAlarms, const std::string &strSep="; ")
Get a formatted servo alarms string associated with the alarms.
Definition: DynaComm.cxx:312
virtual int SyncWrite(uint_t uAddr, uint_t uValSize, DynaSyncWriteTuple_T tuples[], uint_t uCount)=0
Synchronous Write 8/16-bit values to a list of Dynamixel servos.
RoadNarrows Dynamixel Top-Level Package Header File.
const int GetBaudRate() const
Definition: DynaComm.h:146
virtual int Close()=0
Close communication to dynamixel bus.
virtual int Read8(int nServoId, uint_t uAddr, uint_t *pVal)
Read an 8-bit value from Dynamixel servo control table.
Definition: DynaComm.h:231
virtual int Write16(int nServoId, uint_t uAddr, uint_t uVal)
Write a 16-bit value to Dynamixel servo control table.
Definition: DynaComm.h:322
virtual uint_t GetBusStatus()
Get the Dynamixel Bus status.
Definition: DynaComm.h:434
RoadNarrows Dynamixel Library Error and Logging Routines.
static int BaudRateToNum(int nBaudRate)
Map baud rate to Dynamixel baud number.
Definition: DynaComm.cxx:368
Dynamixel Bus Communications Abstract Base Class.
Definition: DynaComm.h:80