Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeImu.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelaps
4 //
5 // File: laeImu.h
6 //
7 /*! \file
8  *
9  * \brief Laelaps built-in Inertial Measurement Unit class interface.
10  *
11  * The current Laelaps uses the open-source CleanFlight firmware loaded
12  * on a Naze32 controller. The interface is serial USB.
13  *
14  * \sa https://github.com/cleanflight
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \par Copyright
19  * \h_copy 2015-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 #ifndef _LAE_IMU_H
50 #define _LAE_IMU_H
51 
52 #include <pthread.h>
53 
54 #ifndef SWIG
55 #include <string>
56 #endif // SWIG
57 
58 #include "rnr/rnrconfig.h"
59 
60 #include "Laelaps/laelaps.h"
61 #ifndef SWIG
62 #include "Laelaps/laeDesc.h"
63 #include "Laelaps/laeTune.h"
64 #endif // SWIG
65 
66 
67 #ifdef SWIG
68 // const --> %constant in swig interface .i file.
69 #define STATIC_STR const char*
70 #else
71 #define STATIC_STR static const char* const
72 #endif // SWIG
73 
74 /*!
75  * \brief The sensor namespace.
76  */
77 #ifndef SWIG
78 namespace sensor
79 {
80  // the IMU namespace
81  namespace imu
82  {
83 #endif // SWIG
84  //--------------------------------------------------------------------------
85  // IMU Command Data
86  //--------------------------------------------------------------------------
87 
88  /*!
89  * \brief Axes indices.
90  */
91  enum Axis
92  {
93  X = 0, ///< x axis index
94  Y = 1, ///< y axis index
95  Z = 2 ///< z axis index
96  };
97 
98  const int NumOfAxes = 3; ///< maximum number of axes per sensor component.
99 
100  /*!
101  * \brief Roll, pitch, and yaw indices.
102  */
103  enum RPY
104  {
105  ROLL = 0, ///< roll index
106  PITCH = 1, ///< pitch index
107  YAW = 2 ///< yaw index
108  };
109 
110 #ifndef SWIG
111  //--------------------------------------------------------------------------
112  // Quaternion Class
113  //--------------------------------------------------------------------------
114 
115  /*!
116  * \brief Quaternion class.
117  */
119  {
120  public:
121  double m_x; ///< x
122  double m_y; ///< y
123  double m_z; ///< z
124  double m_w; ///< w
125 
126  /*!
127  * \brief Default constructor.
128  */
130  {
131  clear();
132  }
133 
134  /*!
135  * \brief Destructor.
136  */
138  {
139  }
140 
141  /*!
142  * \brief Assignment operator.
143  *
144  * \param rhs Right hand side object.
145  *
146  * \return Returns copy of this.
147  */
148  Quaternion operator=(const Quaternion &rhs);
149 
150  /*!
151  * \brief Clear quaternion values.
152  */
153  void clear();
154 
155  /*!
156  * \brief Convert Euler angles to quaternion.
157  *
158  * \param phi Angle between x axis and the N axis (radians).
159  * \param theta Angle between z axis and the Z axis (radians).
160  * \param phi Angle between N axis and the X axis (radians).
161  */
162  void convertEuler(double phi, double theta, double psi);
163 
164  /*!
165  * \brief Convert Tait-Bryan angles to quaternion.
166  *
167  * \param roll Rotation about the x-axis.
168  * \param pitch Rotation about the y-axis.
169  * \param yaw Rotation about the z-axis.
170  */
171  void convertTaitBryan(double roll, double pitch, double yaw);
172 
173  protected:
174 
175  }; // class Quaternion
176 
177 
178  //--------------------------------------------------------------------------
179  // LaeImu Virtual Base Class
180  //--------------------------------------------------------------------------
181 
182  /*!
183  * Inertia Measurement Unit virtual base class with serial interface.
184  */
185  class LaeImu
186  {
187  public:
188  /*!
189  * \brief Default constructor.
190  *
191  * \param strIdent IMU identity string.
192  */
193  LaeImu(std::string strIdent="Virtual IMU");
194 
195  /*!
196  * \brief Destructor.
197  */
198  virtual ~LaeImu();
199 
200  /*!
201  * \bried Open connection to IMU.
202  *
203  * \param strDevName Serial device name.
204  * \param nBaudRate Serial device baud rate.
205  *
206  * \copydoc doc_return_std
207  */
208  virtual int open(const std::string &strDevName, int nBaudRate);
209 
210  /*!
211  * \brief Close connection to motor controller.
212  *
213  * \copydoc doc_return_std
214  */
215  virtual int close();
216 
217  /*!
218  * \brief Check if IMU serial interface is open.
219  *
220  * \return Returns true or false.
221  */
222  virtual bool isOpen()
223  {
224  return m_fd >= 0? true: false;
225  }
226 
227  /*!
228  * \brief Black list IMU from robot sensors.
229  */
230  virtual void blacklist();
231 
232  /*!
233  * \brief White list IMU sensor.
234  */
235  virtual void whitelist();
236 
237  /*!
238  * \brief Test if IMU is black listed.
239  *
240  * \return Returns true or false.
241  */
242  virtual bool isBlackListed()
243  {
244  return m_bBlackListed;
245  }
246 
247  /*!
248  * \brief Clear IMU sensed data.
249  */
250  virtual void clearSensedData();
251 
252  /*!
253  * \brief Get the total IMU degress of freedom.
254  *
255  * \return DoF
256  */
257  virtual int numDoFs()
258  {
259  return 0;
260  }
261 
262  /*!
263  * \brief Test if IMU has an accelerometer.
264  *
265  * \return Returns true or false.
266  */
267  virtual bool hasAccelerometer()
268  {
269  return false;
270  }
271 
272  /*!
273  * \brief Test if IMU has an gyroscope.
274  *
275  * \return Returns true or false.
276  */
277  virtual bool hasGyroscope()
278  {
279  return false;
280  }
281 
282  /*!
283  * \brief Test if IMU has an magnetometer.
284  *
285  * \return Returns true or false.
286  */
287  virtual bool hasMagnetometer()
288  {
289  return false;
290  }
291 
292  /*!
293  * \brief Configure IMU from product description.
294  *
295  * Call after connection is opened.
296  *
297  * \param desc Product description.
298  *
299  * \copydoc doc_return_std
300  */
301  virtual int configure(const laelaps::LaeDesc &desc);
302 
303  /*!
304  * \brief Configure IMU from tunable parameters.
305  *
306  * Call after connection is opened.
307  *
308  * \param tunes Laelaps tuning parameters.
309  *
310  * \copydoc doc_return_std
311  */
312  virtual int configure(const laelaps::LaeTunes &tunes);
313 
314  /*!
315  * \brief Reload with new tuning parameters.
316  *
317  * \param tunes Laelaps tuning parameters.
318  *
319  * \copydoc doc_return_std
320  */
321  virtual int reload(const laelaps::LaeTunes &tunes);
322 
323  /*!
324  * \brief Read sensor identity values.
325  *
326  * \param [out] strIdent Hardware specific, formated identity string.
327  *
328  * \copydoc doc_return_std
329  */
330  virtual int readIdentity(std::string &strIdent) = 0;
331 
332  /*!
333  * \brief Read sensor raw IMU values.
334  *
335  * Depending on the sensor and the board, raw values read:
336  * \li accelerometer
337  * \li gyroscope
338  * \li magnetometer
339  * \li GPS
340  * \li barometer
341  * \li temperature
342  * \li accurate timing
343  * \li attitude
344  * \li quaternions
345  *
346  * \copydoc doc_return_std
347  */
348  virtual int readRawImu() = 0;
349 
350  /*!
351  * \brief Convert last read IMU values to International System of Units.
352  *
353  * \copydoc doc_return_std
354  */
355  virtual int convertRawToSI() = 0;
356 
357  /*!
358  * \brief Compute all IMU values form converted, raw values.
359  *
360  * \todo
361  */
362  virtual void compute();
363 
364  /*!
365  * \brief Compute the quaternion from the IMU data.
366  */
367  virtual void computeQuaternion();
368 
369  /*!
370  * \brief Compute the velocity, position, and any other dynamics from the
371  * IMU data.
372  *
373  * \todo
374  */
375  virtual void computeDynamics();
376 
377  /*!
378  * \brief Exectute one step to read, convert, and compute IMU values.
379  */
380  virtual void exec();
381 
382  /*!
383  * \brief Get IMU identity.
384  *
385  * \return String.
386  */
387  std::string getIdentity()
388  {
389  return m_strIdent;
390  }
391  /*!
392  * \brief Get IMU device name.
393  *
394  * \return String.
395  */
396  std::string getDevName()
397  {
398  return m_strDevName;
399  }
400 
401  /*!
402  * \brief Get the last read raw inertia data.
403  *
404  * \note N/A data are set to zero.
405  *
406  * \param [out] accel Raw accelerometer data.
407  * The array size must be \h_ge \ref NumOfAxes.
408  * \param [out] gyro Raw gyroscope data.
409  * The array size must be \h_ge \ref NumOfAxes.
410  */
411  virtual void getRawInertiaData(int accel[], int gyro[]);
412 
413  /*!
414  * \brief Get the last read and converted inertia data.
415  *
416  * \note N/A data are set to zero.
417  *
418  * \param [out] accel Accelerometer data (m/s^2).
419  * The array size must be \h_ge \ref NumOfAxes.
420  * \param [out] gyro Gyroscope data (radians/s).
421  * The array size must be \h_ge \ref NumOfAxes.
422  */
423  virtual void getInertiaData(double accel[], double gyro[]);
424 
425  /*!
426  * \brief Get the last read magnetometer values.
427  *
428  * \note N/A data are set to zero.
429  *
430  * \param [out] mag Magnetometer data (tesla).
431  * The array size must be \h_ge \ref NumOfAxes.
432  */
433  virtual void getMagnetometerData(double mag[]);
434 
435  /*!
436  * \brief Get the last read IMU (vehicle) attitude.
437  *
438  * \note N/A data are set to zero.
439  *
440  * \param [out] rpy Vehicle roll, pitch, and yaw (radians).
441  * The array size must be \h_ge \ref NumOfAxes.
442  */
443  virtual void getAttitude(double rpy[]);
444 
445  /*!
446  * \brief Get the last read IMU (vehicle) attitude.
447  *
448  * \note N/A data are set to zero.
449  *
450  * \param [out] roll Vehicle roll (radians).
451  * \param [out] pitch Vehicle pitch (radians).
452  * \param [out] yaw Vehicle yaw (radians).
453  */
454  virtual void getAttitude(double &roll, double &pitch, double &yaw);
455 
456  /*!
457  * \brief Get the last computed quaternion.
458  *
459  * \param [out] q Vehicle quaternion.
460  */
461  virtual void getQuaternion(Quaternion &q);
462 
463  // FUTURE
464  //virtual void getRawMagData(int mag[]);
465  //virtual void getMagData(double mag[]);
466  //virtual void getRawBarometerData(int &baro);
467  //virtual void getBarometerData(double &baro);
468  //virtual void getRawTemperatureData(int &temp);
469  //virtual void getGps(GPS &gps);
470  //virtual void getTemperatureData(double &temp);
471  //virtual void getDynamics(double &vel[], double &pos[]);
472  // FUTURE
473 
474  /*!
475  * \brief Get the last sensed, converted, and computed IMU data.
476  *
477  * \note N/A data are set to zero.
478  *
479  * \param [out] accel Accelerometer data (m/s^2).
480  * The array size must be \h_ge \ref NumOfAxes.
481  * \param [out] gyro Gyroscope data (radians/s).
482  * The array size must be \h_ge \ref NumOfAxes.
483  * \param [out] mag Magnetometer data (tesla).
484  * The array size must be \h_ge \ref NumOfAxes.
485  * \param [out] rpy Vehicle roll, pitch, and yaw (radians).
486  * The array size must be \h_ge \ref NumOfAxes.
487  * \param [out] q Vehicle quaternion.
488  */
489  virtual void getImuData(double accel[], double gyro[], double mag[],
490  double rpy[], Quaternion &q);
491 
492  protected:
493  //
494  // Hardware interface.
495  //
496  std::string m_strIdent; ///< IMU identity
497  std::string m_strDevName; ///< serial device name
498  int m_nBaudRate; ///< device baudrate
499  int m_fd; ///< opened device file descriptor
500  bool m_bBlackListed; ///< IMU is [not] black listed.
501 
502  //
503  // Raw sensor values.
504  //
505  int m_accelRaw[NumOfAxes]; ///< accelerometer raw values
506  int m_gyroRaw[NumOfAxes]; ///< gyroscope raw values
507  int m_magRaw[NumOfAxes]; ///< magnetometer raw values
508  int m_rpyRaw[NumOfAxes]; ///< roll,pitch,yaw raw values
509 
510  //
511  // Converted sensor values in SI units.
512  //
513  double m_accel[NumOfAxes]; ///< accelerometer (m/s^2)
514  double m_gyro[NumOfAxes]; ///< gyrscope (radians/s)
515  double m_mag[NumOfAxes]; ///< magnetometer (tesla)
516  double m_rpy[NumOfAxes]; ///< roll,pitch,yaw (radians)
517  Quaternion m_quaternion; ///< imu orientation (and robot)
518 
519  // FUTURE
520  // double m_vel[];
521  // double m_pos[];
522  // int m_baroRaw;
523  // double m_baro;
524  // int m_tempRaw;
525  // double m_temp;
526  // GPS m_gps;
527  // FUTURE
528 
529  // mutual exclusions
530  pthread_mutex_t m_mutexIo; ///< low-level I/O mutex
531  pthread_mutex_t m_mutexOp; ///< high-level operation mutex
532 
533  /*!
534  * \brief Lock the shared I/O resource.
535  *
536  * The lock()/unlock() primitives provide for safe multi-threading.
537  *
538  * \par Context:
539  * Any.
540  */
541  void lockIo()
542  {
543  pthread_mutex_lock(&m_mutexIo);
544  }
545 
546  /*!
547  * \brief Unlock the shared I/O resource.
548  *
549  * \par Context:
550  * Any.
551  */
552  void unlockIo()
553  {
554  pthread_mutex_unlock(&m_mutexIo);
555  }
556 
557  /*!
558  * \brief Lock the extended operation.
559  *
560  * The lock()/unlock() primitives provide for safe multi-threading.
561  *
562  * \par Context:
563  * Any.
564  */
565  void lockOp()
566  {
567  pthread_mutex_lock(&m_mutexOp);
568  }
569 
570  /*!
571  * \brief Unlock the extended operation.
572  *
573  * \par Context:
574  * Any.
575  */
576  void unlockOp()
577  {
578  pthread_mutex_unlock(&m_mutexOp);
579  }
580  }; // class LaeImu
581 #endif // SWIG
582 
583 
584  //--------------------------------------------------------------------------
585  // Multiwii Serial Protocol Interface
586  //--------------------------------------------------------------------------
587 
588  /*!
589  * \brief Multiwii Serial Protocal Interface
590  *
591  * Several IMU boards use the MSP interface.
592  *
593  * Command: <preamble>MspDirTo<size><cmdid>[<data>]<crc>
594  *
595  * Response: <preamble>MspDirFrom<size><cmdid>[<data>]<crc>
596  */
597 #ifndef SWIG
598  namespace msp
599  {
600 #endif // SWIG
601  //
602  // Message preambles
603  //
604  STATIC_STR MspPreamble = "$M"; ///< message preamble
605  STATIC_STR MspDirTo = "<"; ///< to imu
606  STATIC_STR MspDirFrom = ">"; ///< from imu
607  STATIC_STR MspCmdPreamble = "$M<"; ///< command preamble
608  STATIC_STR MspRspPreamble = "$M>"; ///< response preamble
609 
610  //
611  // Field positions
612  //
613  static const int MspFieldPosDir = 3; ///< dir field position
614  static const int MspFieldPosSize = 3; ///< data size field position
615  static const int MspFieldPosCmdId = 4; ///< command id field position
616  static const int MspFieldPosDataStart = 5; ///< data start field position
617 
618  //
619  // Message lengths
620  //
621  static const int MspCmdHdrLen = 5; ///< command header len
622  static const int MspCmdMinLen = 6; ///< MspCmdHdrLen+1 command min len
623  static const int MspCmdMaxLen = 32; ///< response max len
624  static const int MspRspHdrLen = 5; ///< response header len
625  static const int MspRspMinLen = 6; ///< MspRspHdrLen+1 response min len
626  static const int MspRspMaxLen = 32; ///< response max len
627 
628  //
629  // Command Ids
630  //
631  static const uint_t MspCmdIdIdent = 100; ///< read board/sensor idents
632  static const uint_t MspCmdIdRawImu = 102; ///< read raw imu data
633  static const uint_t MspCmdIdAttitude = 108; ///< read raw attitude data
634 
635  /*!
636  * \brief Board identity structure.
637  */
638  struct MspIdent
639  {
640  uint_t m_uFwVersion; ///< firmware version
641  uint_t m_uMultiType; ///< multiwii type
642  uint_t m_uMspVersion; ///< MSP version
643  uint_t m_uCaps; ///< capabilities or'd bits
644  };
645 
646  //
647  // Conversion Factors
648  //
649  static const double MspGToMPerSec2 = 9.80665;
650  ///< standard g in m/s^2
651  static const double MspMpu6050RawToG = 1.0 / 512.0;
652  ///< MPU-6050 acceleration raw value to g's
653  static const double MspMpu6050RawToDegPerSec = 1.0 / 4.096;
654  ///< MPU-6050 rotation raw value to degrees/second
655  static const double MspAttitudeRawToDeg = 0.1;
656  ///< board computed attitude raw value to degreess
657 
658 #ifndef SWIG
659  } // namespace msp
660 #endif // SWIG
661 
662 #ifndef SWIG
663  //--------------------------------------------------------------------------
664  // LaeImuCleanFlight Class
665  //--------------------------------------------------------------------------
666 
667  /*!
668  * Inertia Measurement Unit class running the CleanFlight firmware.
669  */
670  class LaeImuCleanFlight : public LaeImu
671  {
672  public:
673  //
674  // Serial baud rates
675  //
676  static const int SerBaud9600 = 9600; ///< 9600 serial baudrate
677  static const int SerBaud19200 = 19200; ///< 19200 serial baudrate
678  static const int SerBaud38400 = 38400; ///< 38400 serial baudrate
679  static const int SerBaud57600 = 38400; ///< 38400 serial baudrate
680  static const int SerBaud115200 = 115200; ///< 115200 serial baudrate
681 
682  static const int SerBaudDft = SerBaud115200; ///< default serial baudrate
683 
684  //
685  // Response timeout
686  //
687  static const uint_t TCmdTimeout = 1000; ///< command timeout (usec)
688  static const uint_t TRspTimeout = 12000; ///< response timeout (usec)
689  static const uint_t TFlushDelay = 100; ///< flush buffer delay (usec)
690 
691  /*!
692  * \brief Default constructor.
693  */
695 
696  /*!
697  * \brief Destructor.
698  */
699  virtual ~LaeImuCleanFlight();
700 
701  /*!
702  * \brief Get the total degress of freedom of the IMU sensor.
703  *
704  * \return DoF
705  */
706  virtual int numDoFs()
707  {
708  return 6;
709  }
710 
711  /*!
712  * \brief Test if IMU has an accelerometer.
713  *
714  * \return Returns true or false.
715  */
716  virtual bool hasAccelerometer()
717  {
718  return true;
719  }
720 
721  /*!
722  * \brief Test if IMU has an gyroscope.
723  *
724  * \return Returns true or false.
725  */
726  virtual bool hasGyroscope()
727  {
728  return true;
729  }
730 
731  /*
732  * ----
733  * The CleanFlight defaults are acceptable. No special configuration
734  * nor tuning parameters are required todate.
735  * ----
736  */
737 
738  /*!
739  * \brief Read sensor identity values.
740  *
741  * \param [out] strIdent Hardware specific, formated identity string.
742  *
743  * \copydoc doc_return_std
744  */
745  virtual int readIdentity(std::string &strIdent);
746 
747  /*!
748  * \brief Read sensor and board raw IMU values.
749  *
750  * The CleanFlight Naze32 with MPU-6050 sensor has:
751  * \li accelerometer
752  * \li gyroscope
753  * \li attitude
754  *
755  * \copydoc doc_return_std
756  */
757  virtual int readRawImu();
758 
759  /*!
760  * \brief Read sensors inertia data.
761  *
762  * \copydoc doc_return_std
763  */
764  virtual int readRawInertia();
765 
766  /*!
767  * \brief Read board's calculated roll, pitch, and yaw raw values.
768  *
769  * \copydoc doc_return_std
770  */
771  virtual int readRawRollPitchYaw();
772 
773  /*!
774  * \brief Convert last read IMU values to International System of Units.
775  *
776  * \copydoc doc_return_std
777  */
778  virtual int convertRawToSI();
779 
780  protected:
781  /*!
782  * \brief Read CleanFlight board's identity values.
783  *
784  * MSP command/response.
785  *
786  * \param [out] ident Identity.
787  *
788  * \copydoc doc_return_std
789  */
790  int mspReadIdent(msp::MspIdent &ident);
791 
792  /*!
793  * \brief Read raw IMU data.
794  *
795  * Read data are updated to local class object member data.
796  *
797  * MSP command/response.
798  *
799  * \copydoc doc_return_std
800  */
801  int mspReadRawImu();
802 
803  /*!
804  * \brief
805  *
806  * MSP command/response.
807  *
808  * \param
809  *
810  * \copydoc doc_return_std
811  */
812  int mspReadAttitude();
813 
814  /*!
815  * \brief Send command to IMU.
816  *
817  * \param cmdId MSP command id.
818  * \param [in] cmdData Optional command data.
819  * \param lenData Length of command data. Zero if no data.
820  *
821  * \copydoc doc_return_std
822  */
823  int sendCmd(uint_t cmdId, byte_t cmdData[], size_t lenData);
824 
825  /*!
826  * \brief
827  *
828  * \param cmdId Response's associated MSP command id.
829  * \param [out] rspData Optional response data.
830  * \param lenData Length of response data. Zero if no data.
831  *
832  * \copydoc doc_return_std
833  */
834  int receiveRsp(uint_t cmdId, byte_t rspData[], size_t lenData);
835 
836  /*!
837  * \brief Attempt to resynchronize the serial communication between the
838  * host and the IMU.
839  */
840  void resyncComm();
841 
842  /*!
843  * \brief Flush serial input and output FIFOs, discarding all data.
844  *
845  * \param t Delay time (usec) before flushing. Zero means no delay.
846  */
847  void flush(uint_t t);
848 
849  /*!
850  * \brief Pack 16-bit signed value into buffer.
851  *
852  * Order is little-endian (LSB first).
853  *
854  * \param [in] val Value to pack.
855  * \param [out] buf Destination buffer.
856  *
857  * \return Number of bytes packed(2).
858  */
859  int pack16(int val, byte_t buf[])
860  {
861  return pack16((uint_t &)val, buf);
862  }
863 
864  /*!
865  * \brief Pack 16-bit unsigned value into buffer.
866  *
867  * Order is little-endian (LSB first).
868  *
869  * \param [in] val Value to pack.
870  * \param [out] buf Destination buffer.
871  *
872  * \return Number of bytes packed(2).
873  */
874  int pack16(uint_t val, byte_t buf[]);
875 
876  /*!
877  * \brief Unpack 16-bit signed value from buffer.
878  *
879  * Buffer is little-endian (LSB first).
880  *
881  * \param [in] buf Source buffer.
882  * \param [out] val Unpacked value.
883  *
884  * \return Number of bytes unpacked(2).
885  */
886  int unpack16(byte_t buf[], int &val);
887 
888  /*!
889  * \brief Unpack 16-bit unsigned value from buffer.
890  *
891  * Buffer is little-endian (LSB first).
892  *
893  * \param [in] buf Source buffer.
894  * \param [out] val Unpacked value.
895  *
896  * \return Number of bytes unpacked(2).
897  */
898  int unpack16(byte_t buf[], uint_t &val);
899 
900  /*!
901  * \brief Pack 32-bit signed value into buffer.
902  *
903  * Order is little-endian (LSB first).
904  *
905  * \param [in] val Value to pack.
906  * \param [out] buf Destination buffer.
907  *
908  * \return Number of bytes packed(4).
909  */
910  int pack32(int val, byte_t buf[])
911  {
912  return pack32((uint_t)val, buf);
913  }
914 
915  /*!
916  * \brief Pack 32-bit unsigned value into buffer.
917  *
918  * Order is little-endian (LSB first).
919  *
920  * \param [in] val Value to pack.
921  * \param [out] buf Destination buffer.
922  *
923  * \return Number of bytes packed(4).
924  */
925  int pack32(uint_t val, byte_t buf[]);
926 
927  /*!
928  * \brief Unpack 32-bit signed value from buffer.
929  *
930  * Buffer is little-endian (LSB first).
931  *
932  * \param [in] buf Source buffer.
933  * \param [out] val Unpacked value.
934  *
935  * \return Number of bytes unpacked(4).
936  */
937  int unpack32(byte_t buf[], int &val);
938 
939  /*!
940  * \brief Unpack 32-bit unsigned value from buffer.
941  *
942  * Buffer is little-endian (LSB first).
943  *
944  * \param [in] buf Source buffer.
945  * \param [out] val Unpacked value.
946  *
947  * \return Number of bytes unpacked(4).
948  */
949  int unpack32(byte_t buf[], uint_t &val);
950 
951  }; // class LaeImuCleanFlight;
952 #endif // SWIG
953 
954 #ifndef SWIG
955  } // namespace imu
956 
957 } // namespace sensor
958 #endif // SWIG
959 
960 
961 #endif // _LAE_IMU_H
virtual bool hasGyroscope()
Test if IMU has an gyroscope.
Definition: laeImu.h:726
Quaternion operator=(const Quaternion &rhs)
Assignment operator.
Definition: laeImu.cxx:77
static const double MspMpu6050RawToDegPerSec
MPU-6050 rotation raw value to degrees/second.
Definition: laeImu.h:653
uint_t m_uCaps
capabilities or&#39;d bits
Definition: laeImu.h:643
uint_t m_uMultiType
multiwii type
Definition: laeImu.h:641
Laelaps tuning data class.
Definition: laeTune.h:566
static const char *const MspPreamble
message preamble
Definition: laeImu.h:604
Laelaps robotic mobile platform full description class.
Definition: laeDesc.h:451
void lockIo()
Lock the shared I/O resource.
Definition: laeImu.h:541
void unlockIo()
Unlock the shared I/O resource.
Definition: laeImu.h:552
~Quaternion()
Destructor.
Definition: laeImu.h:137
std::string m_strIdent
IMU identity.
Definition: laeImu.h:496
int m_fd
opened device file descriptor
Definition: laeImu.h:499
pthread_mutex_t m_mutexIo
low-level I/O mutex
Definition: laeImu.h:530
static const double MspGToMPerSec2
standard g in m/s^2
Definition: laeImu.h:649
virtual int numDoFs()
Get the total degress of freedom of the IMU sensor.
Definition: laeImu.h:706
int pack16(int val, byte_t buf[])
Pack 16-bit signed value into buffer.
Definition: laeImu.h:859
yaw index
Definition: laeImu.h:107
roll index
Definition: laeImu.h:105
void unlockOp()
Unlock the extended operation.
Definition: laeImu.h:576
static const char *const MspCmdPreamble
command preamble
Definition: laeImu.h:607
RPY
Roll, pitch, and yaw indices.
Definition: laeImu.h:103
z axis index
Definition: laeImu.h:95
static const uint_t MspCmdIdIdent
read board/sensor idents
Definition: laeImu.h:631
int m_nBaudRate
device baudrate
Definition: laeImu.h:498
static const int MspRspMinLen
MspRspHdrLen+1 response min len.
Definition: laeImu.h:625
static const char *const MspDirTo
to imu
Definition: laeImu.h:605
virtual int numDoFs()
Get the total IMU degress of freedom.
Definition: laeImu.h:257
static const int MspRspHdrLen
response header len
Definition: laeImu.h:624
virtual bool hasMagnetometer()
Test if IMU has an magnetometer.
Definition: laeImu.h:287
virtual bool isOpen()
Check if IMU serial interface is open.
Definition: laeImu.h:222
static const char *const MspDirFrom
from imu
Definition: laeImu.h:606
pitch index
Definition: laeImu.h:106
static const int MspCmdMaxLen
response max len
Definition: laeImu.h:623
uint_t m_uMspVersion
MSP version.
Definition: laeImu.h:642
virtual bool hasGyroscope()
Test if IMU has an gyroscope.
Definition: laeImu.h:277
Board identity structure.
Definition: laeImu.h:638
static const char *const MspRspPreamble
response preamble
Definition: laeImu.h:608
Laelaps robotic base mobile platform description class interface.
static const int MspCmdMinLen
MspCmdHdrLen+1 command min len.
Definition: laeImu.h:622
const int NumOfAxes
maximum number of axes per sensor component.
Definition: laeImu.h:98
uint_t m_uFwVersion
firmware version
Definition: laeImu.h:640
static const double MspAttitudeRawToDeg
board computed attitude raw value to degreess
Definition: laeImu.h:655
static const int MspCmdHdrLen
command header len
Definition: laeImu.h:621
The sensor namespace.
Definition: laeImu.h:78
static const int MspFieldPosDataStart
data start field position
Definition: laeImu.h:616
void lockOp()
Lock the extended operation.
Definition: laeImu.h:565
Laelaps tuning.
static const double MspMpu6050RawToG
MPU-6050 acceleration raw value to g&#39;s.
Definition: laeImu.h:651
std::string getIdentity()
Get IMU identity.
Definition: laeImu.h:387
Quaternion class.
Definition: laeImu.h:118
virtual bool hasAccelerometer()
Test if IMU has an accelerometer.
Definition: laeImu.h:267
static const int MspFieldPosSize
data size field position
Definition: laeImu.h:614
int pack32(int val, byte_t buf[])
Pack 32-bit signed value into buffer.
Definition: laeImu.h:910
static const uint_t MspCmdIdRawImu
read raw imu data
Definition: laeImu.h:632
static const int MspFieldPosDir
dir field position
Definition: laeImu.h:613
pthread_mutex_t m_mutexOp
high-level operation mutex
Definition: laeImu.h:531
Quaternion m_quaternion
imu orientation (and robot)
Definition: laeImu.h:517
bool m_bBlackListed
IMU is [not] black listed.
Definition: laeImu.h:500
Quaternion()
Default constructor.
Definition: laeImu.h:129
x axis index
Definition: laeImu.h:93
void convertEuler(double phi, double theta, double psi)
Convert Euler angles to quaternion.
Definition: laeImu.cxx:95
y axis index
Definition: laeImu.h:94
static const uint_t MspCmdIdAttitude
read raw attitude data
Definition: laeImu.h:633
void convertTaitBryan(double roll, double pitch, double yaw)
Convert Tait-Bryan angles to quaternion.
Definition: laeImu.cxx:119
std::string getDevName()
Get IMU device name.
Definition: laeImu.h:396
static const int MspRspMaxLen
response max len
Definition: laeImu.h:626
virtual bool hasAccelerometer()
Test if IMU has an accelerometer.
Definition: laeImu.h:716
virtual bool isBlackListed()
Test if IMU is black listed.
Definition: laeImu.h:242
std::string m_strDevName
serial device name
Definition: laeImu.h:497
Top-level package include file.
static const int MspFieldPosCmdId
command id field position
Definition: laeImu.h:615
Axis
Axes indices.
Definition: laeImu.h:91
void clear()
Clear quaternion values.
Definition: laeImu.cxx:87