24 #include <sys/ioctl.h> 26 #include <sys/types.h> 33 #include <linux/serial.h> 37 #include "rnr/rnrconfig.h" 39 #include "rnr/serdev.h" 60 int dxlhal::open(
const char *deviceName,
int baudrate)
64 m_strDeviceName = deviceName;
65 m_nBaudRate = baudrate;
67 m_fd = SerDevOpen(m_strDeviceName.c_str(), baudrate, 8,
'N', 1,
false,
false);
71 m_fSecPerByte = calcSecPerByte(baudrate);
77 LOGSYSERROR(
"%s: Failed to open.", m_strDeviceName.c_str());
79 m_strDeviceName.clear();
86 int dxlhal::dxl_hal_open(
int deviceIndex,
float baudrate)
89 struct termios newtio;
90 struct serial_struct serinfo;
94 snprintf(buf,
sizeof(buf),
"/dev/ttyUSB%d", deviceIndex);
96 buf[
sizeof(buf)-1] = 0;
98 m_strDeviceName = buf;
99 m_nBaudRate = baudrate;
101 memset(&newtio, 0,
sizeof(newtio));
103 if( (m_fd = open(m_strDeviceName.c_str(), O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0 )
105 LOGSYSERROR(
"%s: Device open error.", m_strDeviceName.c_str());
106 goto DXL_HAL_OPEN_ERROR;
109 newtio.c_cflag = B38400|CS8|CLOCAL|CREAD;
110 newtio.c_iflag = IGNPAR;
113 newtio.c_cc[VTIME] = 0;
114 newtio.c_cc[VMIN] = 0;
116 tcflush(m_fd, TCIFLUSH);
117 tcsetattr(m_fd, TCSANOW, &newtio);
119 if(ioctl(m_fd, TIOCGSERIAL, &serinfo) < 0)
121 LOGSYSERROR(
"%s(fd=%d): Cannot get serial info.",
122 m_strDeviceName.c_str(), m_fd);
126 serinfo.flags &= (int)(~ASYNC_SPD_MASK);
127 serinfo.flags |= (int)ASYNC_SPD_CUST;
128 serinfo.custom_divisor = (int)((
float)serinfo.baud_base / baudrate);
130 if(ioctl(m_fd, TIOCSSERIAL, &serinfo) < 0)
132 LOGSYSERROR(
"%s(fd=%d): Cannot set serial info.",
133 m_strDeviceName.c_str(), m_fd);
134 goto DXL_HAL_OPEN_ERROR;
137 m_fSecPerByte = calcSecPerByte(baudrate);
153 m_strDeviceName.clear();
158 string dxlhal::getDeviceName()
160 return m_strDeviceName;
163 int dxlhal::getBaudRate()
173 int dxlhal::setBaudRate(
int baudrate)
175 struct serial_struct serinfo;
182 if(ioctl(m_fd, TIOCGSERIAL, &serinfo) < 0)
184 LOGERROR(
"%s(fd=%d): Cannot get serial info.",
185 m_strDeviceName.c_str(), m_fd);
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);
193 if(ioctl(m_fd, TIOCSSERIAL, &serinfo) < 0)
195 LOGERROR(
"%s(fd=%d): Cannot set serial info.",
196 m_strDeviceName.c_str(), m_fd);
200 m_fSecPerByte = calcSecPerByte(baudrate);
202 m_nBaudRate = baudrate;
207 void dxlhal::clear(
void)
209 tcflush(m_fd, TCIFLUSH);
212 int dxlhal::tx(
unsigned char *pPacket,
int numPacket )
216 n = SerDevWrite(m_fd, pPacket, (
size_t)numPacket, 0);
224 LOGERROR(
"tx failed.");
229 int dxlhal::rx(
unsigned char *pPacket,
int numPacket)
231 static unsigned int Slop = 1000;
236 memset(pPacket, 0, (
size_t)numPacket);
238 usec = (
unsigned int)((
double)numPacket * m_fSecPerByte * 1000000.0) + Slop;
243 n = SerDevRead(m_fd, pPacket, (
size_t)numPacket, usec);
245 return n < 0? 0: (int)n;
248 void dxlhal::setTimeout(
int NumRcvByte)
250 static double Fudge = 0.010;
252 m_fStartTime = now();
254 m_fRcvWaitTime = m_fSecPerByte * (double)NumRcvByte +
MAX_T_RTD + Fudge;
260 bool dxlhal::hasTimedOut()
265 t = t1 - m_fStartTime;
269 if( t > m_fRcvWaitTime )
284 clock_gettime(CLOCK_REALTIME, &ts);
286 return (
double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
289 double dxlhal::calcSecPerByte(
int baudrate)
293 return 10.0/(double)baudrate;
Dynamixel Hardware Abstraction Layer implementation interface.
#define MAX_T_RTD
maximum time delay at maximum RTD