imu.hpp
Go to the documentation of this file.
1 /*
2  * imu.hpp
3  *
4  * Copyright (c) 2014 Kumar Robotics. All rights reserved.
5  *
6  * This file is part of galt.
7  *
8  * Created on: 2/6/2014
9  * Author: gareth
10  */
11 
12 #ifndef IMU_H_
13 #define IMU_H_
14 
15 #include <stdexcept>
16 #include <memory>
17 #include <string>
18 #include <deque>
19 #include <queue>
20 #include <vector>
21 #include <bitset>
22 #include <map>
23 #include <boost/bind.hpp>
24 
25 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ // will fail outside of gcc/clang
26 #define HOST_LITTLE_ENDIAN
27 #else
28 #define HOST_BIG_ENDIAN
29 #endif
30 
31 namespace imu_3dm_gx4 {
32 
43 class Imu {
44 public:
45  struct Packet {
46  static constexpr uint8_t kHeaderLength = 4;
47  static constexpr uint8_t kSyncMSB = 0x75;
48  static constexpr uint8_t kSyncLSB = 0x65;
49 
50  union {
51  struct {
52  uint8_t syncMSB;
53  uint8_t syncLSB;
54  };
55  uint16_t sync;
56  };
57 
58  uint8_t descriptor;
59  uint8_t length;
61  uint8_t payload[255];
63  union {
64  struct {
65  uint8_t checkMSB;
66  uint8_t checkLSB;
67  };
68  uint16_t checksum;
69  };
70 
74  bool isIMUData() const;
75 
79  bool isFilterData() const;
80 
88  int ackErrorCodeFor(const Packet &command) const;
89 
93  void calcChecksum();
94 
100  Packet(uint8_t desc = 0);
101 
106  std::string toString() const;
107  } __attribute__((packed));
108 
112  struct Info {
113  uint16_t firmwareVersion;
114  std::string modelName;
115  std::string modelNumber;
116  std::string serialNumber;
117  std::string lotNumber;
118  std::string deviceOptions;
119 
123  std::map<std::string, std::string> toMap() const;
124  };
125 
130  uint16_t modelNumber;
131  uint8_t selector;
132  uint32_t statusFlags;
133  uint32_t systemTimer;
134  uint32_t num1PPSPulses;
135  uint32_t last1PPSPulse;
140  uint32_t comBytesWritten;
141  uint32_t comBytesRead;
144  uint32_t usbBytesWritten;
145  uint32_t usbBytesRead;
150  uint32_t lastIMUMessage;
151 
155  std::map<std::string, unsigned int> toMap() const;
156 
157  } __attribute__((packed));
158 
162  struct IMUData {
163  enum {
164  Accelerometer = (1 << 0),
165  Gyroscope = (1 << 1),
166  Magnetometer = (1 << 2),
167  Barometer = (1 << 3),
168  };
169 
170  unsigned int fields;
172  float accel[3];
173  float gyro[3];
174  float mag[3];
175  float pressure;
177  IMUData() : fields(0) {}
178  };
179 
183  struct FilterData {
184  enum {
185  Quaternion = (1 << 0),
186  Bias = (1 << 1),
187  AngleUnertainty = (1 << 2),
188  BiasUncertainty = (1 << 3),
189  };
190 
191  unsigned int fields;
193  float quaternion[4];
194  uint16_t quaternionStatus;
196  float bias[3];
197  uint16_t biasStatus;
199  float angleUncertainty[3];
202  float biasUncertainty[3];
205  FilterData() : fields(0) {}
206  };
207 
208  /* Exceptions */
209 
213  struct command_error : public std::runtime_error {
214  command_error(const Packet &p, uint8_t code);
215 
216  private:
217  std::string generateString(const Packet &p, uint8_t code);
218  };
219 
223  struct io_error : public std::runtime_error {
224  io_error(const std::string &desc) : std::runtime_error(desc) {}
225  };
226 
231  struct timeout_error : public std::runtime_error {
232  timeout_error(bool write, unsigned int to);
233 
234  private:
235  std::string generateString(bool write, unsigned int to);
236  };
237 
243  Imu(const std::string &device, bool verbose);
244 
249  virtual ~Imu();
250 
256  void connect();
257 
261  void runOnce();
262 
267  void disconnect();
268 
280  void selectBaudRate(unsigned int baud);
281 
285  void ping();
286 
291  void idle(bool needReply = true);
292 
296  void resume();
297 
303 
308  void getIMUDataBaseRate(uint16_t &baseRate);
309 
314  void getFilterDataBaseRate(uint16_t &baseRate);
315 
321 
330  void setIMUDataRate(uint16_t decimation, const std::bitset<4> &sources);
331 
340  void setFilterDataRate(uint16_t decimation, const std::bitset<4> &sources);
341 
347  void enableMeasurements(bool accel, bool magnetometer);
348 
353  void enableBiasEstimation(bool enabled);
354 
360  void setHardIronOffset(float offset[3]);
361 
366  void setSoftIronMatrix(float matrix[9]);
367 
372  void enableIMUStream(bool enabled);
373 
379  void enableFilterStream(bool enabled);
380 
385  void setIMUDataCallback(const std::function<void(const Imu::IMUData &)> &);
386 
391  void
392  setFilterDataCallback(const std::function<void(const Imu::FilterData &)> &);
393 
394  private:
395  // non-copyable
396  Imu(const Imu &) = delete;
397  Imu &operator=(const Imu &) = delete;
398 
399  int pollInput(unsigned int to);
400 
401  std::size_t handleByte(const uint8_t& byte, bool& found);
402 
403  int handleRead(size_t);
404 
405  void processPacket();
406 
407  int writePacket(const Packet &p, unsigned int to);
408 
409  void sendPacket(const Packet &p, unsigned int to);
410 
411  void receiveResponse(const Packet &command, unsigned int to);
412 
413  void sendCommand(const Packet &p, bool readReply = true);
414 
415  bool termiosBaudRate(unsigned int baud);
416 
417  const std::string device_;
418  const bool verbose_;
419  int fd_;
420  unsigned int rwTimeout_;
421 
422  std::vector<uint8_t> buffer_;
423  std::deque<uint8_t> queue_;
425 
426  std::function<void(const Imu::IMUData &)>
428  std::function<void(const Imu::FilterData &)>
430 
431  enum { Idle = 0, Reading, } state_;
433 };
434 
435 } // imu_3dm_gx4
436 
437 #endif // IMU_H_
void setIMUDataRate(uint16_t decimation, const std::bitset< 4 > &sources)
setIMUDataRate Set imu data rate for different sources.
Definition: imu.cpp:741
Packet packet_
Definition: imu.hpp:432
void getDeviceInfo(Imu::Info &info)
getDeviceInfo Get hardware information about the device.
Definition: imu.cpp:664
command_error Generated when device replies with NACK.
Definition: imu.hpp:213
void setHardIronOffset(float offset[3])
setHardIronOffset Set the hard-iron bias vector for the magnetometer.
Definition: imu.cpp:830
std::string deviceOptions
Lot number - appears to be unused.
Definition: imu.hpp:118
std::string modelNumber
Model name.
Definition: imu.hpp:115
void runOnce()
runOnce Poll for input and read packets if available.
Definition: imu.cpp:520
void connect()
connect Open a file descriptor to the serial device.
Definition: imu.cpp:407
std::string modelName
Firmware version.
Definition: imu.hpp:114
io_error(const std::string &desc)
Definition: imu.hpp:224
static constexpr uint8_t kSyncLSB
Definition: imu.hpp:48
static constexpr uint8_t kSyncMSB
Definition: imu.hpp:47
int pollInput(unsigned int to)
Definition: imu.cpp:893
int writePacket(const Packet &p, unsigned int to)
Definition: imu.cpp:1126
void enableFilterStream(bool enabled)
enableFilterStream Enable/disable streaming of estimation filter data.
Definition: imu.cpp:869
const bool verbose_
Definition: imu.hpp:418
std::vector< uint8_t > buffer_
Definition: imu.hpp:422
io_error Generated when a low-level IO command fails.
Definition: imu.hpp:223
void enableBiasEstimation(bool enabled)
enableBiasEstimation Enable gyroscope bias estimation
Definition: imu.cpp:816
Imu & operator=(const Imu &)=delete
const std::string device_
Definition: imu.hpp:417
void calcChecksum()
Calculate the packet checksum. Sets the checksum variable.
Definition: imu.cpp:291
DiagnosticFields struct (See 3DM documentation for these fields)
Definition: imu.hpp:129
bool isIMUData() const
True if this packet corresponds to an imu data message.
Definition: imu.cpp:257
uint16_t biasUncertaintyStatus
Definition: imu.hpp:203
void getDiagnosticInfo(Imu::DiagnosticFields &fields)
getDiagnosticInfo Get diagnostic information from the IMU.
Definition: imu.cpp:719
Imu::DiagnosticFields fields
Definition: imu_3dm_gx4.cpp:28
ROSLIB_DECL std::string command(const std::string &cmd)
void sendPacket(const Packet &p, unsigned int to)
Definition: imu.cpp:1167
std::deque< uint8_t > queue_
Definition: imu.hpp:423
std::function< void(const Imu::FilterData &)> filterDataCallback_
Called with IMU data is ready.
Definition: imu.hpp:429
FilterData Estimator readings produced by the sensor.
Definition: imu.hpp:183
std::string lotNumber
Serial number.
Definition: imu.hpp:117
enum imu_3dm_gx4::Imu::@0 state_
Called when filter data is ready.
struct imu_3dm_gx4::Imu::Info __attribute__
bool termiosBaudRate(unsigned int baud)
Definition: imu.cpp:477
virtual ~Imu()
~Imu Destructor
Definition: imu.cpp:405
int ackErrorCodeFor(const Packet &command) const
Extract the ACK code from this packet.
Definition: imu.cpp:265
IMUData IMU readings produced by the sensor.
Definition: imu.hpp:162
void disconnect()
disconnect Close the file descriptor, sending the IDLE command first.
Definition: imu.cpp:468
void getFilterDataBaseRate(uint16_t &baseRate)
getFilterDataBaseRate Get the filter data base rate (should be 500)
Definition: imu.cpp:708
timeout_error Generated when read or write times out, usually indicates device hang up...
Definition: imu.hpp:231
std::map< std::string, unsigned int > toMap() const
Convert to map of human readable strings and integers.
void ping()
ping Ping the device.
Definition: imu.cpp:634
std::string toString() const
Make a &#39;human-readable&#39; version of the packet.
Definition: imu.cpp:322
uint8_t payload[255]
Definition: imu.hpp:61
Imu::Info info
Definition: imu_3dm_gx4.cpp:27
uint16_t angleUncertaintyStatus
Definition: imu.hpp:200
std::function< void(const Imu::IMUData &)> imuDataCallback_
Definition: imu.hpp:427
void receiveResponse(const Packet &command, unsigned int to)
Definition: imu.cpp:1176
Imu Interface to the Microstrain 3DM-GX4-25 IMU.
Definition: imu.hpp:43
bool isFilterData() const
True if this packet corresponds to a filter data message.
Definition: imu.cpp:261
std::size_t handleByte(const uint8_t &byte, bool &found)
Definition: imu.cpp:927
void setFilterDataCallback(const std::function< void(const Imu::FilterData &)> &)
Set the onboard filter data callback.
Definition: imu.cpp:888
static constexpr uint8_t kHeaderLength
Definition: imu.hpp:46
int handleRead(size_t)
Definition: imu.cpp:1004
void setSoftIronMatrix(float matrix[9])
setSoftIronMatrix Set the soft-iron matrix for the magnetometer.
Definition: imu.cpp:841
void setFilterDataRate(uint16_t decimation, const std::bitset< 4 > &sources)
setFilterDataRate Set estimator data rate for different sources.
Definition: imu.cpp:771
void resume()
resume Resume the device.
Definition: imu.cpp:654
unsigned int fields
Definition: imu.hpp:170
unsigned int rwTimeout_
Definition: imu.hpp:420
void getIMUDataBaseRate(uint16_t &baseRate)
getIMUDataBaseRate Get the imu data base rate (should be 1000)
Definition: imu.cpp:693
size_t dstIndex_
Definition: imu.hpp:424
void setIMUDataCallback(const std::function< void(const Imu::IMUData &)> &)
Set the IMU data callback.
Definition: imu.cpp:884
void processPacket()
Definition: imu.cpp:1038
void enableMeasurements(bool accel, bool magnetometer)
enableMeasurements Set which measurements to enable in the filter
Definition: imu.cpp:799
void selectBaudRate(unsigned int baud)
selectBaudRate Select baud rate.
Definition: imu.cpp:528
size_t srcIndex_
Definition: imu.hpp:424
void enableIMUStream(bool enabled)
enableIMUStream Enable/disable streaming of IMU data
Definition: imu.cpp:855
void sendCommand(const Packet &p, bool readReply=true)
Definition: imu.cpp:1212
Imu(const std::string &device, bool verbose)
Imu Constructor.
Definition: imu.cpp:396
uint16_t firmwareVersion
Definition: imu.hpp:113
Info Structure generated by the getDeviceInfo command.
Definition: imu.hpp:112
void idle(bool needReply=true)
idle Switch the device to idle mode.
Definition: imu.cpp:644
Packet(uint8_t desc=0)
Constructor.
Definition: imu.cpp:316
std::string serialNumber
Model number.
Definition: imu.hpp:116


thormang3_imu_3dm_gx4
Author(s): Gareth Cross, SCH
autogenerated on Mon Jun 10 2019 15:26:53