Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
libdxl::dxlhal Class Reference

Public Member Functions

 dxlhal ()
 Default constructor.
 
 ~dxlhal ()
 Destructor.
 
int open (const char *deviceName, int baudrate)
 Open Dynamixel Bus USB serial device by name. More...
 
int dxl_hal_open (int deviceIndex, float baudrate)
 Open Dynamixel Bus USB serial device by index. More...
 
void close ()
 Close Dynamixel Bus serial interface.
 
std::string getDeviceName ()
 Get the Dynamixel Bus serial device name. More...
 
int getBaudRate ()
 Get the Dynamixel Bus serial device baud rate. More...
 
int getFd ()
 Get the Dynamixel Bus serial device file descriptor. More...
 
int setBaudRate (int baudrate)
 Set opened Dynamixel Bus serial interface baud rate. More...
 
void clear ()
 Discard any pending recieve data.
 
int tx (unsigned char *pPacket, int numPacket)
 Transmit Dynamixel instruction packet. More...
 
int rx (unsigned char *pPacket, int numPacket)
 Receive Dynamixel status packet. More...
 
void setTimeout (int NumRcvByte)
 Set receive timeout. More...
 
bool hasTimedOut ()
 Test if receive timeout has expired. More...
 

Protected Member Functions

double calcSecPerByte (int baudrate)
 Calculate seconds/byte transmit/recieve time in seconds. More...
 
double now ()
 Mark time. More...
 

Protected Attributes

std::string m_strDeviceName
 device name
 
int m_nBaudRate
 device baud rate
 
int m_fd
 device file descriptor
 
double m_fSecPerByte
 seconds per byte transfer time
 
double m_fStartTime
 event start time
 
double m_fRcvWaitTime
 receive max elapse wait time
 

Detailed Description

Definition at line 39 of file dxlhal.h.

Member Function Documentation

double dxlhal::calcSecPerByte ( int  baudrate)
protected

Calculate seconds/byte transmit/recieve time in seconds.

Note
Over serial, START_BIT byte STOP_BIT = 10 bits.
Parameters
baudrateSerial baud rate.
Returns
Seconds per byte.

Definition at line 289 of file dxlhal.cxx.

290 {
291  if( baudrate > 0 )
292  {
293  return 10.0/(double)baudrate;
294  }
295  else
296  {
297  return 0.0;
298  }
299 }
int dxlhal::dxl_hal_open ( int  deviceIndex,
float  baudrate 
)

Open Dynamixel Bus USB serial device by index.

The serial device is opened and attributes set.

Note
This method is deprecated. The preferred method is open().
Parameters
deviceIndexDevice number of /dev/ttyUSBn, n == deviceIndex.
baudrateSerial baud rate.
Returns
Returns 1 on success, 0 on failure.

Definition at line 86 of file dxlhal.cxx.

Referenced by libdxl::dxl::dxl_initialize().

87 {
88  char buf[256];
89  struct termios newtio;
90  struct serial_struct serinfo;
91 
92  close();
93 
94  snprintf(buf, sizeof(buf), "/dev/ttyUSB%d", deviceIndex);
95 
96  buf[sizeof(buf)-1] = 0;
97 
98  m_strDeviceName = buf;
99  m_nBaudRate = baudrate;
100 
101  memset(&newtio, 0, sizeof(newtio));
102 
103  if( (m_fd = open(m_strDeviceName.c_str(), O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0 )
104  {
105  LOGSYSERROR("%s: Device open error.", m_strDeviceName.c_str());
106  goto DXL_HAL_OPEN_ERROR;
107  }
108 
109  newtio.c_cflag = B38400|CS8|CLOCAL|CREAD;
110  newtio.c_iflag = IGNPAR;
111  newtio.c_oflag = 0;
112  newtio.c_lflag = 0;
113  newtio.c_cc[VTIME] = 0; // time-out 값 (TIME * 0.1초) 0 : disable
114  newtio.c_cc[VMIN] = 0; // MIN 은 read 가 return 되기 위한 최소 문자 개수
115 
116  tcflush(m_fd, TCIFLUSH);
117  tcsetattr(m_fd, TCSANOW, &newtio);
118 
119  if(ioctl(m_fd, TIOCGSERIAL, &serinfo) < 0)
120  {
121  LOGSYSERROR("%s(fd=%d): Cannot get serial info.",
122  m_strDeviceName.c_str(), m_fd);
123  return 0;
124  }
125 
126  serinfo.flags &= (int)(~ASYNC_SPD_MASK);
127  serinfo.flags |= (int)ASYNC_SPD_CUST;
128  serinfo.custom_divisor = (int)((float)serinfo.baud_base / baudrate);
129 
130  if(ioctl(m_fd, TIOCSSERIAL, &serinfo) < 0)
131  {
132  LOGSYSERROR("%s(fd=%d): Cannot set serial info.",
133  m_strDeviceName.c_str(), m_fd);
134  goto DXL_HAL_OPEN_ERROR;
135  }
136 
137  m_fSecPerByte = calcSecPerByte(baudrate);
138 
139  return 1;
140 
141 DXL_HAL_OPEN_ERROR:
142  close();
143  return 0;
144 }
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
int m_nBaudRate
device baud rate
Definition: dxlhal.h:154
double calcSecPerByte(int baudrate)
Calculate seconds/byte transmit/recieve time in seconds.
Definition: dxlhal.cxx:289
int open(const char *deviceName, int baudrate)
Open Dynamixel Bus USB serial device by name.
Definition: dxlhal.cxx:60
std::string m_strDeviceName
device name
Definition: dxlhal.h:153
int dxlhal::getBaudRate ( )

Get the Dynamixel Bus serial device baud rate.

Returns
Returns baud rate.

Definition at line 163 of file dxlhal.cxx.

164 {
165  return m_nBaudRate;
166 }
int m_nBaudRate
device baud rate
Definition: dxlhal.h:154
string dxlhal::getDeviceName ( )

Get the Dynamixel Bus serial device name.

Returns
String.

Definition at line 158 of file dxlhal.cxx.

159 {
160  return m_strDeviceName;
161 }
std::string m_strDeviceName
device name
Definition: dxlhal.h:153
int dxlhal::getFd ( )

Get the Dynamixel Bus serial device file descriptor.

Returns
Returns >= 0 if the device is open, -1 otherwise.

Definition at line 168 of file dxlhal.cxx.

Referenced by libdxl::dxl::getFd().

169 {
170  return m_fd;
171 }
int m_fd
device file descriptor
Definition: dxlhal.h:155
bool dxlhal::hasTimedOut ( )

Test if receive timeout has expired.

Returns
Returns 1 if true, false otherwise.

Definition at line 260 of file dxlhal.cxx.

Referenced by libdxl::dxl::rxPacket().

261 {
262  double t1, t;
263 
264  t1 = now();
265  t = t1 - m_fStartTime;
266 
267  //fprintf(stderr, "DBG: st=%lf, now=%lf, elapse=%lf\n", m_fStartTime, t1, t);
268 
269  if( t > m_fRcvWaitTime )
270  {
271  return true;
272  }
273 
274  else
275  {
276  return false;
277  }
278 }
Definition: t.py:1
double m_fRcvWaitTime
receive max elapse wait time
Definition: dxlhal.h:158
double now()
Mark time.
Definition: dxlhal.cxx:280
double m_fStartTime
event start time
Definition: dxlhal.h:157
double dxlhal::now ( )
protected

Mark time.

Returns
Return marked time in seconds.

Definition at line 280 of file dxlhal.cxx.

281 {
282  struct timespec ts;
283 
284  clock_gettime(CLOCK_REALTIME, &ts);
285 
286  return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
287 }
int dxlhal::open ( const char *  deviceName,
int  baudrate 
)

Open Dynamixel Bus USB serial device by name.

The serial device is opened and attributes set.

Parameters
deviceNameName of serial device.
baudrateSerial baud rate.
Returns
Returns 1 on success, 0 on failure.

Definition at line 60 of file dxlhal.cxx.

Referenced by libdxl::dxl::open().

61 {
62  close();
63 
64  m_strDeviceName = deviceName;
65  m_nBaudRate = baudrate;
66 
67  m_fd = SerDevOpen(m_strDeviceName.c_str(), baudrate, 8, 'N', 1, false, false);
68 
69  if( m_fd >= 0 )
70  {
71  m_fSecPerByte = calcSecPerByte(baudrate);
72 
73  return 1;
74  }
75  else
76  {
77  LOGSYSERROR("%s: Failed to open.", m_strDeviceName.c_str());
78 
79  m_strDeviceName.clear();
80  m_nBaudRate = 0;
81 
82  return 0;
83  }
84 }
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
int m_nBaudRate
device baud rate
Definition: dxlhal.h:154
double calcSecPerByte(int baudrate)
Calculate seconds/byte transmit/recieve time in seconds.
Definition: dxlhal.cxx:289
std::string m_strDeviceName
device name
Definition: dxlhal.h:153
int dxlhal::rx ( unsigned char *  pPacket,
int  numPacket 
)

Receive Dynamixel status packet.

Parameters
[out]pPacketPacked packet.
numPacketNumber of bytes to read.
Returns
On success, number of bytes received. -1 on error.

Definition at line 229 of file dxlhal.cxx.

Referenced by libdxl::dxl::rxPacket().

230 {
231  static unsigned int Slop = 1000; // 1 millisecond slop
232 
233  unsigned int usec;
234  ssize_t n;
235 
236  memset(pPacket, 0, (size_t)numPacket);
237 
238  usec = (unsigned int)((double)numPacket * m_fSecPerByte * 1000000.0) + Slop;
239 
240  //fprintf(stderr, "DBG: rx wait time for %d bytes is %uus.\n",
241  // numPacket, usec);
242 
243  n = SerDevRead(m_fd, pPacket, (size_t)numPacket, usec);
244 
245  return n < 0? 0: (int)n;
246 }
int m_fd
device file descriptor
Definition: dxlhal.h:155
double m_fSecPerByte
seconds per byte transfer time
Definition: dxlhal.h:156
int dxlhal::setBaudRate ( int  baudrate)

Set opened Dynamixel Bus serial interface baud rate.

Parameters
baudrateBaud rate.
Returns
Returns 1 on success, 0 on failure.

Definition at line 173 of file dxlhal.cxx.

Referenced by libdxl::dxl::setBaudRate().

174 {
175  struct serial_struct serinfo;
176 
177  if( m_fd == -1 )
178  {
179  return 0;
180  }
181 
182  if(ioctl(m_fd, TIOCGSERIAL, &serinfo) < 0)
183  {
184  LOGERROR("%s(fd=%d): Cannot get serial info.",
185  m_strDeviceName.c_str(), m_fd);
186  return 0;
187  }
188 
189  serinfo.flags &= (int)(~ASYNC_SPD_MASK);
190  serinfo.flags |= (int)ASYNC_SPD_CUST;
191  serinfo.custom_divisor = (int)((float)serinfo.baud_base / (float)baudrate);
192 
193  if(ioctl(m_fd, TIOCSSERIAL, &serinfo) < 0)
194  {
195  LOGERROR("%s(fd=%d): Cannot set serial info.",
196  m_strDeviceName.c_str(), m_fd);
197  return 0;
198  }
199 
200  m_fSecPerByte = calcSecPerByte(baudrate);
201 
202  m_nBaudRate = baudrate;
203 
204  return 1;
205 }
int m_fd
device file descriptor
Definition: dxlhal.h:155
double m_fSecPerByte
seconds per byte transfer time
Definition: dxlhal.h:156
int m_nBaudRate
device baud rate
Definition: dxlhal.h:154
double calcSecPerByte(int baudrate)
Calculate seconds/byte transmit/recieve time in seconds.
Definition: dxlhal.cxx:289
std::string m_strDeviceName
device name
Definition: dxlhal.h:153
void dxlhal::setTimeout ( int  NumRcvByte)

Set receive timeout.

Parameters
NumRcvByteNumber of expected receive bytes

Definition at line 248 of file dxlhal.cxx.

References MAX_T_RTD.

Referenced by libdxl::dxl::txPacket().

249 {
250  static double Fudge = 0.010; // yea ol fudge factor
251 
252  m_fStartTime = now();
253 
254  m_fRcvWaitTime = m_fSecPerByte * (double)NumRcvByte + MAX_T_RTD + Fudge;
255 
256  //fprintf(stderr, "DBG: to wait time = %lfs from now = %lfs.\n",
257  // m_fRcvWaitTime, m_fStartTime);
258 }
#define MAX_T_RTD
maximum time delay at maximum RTD
Definition: dxlhal.h:36
double m_fSecPerByte
seconds per byte transfer time
Definition: dxlhal.h:156
double m_fRcvWaitTime
receive max elapse wait time
Definition: dxlhal.h:158
double now()
Mark time.
Definition: dxlhal.cxx:280
double m_fStartTime
event start time
Definition: dxlhal.h:157
int dxlhal::tx ( unsigned char *  pPacket,
int  numPacket 
)

Transmit Dynamixel instruction packet.

Parameters
[in]pPacketPacked packet.
numPacketNumber of bytes to transmit.
Returns
Returns 1 on success, 0 on failure.

Definition at line 212 of file dxlhal.cxx.

Referenced by libdxl::dxl::txPacket().

213 {
214  ssize_t n;
215 
216  n = SerDevWrite(m_fd, pPacket, (size_t)numPacket, 0);
217 
218  if( n > 0 )
219  {
220  return (int)n;
221  }
222  else
223  {
224  LOGERROR("tx failed.");
225  return 0;
226  }
227 }
int m_fd
device file descriptor
Definition: dxlhal.h:155

The documentation for this class was generated from the following files: