Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
motor::roboclaw::RoboClawComm Class Reference

RoboClaw communication class. More...

#include <RoboClaw.h>

Public Member Functions

 RoboClawComm ()
 Default constructor.
 
 RoboClawComm (std::string &strDevName, int nBaudRate, RoboClawChipSelect *pChipSelect=NULL)
 Initialization constructor. More...
 
virtual ~RoboClawComm ()
 Destructor.
 
virtual int open (std::string &strDevName, int nBaudRate, RoboClawChipSelect *pChipSelect=NULL)
 Open connection to motor controller(s). More...
 
virtual int close ()
 Close connection to motor controller. More...
 
virtual int flushInput ()
 Flush UART input FIFO. More...
 
virtual bool isOpen () const
 Test if connection is open. More...
 
virtual void enableDbg (bool bEnDis)
 Test if connection is open. More...
 
uint_t getLastCmdCrc ()
 Get last sent command's calculated 16-bit CRC. More...
 
uint_t getLastRspCrc ()
 Get last received response's appended 16-bit CRC. More...
 
virtual int execCmd (byte_t cmd[], size_t lenCmd)
 Execute a command with no response. More...
 
virtual int execCmdWithAckRsp (byte_t cmd[], size_t lenCmd, MsgFmt fmtCmd=MsgWithNoCrc)
 Execute a command with an acknowledgement response. More...
 
virtual int execCmdWithDataRsp (byte_t cmd[], size_t lenCmd, byte_t rsp[], size_t lenRsp, MsgFmt fmtCmd=MsgWithNoCrc, MsgFmt fmtRsp=MsgWithCrc)
 Execute a command with an data response. More...
 
virtual int sendCmd (byte_t cmd[], size_t lenCmd, MsgFmt fmtCmd=MsgWithNoCrc)
 Send command over serial connection to motor controller. More...
 
virtual int recvDataRsp (byte_t rsp[], size_t lenRsp)
 Receive data response over serial connection from motor controller. More...
 
virtual int recvAck ()
 Receive acknowledgement response over serial connection from motor controller. More...
 
int pack16 (uint_t val, byte_t buf[])
 Pack 16-bit unsigned value into buffer. More...
 
int unpack16 (byte_t buf[], uint_t &val)
 Unpack 16-bit unsigned value from buffer. More...
 
int pack16 (int val, byte_t buf[])
 Pack 16-bit signed value into buffer. More...
 
int unpack16 (byte_t buf[], int &val)
 Unpack 16-bit signed value from buffer. More...
 
int pack32 (uint_t val, byte_t buf[])
 Pack 32-bit unsigned value into buffer. More...
 
int unpack32 (byte_t buf[], uint_t &val)
 Unpack 32-bit unsigned value from buffer. More...
 
int pack32 (int val, byte_t buf[])
 Pack 32-bit signed value into buffer. More...
 
int unpack32 (byte_t buf[], int &val)
 Unpack 32-bit signed value from buffer. More...
 
virtual byte_t checksum (byte_t buf[], size_t lenBuf, bool bAck=false)
 Calculate 7-bit checksum over buffer. More...
 
virtual uint_t crc16 (uint_t crc, byte_t buf[], size_t lenBuf)
 Calculate 16-bit CRC over buffer. More...
 

Protected Attributes

std::string m_strDevName
 serial device name
 
int m_nBaudRate
 serial baud rate
 
int m_fd
 opened file descriptor
 
RoboClawChipSelectm_pChipSelect
 motor ctlr select object
 
uint_t m_uCrcCmd
 last sent command's CRC
 
uint_t m_uCrcRsp
 last received response's CRC
 
bool m_bDbgEnable
 do [not] enable debugging
 

Detailed Description

RoboClaw communication class.

The RoboClaw class instances bind to this class.

Definition at line 487 of file RoboClaw.h.

Constructor & Destructor Documentation

RoboClawComm::RoboClawComm ( std::string &  strDevName,
int  nBaudRate,
RoboClawChipSelect pChipSelect = NULL 
)

Initialization constructor.

Parameters
strDevNameSerial device name.
nBaudRateSerial device baud rate.
pChipSelect(Derived) motor controller selection class object.

Definition at line 217 of file RoboClaw.cxx.

219 {
220  close();
221 }
222 
223 int RoboClawComm::open(std::string &strDevName,
224  int nBaudRate,
225  RoboClawChipSelect *pChipSelect)
226 {
227  m_fd = SerDevOpen(strDevName.c_str(), nBaudRate, 8, 'N', 1, false, false);
virtual int open(std::string &strDevName, int nBaudRate, RoboClawChipSelect *pChipSelect=NULL)
Open connection to motor controller(s).
Definition: RoboClaw.cxx:234
int m_fd
opened file descriptor
Definition: RoboClaw.h:789
RoboClaw motor controller chip select class.
Definition: RoboClaw.h:444
virtual int close()
Close connection to motor controller.
Definition: RoboClaw.cxx:258

Member Function Documentation

byte_t RoboClawComm::checksum ( byte_t  buf[],
size_t  lenBuf,
bool  bAck = false 
)
virtual

Calculate 7-bit checksum over buffer.

Note
For FW versions < 4.0
Parameters
[in]bufBuffer holding command/response.
lenBufLength of data in buffer (bytes).
bAckIf true, or-in request ack bit to checksum (commands only).
Returns
Checksum byte.

Definition at line 517 of file RoboClaw.cxx.

520  {
521  chksum |= AckReqBit;
522  }
523 
524  return chksum;
525 }
526 
527 uint_t RoboClawComm::crc16(uint_t crc, byte_t buf[], size_t lenBuf)
528 {
529  size_t i;
530  size_t bit;
531 
532  for(i = 0; i < lenBuf; ++i)
533  {
534  crc = crc ^ ((uint_t)buf[i] << 8);
535 
536  for(bit = 0; bit < 8; ++bit)
virtual uint_t crc16(uint_t crc, byte_t buf[], size_t lenBuf)
Calculate 16-bit CRC over buffer.
Definition: RoboClaw.cxx:538
int RoboClawComm::close ( )
virtual

Close connection to motor controller.

Definition at line 258 of file RoboClaw.cxx.

Referenced by Laelaps.WatchDog.WatchDog::attach(), and Laelaps.WatchDog.WatchDog::open().

266 {
267  SerDevFIFOInputFlush(m_fd);
268 }
269 
270 int RoboClawComm::execCmd(byte_t cmd[], size_t lenCmd)
271 {
272  int rc;
273 
274  LOGWARN("Motor controller 0x%02x: execCmd() function is deprecated.");
int m_fd
opened file descriptor
Definition: RoboClaw.h:789
virtual int execCmd(byte_t cmd[], size_t lenCmd)
Execute a command with no response.
Definition: RoboClaw.cxx:281
uint_t RoboClawComm::crc16 ( uint_t  crc,
byte_t  buf[],
size_t  lenBuf 
)
virtual

Calculate 16-bit CRC over buffer.

Parameters
crcCRC starting seed value.
[in]bufBuffer.
lenbufLength of data in buffer (bytes).
Returns
Returns calculated 16-bit CRC over buffer.

Definition at line 538 of file RoboClaw.cxx.

Referenced by Laelaps.RoboClaw.RoboClaw::execCmdWithAckRsp(), Laelaps.RoboClaw.RoboClaw::execCmdWithDataRsp(), and Laelaps.RoboClaw.RoboClaw::readVersion().

539  {
540  crc = (crc << 1) ^ 0x1021;
541  }
542  else
543  {
544  crc = crc << 1;
545  }
546  }
547  }
548  return crc & 0xffff;
549 }
550 
551 
552 //-----------------------------------------------------------------------------
553 // Class RoboClaw
554 //-----------------------------------------------------------------------------
555 
557  const byte_t address,
558  const string &strNameId) :
559  m_comm(comm), m_address(address), m_strNameId(strNameId)
560 {
RoboClaw(RoboClawComm &comm, const byte_t address=AddrDft, const std::string &strNameId="RoboClaw")
Initialization constructor.
Definition: RoboClaw.cxx:567
RoboClaw communication class.
Definition: RoboClaw.h:487
virtual void motor::roboclaw::RoboClawComm::enableDbg ( bool  bEnDis)
inlinevirtual

Test if connection is open.

Parameters
bEnDisEnable/disable debugging.

Definition at line 555 of file RoboClaw.h.

556  {
557  m_bDbgEnable = bEnDis;
558  }
bool m_bDbgEnable
do [not] enable debugging
Definition: RoboClaw.h:793
int RoboClawComm::execCmd ( byte_t  cmd[],
size_t  lenCmd 
)
virtual

Execute a command with no response.

Note
Deprecated.
Parameters
[in]cmdPacked command buffer.
lenCmdLength of command (bytes).

Definition at line 281 of file RoboClaw.cxx.

287 {
288  int rc;
289 
290  if( (rc = sendCmd(cmd, lenCmd, fmtCmd)) == OK )
291  {
292  if( (rc = recvAck()) != OK )
293  {
294  LOGERROR("Motor controller 0x%02x: Failed to receive ack for command %u.",
295  cmd[FieldPosAddr], cmd[FieldPosCmd]);
virtual int sendCmd(byte_t cmd[], size_t lenCmd, MsgFmt fmtCmd=MsgWithNoCrc)
Send command over serial connection to motor controller.
Definition: RoboClaw.cxx:380
virtual int recvAck()
Receive acknowledgement response over serial connection from motor controller.
Definition: RoboClaw.cxx:436
int RoboClawComm::execCmdWithAckRsp ( byte_t  cmd[],
size_t  lenCmd,
MsgFmt  fmtCmd = MsgWithNoCrc 
)
virtual

Execute a command with an acknowledgement response.

Parameters
[in]cmdPacked command buffer.
lenCmdLength of command (bytes).
fmtCmdCommand format. Do [not] append command with a 16-bit CRC. If included, the command buffer must be of length ≥ lenCmd+2.

Definition at line 297 of file RoboClaw.cxx.

Referenced by Laelaps.RoboClaw.RoboClaw::BackwardMixed(), Laelaps.RoboClaw.RoboClaw::DriveM1(), Laelaps.RoboClaw.RoboClaw::DriveM2(), Laelaps.RoboClaw.RoboClaw::DriveMixed(), Laelaps.RoboClaw.RoboClaw::ForwardMixed(), Laelaps.RoboClaw.RoboClaw::LeftMixed(), Laelaps.RoboClaw.RoboClaw::M1Backward(), Laelaps.RoboClaw.RoboClaw::M1Forward(), Laelaps.RoboClaw.RoboClaw::M2Backward(), Laelaps.RoboClaw.RoboClaw::M2Forward(), Laelaps.RoboClaw.RoboClaw::resetEncoderCnts(), Laelaps.RoboClaw.RoboClaw::RightMixed(), Laelaps.RoboClaw.RoboClaw::setLogicBatterySettings(), Laelaps.RoboClaw.RoboClaw::setM1Duty(), Laelaps.RoboClaw.RoboClaw::setM1DutyAccel(), Laelaps.RoboClaw.RoboClaw::setM1EncoderMode(), Laelaps.RoboClaw.RoboClaw::setM1MaxCurrentLimit(), Laelaps.RoboClaw.RoboClaw::setM1Pidq(), Laelaps.RoboClaw.RoboClaw::setM1PositionConstants(), Laelaps.RoboClaw.RoboClaw::setM1Speed(), Laelaps.RoboClaw.RoboClaw::setM1SpeedAccel(), Laelaps.RoboClaw.RoboClaw::setM1SpeedAccelDeccelPosition(), Laelaps.RoboClaw.RoboClaw::setM1SpeedAccelDistance(), Laelaps.RoboClaw.RoboClaw::setM1SpeedDistance(), Laelaps.RoboClaw.RoboClaw::setM2Duty(), Laelaps.RoboClaw.RoboClaw::setM2DutyAccel(), Laelaps.RoboClaw.RoboClaw::setM2EncoderMode(), Laelaps.RoboClaw.RoboClaw::setM2MaxCurrentLimit(), Laelaps.RoboClaw.RoboClaw::setM2Pidq(), Laelaps.RoboClaw.RoboClaw::setM2PositionConstants(), Laelaps.RoboClaw.RoboClaw::setM2Speed(), Laelaps.RoboClaw.RoboClaw::setM2SpeedAccel(), Laelaps.RoboClaw.RoboClaw::setM2SpeedAccelDeccelPosition(), Laelaps.RoboClaw.RoboClaw::setM2SpeedAccelDistance(), Laelaps.RoboClaw.RoboClaw::setM2SpeedDistance(), Laelaps.RoboClaw.RoboClaw::setMainBatterySettings(), Laelaps.RoboClaw.RoboClaw::setMaxMainBattery(), Laelaps.RoboClaw.RoboClaw::setMinMainBattery(), Laelaps.RoboClaw.RoboClaw::setMixedDuty(), Laelaps.RoboClaw.RoboClaw::setMixedDutyAccel(), Laelaps.RoboClaw.RoboClaw::setMixedSpeed(), Laelaps.RoboClaw.RoboClaw::setMixedSpeedAccel(), Laelaps.RoboClaw.RoboClaw::setMixedSpeedAccelDeccelPosition(), Laelaps.RoboClaw.RoboClaw::setMixedSpeedAccelDistance(), Laelaps.RoboClaw.RoboClaw::setMixedSpeedDistance(), Laelaps.RoboClaw.RoboClaw::setMixedSpeedIAccel(), Laelaps.RoboClaw.RoboClaw::setMixedSpeedIAccelDistance(), Laelaps.RoboClaw.RoboClaw::TurnMixed(), and Laelaps.RoboClaw.RoboClaw::writeSettings().

300  {
301  LOGERROR("Motor controller 0x%02x: Failed to send command %u.",
302  cmd[FieldPosAddr], cmd[FieldPosCmd]);
303  rc = RC_ERROR;
304  }
305 
306  if( rc == RC_ERROR )
307  {
308  flushInput();
309  }
310 
311  return rc;
312 }
313 
314 int RoboClawComm::execCmdWithDataRsp(byte_t cmd[], size_t lenCmd,
315  byte_t rsp[], size_t lenRsp,
316  MsgFmt fmtCmd, MsgFmt fmtRsp)
317 {
318  uint_t uCrcCalc;
319  int n;
320  int rc;
321 
322  if( (rc = sendCmd(cmd, lenCmd, fmtCmd)) == OK )
323  {
virtual int sendCmd(byte_t cmd[], size_t lenCmd, MsgFmt fmtCmd=MsgWithNoCrc)
Send command over serial connection to motor controller.
Definition: RoboClaw.cxx:380
virtual int execCmdWithDataRsp(byte_t cmd[], size_t lenCmd, byte_t rsp[], size_t lenRsp, MsgFmt fmtCmd=MsgWithNoCrc, MsgFmt fmtRsp=MsgWithCrc)
Execute a command with an data response.
Definition: RoboClaw.cxx:325
MsgFmt
Command and response message formats.
Definition: RoboClaw.h:105
virtual int flushInput()
Flush UART input FIFO.
Definition: RoboClaw.cxx:276
int RoboClawComm::execCmdWithDataRsp ( byte_t  cmd[],
size_t  lenCmd,
byte_t  rsp[],
size_t  lenRsp,
MsgFmt  fmtCmd = MsgWithNoCrc,
MsgFmt  fmtRsp = MsgWithCrc 
)
virtual

Execute a command with an data response.

Parameters
[in]cmdPacked command buffer.
lenCmdLength of command (bytes).
[out]rspResponse buffer.
lenRspExpected length of packed response (bytes).
fmtCmdCommand format. Do [not] append command with a 16-bit CRC. If included, the command buffer must be of length ≥ lenCmd+2.
fmtRspResponse format. Response has with a 16-bit CRC. If ignore, firmware bug.

Definition at line 325 of file RoboClaw.cxx.

References motor::roboclaw::MsgWithCrc.

Referenced by Laelaps.RoboClaw.RoboClaw::readBufferCnts(), Laelaps.RoboClaw.RoboClaw::readCurrents(), Laelaps.RoboClaw.RoboClaw::readEncoderMode(), Laelaps.RoboClaw.RoboClaw::readLogicBattery(), Laelaps.RoboClaw.RoboClaw::readLogicBatterySettings(), Laelaps.RoboClaw.RoboClaw::readM1Encoder(), Laelaps.RoboClaw.RoboClaw::readM1instspeed(), Laelaps.RoboClaw.RoboClaw::readM1MaxCurrentLimit(), Laelaps.RoboClaw.RoboClaw::readM1Pidq(), Laelaps.RoboClaw.RoboClaw::readM1PositionConstants(), Laelaps.RoboClaw.RoboClaw::readM1Speed(), Laelaps.RoboClaw.RoboClaw::readM2Encoder(), Laelaps.RoboClaw.RoboClaw::readM2instspeed(), Laelaps.RoboClaw.RoboClaw::readM2MaxCurrentLimit(), Laelaps.RoboClaw.RoboClaw::readM2Pidq(), Laelaps.RoboClaw.RoboClaw::readM2PositionConstants(), Laelaps.RoboClaw.RoboClaw::readM2Speed(), Laelaps.RoboClaw.RoboClaw::readMainBattery(), Laelaps.RoboClaw.RoboClaw::readMainBatterySettings(), Laelaps.RoboClaw.RoboClaw::readStatus(), and Laelaps.RoboClaw.RoboClaw::readTemperature().

325  {
326  if( fmtRsp == MsgWithCrc )
327  {
328  uCrcCalc = crc16(m_uCrcCmd, rsp, lenRsp-2);
329  if( m_uCrcRsp != uCrcCalc )
330  {
331  LOGERROR("Motor controller 0x%02x: "
332  "Command %u response CRC-16 mismatch: "
333  "Expected 0x%04x, received 0x%04x.",
334  cmd[FieldPosAddr], cmd[FieldPosCmd],
335  uCrcCalc, m_uCrcRsp);
336  rc = RC_ERROR;
337  }
338  }
339  }
340  else if( n > 0 )
341  {
342  LOGWARN("Motor controller 0x%02x: "
343  "Command %u received partial response of %d/%zu bytes - ignoring.",
344  cmd[FieldPosAddr], cmd[FieldPosCmd], n, lenRsp);
345  rc = RC_ERROR;
346  }
347  else
348  {
349  LOGERROR("Motor controller 0x%02x: Command %u received no response.",
350  cmd[FieldPosAddr], cmd[FieldPosCmd]);
351  rc = RC_ERROR;
352  }
353  }
354  else
355  {
356  LOGERROR("Motor controller 0x%02x: Failed to send command %u.",
357  cmd[FieldPosAddr], cmd[FieldPosCmd]);
358  rc = RC_ERROR;
359  }
360 
361  if( rc == RC_ERROR )
362  {
363  flushInput();
364  }
365 
366  return rc;
367 }
368 
369 int RoboClawComm::sendCmd(byte_t cmd[], size_t lenCmd, MsgFmt fmtCmd)
370 {
371  m_pChipSelect->select(m_fd, cmd[FieldPosAddr]);
372 
373  // command's CRC
374  m_uCrcCmd = crc16(0, cmd, lenCmd);
375 
376  // append CRC
377  if( fmtCmd == MsgWithCrc )
378  {
uint_t m_uCrcRsp
last received response&#39;s CRC
Definition: RoboClaw.h:792
int m_fd
opened file descriptor
Definition: RoboClaw.h:789
virtual void select(int fd, byte_t addrSel)
Motor controller select function.
Definition: RoboClaw.cxx:196
virtual int sendCmd(byte_t cmd[], size_t lenCmd, MsgFmt fmtCmd=MsgWithNoCrc)
Send command over serial connection to motor controller.
Definition: RoboClaw.cxx:380
RoboClawChipSelect * m_pChipSelect
motor ctlr select object
Definition: RoboClaw.h:790
virtual uint_t crc16(uint_t crc, byte_t buf[], size_t lenBuf)
Calculate 16-bit CRC over buffer.
Definition: RoboClaw.cxx:538
uint_t m_uCrcCmd
last sent command&#39;s CRC
Definition: RoboClaw.h:791
MsgFmt
Command and response message formats.
Definition: RoboClaw.h:105
virtual int flushInput()
Flush UART input FIFO.
Definition: RoboClaw.cxx:276
int RoboClawComm::flushInput ( )
virtual

Flush UART input FIFO.

Definition at line 276 of file RoboClaw.cxx.

277  {
278  LOGERROR("Motor controller 0x%02x: Failed to send command %u.",
279  cmd[FieldPosAddr], cmd[FieldPosCmd]);
uint_t motor::roboclaw::RoboClawComm::getLastCmdCrc ( )
inline

Get last sent command's calculated 16-bit CRC.

Returns
Returns CRC.

Definition at line 565 of file RoboClaw.h.

566  {
567  return m_uCrcCmd;
568  }
uint_t m_uCrcCmd
last sent command&#39;s CRC
Definition: RoboClaw.h:791
uint_t motor::roboclaw::RoboClawComm::getLastRspCrc ( )
inline

Get last received response's appended 16-bit CRC.

Returns
Returns CRC.

Definition at line 575 of file RoboClaw.h.

References motor::roboclaw::MsgWithCrc, and motor::roboclaw::MsgWithNoCrc.

576  {
577  return m_uCrcRsp;
578  }
uint_t m_uCrcRsp
last received response&#39;s CRC
Definition: RoboClaw.h:792
int RoboClawComm::open ( std::string &  strDevName,
int  nBaudRate,
RoboClawChipSelect pChipSelect = NULL 
)
virtual

Open connection to motor controller(s).

Parameters
strDevNameSerial device name.
nBaudRateSerial device baud rate.
pChipSelect(Derived) motor controller selection class object.

Definition at line 234 of file RoboClaw.cxx.

239  : pChipSelect;
240 
241  LOGDIAG3("Opened interface to RoboClaw motor controller %s@%d.",
242  strDevName.c_str(), nBaudRate);
243 
244  return OK;
245 }
246 
248 {
249  if( m_fd >= 0 )
250  {
251  SerDevClose(m_fd);
252  LOGDIAG3("Closed %s interface to RoboClaw motor controller.",
253  m_strDevName.c_str());
254  }
255 
256  m_strDevName.clear();
int m_fd
opened file descriptor
Definition: RoboClaw.h:789
std::string m_strDevName
serial device name
Definition: RoboClaw.h:787
virtual int close()
Close connection to motor controller.
Definition: RoboClaw.cxx:258
int RoboClawComm::pack16 ( uint_t  val,
byte_t  buf[] 
)

Pack 16-bit unsigned value into buffer.

Order is big-endian (MSB first).

Parameters
[in]valValue to pack.
[out]bufDestination buffer.
Returns
Number of bytes packed(2).

Definition at line 463 of file RoboClaw.cxx.

466 {
467  s16_t v;
468 
int motor::roboclaw::RoboClawComm::pack16 ( int  val,
byte_t  buf[] 
)
inline

Pack 16-bit signed value into buffer.

Order is big-endian (MSB first).

Parameters
[in]valValue to pack.
[out]bufDestination buffer.
Returns
Number of bytes packed(2).

Definition at line 693 of file RoboClaw.h.

694  {
695  return pack16((uint_t)val, buf);
696  }
int pack16(uint_t val, byte_t buf[])
Pack 16-bit unsigned value into buffer.
Definition: RoboClaw.cxx:463
int RoboClawComm::pack32 ( uint_t  val,
byte_t  buf[] 
)

Pack 32-bit unsigned value into buffer.

Order is big-endian (MSB first).

Parameters
[in]valValue to pack.
[out]bufDestination buffer.
Returns
Number of bytes packed(4).

Definition at line 486 of file RoboClaw.cxx.

494 {
int motor::roboclaw::RoboClawComm::pack32 ( int  val,
byte_t  buf[] 
)
inline

Pack 32-bit signed value into buffer.

Order is big-endian (MSB first).

Parameters
[in]valValue to pack.
[out]bufDestination buffer.
Returns
Number of bytes packed(4).

Definition at line 744 of file RoboClaw.h.

745  {
746  return pack32((uint_t)val, buf);
747  }
int pack32(uint_t val, byte_t buf[])
Pack 32-bit unsigned value into buffer.
Definition: RoboClaw.cxx:486
int RoboClawComm::recvAck ( )
virtual

Receive acknowledgement response over serial connection from motor controller.

Definition at line 436 of file RoboClaw.cxx.

References ROBOCLAW_DBG_BUF.

438  {
439  ROBOCLAW_DBG_BUF((&ack), n, "recvAck(): ");
440  }
441 
442  if( ack != RspAck )
443  {
444  return RC_ERROR;
445  }
446  else
447  {
448  return OK;
449  }
450 }
451 
452 int RoboClawComm::pack16(uint_t val, byte_t buf[])
453 {
454  buf[0] = (byte_t)((val >> 8) & 0xff);
455  buf[1] = (byte_t)(val & 0xff);
456  return 2;
457 }
458 
459 int RoboClawComm::unpack16(byte_t buf[], uint_t &val)
460 {
461  val = ((uint_t)(buf[0]) << 8) | (uint_t)buf[1];
int unpack16(byte_t buf[], uint_t &val)
Unpack 16-bit unsigned value from buffer.
Definition: RoboClaw.cxx:470
int pack16(uint_t val, byte_t buf[])
Pack 16-bit unsigned value into buffer.
Definition: RoboClaw.cxx:463
#define ROBOCLAW_DBG_BUF(buf, len, fmt,...)
Debug print buffer disabled.
Definition: RoboClaw.cxx:165
int RoboClawComm::recvDataRsp ( byte_t  rsp[],
size_t  lenRsp 
)
virtual

Receive data response over serial connection from motor controller.

Parameters
[out]rspResponse buffer.
lenRspExpected length of packed response (bytes).
Returns
Number of bytes received or -1 on error.

Definition at line 411 of file RoboClaw.cxx.

References ROBOCLAW_DBG_BUF.

413  {
414  ROBOCLAW_DBG_BUF(rsp, n, "recvDataRsp(): ");
415  }
416 
417  return (int)n;
418  }
419  else
420  {
421  return RC_ERROR;
422  }
423 }
424 
426 {
427  ssize_t n;
428  byte_t ack;
429 
430  n = SerDevRead(m_fd, &ack, 1, RspTimeout);
431 
432  if( n != 1 )
433  {
434  return RC_ERROR;
int m_fd
opened file descriptor
Definition: RoboClaw.h:789
virtual int recvAck()
Receive acknowledgement response over serial connection from motor controller.
Definition: RoboClaw.cxx:436
#define ROBOCLAW_DBG_BUF(buf, len, fmt,...)
Debug print buffer disabled.
Definition: RoboClaw.cxx:165
int RoboClawComm::sendCmd ( byte_t  cmd[],
size_t  lenCmd,
MsgFmt  fmtCmd = MsgWithNoCrc 
)
virtual

Send command over serial connection to motor controller.

Parameters
[in]cmdPacked command buffer.
lenCmdLength of command (bytes).
fmtCmdCommand format. Do [not] append command with a 16-bit CRC. If included, the command buffer must be of length ≥ lenCmd+2.

Definition at line 380 of file RoboClaw.cxx.

References ROBOCLAW_DBG_BUF.

Referenced by Laelaps.Imu.Imu::unpackS32().

384  {
385  ROBOCLAW_DBG_BUF(cmd, lenCmd,
386  "Motor controller=0x%02x: Cmd=%d: CRC=0x%04x: sendCmd(): ",
387  cmd[FieldPosAddr], cmd[FieldPosCmd], m_uCrcCmd);
388  }
389 
390  if( SerDevWrite(m_fd, cmd, lenCmd, CmdTimeout) == lenCmd )
391  {
392  return OK;
393  }
394  else
395  {
396  return RC_ERROR;
397  }
398 }
399 
400 int RoboClawComm::recvDataRsp(byte_t rsp[], size_t lenRsp)
401 {
402  ssize_t n;
403 
404  if( (n = SerDevRead(m_fd, rsp, lenRsp, RspTimeout)) >= 0 )
405  {
406  // last two bytes are the 16-bit crc
407  if( n >= 2 )
408  {
409  unpack16(&rsp[n-2], m_uCrcRsp);
uint_t m_uCrcRsp
last received response&#39;s CRC
Definition: RoboClaw.h:792
int unpack16(byte_t buf[], uint_t &val)
Unpack 16-bit unsigned value from buffer.
Definition: RoboClaw.cxx:470
int m_fd
opened file descriptor
Definition: RoboClaw.h:789
virtual int recvDataRsp(byte_t rsp[], size_t lenRsp)
Receive data response over serial connection from motor controller.
Definition: RoboClaw.cxx:411
#define ROBOCLAW_DBG_BUF(buf, len, fmt,...)
Debug print buffer disabled.
Definition: RoboClaw.cxx:165
uint_t m_uCrcCmd
last sent command&#39;s CRC
Definition: RoboClaw.h:791
int RoboClawComm::unpack16 ( byte_t  buf[],
uint_t &  val 
)

Unpack 16-bit unsigned value from buffer.

Buffer is big-endian (MSB first).

Parameters
[in]bufSource buffer.
[out]valUnpacked value.
Returns
Number of bytes unpacked(2).

Definition at line 470 of file RoboClaw.cxx.

int RoboClawComm::unpack16 ( byte_t  buf[],
int &  val 
)

Unpack 16-bit signed value from buffer.

Buffer is big-endian (MSB first).

Parameters
[in]bufSource buffer.
[out]valUnpacked value.
Returns
Number of bytes unpacked(2).

Definition at line 476 of file RoboClaw.cxx.

476 {
477  buf[0] = (byte_t)((val >> 24) & 0xff);
478  buf[1] = (byte_t)((val >> 16) & 0xff);
479  buf[2] = (byte_t)((val >> 8) & 0xff);
480  buf[3] = (byte_t)(val & 0xff);
481  return 4;
482 }
483 
484 int RoboClawComm::unpack32(byte_t buf[], uint_t &val)
int unpack32(byte_t buf[], uint_t &val)
Unpack 32-bit unsigned value from buffer.
Definition: RoboClaw.cxx:495
int RoboClawComm::unpack32 ( byte_t  buf[],
uint_t &  val 
)

Unpack 32-bit unsigned value from buffer.

Buffer is big-endian (MSB first).

Parameters
[in]bufSource buffer.
[out]valUnpacked value.
Returns
Number of bytes unpacked(4).

Definition at line 495 of file RoboClaw.cxx.

int RoboClawComm::unpack32 ( byte_t  buf[],
int &  val 
)

Unpack 32-bit signed value from buffer.

Buffer is big-endian (MSB first).

Parameters
[in]bufSource buffer.
[out]valUnpacked value.
Returns
Number of bytes unpacked(4).

Definition at line 504 of file RoboClaw.cxx.

507 {
508  uint_t sum;
509  byte_t chksum;
510  size_t i;
511 
512  for(i=0, sum=0; i<lenBuf; ++i)
513  {
514  sum += (uint_t)buf[i];
515  }

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