Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaError.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: libDynamixel
6 //
7 // File: DynaError.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-03-13 13:28:02 -0600 (Fri, 13 Mar 2015) $
12  * $Rev: 3890 $
13  *
14  * \brief Error and logging handling routines.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2011-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  *
26  * Unless otherwise stated explicitly, all materials contained are copyrighted
27  * and may not be used without RoadNarrows LLC's written consent,
28  * except as provided in these terms and conditions or in the copyright
29  * notice (documents and software) or other proprietary notice provided with
30  * the relevant materials.
31  *
32  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
33  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
34  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
35  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
36  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
40  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
42  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
43  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
44  *
45  * @EulaEnd@
46  */
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <libgen.h>
52 #include <string.h>
53 
54 #include <string>
55 
56 #include "rnr/rnrconfig.h"
57 #include "rnr/log.h"
58 #include "rnr/new.h"
59 #include "rnr/units.h"
60 
61 #include "Dynamixel/dxl/dxl.h"
62 
63 #include "Dynamixel/Dynamixel.h"
64 #include "Dynamixel/DynaError.h"
65 #include "Dynamixel/DynaComm.h"
66 
67 #include "DynaLibInternal.h"
68 
69 using namespace std;
70 
71 
72 // ---------------------------------------------------------------------------
73 // Private Interface
74 // ---------------------------------------------------------------------------
75 
76 /*!
77  * \brief Package Dynamixel Error Code String Table.
78  *
79  * Table is indexed by error codes (see \ref dyna_ecodes). Keep
80  * in sync.
81  */
82 static const char *DynaEcodeStrTbl[] =
83 {
84  "Ok", ///< [DYNA_OK]
85 
86  "Error", ///< [DYNA_ECODE_GEN]
87  "System error", ///< [DYNA_ECODE_SYS]
88  "Internal error", ///< [DYNA_ECODE_INTERNAL]
89  "Invalid error code", ///< [DYNA_ECODE_BADEC]
90  "Invalid units", ///< [DYNA_ECODE_BAD_UNITS]
91  "Invalid value", ///< [DYNA_ECODE_BAD_VAL]
92  "No servo", ///< [DYNA_ECODE_NO_SERVO]
93  "Serial device error", ///< [DYNA_ECODE_BAD_DEV]
94  "Not open", ///< [DYNA_ECODE_BADF]
95  "Communication error", ///< [DYNA_ECODE_ECOMM]
96  "Transmit packet failure", ///< [DYNA_ECODE_TX_FAIL]
97  "Receive packet failure", ///< [DYNA_ECODE_RX_FAIL]
98  "Transmit packet error", ///< [DYNA_ECODE_TX_ERROR]
99  "Receive packet time out", ///< [DYNA_ECODE_RX_TIMEOUT]
100  "Received corrupted packet", ///< [DYNA_ECODE_RX_BAD_PKT]
101  "Servo errored condition", ///< [DYNA_ECODE_ESERVO]
102  "Resource not available", ///< [DYNA_ECODE_RSRC]
103  "feature/function no supported", ///< [DYNA_ECODE_NOT_SUPP]
104  "Linked servos error", ///< [DYNA_ECODE_LINKED]
105  "Operation not permitted on slave servo", ///< [DYNA_ECODE_SLAVE]
106  "BotSense proxy error", ///< [DYNA_ECODE_BOTSENSE]
107  "Shell parse error", ///< [DYNA_ECODE_PARSE]
108  "Shell run-time error", ///< [DYNA_ECODE_RUNTIME]
109  "Cannot execute" ///< [DYNA_ECODE_NOEXEC]
110 };
111 
112 
113 // ---------------------------------------------------------------------------
114 // Internal Interface
115 // ---------------------------------------------------------------------------
116 
117 int DynaMapDxlToEcode(int nDxlError)
118 {
119  switch( nDxlError )
120  {
121  case DXL_COMM_TXFAIL:
122  return DYNA_ECODE_TX_FAIL;
123  case DXL_COMM_RXFAIL:
124  return DYNA_ECODE_RX_FAIL;
125  case DXL_COMM_TXERROR:
126  return DYNA_ECODE_TX_ERROR;
127  case DXL_COMM_RXTIMEOUT:
128  return DYNA_ECODE_RX_TIMEOUT;
129  case DXL_COMM_RXCORRUPT:
130  return DYNA_ECODE_RX_BAD_PKT;
131  default:
132  return DYNA_ECODE_ECOMM;
133  }
134 }
135 
136 
137 // ---------------------------------------------------------------------------
138 // Public Interface
139 // ---------------------------------------------------------------------------
140 
141 const char *DynaStrError(int ecode)
142 {
143  if( ecode < 0 )
144  {
145  ecode = -ecode;
146  }
147 
148  if( ecode >= arraysize(DynaEcodeStrTbl) )
149  {
150  ecode = DYNA_ECODE_BADEC;
151  }
152  return DynaEcodeStrTbl[ecode];
153 }
154 
155 void DynaPrintBuf(FILE *fp,
156  const char *sPreface,
157  byte_t buf[],
158  const char *sFmt,
159  size_t uCount,
160  size_t uNLFreq,
161  uint_t uCol)
162 {
163  size_t i;
164 
165  if( sPreface && *sPreface )
166  {
167  fprintf(fp, "%s", sPreface);
168  }
169 
170  for(i=0; i<uCount; ++i)
171  {
172  if( (uNLFreq > 0) && ((i % uNLFreq) == 0) && (i != 0) )
173  {
174  fprintf(fp, "\n%*s", uCol, "");
175  }
176  fprintf(fp, sFmt, buf[i]);
177  }
178  fprintf(fp, "\n");
179 }
180 
181 #ifdef LOG
182 
183 void DynaLogServoAlarms(int nServoId, uint_t uAlarms)
184 {
185  FILE *fp;
186  string strAlarms;
187 
188  fp = LOG_GET_LOGFP();
189 
190  fprintf(fp, "%sDiag%d: ", LOG_PREFACE, LOG_GET_THRESHOLD()-1);
191  fprintf(fp, "Servo %d alarms: ", nServoId);
192 
193  if( uAlarms == DYNA_ALARM_NONE )
194  {
195  fprintf(fp, "no alarms;\n");
196  return;
197  }
198 
199  else
200  {
201  strAlarms = DynaComm::GetAlarmsString(uAlarms);
202  fprintf(fp, "%s\n", strAlarms.c_str());
203  }
204 }
205 
206 void DynaLogBuf(const char *sPreface,
207  byte_t buf[],
208  size_t uCount,
209  const char *sFmt)
210 {
211  FILE *fp; // log file pointer
212  size_t i; // working index
213 
214  fp = LOG_GET_LOGFP();
215 
216  fprintf(fp, "%sDiag%d: %s", LOG_PREFACE, LOG_GET_THRESHOLD()-1, sPreface);
217  for(i=0; i<uCount; ++i)
218  {
219  if( (i % 16) == 0 )
220  {
221  fprintf(fp, "\n");
222  }
223  fprintf(fp, sFmt, buf[i]);
224  }
225  fprintf(fp, "\n");
226 }
227 
228 #endif // LOG
RoadNarrows Dynamixel Bus Communications Abstract Base Class Interface.
int DynaMapDxlToEcode(int nDxlError)
Map DXL library error code to Dynamixel error code.
Definition: DynaError.cxx:117
Modified dynamixel SDK interface.
void DynaLogServoAlarms(int nServoId, uint_t uAlarms)
Log servo alarms.
Definition: DynaError.cxx:183
#define DXL_COMM_TXFAIL
transmit failure error
Definition: dxl.h:110
#define DYNA_ECODE_RX_TIMEOUT
dynamixel receive packet time out
Definition: Dynamixel.h:93
#define DYNA_ALARM_NONE
no alarms
Definition: Dynamixel.h:646
#define DXL_COMM_TXERROR
packed transmit packet format error
Definition: dxl.h:112
#define DYNA_ECODE_RX_BAD_PKT
dynamixel receive bad packet
Definition: Dynamixel.h:94
const char * DynaStrError(int ecode)
Get the error string describing the Dynamixel error code.
Definition: DynaError.cxx:141
#define DXL_COMM_RXTIMEOUT
receive timeout error
Definition: dxl.h:114
#define DYNA_ECODE_ECOMM
dynamixel communication error
Definition: Dynamixel.h:89
#define DYNA_ECODE_TX_ERROR
dynamixel transmit packet error
Definition: Dynamixel.h:92
The libDynamixel internal declarations.
void DynaLogBuf(const char *sPreface, byte_t buf[], size_t uCount, const char *sFmt)
Log integer data.
Definition: DynaError.cxx:206
static const char * DynaEcodeStrTbl[]
Package Dynamixel Error Code String Table.
Definition: DynaError.cxx:82
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
#define DXL_COMM_RXCORRUPT
receive corrupted packet
Definition: dxl.h:115
RoadNarrows Dynamixel Top-Level Package Header File.
#define DXL_COMM_RXFAIL
receive failure error
Definition: dxl.h:111
#define DYNA_ECODE_BADEC
bad error code
Definition: Dynamixel.h:83
void DynaPrintBuf(FILE *fp, const char *sPreface, byte_t buf[], const char *sFmt, size_t uCount, size_t uNLFreq, uint_t uCol)
Pretty print a byte buffer to opened file stream.
Definition: DynaError.cxx:155
#define DYNA_ECODE_TX_FAIL
dynamixel transmit packet failure
Definition: Dynamixel.h:90
#define DYNA_ECODE_RX_FAIL
dynamixel receive packet failure
Definition: Dynamixel.h:91
RoadNarrows Dynamixel Library Error and Logging Routines.