Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
sensor::imu::LaeImuCleanFlight Class Reference

#include <laeImu.h>

Inheritance diagram for sensor::imu::LaeImuCleanFlight:
sensor::imu::LaeImu

Public Member Functions

 LaeImuCleanFlight ()
 Default constructor.
 
virtual ~LaeImuCleanFlight ()
 Destructor.
 
virtual int numDoFs ()
 Get the total degress of freedom of the IMU sensor. More...
 
virtual bool hasAccelerometer ()
 Test if IMU has an accelerometer. More...
 
virtual bool hasGyroscope ()
 Test if IMU has an gyroscope. More...
 
virtual int readIdentity (std::string &strIdent)
 Read sensor identity values. More...
 
virtual int readRawImu ()
 Read sensor and board raw IMU values. More...
 
virtual int readRawInertia ()
 Read sensors inertia data. More...
 
virtual int readRawRollPitchYaw ()
 Read board's calculated roll, pitch, and yaw raw values. More...
 
virtual int convertRawToSI ()
 Convert last read IMU values to International System of Units. More...
 
- Public Member Functions inherited from sensor::imu::LaeImu
 LaeImu (std::string strIdent="Virtual IMU")
 Default constructor. More...
 
virtual ~LaeImu ()
 Destructor.
 
virtual int open (const std::string &strDevName, int nBaudRate)
 
virtual int close ()
 Close connection to motor controller. More...
 
virtual bool isOpen ()
 Check if IMU serial interface is open. More...
 
virtual void blacklist ()
 Black list IMU from robot sensors.
 
virtual void whitelist ()
 White list IMU sensor.
 
virtual bool isBlackListed ()
 Test if IMU is black listed. More...
 
virtual void clearSensedData ()
 Clear IMU sensed data.
 
virtual bool hasMagnetometer ()
 Test if IMU has an magnetometer. More...
 
virtual int configure (const laelaps::LaeDesc &desc)
 Configure IMU from product description. More...
 
virtual int configure (const laelaps::LaeTunes &tunes)
 Configure IMU from tunable parameters. More...
 
virtual int reload (const laelaps::LaeTunes &tunes)
 Reload with new tuning parameters. More...
 
virtual void compute ()
 Compute all IMU values form converted, raw values. More...
 
virtual void computeQuaternion ()
 Compute the quaternion from the IMU data.
 
virtual void computeDynamics ()
 Compute the velocity, position, and any other dynamics from the IMU data. More...
 
virtual void exec ()
 Exectute one step to read, convert, and compute IMU values.
 
std::string getIdentity ()
 Get IMU identity. More...
 
std::string getDevName ()
 Get IMU device name. More...
 
virtual void getRawInertiaData (int accel[], int gyro[])
 Get the last read raw inertia data. More...
 
virtual void getInertiaData (double accel[], double gyro[])
 Get the last read and converted inertia data. More...
 
virtual void getMagnetometerData (double mag[])
 Get the last read magnetometer values. More...
 
virtual void getAttitude (double rpy[])
 Get the last read IMU (vehicle) attitude. More...
 
virtual void getAttitude (double &roll, double &pitch, double &yaw)
 Get the last read IMU (vehicle) attitude. More...
 
virtual void getQuaternion (Quaternion &q)
 Get the last computed quaternion. More...
 
virtual void getImuData (double accel[], double gyro[], double mag[], double rpy[], Quaternion &q)
 Get the last sensed, converted, and computed IMU data. More...
 

Static Public Attributes

static const int SerBaud9600 = 9600
 9600 serial baudrate
 
static const int SerBaud19200 = 19200
 19200 serial baudrate
 
static const int SerBaud38400 = 38400
 38400 serial baudrate
 
static const int SerBaud57600 = 38400
 38400 serial baudrate
 
static const int SerBaud115200 = 115200
 115200 serial baudrate
 
static const int SerBaudDft = SerBaud115200
 default serial baudrate
 
static const uint_t TCmdTimeout = 1000
 command timeout (usec)
 
static const uint_t TRspTimeout = 12000
 response timeout (usec)
 
static const uint_t TFlushDelay = 100
 flush buffer delay (usec)
 

Protected Member Functions

int mspReadIdent (msp::MspIdent &ident)
 Read CleanFlight board's identity values. More...
 
int mspReadRawImu ()
 Read raw IMU data. More...
 
int mspReadAttitude ()
 MSP command/response. More...
 
int sendCmd (uint_t cmdId, byte_t cmdData[], size_t lenData)
 Send command to IMU. More...
 
int receiveRsp (uint_t cmdId, byte_t rspData[], size_t lenData)
 
void resyncComm ()
 Attempt to resynchronize the serial communication between the host and the IMU.
 
void flush (uint_t t)
 Flush serial input and output FIFOs, discarding all data. More...
 
int pack16 (int val, byte_t buf[])
 Pack 16-bit signed value into buffer. More...
 
int pack16 (uint_t val, byte_t buf[])
 Pack 16-bit unsigned value into buffer. More...
 
int unpack16 (byte_t buf[], int &val)
 Unpack 16-bit signed value from buffer. More...
 
int unpack16 (byte_t buf[], uint_t &val)
 Unpack 16-bit unsigned value from buffer. More...
 
int pack32 (int val, byte_t buf[])
 Pack 32-bit signed value into buffer. More...
 
int pack32 (uint_t val, byte_t buf[])
 Pack 32-bit unsigned value into buffer. More...
 
int unpack32 (byte_t buf[], int &val)
 Unpack 32-bit signed value from buffer. More...
 
int unpack32 (byte_t buf[], uint_t &val)
 Unpack 32-bit unsigned value from buffer. More...
 
- Protected Member Functions inherited from sensor::imu::LaeImu
void lockIo ()
 Lock the shared I/O resource. More...
 
void unlockIo ()
 Unlock the shared I/O resource. More...
 
void lockOp ()
 Lock the extended operation. More...
 
void unlockOp ()
 Unlock the extended operation. More...
 

Additional Inherited Members

- Protected Attributes inherited from sensor::imu::LaeImu
std::string m_strIdent
 IMU identity.
 
std::string m_strDevName
 serial device name
 
int m_nBaudRate
 device baudrate
 
int m_fd
 opened device file descriptor
 
bool m_bBlackListed
 IMU is [not] black listed.
 
int m_accelRaw [NumOfAxes]
 accelerometer raw values
 
int m_gyroRaw [NumOfAxes]
 gyroscope raw values
 
int m_magRaw [NumOfAxes]
 magnetometer raw values
 
int m_rpyRaw [NumOfAxes]
 roll,pitch,yaw raw values
 
double m_accel [NumOfAxes]
 accelerometer (m/s^2)
 
double m_gyro [NumOfAxes]
 gyrscope (radians/s)
 
double m_mag [NumOfAxes]
 magnetometer (tesla)
 
double m_rpy [NumOfAxes]
 roll,pitch,yaw (radians)
 
Quaternion m_quaternion
 imu orientation (and robot)
 
pthread_mutex_t m_mutexIo
 low-level I/O mutex
 
pthread_mutex_t m_mutexOp
 high-level operation mutex
 

Detailed Description

Inertia Measurement Unit class running the CleanFlight firmware.

Definition at line 670 of file laeImu.h.

Member Function Documentation

int LaeImuCleanFlight::convertRawToSI ( )
virtual

Convert last read IMU values to International System of Units.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Implements sensor::imu::LaeImu.

Definition at line 452 of file laeImu.cxx.

References laelaps::degToRad(), sensor::imu::LaeImu::m_accel, sensor::imu::LaeImu::m_accelRaw, sensor::imu::LaeImu::m_gyro, sensor::imu::LaeImu::m_gyroRaw, sensor::imu::LaeImu::m_rpy, sensor::imu::LaeImu::m_rpyRaw, sensor::imu::msp::MspAttitudeRawToDeg, sensor::imu::msp::MspGToMPerSec2, sensor::imu::msp::MspMpu6050RawToDegPerSec, sensor::imu::msp::MspMpu6050RawToG, sensor::imu::NumOfAxes, and sensor::imu::YAW.

453 {
454  for(int i = 0; i < NumOfAxes; ++i)
455  {
457  RtDb.m_imu.m_accel[i] = m_accel[i];
458 
460  RtDb.m_imu.m_gyro[i] = m_gyro[i];
461 
462  if( i != YAW )
463  {
465  RtDb.m_imu.m_rpy[i] = m_rpy[i];
466  }
467  }
468  m_rpy[YAW] = degToRad(m_rpyRaw[YAW]);
469  RtDb.m_imu.m_rpy[YAW] = m_rpy[YAW];
470 
471  return LAE_OK;
472 }
int m_gyroRaw[NumOfAxes]
gyroscope raw values
Definition: laeImu.h:506
static const double MspMpu6050RawToDegPerSec
MPU-6050 rotation raw value to degrees/second.
Definition: laeImu.h:653
double m_rpy[NumOfAxes]
roll,pitch,yaw (radians)
Definition: laeImu.h:516
double degToRad(double d)
Convert degrees to radians.
Definition: laeUtils.h:124
double m_accel[NumOfAxes]
accelerometer (m/s^2)
Definition: laeImu.h:513
static const double MspGToMPerSec2
standard g in m/s^2
Definition: laeImu.h:649
yaw index
Definition: laeImu.h:107
int m_accelRaw[NumOfAxes]
accelerometer raw values
Definition: laeImu.h:505
const int NumOfAxes
maximum number of axes per sensor component.
Definition: laeImu.h:98
static const double MspAttitudeRawToDeg
board computed attitude raw value to degreess
Definition: laeImu.h:655
static const double MspMpu6050RawToG
MPU-6050 acceleration raw value to g&#39;s.
Definition: laeImu.h:651
double m_gyro[NumOfAxes]
gyrscope (radians/s)
Definition: laeImu.h:514
int m_rpyRaw[NumOfAxes]
roll,pitch,yaw raw values
Definition: laeImu.h:508
void LaeImuCleanFlight::flush ( uint_t  t)
protected

Flush serial input and output FIFOs, discarding all data.

Parameters
tDelay time (usec) before flushing. Zero means no delay.

Definition at line 718 of file laeImu.cxx.

References sensor::imu::LaeImu::m_fd.

Referenced by receiveRsp(), resyncComm(), and sendCmd().

719 {
720  if( t > 0 )
721  {
722  usleep(t);
723  }
724 
725  SerDevFIFOOutputFlush(m_fd);
726  SerDevFIFOInputFlush(m_fd);
727 }
int m_fd
opened device file descriptor
Definition: laeImu.h:499
virtual bool sensor::imu::LaeImuCleanFlight::hasAccelerometer ( )
inlinevirtual

Test if IMU has an accelerometer.

Returns
Returns true or false.

Reimplemented from sensor::imu::LaeImu.

Definition at line 716 of file laeImu.h.

717  {
718  return true;
719  }
virtual bool sensor::imu::LaeImuCleanFlight::hasGyroscope ( )
inlinevirtual

Test if IMU has an gyroscope.

Returns
Returns true or false.

Reimplemented from sensor::imu::LaeImu.

Definition at line 726 of file laeImu.h.

727  {
728  return true;
729  }
int LaeImuCleanFlight::mspReadAttitude ( )
protected

MSP command/response.

Parameters

Definition at line 549 of file laeImu.cxx.

References sensor::imu::LaeImu::isOpen(), sensor::imu::LaeImu::lockIo(), sensor::imu::LaeImu::m_rpyRaw, sensor::imu::msp::MspCmdIdAttitude, sensor::imu::NumOfAxes, receiveRsp(), sendCmd(), sensor::imu::LaeImu::unlockIo(), and unpack16().

Referenced by readRawImu(), and readRawRollPitchYaw().

550 {
551  static const uint_t CmdId = MspCmdIdAttitude;
552  static const size_t RspDataLen = 6;
553 
554  byte_t rspData[RspDataLen];
555  int i, n;
556  int rc;
557 
558  if( !isOpen() )
559  {
560  return -LAE_ECODE_IO;
561  }
562 
563  lockIo();
564 
565  if( (rc = sendCmd(CmdId, NULL, 0)) == LAE_OK )
566  {
567  rc = receiveRsp(CmdId, rspData, RspDataLen);
568  }
569 
570  if( rc == LAE_OK )
571  {
572  for(i = 0, n = 0; i < NumOfAxes; ++i, n += 2)
573  {
574  unpack16(&rspData[n], m_rpyRaw[i]);
575  }
576  }
577 
578  unlockIo();
579 
580  return rc;
581 }
void lockIo()
Lock the shared I/O resource.
Definition: laeImu.h:541
void unlockIo()
Unlock the shared I/O resource.
Definition: laeImu.h:552
int sendCmd(uint_t cmdId, byte_t cmdData[], size_t lenData)
Send command to IMU.
Definition: laeImu.cxx:584
int unpack16(byte_t buf[], int &val)
Unpack 16-bit signed value from buffer.
Definition: laeImu.cxx:742
virtual bool isOpen()
Check if IMU serial interface is open.
Definition: laeImu.h:222
int receiveRsp(uint_t cmdId, byte_t rspData[], size_t lenData)
Definition: laeImu.cxx:623
const int NumOfAxes
maximum number of axes per sensor component.
Definition: laeImu.h:98
static const uint_t MspCmdIdAttitude
read raw attitude data
Definition: laeImu.h:633
int m_rpyRaw[NumOfAxes]
roll,pitch,yaw raw values
Definition: laeImu.h:508
int LaeImuCleanFlight::mspReadIdent ( msp::MspIdent ident)
protected

Read CleanFlight board's identity values.

MSP command/response.

Parameters
[out]identIdentity.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 474 of file laeImu.cxx.

References sensor::imu::LaeImu::isOpen(), sensor::imu::LaeImu::lockIo(), sensor::imu::msp::MspIdent::m_uCaps, sensor::imu::msp::MspIdent::m_uFwVersion, sensor::imu::msp::MspIdent::m_uMspVersion, sensor::imu::msp::MspIdent::m_uMultiType, sensor::imu::msp::MspCmdIdIdent, receiveRsp(), sendCmd(), sensor::imu::LaeImu::unlockIo(), and unpack32().

Referenced by readIdentity(), and resyncComm().

475 {
476  static const uint_t CmdId = MspCmdIdIdent;
477  static const size_t RspDataLen = 7;
478 
479  byte_t rspData[RspDataLen];
480  int rc;
481 
482  if( !isOpen() )
483  {
484  return -LAE_ECODE_IO;
485  }
486 
487  lockIo();
488 
489  if( (rc = sendCmd(CmdId, NULL, 0)) == LAE_OK )
490  {
491  rc = receiveRsp(CmdId, rspData, RspDataLen);
492  }
493 
494  if( rc == LAE_OK )
495  {
496  ident.m_uFwVersion = (uint_t)rspData[0];
497  ident.m_uMultiType = (uint_t)rspData[1];
498  ident.m_uMspVersion = (uint_t)rspData[2];
499  unpack32(&rspData[3], ident.m_uCaps);
500  }
501 
502  unlockIo();
503 
504  return rc;
505 }
uint_t m_uCaps
capabilities or&#39;d bits
Definition: laeImu.h:643
uint_t m_uMultiType
multiwii type
Definition: laeImu.h:641
void lockIo()
Lock the shared I/O resource.
Definition: laeImu.h:541
void unlockIo()
Unlock the shared I/O resource.
Definition: laeImu.h:552
int sendCmd(uint_t cmdId, byte_t cmdData[], size_t lenData)
Send command to IMU.
Definition: laeImu.cxx:584
static const uint_t MspCmdIdIdent
read board/sensor idents
Definition: laeImu.h:631
virtual bool isOpen()
Check if IMU serial interface is open.
Definition: laeImu.h:222
int receiveRsp(uint_t cmdId, byte_t rspData[], size_t lenData)
Definition: laeImu.cxx:623
uint_t m_uMspVersion
MSP version.
Definition: laeImu.h:642
uint_t m_uFwVersion
firmware version
Definition: laeImu.h:640
int unpack32(byte_t buf[], int &val)
Unpack 32-bit signed value from buffer.
Definition: laeImu.cxx:768
int LaeImuCleanFlight::mspReadRawImu ( )
protected

Read raw IMU data.

Read data are updated to local class object member data.

MSP command/response.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 507 of file laeImu.cxx.

References sensor::imu::LaeImu::isOpen(), sensor::imu::LaeImu::lockIo(), sensor::imu::LaeImu::m_accelRaw, sensor::imu::LaeImu::m_gyroRaw, sensor::imu::LaeImu::m_magRaw, sensor::imu::msp::MspCmdIdRawImu, sensor::imu::NumOfAxes, receiveRsp(), sendCmd(), sensor::imu::LaeImu::unlockIo(), and unpack16().

Referenced by readRawImu(), and readRawInertia().

508 {
509  static const uint_t CmdId = MspCmdIdRawImu;
510  static const size_t RspDataLen = 18;
511 
512  byte_t rspData[RspDataLen];
513  int i, n;
514  int rc;
515 
516  if( !isOpen() )
517  {
518  return -LAE_ECODE_IO;
519  }
520 
521  lockIo();
522 
523  if( (rc = sendCmd(CmdId, NULL, 0)) == LAE_OK )
524  {
525  rc = receiveRsp(CmdId, rspData, RspDataLen);
526  }
527 
528  if( rc == LAE_OK )
529  {
530  for(i = 0, n = 0; i < NumOfAxes; ++i, n += 2)
531  {
532  unpack16(&rspData[n], m_accelRaw[i]);
533  }
534  for(i = 0; i < NumOfAxes; ++i, n += 2)
535  {
536  unpack16(&rspData[n], m_gyroRaw[i]);
537  }
538  for(i = 0; i < NumOfAxes; ++i, n += 2)
539  {
540  unpack16(&rspData[n], m_magRaw[i]);
541  }
542  }
543 
544  unlockIo();
545 
546  return rc;
547 }
int m_gyroRaw[NumOfAxes]
gyroscope raw values
Definition: laeImu.h:506
void lockIo()
Lock the shared I/O resource.
Definition: laeImu.h:541
void unlockIo()
Unlock the shared I/O resource.
Definition: laeImu.h:552
int sendCmd(uint_t cmdId, byte_t cmdData[], size_t lenData)
Send command to IMU.
Definition: laeImu.cxx:584
int unpack16(byte_t buf[], int &val)
Unpack 16-bit signed value from buffer.
Definition: laeImu.cxx:742
int m_accelRaw[NumOfAxes]
accelerometer raw values
Definition: laeImu.h:505
virtual bool isOpen()
Check if IMU serial interface is open.
Definition: laeImu.h:222
int receiveRsp(uint_t cmdId, byte_t rspData[], size_t lenData)
Definition: laeImu.cxx:623
const int NumOfAxes
maximum number of axes per sensor component.
Definition: laeImu.h:98
int m_magRaw[NumOfAxes]
magnetometer raw values
Definition: laeImu.h:507
static const uint_t MspCmdIdRawImu
read raw imu data
Definition: laeImu.h:632
virtual int sensor::imu::LaeImuCleanFlight::numDoFs ( )
inlinevirtual

Get the total degress of freedom of the IMU sensor.

Returns
DoF

Reimplemented from sensor::imu::LaeImu.

Definition at line 706 of file laeImu.h.

707  {
708  return 6;
709  }
int sensor::imu::LaeImuCleanFlight::pack16 ( int  val,
byte_t  buf[] 
)
inlineprotected

Pack 16-bit signed value into buffer.

Order is little-endian (LSB first).

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

Definition at line 859 of file laeImu.h.

860  {
861  return pack16((uint_t &)val, buf);
862  }
int pack16(int val, byte_t buf[])
Pack 16-bit signed value into buffer.
Definition: laeImu.h:859
int LaeImuCleanFlight::pack16 ( uint_t  val,
byte_t  buf[] 
)
protected

Pack 16-bit unsigned value into buffer.

Order is little-endian (LSB first).

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

Definition at line 729 of file laeImu.cxx.

730 {
731  buf[0] = (byte_t)(val & 0xff);
732  buf[1] = (byte_t)((val >> 8) & 0xff);
733  return 2;
734 }
int sensor::imu::LaeImuCleanFlight::pack32 ( int  val,
byte_t  buf[] 
)
inlineprotected

Pack 32-bit signed value into buffer.

Order is little-endian (LSB first).

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

Definition at line 910 of file laeImu.h.

911  {
912  return pack32((uint_t)val, buf);
913  }
int pack32(int val, byte_t buf[])
Pack 32-bit signed value into buffer.
Definition: laeImu.h:910
int LaeImuCleanFlight::pack32 ( uint_t  val,
byte_t  buf[] 
)
protected

Pack 32-bit unsigned value into buffer.

Order is little-endian (LSB first).

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

Definition at line 750 of file laeImu.cxx.

751 {
752  buf[0] = (byte_t)(val & 0xff);
753  buf[1] = (byte_t)((val >> 8) & 0xff);
754  buf[2] = (byte_t)((val >> 16) & 0xff);
755  buf[3] = (byte_t)((val >> 24) & 0xff);
756  return 4;
757 }
int LaeImuCleanFlight::readIdentity ( std::string &  strIdent)
virtual

Read sensor identity values.

Parameters
[out]strIdentHardware specific, formated identity string.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Implements sensor::imu::LaeImu.

Definition at line 376 of file laeImu.cxx.

References sensor::imu::LaeImu::isBlackListed(), sensor::imu::LaeImu::m_strIdent, sensor::imu::msp::MspIdent::m_uFwVersion, and mspReadIdent().

Referenced by laelaps::LaeRobot::connSensors().

377 {
378  MspIdent ident;
379  int rc;
380 
381  if( isBlackListed() )
382  {
383  strIdent = "Unknown - blacklisted";
384  rc = LAE_OK;
385  }
386 
387  else if( (rc = mspReadIdent(ident)) == LAE_OK )
388  {
389  stringstream ss;
390 
391  ss << "CleanFlight v" << ident.m_uFwVersion
392  << " Naze32 with MPU-6050 IMU sensor";
393 
394  m_strIdent = ss.str();
395  strIdent = m_strIdent;
396  }
397 
398  return rc;
399 }
std::string m_strIdent
IMU identity.
Definition: laeImu.h:496
Board identity structure.
Definition: laeImu.h:638
uint_t m_uFwVersion
firmware version
Definition: laeImu.h:640
int mspReadIdent(msp::MspIdent &ident)
Read CleanFlight board&#39;s identity values.
Definition: laeImu.cxx:474
virtual bool isBlackListed()
Test if IMU is black listed.
Definition: laeImu.h:242
int LaeImuCleanFlight::readRawImu ( )
virtual

Read sensor and board raw IMU values.

The CleanFlight Naze32 with MPU-6050 sensor has:

  • accelerometer
  • gyroscope
  • attitude
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Implements sensor::imu::LaeImu.

Definition at line 401 of file laeImu.cxx.

References sensor::imu::LaeImu::isBlackListed(), mspReadAttitude(), and mspReadRawImu().

402 {
403  int rc;
404 
405  if( isBlackListed() )
406  {
407  rc = LAE_OK;
408  }
409 
410  else if( (rc = mspReadRawImu()) == LAE_OK )
411  {
412  rc = mspReadAttitude();
413  }
414 
415  return rc;
416 }
int mspReadRawImu()
Read raw IMU data.
Definition: laeImu.cxx:507
int mspReadAttitude()
MSP command/response.
Definition: laeImu.cxx:549
virtual bool isBlackListed()
Test if IMU is black listed.
Definition: laeImu.h:242
int LaeImuCleanFlight::readRawInertia ( )
virtual

Read sensors inertia data.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 418 of file laeImu.cxx.

References sensor::imu::LaeImu::isBlackListed(), and mspReadRawImu().

419 {
420  int rc;
421 
422  if( isBlackListed() )
423  {
424  rc = LAE_OK;
425  }
426 
427  else
428  {
429  rc = mspReadRawImu();
430  }
431 
432  return rc;
433 }
int mspReadRawImu()
Read raw IMU data.
Definition: laeImu.cxx:507
virtual bool isBlackListed()
Test if IMU is black listed.
Definition: laeImu.h:242
int LaeImuCleanFlight::readRawRollPitchYaw ( )
virtual

Read board's calculated roll, pitch, and yaw raw values.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 435 of file laeImu.cxx.

References sensor::imu::LaeImu::isBlackListed(), and mspReadAttitude().

436 {
437  int rc;
438 
439  if( isBlackListed() )
440  {
441  rc = LAE_OK;
442  }
443 
444  else
445  {
446  rc = mspReadAttitude();
447  }
448 
449  return rc;
450 }
int mspReadAttitude()
MSP command/response.
Definition: laeImu.cxx:549
virtual bool isBlackListed()
Test if IMU is black listed.
Definition: laeImu.h:242
int LaeImuCleanFlight::receiveRsp ( uint_t  cmdId,
byte_t  rspData[],
size_t  lenData 
)
protected

Parameters
cmdIdResponse's associated MSP command id.
[out]rspDataOptional response data.
lenDataLength of response data. Zero if no data.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 623 of file laeImu.cxx.

References flush(), sensor::imu::LaeImu::m_fd, sensor::imu::msp::MspFieldPosCmdId, sensor::imu::msp::MspFieldPosDataStart, sensor::imu::msp::MspFieldPosSize, sensor::imu::msp::MspRspMaxLen, sensor::imu::msp::MspRspMinLen, resyncComm(), TFlushDelay, and TRspTimeout.

Referenced by mspReadAttitude(), mspReadIdent(), mspReadRawImu(), and Laelaps.Imu.Imu::unpackS32().

626 {
627  byte_t rsp[MspRspMaxLen];
628  uint_t chksum;
629  size_t lenRsp;
630  size_t n;
631  size_t fldSize;
632  uint_t fldCmdId;
633  byte_t fldChkSum;
634  size_t i;
635 
636  lenRsp = MspRspMinLen + lenData;
637 
638  if( (n = SerDevRead(m_fd, rsp, lenRsp, TRspTimeout)) < 0 )
639  {
640  LOGERROR("IMU: Cmd %d response: Receive failed.", cmdId);
642  return -LAE_ECODE_IO;
643  }
644  else if( n < lenRsp )
645  {
646  LOGERROR("IMU: Cmd %d response: Receive partial response: "
647  "rcv'd %zu bytes, expected %zu bytes.", cmdId, n, lenRsp);
649  return -LAE_ECODE_IO;
650  }
651 
652  fldSize = (size_t)rsp[MspFieldPosSize];
653 
654  if( fldSize != lenData )
655  {
656  LOGERROR("IMU: Cmd %d response: Data length mismatch: " \
657  "Received %zu bytes, expected %zu bytes.", cmdId, fldSize, lenData);
658  resyncComm();
659  return -LAE_ECODE_IO;
660  }
661 
662  fldCmdId = (uint_t)rsp[MspFieldPosCmdId];
663 
664  if( fldCmdId != cmdId )
665  {
666  LOGERROR("IMU: Cmd %d response: Command Id mismatch: Received %d.",
667  cmdId, fldCmdId);
668  resyncComm();
669  return -LAE_ECODE_IO;
670  }
671 
672  chksum = rsp[MspFieldPosSize];
673  chksum ^= rsp[MspFieldPosCmdId];
674 
675  for(i = 0, n = MspFieldPosDataStart; i < lenData; ++i, ++n)
676  {
677  chksum ^= rsp[n];
678  rspData[i] = rsp[n];
679  }
680 
681  fldChkSum = rsp[lenRsp-1];
682 
683  if( chksum != fldChkSum )
684  {
685  LOGERROR("IMU: Cmd %d response: Checksum mismatch: " \
686  "Received 0x%02x, calculated 0x%02x.", cmdId, fldChkSum, chksum);
687  resyncComm();
688  return -LAE_ECODE_IO;
689  }
690 
691  return LAE_OK;
692 }
static const uint_t TFlushDelay
flush buffer delay (usec)
Definition: laeImu.h:689
void flush(uint_t t)
Flush serial input and output FIFOs, discarding all data.
Definition: laeImu.cxx:718
int m_fd
opened device file descriptor
Definition: laeImu.h:499
static const uint_t TRspTimeout
response timeout (usec)
Definition: laeImu.h:688
void resyncComm()
Attempt to resynchronize the serial communication between the host and the IMU.
Definition: laeImu.cxx:694
static const int MspRspMinLen
MspRspHdrLen+1 response min len.
Definition: laeImu.h:625
static const int MspFieldPosDataStart
data start field position
Definition: laeImu.h:616
static const int MspFieldPosSize
data size field position
Definition: laeImu.h:614
static const int MspRspMaxLen
response max len
Definition: laeImu.h:626
static const int MspFieldPosCmdId
command id field position
Definition: laeImu.h:615
int LaeImuCleanFlight::sendCmd ( uint_t  cmdId,
byte_t  cmdData[],
size_t  lenData 
)
protected

Send command to IMU.

Parameters
cmdIdMSP command id.
[in]cmdDataOptional command data.
lenDataLength of command data. Zero if no data.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 584 of file laeImu.cxx.

References flush(), sensor::imu::LaeImu::m_fd, sensor::imu::msp::MspCmdMaxLen, sensor::imu::msp::MspCmdPreamble, TCmdTimeout, and TFlushDelay.

Referenced by mspReadAttitude(), mspReadIdent(), mspReadRawImu(), and Laelaps.Imu.Imu::unpackS32().

585 {
586  byte_t cmd[MspCmdMaxLen];
587  uint_t chksum;
588  size_t n;
589 
590  for(n = 0; n<strlen(MspCmdPreamble); ++n)
591  {
592  cmd[n] = MspCmdPreamble[n];
593  }
594 
595  chksum = 0;
596 
597  cmd[n++] = (byte_t)lenData;
598  chksum ^= (byte_t)lenData;
599 
600  cmd[n++] = (byte_t)cmdId;
601  chksum ^= (byte_t)cmdId;
602 
603  for(size_t i = 0; i < lenData; ++i)
604  {
605  cmd[n++] = (byte_t)cmdData[i];
606  chksum ^= (byte_t)cmdData[i];
607  }
608 
609  cmd[n++] = (byte_t)chksum;
610 
611  if( SerDevWrite(m_fd, cmd, n, TCmdTimeout) == n )
612  {
613  return LAE_OK;
614  }
615  else
616  {
618  LOGERROR("IMU: Cmd %d: Send failed.", cmdId);
619  return -LAE_ECODE_IO;
620  }
621 }
static const uint_t TFlushDelay
flush buffer delay (usec)
Definition: laeImu.h:689
void flush(uint_t t)
Flush serial input and output FIFOs, discarding all data.
Definition: laeImu.cxx:718
int m_fd
opened device file descriptor
Definition: laeImu.h:499
static const char *const MspCmdPreamble
command preamble
Definition: laeImu.h:607
static const uint_t TCmdTimeout
command timeout (usec)
Definition: laeImu.h:687
static const int MspCmdMaxLen
response max len
Definition: laeImu.h:623
int LaeImuCleanFlight::unpack16 ( byte_t  buf[],
int &  val 
)
protected

Unpack 16-bit signed value from buffer.

Buffer is little-endian (LSB first).

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

Definition at line 742 of file laeImu.cxx.

Referenced by mspReadAttitude(), and mspReadRawImu().

743 {
744  s16_t v;
745  v = ((s16_t)(buf[1]) << 8) | (s16_t)buf[0];
746  val = (int)v;
747  return 2;
748 }
int LaeImuCleanFlight::unpack16 ( byte_t  buf[],
uint_t &  val 
)
protected

Unpack 16-bit unsigned value from buffer.

Buffer is little-endian (LSB first).

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

Definition at line 736 of file laeImu.cxx.

737 {
738  val = ((uint_t)(buf[1]) << 8) | (uint_t)buf[0];
739  return 2;
740 }
int LaeImuCleanFlight::unpack32 ( byte_t  buf[],
int &  val 
)
protected

Unpack 32-bit signed value from buffer.

Buffer is little-endian (LSB first).

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

Definition at line 768 of file laeImu.cxx.

Referenced by mspReadIdent().

769 {
770  s32_t v;
771 
772  v = ((s32_t)(buf[3]) << 24) |
773  ((s32_t)(buf[2]) << 16) |
774  ((s32_t)(buf[1]) << 8) |
775  (s32_t)buf[0];
776  val = (int)v;
777  return 4;
778 }
int LaeImuCleanFlight::unpack32 ( byte_t  buf[],
uint_t &  val 
)
protected

Unpack 32-bit unsigned value from buffer.

Buffer is little-endian (LSB first).

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

Definition at line 759 of file laeImu.cxx.

760 {
761  val = ((uint_t)(buf[3]) << 24) |
762  ((uint_t)(buf[2]) << 16) |
763  ((uint_t)(buf[1]) << 8) |
764  (uint_t)buf[0];
765  return 4;
766 }

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