Contains added code for stm32-L432KC compatibility
Dependents: BNO080_stm32_compatible
BNO080.h@8:92d4109beb3c, 2019-07-30 (annotated)
- Committer:
- JesiMiranda
- Date:
- Tue Jul 30 16:44:07 2019 +0000
- Revision:
- 8:92d4109beb3c
- Parent:
- 3:197ad972fb7c
Jamie changed the cpp;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jamie Smith |
1:aac28ffd63ed | 1 | /* |
Jamie Smith |
1:aac28ffd63ed | 2 | * This is USC RPL's ARM MBed BNO080 IMU driver, by Jamie Smith. |
Jamie Smith |
1:aac28ffd63ed | 3 | * |
Jamie Smith |
1:aac28ffd63ed | 4 | * It is based on SparkFun and Nathan Seidle's Arduino driver for this chip, but is substantially rewritten and adapted. |
Jamie Smith |
1:aac28ffd63ed | 5 | * It also supports some extra features, such as setting the mounting orientation and |
Jamie Smith |
1:aac28ffd63ed | 6 | * enabling some additional data reports. |
Jamie Smith |
1:aac28ffd63ed | 7 | * |
Jamie Smith |
1:aac28ffd63ed | 8 | * This driver uses no dynamic allocation, but does allocate a couple hundred bytes of class variables as buffers. |
Jamie Smith |
1:aac28ffd63ed | 9 | * This should allow you to monitor its memory usage using MBed's size printout. |
Jamie Smith |
1:aac28ffd63ed | 10 | * |
Jamie Smith |
1:aac28ffd63ed | 11 | * The BNO080 is a very complex chip; it's capable of monitoring and controlling other sensors and making |
Jamie Smith |
1:aac28ffd63ed | 12 | * intelligent decisions and calculations using its data. Accordingly, the protocol for communicating with it |
Jamie Smith |
1:aac28ffd63ed | 13 | * is quite complex, and it took me quite a while to wrap my head around it. If you need to modify or debug |
Jamie Smith |
1:aac28ffd63ed | 14 | * this driver, look at the CPP file for an overview of the chip's communication protocol. |
Jamie Smith |
1:aac28ffd63ed | 15 | * |
Jamie Smith |
1:aac28ffd63ed | 16 | * Note: this driver only supports I2C. I attempted to create an SPI version, but as far as I can tell, |
Jamie Smith |
1:aac28ffd63ed | 17 | * the BNO's SPI interface has a bug that causes you to be unable to wake the chip from sleep in some conditions. |
Jamie Smith |
1:aac28ffd63ed | 18 | * Until this is fixed, SPI on it is virtually unusable. |
Jamie Smith |
1:aac28ffd63ed | 19 | */ |
Jamie Smith |
1:aac28ffd63ed | 20 | |
Jamie Smith |
1:aac28ffd63ed | 21 | #ifndef HAMSTER_BNO080_H |
Jamie Smith |
1:aac28ffd63ed | 22 | #define HAMSTER_BNO080_H |
Jamie Smith |
1:aac28ffd63ed | 23 | |
Jamie Smith |
1:aac28ffd63ed | 24 | #include <mbed.h> |
Jamie Smith |
1:aac28ffd63ed | 25 | #include <quaternion.h> |
Jamie Smith |
1:aac28ffd63ed | 26 | |
JesiMiranda | 8:92d4109beb3c | 27 | |
Jamie Smith |
1:aac28ffd63ed | 28 | #include "BNO080Constants.h" |
Jamie Smith |
1:aac28ffd63ed | 29 | |
Jamie Smith |
1:aac28ffd63ed | 30 | // useful define when working with orientation quaternions |
Jamie Smith |
3:197ad972fb7c | 31 | #define SQRT_2 1.414213562f |
Jamie Smith |
1:aac28ffd63ed | 32 | |
Jamie Smith |
1:aac28ffd63ed | 33 | /** |
Jamie Smith |
1:aac28ffd63ed | 34 | Class to drive the BNO080 9-axis IMU. |
Jamie Smith |
1:aac28ffd63ed | 35 | |
Jamie Smith |
1:aac28ffd63ed | 36 | There should be one instance of this class per IMU chip. I2C address and pin assignments are passed in the constructor. |
Jamie Smith |
1:aac28ffd63ed | 37 | */ |
Jamie Smith |
1:aac28ffd63ed | 38 | class BNO080 |
Jamie Smith |
1:aac28ffd63ed | 39 | { |
Jamie Smith |
1:aac28ffd63ed | 40 | /** |
Jamie Smith |
1:aac28ffd63ed | 41 | * Serial stream to print debug info to. Used for errors, and debugging output if debugging is enabled. |
Jamie Smith |
1:aac28ffd63ed | 42 | */ |
Jamie Smith |
1:aac28ffd63ed | 43 | Serial * _debugPort; |
Jamie Smith |
1:aac28ffd63ed | 44 | |
Jamie Smith |
1:aac28ffd63ed | 45 | /** |
Jamie Smith |
1:aac28ffd63ed | 46 | * I2C port object. Provides physical layer communications with the chip. |
Jamie Smith |
1:aac28ffd63ed | 47 | */ |
JesiMiranda | 8:92d4109beb3c | 48 | |
Jamie Smith |
1:aac28ffd63ed | 49 | I2C _i2cPort; |
JesiMiranda | 8:92d4109beb3c | 50 | //SoftI2C _i2cPort; |
JesiMiranda | 8:92d4109beb3c | 51 | |
Jamie Smith |
1:aac28ffd63ed | 52 | /// user defined port speed |
Jamie Smith |
1:aac28ffd63ed | 53 | int _i2cPortSpeed; |
Jamie Smith |
1:aac28ffd63ed | 54 | |
Jamie Smith |
1:aac28ffd63ed | 55 | /// i2c address of IMU (7 bits) |
Jamie Smith |
1:aac28ffd63ed | 56 | uint8_t _i2cAddress; |
Jamie Smith |
1:aac28ffd63ed | 57 | |
Jamie Smith |
1:aac28ffd63ed | 58 | /// Interrupt pin -- signals to the host that the IMU has data to send |
Jamie Smith |
1:aac28ffd63ed | 59 | DigitalIn _int; |
Jamie Smith |
1:aac28ffd63ed | 60 | |
Jamie Smith |
1:aac28ffd63ed | 61 | // Reset pin -- resets IMU when held low. |
Jamie Smith |
1:aac28ffd63ed | 62 | DigitalOut _rst; |
Jamie Smith |
1:aac28ffd63ed | 63 | |
Jamie Smith |
1:aac28ffd63ed | 64 | // packet storage |
Jamie Smith |
1:aac28ffd63ed | 65 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 66 | |
Jamie Smith |
1:aac28ffd63ed | 67 | #define SHTP_HEADER_SIZE 4 |
Jamie Smith |
1:aac28ffd63ed | 68 | |
Jamie Smith |
3:197ad972fb7c | 69 | // Arbitrarily chosen, but should hopefully be large enough for all packets we need. |
Jamie Smith |
3:197ad972fb7c | 70 | // If you enable lots of sensor reports and get an error, you might need to increase this. |
Jamie Smith |
1:aac28ffd63ed | 71 | #define STORED_PACKET_SIZE 128 |
Jamie Smith |
1:aac28ffd63ed | 72 | |
Jamie Smith |
1:aac28ffd63ed | 73 | /// Each SHTP packet has a header of 4 uint8_ts |
Jamie Smith |
1:aac28ffd63ed | 74 | uint8_t shtpHeader[SHTP_HEADER_SIZE]; |
Jamie Smith |
1:aac28ffd63ed | 75 | |
Jamie Smith |
1:aac28ffd63ed | 76 | /// Stores data contained in each packet. Packets can contain an arbitrary amount of data, but |
Jamie Smith |
1:aac28ffd63ed | 77 | /// rarely get over a hundred bytes unless you have a million sensor reports enabled. |
Jamie Smith |
1:aac28ffd63ed | 78 | /// The only long packets we actually care about are batched sensor data packets. |
Jamie Smith |
1:aac28ffd63ed | 79 | uint8_t shtpData[STORED_PACKET_SIZE]; |
JesiMiranda | 8:92d4109beb3c | 80 | |
JesiMiranda | 8:92d4109beb3c | 81 | #define READ_BUFFER_SIZE 512 |
JesiMiranda | 8:92d4109beb3c | 82 | uint8_t readBuffer[READ_BUFFER_SIZE]; |
Jamie Smith |
1:aac28ffd63ed | 83 | |
Jamie Smith |
1:aac28ffd63ed | 84 | /// Length of packet that was received into buffer. Does NOT include header bytes. |
Jamie Smith |
1:aac28ffd63ed | 85 | uint16_t packetLength; |
Jamie Smith |
1:aac28ffd63ed | 86 | |
Jamie Smith |
1:aac28ffd63ed | 87 | /// Current sequence number for each channel, incremented after transmission. |
Jamie Smith |
2:2269b723d16a | 88 | uint8_t sequenceNumber[6]; |
Jamie Smith |
1:aac28ffd63ed | 89 | |
Jamie Smith |
1:aac28ffd63ed | 90 | /// Commands have a seqNum as well. These are inside command packet, the header uses its own seqNum per channel |
Jamie Smith |
2:2269b723d16a | 91 | uint8_t commandSequenceNumber; |
Jamie Smith |
1:aac28ffd63ed | 92 | |
Jamie Smith |
1:aac28ffd63ed | 93 | |
Jamie Smith |
1:aac28ffd63ed | 94 | // frs metadata |
Jamie Smith |
1:aac28ffd63ed | 95 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 96 | |
Jamie Smith |
1:aac28ffd63ed | 97 | /// Record ID of the metadata record currently stored in the metadataRecord[] buffer. |
Jamie Smith |
1:aac28ffd63ed | 98 | /// Used so that we can avoid requerying the FRS record if we need to make multiple metadata reads |
Jamie Smith |
1:aac28ffd63ed | 99 | /// in succession. |
Jamie Smith |
1:aac28ffd63ed | 100 | uint16_t bufferMetadataRecord; |
Jamie Smith |
1:aac28ffd63ed | 101 | |
Jamie Smith |
1:aac28ffd63ed | 102 | /// currently we only need the first 10 words of the metadata |
Jamie Smith |
1:aac28ffd63ed | 103 | #define METADATA_BUFFER_LEN 10 |
Jamie Smith |
1:aac28ffd63ed | 104 | |
Jamie Smith |
1:aac28ffd63ed | 105 | /// Buffer for current metadata record. |
Jamie Smith |
2:2269b723d16a | 106 | uint32_t metadataRecord[METADATA_BUFFER_LEN]; |
Jamie Smith |
1:aac28ffd63ed | 107 | |
Jamie Smith |
1:aac28ffd63ed | 108 | // data storage |
Jamie Smith |
1:aac28ffd63ed | 109 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 110 | |
Jamie Smith |
1:aac28ffd63ed | 111 | // 1 larger than the largest sensor report ID |
Jamie Smith |
1:aac28ffd63ed | 112 | #define STATUS_ARRAY_LEN MAX_SENSOR_REPORTID + 1 |
Jamie Smith |
1:aac28ffd63ed | 113 | |
Jamie Smith |
1:aac28ffd63ed | 114 | /// stores status of each sensor, indexed by report ID |
Jamie Smith |
2:2269b723d16a | 115 | uint8_t reportStatus[STATUS_ARRAY_LEN]; |
Jamie Smith |
1:aac28ffd63ed | 116 | |
Jamie Smith |
1:aac28ffd63ed | 117 | /// stores whether a sensor has been updated since the last call to hasNewData() |
Jamie Smith |
2:2269b723d16a | 118 | bool reportHasBeenUpdated[STATUS_ARRAY_LEN]; |
Jamie Smith |
1:aac28ffd63ed | 119 | |
Jamie Smith |
1:aac28ffd63ed | 120 | public: |
Jamie Smith |
1:aac28ffd63ed | 121 | |
Jamie Smith |
1:aac28ffd63ed | 122 | // list of reports |
Jamie Smith |
1:aac28ffd63ed | 123 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 124 | |
Jamie Smith |
1:aac28ffd63ed | 125 | /// List of all sensor reports that the IMU supports. |
Jamie Smith |
2:2269b723d16a | 126 | enum Report |
Jamie Smith |
1:aac28ffd63ed | 127 | { |
Jamie Smith |
1:aac28ffd63ed | 128 | /** |
Jamie Smith |
1:aac28ffd63ed | 129 | * Total acceleration of the IMU in world space. |
Jamie Smith |
1:aac28ffd63ed | 130 | * See BNO datasheet section 2.1.1 |
Jamie Smith |
1:aac28ffd63ed | 131 | */ |
Jamie Smith |
1:aac28ffd63ed | 132 | TOTAL_ACCELERATION = SENSOR_REPORTID_ACCELEROMETER, |
Jamie Smith |
1:aac28ffd63ed | 133 | |
Jamie Smith |
1:aac28ffd63ed | 134 | /** |
Jamie Smith |
1:aac28ffd63ed | 135 | * Acceleration of the IMU not including the acceleration of gravity. |
Jamie Smith |
1:aac28ffd63ed | 136 | * See BNO datasheet section 2.1.1 |
Jamie Smith |
1:aac28ffd63ed | 137 | */ |
Jamie Smith |
1:aac28ffd63ed | 138 | LINEAR_ACCELERATION = SENSOR_REPORTID_LINEAR_ACCELERATION, |
Jamie Smith |
1:aac28ffd63ed | 139 | |
Jamie Smith |
1:aac28ffd63ed | 140 | /** |
Jamie Smith |
1:aac28ffd63ed | 141 | * Acceleration of gravity felt by the IMU. |
Jamie Smith |
1:aac28ffd63ed | 142 | * See BNO datasheet section 2.1.1 |
Jamie Smith |
1:aac28ffd63ed | 143 | */ |
Jamie Smith |
1:aac28ffd63ed | 144 | GRAVITY_ACCELERATION = SENSOR_REPORTID_GRAVITY, |
Jamie Smith |
1:aac28ffd63ed | 145 | |
Jamie Smith |
1:aac28ffd63ed | 146 | /** |
Jamie Smith |
1:aac28ffd63ed | 147 | * (calibrated) gyroscope reading of the rotational speed of the IMU. |
Jamie Smith |
1:aac28ffd63ed | 148 | * See BNO datasheet section 2.1.2 |
Jamie Smith |
1:aac28ffd63ed | 149 | */ |
Jamie Smith |
1:aac28ffd63ed | 150 | GYROSCOPE = SENSOR_REPORTID_GYROSCOPE_CALIBRATED, |
Jamie Smith |
1:aac28ffd63ed | 151 | |
Jamie Smith |
1:aac28ffd63ed | 152 | /** |
Jamie Smith |
1:aac28ffd63ed | 153 | * (calibrated) reading of Earth's magnetic field levels. |
Jamie Smith |
1:aac28ffd63ed | 154 | * See BNO datasheet section 2.1.3 |
Jamie Smith |
1:aac28ffd63ed | 155 | */ |
Jamie Smith |
1:aac28ffd63ed | 156 | MAG_FIELD = SENSOR_REPORTID_MAGNETIC_FIELD_CALIBRATED, |
Jamie Smith |
1:aac28ffd63ed | 157 | |
Jamie Smith |
1:aac28ffd63ed | 158 | /** |
Jamie Smith |
1:aac28ffd63ed | 159 | * Uncalibrated reading of magnetic field levels, without any hard iron offsets applied |
Jamie Smith |
1:aac28ffd63ed | 160 | * See BNO datasheet section 2.1.3 |
Jamie Smith |
1:aac28ffd63ed | 161 | */ |
Jamie Smith |
1:aac28ffd63ed | 162 | MAG_FIELD_UNCALIBRATED = SENSOR_REPORTID_MAGNETIC_FIELD_UNCALIBRATED, |
Jamie Smith |
1:aac28ffd63ed | 163 | |
Jamie Smith |
1:aac28ffd63ed | 164 | /** |
Jamie Smith |
1:aac28ffd63ed | 165 | * Fused reading of the IMU's rotation in space using all three sensors. This is the most accurate reading |
Jamie Smith |
1:aac28ffd63ed | 166 | * of absolute orientation that the IMU can provide. |
Jamie Smith |
1:aac28ffd63ed | 167 | * See BNO datasheet section 2.2.4 |
Jamie Smith |
1:aac28ffd63ed | 168 | */ |
Jamie Smith |
1:aac28ffd63ed | 169 | ROTATION = SENSOR_REPORTID_ROTATION_VECTOR, |
Jamie Smith |
1:aac28ffd63ed | 170 | |
Jamie Smith |
1:aac28ffd63ed | 171 | /** |
Jamie Smith |
1:aac28ffd63ed | 172 | * Fused reading of rotation from accelerometer and magnetometer readings. This report is designed to decrease |
Jamie Smith |
1:aac28ffd63ed | 173 | * power consumption (by turning off the gyroscope) in exchange for reduced responsiveness. |
Jamie Smith |
1:aac28ffd63ed | 174 | */ |
Jamie Smith |
1:aac28ffd63ed | 175 | GEOMAGNETIC_ROTATION = SENSOR_REPORTID_GEOMAGNETIC_ROTATION_VECTOR, |
Jamie Smith |
1:aac28ffd63ed | 176 | |
Jamie Smith |
1:aac28ffd63ed | 177 | /** |
Jamie Smith |
1:aac28ffd63ed | 178 | * Fused reading of the IMU's rotation in space. Unlike the regular rotation vector, the Game Rotation Vector |
Jamie Smith |
1:aac28ffd63ed | 179 | * is not referenced against the magnetic field and the "zero yaw" point is arbitrary. |
Jamie Smith |
1:aac28ffd63ed | 180 | * See BNO datasheet section 2.2.2 |
Jamie Smith |
1:aac28ffd63ed | 181 | */ |
Jamie Smith |
1:aac28ffd63ed | 182 | GAME_ROTATION = SENSOR_REPORTID_GAME_ROTATION_VECTOR, |
Jamie Smith |
1:aac28ffd63ed | 183 | |
Jamie Smith |
1:aac28ffd63ed | 184 | /** |
Jamie Smith |
1:aac28ffd63ed | 185 | * Detects a user tapping on the device containing the IMU. |
Jamie Smith |
1:aac28ffd63ed | 186 | * See BNO datasheet section 2.4.2 |
Jamie Smith |
1:aac28ffd63ed | 187 | */ |
Jamie Smith |
1:aac28ffd63ed | 188 | TAP_DETECTOR = SENSOR_REPORTID_TAP_DETECTOR, |
Jamie Smith |
1:aac28ffd63ed | 189 | |
Jamie Smith |
1:aac28ffd63ed | 190 | /** |
Jamie Smith |
1:aac28ffd63ed | 191 | * Detects whether the device is on a table, being held stably, or being moved. |
Jamie Smith |
1:aac28ffd63ed | 192 | * See BNO datasheet section 2.4.1 |
Jamie Smith |
1:aac28ffd63ed | 193 | */ |
Jamie Smith |
1:aac28ffd63ed | 194 | STABILITY_CLASSIFIER = SENSOR_REPORTID_STABILITY_CLASSIFIER, |
Jamie Smith |
1:aac28ffd63ed | 195 | |
Jamie Smith |
1:aac28ffd63ed | 196 | /** |
Jamie Smith |
1:aac28ffd63ed | 197 | * Detects a user taking a step with the IMU worn on their person. |
Jamie Smith |
1:aac28ffd63ed | 198 | * See BNO datasheet section 2.4.3 |
Jamie Smith |
1:aac28ffd63ed | 199 | */ |
Jamie Smith |
1:aac28ffd63ed | 200 | STEP_DETECTOR = SENSOR_REPORTID_STEP_DETECTOR, |
Jamie Smith |
1:aac28ffd63ed | 201 | |
Jamie Smith |
1:aac28ffd63ed | 202 | /** |
Jamie Smith |
1:aac28ffd63ed | 203 | * Detects how many steps a user has taken. |
Jamie Smith |
1:aac28ffd63ed | 204 | * See BNO datasheet section 2.4.4 |
Jamie Smith |
1:aac28ffd63ed | 205 | */ |
Jamie Smith |
1:aac28ffd63ed | 206 | STEP_COUNTER = SENSOR_REPORTID_STEP_COUNTER, |
Jamie Smith |
1:aac28ffd63ed | 207 | |
Jamie Smith |
1:aac28ffd63ed | 208 | /** |
Jamie Smith |
1:aac28ffd63ed | 209 | * Detects when the IMU has made a "significant" motion, defined as moving a few steps and/or accelerating significantly. |
Jamie Smith |
1:aac28ffd63ed | 210 | * |
Jamie Smith |
1:aac28ffd63ed | 211 | * NOTE: this report automatically disables itself after sending a report, so you'll have to reenable it each time a motion i s detected. |
Jamie Smith |
1:aac28ffd63ed | 212 | * See BNO datasheet section 2.4.6 |
Jamie Smith |
1:aac28ffd63ed | 213 | */ |
Jamie Smith |
1:aac28ffd63ed | 214 | SIGNIFICANT_MOTION = SENSOR_REPORTID_SIGNIFICANT_MOTION, |
Jamie Smith |
1:aac28ffd63ed | 215 | |
Jamie Smith |
1:aac28ffd63ed | 216 | /** |
Jamie Smith |
1:aac28ffd63ed | 217 | * Detects when the IMU is being shaken. |
Jamie Smith |
1:aac28ffd63ed | 218 | * See BNO datasheet section 2.4.7 |
Jamie Smith |
1:aac28ffd63ed | 219 | */ |
Jamie Smith |
1:aac28ffd63ed | 220 | SHAKE_DETECTOR = SENSOR_REPORTID_SHAKE_DETECTOR |
Jamie Smith |
1:aac28ffd63ed | 221 | }; |
Jamie Smith |
1:aac28ffd63ed | 222 | |
Jamie Smith |
1:aac28ffd63ed | 223 | // data variables to read reports from |
Jamie Smith |
1:aac28ffd63ed | 224 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 225 | |
Jamie Smith |
1:aac28ffd63ed | 226 | // @{ |
Jamie Smith |
1:aac28ffd63ed | 227 | /// Version info read from the IMU when it starts up |
Jamie Smith |
1:aac28ffd63ed | 228 | uint8_t majorSoftwareVersion; |
Jamie Smith |
1:aac28ffd63ed | 229 | uint8_t minorSoftwareVersion; |
Jamie Smith |
1:aac28ffd63ed | 230 | uint16_t patchSoftwareVersion; |
Jamie Smith |
1:aac28ffd63ed | 231 | uint32_t partNumber; |
Jamie Smith |
1:aac28ffd63ed | 232 | uint32_t buildNumber; |
Jamie Smith |
1:aac28ffd63ed | 233 | // @} |
Jamie Smith |
1:aac28ffd63ed | 234 | |
Jamie Smith |
1:aac28ffd63ed | 235 | |
Jamie Smith |
1:aac28ffd63ed | 236 | /** |
Jamie Smith |
1:aac28ffd63ed | 237 | * Readout from Accleration report. |
Jamie Smith |
1:aac28ffd63ed | 238 | * Represents total acceleration in m/s^2 felt by the BNO's accelerometer. |
Jamie Smith |
1:aac28ffd63ed | 239 | */ |
Jamie Smith |
1:aac28ffd63ed | 240 | TVector3 totalAcceleration; |
Jamie Smith |
1:aac28ffd63ed | 241 | |
Jamie Smith |
1:aac28ffd63ed | 242 | /** |
Jamie Smith |
1:aac28ffd63ed | 243 | * Readout from Linear Acceleration report. |
Jamie Smith |
1:aac28ffd63ed | 244 | * Represents acceleration felt in m/s^2 by the BNO's accelerometer not including the force of gravity. |
Jamie Smith |
1:aac28ffd63ed | 245 | */ |
Jamie Smith |
1:aac28ffd63ed | 246 | TVector3 linearAcceleration; |
Jamie Smith |
1:aac28ffd63ed | 247 | |
Jamie Smith |
1:aac28ffd63ed | 248 | /** |
Jamie Smith |
1:aac28ffd63ed | 249 | * Readout from Gravity report. |
Jamie Smith |
1:aac28ffd63ed | 250 | * Represents the force of gravity in m/s^2 felt by the BNO's accelerometer. |
Jamie Smith |
1:aac28ffd63ed | 251 | */ |
Jamie Smith |
1:aac28ffd63ed | 252 | TVector3 gravityAcceleration; |
Jamie Smith |
1:aac28ffd63ed | 253 | |
Jamie Smith |
1:aac28ffd63ed | 254 | /** |
Jamie Smith |
1:aac28ffd63ed | 255 | * Readout from Calibrated Gyroscope report |
Jamie Smith |
1:aac28ffd63ed | 256 | * Represents the angular velocities of the chip in rad/s in the X, Y, and Z axes |
Jamie Smith |
1:aac28ffd63ed | 257 | */ |
Jamie Smith |
1:aac28ffd63ed | 258 | TVector3 gyroRotation; |
Jamie Smith |
1:aac28ffd63ed | 259 | |
Jamie Smith |
1:aac28ffd63ed | 260 | /** |
Jamie Smith |
1:aac28ffd63ed | 261 | * Readout from the Magnetic Field Calibrated report. |
Jamie Smith |
1:aac28ffd63ed | 262 | * Represents the magnetic field read by the chip in uT in the X, Y, and Z axes |
Jamie Smith |
1:aac28ffd63ed | 263 | */ |
Jamie Smith |
1:aac28ffd63ed | 264 | TVector3 magField; |
Jamie Smith |
1:aac28ffd63ed | 265 | |
Jamie Smith |
1:aac28ffd63ed | 266 | /** |
Jamie Smith |
1:aac28ffd63ed | 267 | * Readout from the Magnetic Field Uncalibrated report. |
Jamie Smith |
1:aac28ffd63ed | 268 | * Represents the magnetic field read by the chip in uT in the X, Y, and Z axes, without hard iron offsets applied |
Jamie Smith |
1:aac28ffd63ed | 269 | */ |
Jamie Smith |
1:aac28ffd63ed | 270 | TVector3 magFieldUncalibrated; |
Jamie Smith |
1:aac28ffd63ed | 271 | |
Jamie Smith |
1:aac28ffd63ed | 272 | /** |
Jamie Smith |
1:aac28ffd63ed | 273 | * Auxiliary readout from the Magnetic Field Uncalibrated report. |
Jamie Smith |
1:aac28ffd63ed | 274 | * Represents the hard iron offsets that the chip is using in each axis in uT. |
Jamie Smith |
1:aac28ffd63ed | 275 | */ |
Jamie Smith |
1:aac28ffd63ed | 276 | TVector3 hardIronOffset; |
Jamie Smith |
1:aac28ffd63ed | 277 | |
Jamie Smith |
1:aac28ffd63ed | 278 | /** |
Jamie Smith |
1:aac28ffd63ed | 279 | * Readout from the Rotation Vector report. |
Jamie Smith |
1:aac28ffd63ed | 280 | * Represents the rotation of the IMU (relative to magnetic north) in radians. |
Jamie Smith |
1:aac28ffd63ed | 281 | */ |
Jamie Smith |
1:aac28ffd63ed | 282 | Quaternion rotationVector; |
Jamie Smith |
1:aac28ffd63ed | 283 | |
Jamie Smith |
1:aac28ffd63ed | 284 | /** |
Jamie Smith |
1:aac28ffd63ed | 285 | * Auxiliary accuracy readout from the Rotation Vector report. |
Jamie Smith |
1:aac28ffd63ed | 286 | * Represents the estimated accuracy of the rotation vector in radians. |
Jamie Smith |
1:aac28ffd63ed | 287 | */ |
Jamie Smith |
1:aac28ffd63ed | 288 | float rotationAccuracy; |
Jamie Smith |
1:aac28ffd63ed | 289 | |
Jamie Smith |
1:aac28ffd63ed | 290 | /** |
Jamie Smith |
1:aac28ffd63ed | 291 | * Readout from the Game Rotation Vector report. |
Jamie Smith |
1:aac28ffd63ed | 292 | * Represents the rotation of the IMU in radians. Unlike the regular rotation vector, the Game Rotation Vector |
Jamie Smith |
1:aac28ffd63ed | 293 | * is not referenced against the magnetic field and the "zero yaw" point is arbitrary. |
Jamie Smith |
1:aac28ffd63ed | 294 | */ |
Jamie Smith |
1:aac28ffd63ed | 295 | Quaternion gameRotationVector; |
Jamie Smith |
1:aac28ffd63ed | 296 | |
Jamie Smith |
1:aac28ffd63ed | 297 | /** |
Jamie Smith |
1:aac28ffd63ed | 298 | * Readout from the Geomagnetic Rotation Vector report. |
Jamie Smith |
1:aac28ffd63ed | 299 | * Represents the geomagnetic rotation of the IMU (relative to magnetic north) in radians. |
Jamie Smith |
1:aac28ffd63ed | 300 | */ |
Jamie Smith |
1:aac28ffd63ed | 301 | Quaternion geomagneticRotationVector; |
Jamie Smith |
1:aac28ffd63ed | 302 | |
Jamie Smith |
1:aac28ffd63ed | 303 | /** |
Jamie Smith |
1:aac28ffd63ed | 304 | * Auxiliary accuracy readout from the Geomagnetic Rotation Vector report. |
Jamie Smith |
1:aac28ffd63ed | 305 | * Represents the estimated accuracy of the rotation vector in radians. |
Jamie Smith |
1:aac28ffd63ed | 306 | */ |
Jamie Smith |
1:aac28ffd63ed | 307 | float geomagneticRotationAccuracy; |
Jamie Smith |
1:aac28ffd63ed | 308 | |
Jamie Smith |
1:aac28ffd63ed | 309 | /** |
Jamie Smith |
1:aac28ffd63ed | 310 | * Tap readout from the Tap Detector report. This flag is set to true whenever a tap is detected, and you should |
Jamie Smith |
1:aac28ffd63ed | 311 | * manually clear it when you have processed the tap. |
Jamie Smith |
1:aac28ffd63ed | 312 | */ |
Jamie Smith |
1:aac28ffd63ed | 313 | bool tapDetected; |
Jamie Smith |
1:aac28ffd63ed | 314 | |
Jamie Smith |
1:aac28ffd63ed | 315 | /** |
Jamie Smith |
1:aac28ffd63ed | 316 | * Whether the last tap detected was a single or double tap. |
Jamie Smith |
1:aac28ffd63ed | 317 | */ |
Jamie Smith |
1:aac28ffd63ed | 318 | bool doubleTap; |
Jamie Smith |
1:aac28ffd63ed | 319 | |
Jamie Smith |
1:aac28ffd63ed | 320 | /** |
Jamie Smith |
1:aac28ffd63ed | 321 | * Enum to represent the different stability types. |
Jamie Smith |
1:aac28ffd63ed | 322 | * |
Jamie Smith |
1:aac28ffd63ed | 323 | * See BNO datasheet section 2.4.1 and SH-2 section 6.5.31.2 for details. |
Jamie Smith |
1:aac28ffd63ed | 324 | */ |
Jamie Smith |
2:2269b723d16a | 325 | enum Stability |
Jamie Smith |
1:aac28ffd63ed | 326 | { |
Jamie Smith |
1:aac28ffd63ed | 327 | /// Unknown stability type. |
Jamie Smith |
1:aac28ffd63ed | 328 | UNKNOWN = 0, |
Jamie Smith |
1:aac28ffd63ed | 329 | |
Jamie Smith |
1:aac28ffd63ed | 330 | /// At rest on a stable surface with very little motion |
Jamie Smith |
1:aac28ffd63ed | 331 | ON_TABLE = 1, |
Jamie Smith |
1:aac28ffd63ed | 332 | |
Jamie Smith |
1:aac28ffd63ed | 333 | /// Motion is stable, but the duration requirement for stability has not been met. |
Jamie Smith |
1:aac28ffd63ed | 334 | /// Can only occur during gyroscope calibration (why? beats me!) |
Jamie Smith |
1:aac28ffd63ed | 335 | STATIONARY = 2, |
Jamie Smith |
1:aac28ffd63ed | 336 | |
Jamie Smith |
1:aac28ffd63ed | 337 | /// Stable (has been below the acceleration threshold for the required duration) |
Jamie Smith |
1:aac28ffd63ed | 338 | STABLE = 3, |
Jamie Smith |
1:aac28ffd63ed | 339 | |
Jamie Smith |
1:aac28ffd63ed | 340 | /// IMU is moving. |
Jamie Smith |
1:aac28ffd63ed | 341 | MOTION = 4 |
Jamie Smith |
1:aac28ffd63ed | 342 | }; |
Jamie Smith |
1:aac28ffd63ed | 343 | |
Jamie Smith |
1:aac28ffd63ed | 344 | /** |
Jamie Smith |
1:aac28ffd63ed | 345 | * Readout from the stability classifier. |
Jamie Smith |
1:aac28ffd63ed | 346 | * Current stability status of the IMU. |
Jamie Smith |
1:aac28ffd63ed | 347 | */ |
Jamie Smith |
2:2269b723d16a | 348 | Stability stability; |
Jamie Smith |
1:aac28ffd63ed | 349 | |
Jamie Smith |
1:aac28ffd63ed | 350 | /** |
Jamie Smith |
1:aac28ffd63ed | 351 | * Readout from the Step Detector report. This flag is set to true whenever a step is detected, and you should |
Jamie Smith |
1:aac28ffd63ed | 352 | * manually clear it when you have processed the step. |
Jamie Smith |
1:aac28ffd63ed | 353 | */ |
Jamie Smith |
2:2269b723d16a | 354 | bool stepDetected; |
Jamie Smith |
1:aac28ffd63ed | 355 | |
Jamie Smith |
1:aac28ffd63ed | 356 | /** |
Jamie Smith |
1:aac28ffd63ed | 357 | * Readout from the Step Counter report. This count increases as the user takes steps, but can also decrease |
Jamie Smith |
1:aac28ffd63ed | 358 | * if the IMU decides that a motion was not a step. |
Jamie Smith |
1:aac28ffd63ed | 359 | */ |
Jamie Smith |
1:aac28ffd63ed | 360 | uint16_t stepCount; |
Jamie Smith |
1:aac28ffd63ed | 361 | |
Jamie Smith |
1:aac28ffd63ed | 362 | /** |
Jamie Smith |
1:aac28ffd63ed | 363 | * Readout from the Significant Motion Detector report. This flag is set to true whenever significant motion is detected, and you should |
Jamie Smith |
1:aac28ffd63ed | 364 | * manually clear it when you have processed the event. |
Jamie Smith |
1:aac28ffd63ed | 365 | */ |
Jamie Smith |
2:2269b723d16a | 366 | bool significantMotionDetected; |
Jamie Smith |
1:aac28ffd63ed | 367 | |
Jamie Smith |
1:aac28ffd63ed | 368 | /** |
Jamie Smith |
1:aac28ffd63ed | 369 | * Readout from the Shake Detector report. This flag is set to true whenever shaking is detected, and you should |
Jamie Smith |
1:aac28ffd63ed | 370 | * manually clear it when you have processed the event. |
Jamie Smith |
1:aac28ffd63ed | 371 | */ |
Jamie Smith |
2:2269b723d16a | 372 | bool shakeDetected; |
Jamie Smith |
1:aac28ffd63ed | 373 | |
Jamie Smith |
1:aac28ffd63ed | 374 | // @{ |
Jamie Smith |
1:aac28ffd63ed | 375 | /// The axis/axes that shaking was detected in in the latest shaking report. |
Jamie Smith |
2:2269b723d16a | 376 | bool xAxisShake; |
Jamie Smith |
2:2269b723d16a | 377 | bool yAxisShake; |
Jamie Smith |
2:2269b723d16a | 378 | bool zAxisShake; |
Jamie Smith |
1:aac28ffd63ed | 379 | // @} |
Jamie Smith |
1:aac28ffd63ed | 380 | |
Jamie Smith |
1:aac28ffd63ed | 381 | // Management functions |
Jamie Smith |
1:aac28ffd63ed | 382 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 383 | |
Jamie Smith |
1:aac28ffd63ed | 384 | /** |
Jamie Smith |
1:aac28ffd63ed | 385 | * Construct a BNO080, providing pins and parameters. |
Jamie Smith |
1:aac28ffd63ed | 386 | * |
Jamie Smith |
1:aac28ffd63ed | 387 | * This doesn't actally initialize the chip, you will need to call begin() for that. |
Jamie Smith |
1:aac28ffd63ed | 388 | * |
Jamie Smith |
1:aac28ffd63ed | 389 | * NOTE: while some schematics tell you to connect the BOOTN pin to the processor, this driver does not use or require it. |
Jamie Smith |
1:aac28ffd63ed | 390 | * Just tie it to VCC per the datasheet. |
Jamie Smith |
1:aac28ffd63ed | 391 | * |
Jamie Smith |
1:aac28ffd63ed | 392 | * @param debugPort Serial port to write output to. Cannot be nullptr. |
Jamie Smith |
1:aac28ffd63ed | 393 | * @param user_SDApin Hardware I2C SDA pin connected to the IMU |
Jamie Smith |
1:aac28ffd63ed | 394 | * @param user_SCLpin Hardware I2C SCL pin connected to the IMU |
Jamie Smith |
1:aac28ffd63ed | 395 | * @param user_INTPin Input pin connected to HINTN |
Jamie Smith |
1:aac28ffd63ed | 396 | * @param user_RSTPin Output pin connected to NRST |
Jamie Smith |
1:aac28ffd63ed | 397 | * @param i2cAddress I2C address. The BNO defaults to 0x4a, but can also be set to 0x4b via a pin. |
Jamie Smith |
1:aac28ffd63ed | 398 | * @param i2cPortSpeed I2C frequency. The BNO's max is 400kHz. |
Jamie Smith |
1:aac28ffd63ed | 399 | */ |
Jamie Smith |
1:aac28ffd63ed | 400 | BNO080(Serial *debugPort, |
Jamie Smith |
1:aac28ffd63ed | 401 | PinName user_SDApin, |
Jamie Smith |
1:aac28ffd63ed | 402 | PinName user_SCLpin, |
Jamie Smith |
1:aac28ffd63ed | 403 | PinName user_INTPin, |
Jamie Smith |
1:aac28ffd63ed | 404 | PinName user_RSTPin, |
Jamie Smith |
1:aac28ffd63ed | 405 | uint8_t i2cAddress=0x4a, |
Jamie Smith |
3:197ad972fb7c | 406 | int i2cPortSpeed=100000); |
Jamie Smith |
1:aac28ffd63ed | 407 | |
Jamie Smith |
1:aac28ffd63ed | 408 | /** |
Jamie Smith |
1:aac28ffd63ed | 409 | * Resets and connects to the IMU. Verifies that it's connected, and reads out its version |
Jamie Smith |
1:aac28ffd63ed | 410 | * info into the class variables above. |
Jamie Smith |
1:aac28ffd63ed | 411 | * |
Jamie Smith |
1:aac28ffd63ed | 412 | * If this function is failing, it would be a good idea to turn on BNO_DEBUG in the cpp file to get detailed output. |
Jamie Smith |
1:aac28ffd63ed | 413 | * |
Jamie Smith |
1:aac28ffd63ed | 414 | * @return whether or not initialization was successful |
Jamie Smith |
1:aac28ffd63ed | 415 | */ |
Jamie Smith |
1:aac28ffd63ed | 416 | bool begin(); |
Jamie Smith |
1:aac28ffd63ed | 417 | |
Jamie Smith |
1:aac28ffd63ed | 418 | /** |
Jamie Smith |
1:aac28ffd63ed | 419 | * Tells the IMU to use its current rotation vector as the "zero" rotation vector and to reorient |
Jamie Smith |
1:aac28ffd63ed | 420 | * all outputs accordingly. |
Jamie Smith |
1:aac28ffd63ed | 421 | * |
Jamie Smith |
1:aac28ffd63ed | 422 | * @param zOnly If true, only the rotation about the Z axis (the heading) will be tared. |
Jamie Smith |
1:aac28ffd63ed | 423 | */ |
Jamie Smith |
1:aac28ffd63ed | 424 | void tare(bool zOnly = false); |
Jamie Smith |
1:aac28ffd63ed | 425 | |
Jamie Smith |
1:aac28ffd63ed | 426 | /** |
Jamie Smith |
1:aac28ffd63ed | 427 | * Tells the IMU to begin a dynamic sensor calibration. To calibrate the IMU, call this function and move |
Jamie Smith |
1:aac28ffd63ed | 428 | * the IMU according to the instructions in the "BNO080 Sensor Calibration Procedure" app note |
Jamie Smith |
1:aac28ffd63ed | 429 | * (http://www.hillcrestlabs.com/download/59de9014566d0727bd002ae7). |
Jamie Smith |
1:aac28ffd63ed | 430 | * |
Jamie Smith |
1:aac28ffd63ed | 431 | * To tell when the calibration is complete, look at the status bits for Game Rotation Vector (for accel and gyro) |
Jamie Smith |
1:aac28ffd63ed | 432 | * and Magnetic Field (for the magnetometer). |
Jamie Smith |
1:aac28ffd63ed | 433 | * |
Jamie Smith |
1:aac28ffd63ed | 434 | * The gyro and accelerometer should only need to be calibrated once, but the magnetometer will need to be recalibrated |
Jamie Smith |
1:aac28ffd63ed | 435 | * every time the orientation of ferrous metals around the IMU changes (e.g. when it is put into a new enclosure). |
Jamie Smith |
1:aac28ffd63ed | 436 | * |
Jamie Smith |
1:aac28ffd63ed | 437 | * The new calibration will not be saved in flash until you call saveCalibration(). |
Jamie Smith |
1:aac28ffd63ed | 438 | * |
Jamie Smith |
1:aac28ffd63ed | 439 | * NOTE: calling this with all false values will cancel any calibration in progress. However, the calibration data being created will |
Jamie Smith |
1:aac28ffd63ed | 440 | * remain in use until the next chip reset (I think!) |
Jamie Smith |
1:aac28ffd63ed | 441 | * |
Jamie Smith |
1:aac28ffd63ed | 442 | * @param calibrateAccel Whether to calibrate the accelerometer. |
Jamie Smith |
1:aac28ffd63ed | 443 | * @param calibrateGyro Whether to calibrate the gyro. |
Jamie Smith |
1:aac28ffd63ed | 444 | * @param calibrateMag Whether to calibrate the magnetometer. |
Jamie Smith |
1:aac28ffd63ed | 445 | * |
Jamie Smith |
1:aac28ffd63ed | 446 | * @return whether the operation succeeded |
Jamie Smith |
1:aac28ffd63ed | 447 | */ |
Jamie Smith |
1:aac28ffd63ed | 448 | bool enableCalibration(bool calibrateAccel, bool calibrateGyro, bool calibrateMag); |
Jamie Smith |
1:aac28ffd63ed | 449 | |
Jamie Smith |
1:aac28ffd63ed | 450 | /** |
Jamie Smith |
1:aac28ffd63ed | 451 | * Saves the calibration started with startCalibration() and ends the calibration. |
Jamie Smith |
1:aac28ffd63ed | 452 | * You will want to call this once the status bits read as "accuracy high". |
Jamie Smith |
1:aac28ffd63ed | 453 | * |
Jamie Smith |
1:aac28ffd63ed | 454 | * WARNING: if you paid for a factory calibrated IMU, then this WILL OVERWRITE THE FACTORY CALIBRATION in whatever sensors |
Jamie Smith |
1:aac28ffd63ed | 455 | * are being calibrated. Use with caution! |
Jamie Smith |
1:aac28ffd63ed | 456 | * |
Jamie Smith |
1:aac28ffd63ed | 457 | * @return whether the operation succeeded |
Jamie Smith |
1:aac28ffd63ed | 458 | */ |
Jamie Smith |
1:aac28ffd63ed | 459 | bool saveCalibration(); |
Jamie Smith |
1:aac28ffd63ed | 460 | |
Jamie Smith |
1:aac28ffd63ed | 461 | /** |
Jamie Smith |
1:aac28ffd63ed | 462 | * Sets the orientation quaternion, telling the sensor how it's mounted |
Jamie Smith |
1:aac28ffd63ed | 463 | * in relation to world space. |
Jamie Smith |
1:aac28ffd63ed | 464 | * See page 40 of the BNO080 datasheet. |
Jamie Smith |
1:aac28ffd63ed | 465 | * |
Jamie Smith |
1:aac28ffd63ed | 466 | * NOTE: this driver provides the macro SQRT_2 to help with entering values from that table. |
Jamie Smith |
1:aac28ffd63ed | 467 | * |
Jamie Smith |
1:aac28ffd63ed | 468 | * NOTE 2: this setting does not persist and will have to be re-applied every time the chip is reset. |
Jamie Smith |
3:197ad972fb7c | 469 | * Use setPermanentOrientation() for that. |
Jamie Smith |
1:aac28ffd63ed | 470 | * |
Jamie Smith |
1:aac28ffd63ed | 471 | * @param orientation quaternion mapping from IMU space to world space. |
Jamie Smith |
1:aac28ffd63ed | 472 | */ |
Jamie Smith |
1:aac28ffd63ed | 473 | void setSensorOrientation(Quaternion orientation); |
Jamie Smith |
1:aac28ffd63ed | 474 | |
Jamie Smith |
3:197ad972fb7c | 475 | /** |
Jamie Smith |
3:197ad972fb7c | 476 | * Sets the orientation quaternion, telling the sensor how it's mounted |
Jamie Smith |
3:197ad972fb7c | 477 | * in relation to world space. See page 40 of the BNO080 datasheet. |
Jamie Smith |
3:197ad972fb7c | 478 | * |
Jamie Smith |
3:197ad972fb7c | 479 | * Unlike setSensorOrientation(), this setting will persist across sensor restarts. |
Jamie Smith |
3:197ad972fb7c | 480 | * However, it will also take a few hundred milliseconds to write. |
Jamie Smith |
3:197ad972fb7c | 481 | * |
Jamie Smith |
3:197ad972fb7c | 482 | * @param orientation quaternion mapping from IMU space to world space. |
Jamie Smith |
3:197ad972fb7c | 483 | * |
Jamie Smith |
3:197ad972fb7c | 484 | * @return true if the operation succeeded, false if it failed. |
Jamie Smith |
3:197ad972fb7c | 485 | */ |
Jamie Smith |
3:197ad972fb7c | 486 | bool setPermanentOrientation(Quaternion orientation); |
Jamie Smith |
3:197ad972fb7c | 487 | |
Jamie Smith |
1:aac28ffd63ed | 488 | // Report functions |
Jamie Smith |
1:aac28ffd63ed | 489 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 490 | |
Jamie Smith |
1:aac28ffd63ed | 491 | /** |
Jamie Smith |
1:aac28ffd63ed | 492 | * Checks for new data packets queued on the IMU. |
Jamie Smith |
1:aac28ffd63ed | 493 | * If there are packets queued, receives all of them and updates |
Jamie Smith |
1:aac28ffd63ed | 494 | * the class variables with the results. |
Jamie Smith |
1:aac28ffd63ed | 495 | * |
Jamie Smith |
1:aac28ffd63ed | 496 | * @return True iff new data packets of any kind were received. If you need more fine-grained data change reporting, |
Jamie Smith |
1:aac28ffd63ed | 497 | * check out hasNewData(). |
Jamie Smith |
1:aac28ffd63ed | 498 | */ |
Jamie Smith |
1:aac28ffd63ed | 499 | bool updateData(); |
Jamie Smith |
1:aac28ffd63ed | 500 | |
Jamie Smith |
1:aac28ffd63ed | 501 | |
Jamie Smith |
1:aac28ffd63ed | 502 | /** |
Jamie Smith |
1:aac28ffd63ed | 503 | * Gets the status of a report as a 2 bit number. |
Jamie Smith |
1:aac28ffd63ed | 504 | * per SH-2 section 6.5.1, this is interpreted as: <br> |
Jamie Smith |
1:aac28ffd63ed | 505 | * 0 - unreliable <br> |
Jamie Smith |
1:aac28ffd63ed | 506 | * 1 - accuracy low <br> |
Jamie Smith |
1:aac28ffd63ed | 507 | * 2 - accuracy medium <br> |
Jamie Smith |
1:aac28ffd63ed | 508 | * 3 - accuracy high <br> |
Jamie Smith |
1:aac28ffd63ed | 509 | * of course, these are only updated if a given report is enabled. |
Jamie Smith |
1:aac28ffd63ed | 510 | * @param report |
Jamie Smith |
1:aac28ffd63ed | 511 | * @return |
Jamie Smith |
1:aac28ffd63ed | 512 | */ |
Jamie Smith |
1:aac28ffd63ed | 513 | uint8_t getReportStatus(Report report); |
Jamie Smith |
1:aac28ffd63ed | 514 | |
Jamie Smith |
1:aac28ffd63ed | 515 | /** |
Jamie Smith |
1:aac28ffd63ed | 516 | * Get a string for printout describing the status of a sensor. |
Jamie Smith |
1:aac28ffd63ed | 517 | * @return |
Jamie Smith |
1:aac28ffd63ed | 518 | */ |
Jamie Smith |
1:aac28ffd63ed | 519 | const char* getReportStatusString(Report report); |
Jamie Smith |
1:aac28ffd63ed | 520 | |
Jamie Smith |
1:aac28ffd63ed | 521 | /** |
Jamie Smith |
1:aac28ffd63ed | 522 | * Checks if a specific report has gotten new data since the last call to this function. |
Jamie Smith |
1:aac28ffd63ed | 523 | * @param report The report to check. |
Jamie Smith |
1:aac28ffd63ed | 524 | * @return Whether the report has received new data. |
Jamie Smith |
1:aac28ffd63ed | 525 | */ |
Jamie Smith |
1:aac28ffd63ed | 526 | bool hasNewData(Report report); |
Jamie Smith |
1:aac28ffd63ed | 527 | |
Jamie Smith |
1:aac28ffd63ed | 528 | /** |
Jamie Smith |
1:aac28ffd63ed | 529 | * Enable a data report from the IMU. Look at the comments above to see what the reports do. |
Jamie Smith |
1:aac28ffd63ed | 530 | * This function checks your polling period against the report's max speed in the IMU's metadata, |
Jamie Smith |
1:aac28ffd63ed | 531 | * and reports an error if you're trying to poll too fast. |
Jamie Smith |
1:aac28ffd63ed | 532 | * |
Jamie Smith |
1:aac28ffd63ed | 533 | * @param timeBetweenReports time in milliseconds between data updates. |
Jamie Smith |
1:aac28ffd63ed | 534 | */ |
Jamie Smith |
1:aac28ffd63ed | 535 | void enableReport(Report report, uint16_t timeBetweenReports); |
Jamie Smith |
1:aac28ffd63ed | 536 | |
Jamie Smith |
1:aac28ffd63ed | 537 | /** |
Jamie Smith |
1:aac28ffd63ed | 538 | * Disable a data report from the IMU. |
Jamie Smith |
1:aac28ffd63ed | 539 | * |
Jamie Smith |
1:aac28ffd63ed | 540 | * @param report The report to disable. |
Jamie Smith |
1:aac28ffd63ed | 541 | */ |
Jamie Smith |
1:aac28ffd63ed | 542 | void disableReport(Report report); |
Jamie Smith |
1:aac28ffd63ed | 543 | |
Jamie Smith |
1:aac28ffd63ed | 544 | /** |
Jamie Smith |
1:aac28ffd63ed | 545 | * Gets the serial number (used to uniquely identify each individual device). |
Jamie Smith |
1:aac28ffd63ed | 546 | * |
Jamie Smith |
1:aac28ffd63ed | 547 | * NOTE: this function should work according to the datasheet, but the device I was testing with appears to have |
Jamie Smith |
1:aac28ffd63ed | 548 | * an empty serial number record as shipped, and I could never get anything out of it. Your mileage may vary. |
Jamie Smith |
1:aac28ffd63ed | 549 | * |
Jamie Smith |
1:aac28ffd63ed | 550 | * @return The serial number, or 0 on error. |
Jamie Smith |
1:aac28ffd63ed | 551 | */ |
Jamie Smith |
1:aac28ffd63ed | 552 | uint32_t getSerialNumber(); |
Jamie Smith |
1:aac28ffd63ed | 553 | |
Jamie Smith |
1:aac28ffd63ed | 554 | // Metadata functions |
Jamie Smith |
1:aac28ffd63ed | 555 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 556 | |
Jamie Smith |
1:aac28ffd63ed | 557 | /** |
Jamie Smith |
1:aac28ffd63ed | 558 | * Gets the range of a report as reported by the IMU. Units are the same as the report's output data. |
Jamie Smith |
1:aac28ffd63ed | 559 | * @return |
Jamie Smith |
1:aac28ffd63ed | 560 | */ |
Jamie Smith |
1:aac28ffd63ed | 561 | float getRange(Report report); |
Jamie Smith |
1:aac28ffd63ed | 562 | |
Jamie Smith |
1:aac28ffd63ed | 563 | /** |
Jamie Smith |
1:aac28ffd63ed | 564 | * Gets the resolution of a report as reported by the IMU. Units are the same as the report's output data. |
Jamie Smith |
1:aac28ffd63ed | 565 | * @param report |
Jamie Smith |
1:aac28ffd63ed | 566 | * @return |
Jamie Smith |
1:aac28ffd63ed | 567 | */ |
Jamie Smith |
1:aac28ffd63ed | 568 | float getResolution(Report report); |
Jamie Smith |
1:aac28ffd63ed | 569 | |
Jamie Smith |
1:aac28ffd63ed | 570 | /** |
Jamie Smith |
1:aac28ffd63ed | 571 | * Get the power used by a report when it's operating, according to the IMU. |
Jamie Smith |
1:aac28ffd63ed | 572 | * @param report |
Jamie Smith |
1:aac28ffd63ed | 573 | * @return Power used in mA. |
Jamie Smith |
1:aac28ffd63ed | 574 | */ |
Jamie Smith |
1:aac28ffd63ed | 575 | float getPower(Report report); |
Jamie Smith |
1:aac28ffd63ed | 576 | |
Jamie Smith |
1:aac28ffd63ed | 577 | /** |
Jamie Smith |
1:aac28ffd63ed | 578 | * Gets the smallest polling period that a report supports. |
Jamie Smith |
1:aac28ffd63ed | 579 | * @return Period in seconds. |
Jamie Smith |
1:aac28ffd63ed | 580 | */ |
Jamie Smith |
1:aac28ffd63ed | 581 | float getMinPeriod(Report report); |
Jamie Smith |
1:aac28ffd63ed | 582 | |
Jamie Smith |
1:aac28ffd63ed | 583 | /** |
Jamie Smith |
1:aac28ffd63ed | 584 | * Gets the larges polling period that a report supports. |
Jamie Smith |
1:aac28ffd63ed | 585 | * Some reports don't have a max period, in which case this function will return -1.0. |
Jamie Smith |
1:aac28ffd63ed | 586 | * |
Jamie Smith |
1:aac28ffd63ed | 587 | * @return Period in seconds, or -1.0 on error. |
Jamie Smith |
1:aac28ffd63ed | 588 | */ |
Jamie Smith |
1:aac28ffd63ed | 589 | float getMaxPeriod(Report report); |
Jamie Smith |
1:aac28ffd63ed | 590 | |
Jamie Smith |
1:aac28ffd63ed | 591 | /** |
Jamie Smith |
1:aac28ffd63ed | 592 | * Prints a summary of a report's metadata to the |
Jamie Smith |
1:aac28ffd63ed | 593 | * debug stream. Should be useful for debugging and setting up reports since lots of this data |
Jamie Smith |
1:aac28ffd63ed | 594 | * isn't given in the datasheets. |
Jamie Smith |
1:aac28ffd63ed | 595 | * |
Jamie Smith |
1:aac28ffd63ed | 596 | * Note: to save string constant space, this function is only available when BNO_DEBUG is 1. |
Jamie Smith |
1:aac28ffd63ed | 597 | */ |
Jamie Smith |
1:aac28ffd63ed | 598 | void printMetadataSummary(Report report); |
Jamie Smith |
1:aac28ffd63ed | 599 | |
Jamie Smith |
1:aac28ffd63ed | 600 | private: |
Jamie Smith |
1:aac28ffd63ed | 601 | |
Jamie Smith |
1:aac28ffd63ed | 602 | // Internal metadata functions |
Jamie Smith |
1:aac28ffd63ed | 603 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 604 | |
Jamie Smith |
1:aac28ffd63ed | 605 | /** |
Jamie Smith |
1:aac28ffd63ed | 606 | * Gets the version of the metadata stored in the buffer. |
Jamie Smith |
1:aac28ffd63ed | 607 | * We might see version 3 and 4 records, and they have different layouts. |
Jamie Smith |
1:aac28ffd63ed | 608 | * @return |
Jamie Smith |
1:aac28ffd63ed | 609 | */ |
Jamie Smith |
1:aac28ffd63ed | 610 | uint16_t getMetaVersion() {return static_cast<uint16_t>(metadataRecord[3] >> 16);} |
Jamie Smith |
1:aac28ffd63ed | 611 | |
Jamie Smith |
1:aac28ffd63ed | 612 | // @{ |
Jamie Smith |
1:aac28ffd63ed | 613 | /** |
Jamie Smith |
1:aac28ffd63ed | 614 | * Gets the Q point from a report's metadata, which essentially defines where the decimal point goes in the sensor's output. |
Jamie Smith |
1:aac28ffd63ed | 615 | * The 1/2/3 Q values are used in different places in the metadata, see SH-2 section 5.1 for details. |
Jamie Smith |
1:aac28ffd63ed | 616 | * @param report |
Jamie Smith |
1:aac28ffd63ed | 617 | * @return |
Jamie Smith |
1:aac28ffd63ed | 618 | */ |
Jamie Smith |
1:aac28ffd63ed | 619 | int16_t getQ1(Report report); |
Jamie Smith |
1:aac28ffd63ed | 620 | int16_t getQ2(Report report); |
Jamie Smith |
1:aac28ffd63ed | 621 | int16_t getQ3(Report report); |
Jamie Smith |
1:aac28ffd63ed | 622 | // @} |
Jamie Smith |
1:aac28ffd63ed | 623 | |
Jamie Smith |
1:aac28ffd63ed | 624 | // internal utility functions |
Jamie Smith |
1:aac28ffd63ed | 625 | //----------------------------------------------------------------------------------------------------------------- |
Jamie Smith |
1:aac28ffd63ed | 626 | |
Jamie Smith |
1:aac28ffd63ed | 627 | /** |
Jamie Smith |
1:aac28ffd63ed | 628 | * Processes the packet currently stored in the buffer, and updates class variables to reflect the data it contains |
Jamie Smith |
1:aac28ffd63ed | 629 | */ |
Jamie Smith |
1:aac28ffd63ed | 630 | void processPacket(); |
Jamie Smith |
1:aac28ffd63ed | 631 | |
Jamie Smith |
1:aac28ffd63ed | 632 | /** |
Jamie Smith |
1:aac28ffd63ed | 633 | * Processes the sensor data packet currently stored in the buffer. |
Jamie Smith |
1:aac28ffd63ed | 634 | * Only called from processPacket() |
Jamie Smith |
1:aac28ffd63ed | 635 | */ |
Jamie Smith |
1:aac28ffd63ed | 636 | void parseSensorDataPacket(); |
Jamie Smith |
1:aac28ffd63ed | 637 | |
Jamie Smith |
1:aac28ffd63ed | 638 | /** |
Jamie Smith |
1:aac28ffd63ed | 639 | * Call to wait for a packet with the given parameters to come in. |
Jamie Smith |
1:aac28ffd63ed | 640 | * |
Jamie Smith |
1:aac28ffd63ed | 641 | * @param channel Channel of the packet |
Jamie Smith |
1:aac28ffd63ed | 642 | * @param reportID Report ID (first data byte) of the packet |
Jamie Smith |
1:aac28ffd63ed | 643 | * @param timeout how long to wait for the packet |
Jamie Smith |
1:aac28ffd63ed | 644 | * @return true if the packet has been received, false if it timed out |
Jamie Smith |
1:aac28ffd63ed | 645 | */ |
Jamie Smith |
1:aac28ffd63ed | 646 | bool waitForPacket(int channel, uint8_t reportID, float timeout = .125f); |
Jamie Smith |
1:aac28ffd63ed | 647 | |
Jamie Smith |
1:aac28ffd63ed | 648 | /** |
Jamie Smith |
1:aac28ffd63ed | 649 | * Given a Q value, converts fixed point floating to regular floating point number. |
Jamie Smith |
1:aac28ffd63ed | 650 | * @param fixedPointValue |
Jamie Smith |
1:aac28ffd63ed | 651 | * @param qPoint |
Jamie Smith |
1:aac28ffd63ed | 652 | * @return |
Jamie Smith |
1:aac28ffd63ed | 653 | */ |
Jamie Smith |
1:aac28ffd63ed | 654 | float qToFloat(int16_t fixedPointValue, uint8_t qPoint); |
Jamie Smith |
1:aac28ffd63ed | 655 | |
Jamie Smith |
1:aac28ffd63ed | 656 | /** |
Jamie Smith |
1:aac28ffd63ed | 657 | * Given a Q value, converts fixed point floating to regular floating point number. |
Jamie Smith |
1:aac28ffd63ed | 658 | * This version is used for the unsigned 32-bit values in metadata records. |
Jamie Smith |
1:aac28ffd63ed | 659 | * @param fixedPointValue |
Jamie Smith |
1:aac28ffd63ed | 660 | * @param qPoint |
Jamie Smith |
1:aac28ffd63ed | 661 | * @return |
Jamie Smith |
1:aac28ffd63ed | 662 | */ |
Jamie Smith |
1:aac28ffd63ed | 663 | float qToFloat_dword(uint32_t fixedPointValue, int16_t qPoint); |
Jamie Smith |
1:aac28ffd63ed | 664 | |
Jamie Smith |
1:aac28ffd63ed | 665 | /** |
Jamie Smith |
1:aac28ffd63ed | 666 | * Given a floating point value and a Q point, convert to Q |
Jamie Smith |
1:aac28ffd63ed | 667 | * See https://en.wikipedia.org/wiki/Q_(number_format) |
Jamie Smith |
1:aac28ffd63ed | 668 | * @param qFloat |
Jamie Smith |
1:aac28ffd63ed | 669 | * @param qPoint |
Jamie Smith |
1:aac28ffd63ed | 670 | * @return |
Jamie Smith |
1:aac28ffd63ed | 671 | */ |
Jamie Smith |
1:aac28ffd63ed | 672 | int16_t floatToQ(float qFloat, uint8_t qPoint); |
Jamie Smith |
1:aac28ffd63ed | 673 | |
Jamie Smith |
1:aac28ffd63ed | 674 | /** |
Jamie Smith |
3:197ad972fb7c | 675 | * Given a floating point value and a Q point, convert to Q |
Jamie Smith |
3:197ad972fb7c | 676 | * See https://en.wikipedia.org/wiki/Q_(number_format) |
Jamie Smith |
3:197ad972fb7c | 677 | * |
Jamie Smith |
3:197ad972fb7c | 678 | * This version is used for the signed 32-bit values in metadata records. |
Jamie Smith |
3:197ad972fb7c | 679 | * |
Jamie Smith |
3:197ad972fb7c | 680 | * @param qFloat |
Jamie Smith |
3:197ad972fb7c | 681 | * @param qPoint |
Jamie Smith |
3:197ad972fb7c | 682 | * @return |
Jamie Smith |
3:197ad972fb7c | 683 | */ |
Jamie Smith |
3:197ad972fb7c | 684 | int32_t floatToQ_dword(float qFloat, uint16_t qPoint); |
Jamie Smith |
3:197ad972fb7c | 685 | |
Jamie Smith |
3:197ad972fb7c | 686 | /** |
Jamie Smith |
1:aac28ffd63ed | 687 | * Tell the sensor to do a command. |
Jamie Smith |
1:aac28ffd63ed | 688 | * See SH-2 Reference Manual section 6.3.8 page 42, Command request |
Jamie Smith |
1:aac28ffd63ed | 689 | * The caller is expected to set shtpData 3 though 11 prior to calling |
Jamie Smith |
1:aac28ffd63ed | 690 | */ |
Jamie Smith |
1:aac28ffd63ed | 691 | void sendCommand(uint8_t command); |
Jamie Smith |
1:aac28ffd63ed | 692 | |
Jamie Smith |
1:aac28ffd63ed | 693 | /** |
Jamie Smith |
1:aac28ffd63ed | 694 | * Given a sensor's report ID, this tells the BNO080 to begin reporting the values. |
Jamie Smith |
1:aac28ffd63ed | 695 | * |
Jamie Smith |
1:aac28ffd63ed | 696 | * @param reportID |
Jamie Smith |
1:aac28ffd63ed | 697 | * @param timeBetweenReports |
Jamie Smith |
1:aac28ffd63ed | 698 | * @param specificConfig the specific config word. Useful for personal activity classifier. |
Jamie Smith |
1:aac28ffd63ed | 699 | */ |
Jamie Smith |
1:aac28ffd63ed | 700 | void setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports, uint32_t specificConfig = 0); |
Jamie Smith |
1:aac28ffd63ed | 701 | |
Jamie Smith |
1:aac28ffd63ed | 702 | /** |
Jamie Smith |
1:aac28ffd63ed | 703 | * Read a record from the FRS (Flash Record System) on the IMU. FRS records are composed of 32-bit words, |
Jamie Smith |
1:aac28ffd63ed | 704 | * with the size of each record determined by the record type. |
Jamie Smith |
1:aac28ffd63ed | 705 | * |
Jamie Smith |
1:aac28ffd63ed | 706 | * Will block until the entire record has been read. |
Jamie Smith |
1:aac28ffd63ed | 707 | * @param recordID Record ID to read. See SH-2 figures 28 and 29 for a list of these. Sometimes also called |
Jamie Smith |
1:aac28ffd63ed | 708 | * the "FRS Type" by the datasheet (???). |
Jamie Smith |
1:aac28ffd63ed | 709 | * @param readBuffer Buffer to read data into. |
Jamie Smith |
1:aac28ffd63ed | 710 | * @param readLength Amount of words to read from the record. Must be <= the length of the record. |
Jamie Smith |
1:aac28ffd63ed | 711 | * |
Jamie Smith |
1:aac28ffd63ed | 712 | * @return whether the request succeeded |
Jamie Smith |
1:aac28ffd63ed | 713 | */ |
Jamie Smith |
1:aac28ffd63ed | 714 | bool readFRSRecord(uint16_t recordID, uint32_t* readBuffer, uint16_t readLength); |
Jamie Smith |
1:aac28ffd63ed | 715 | |
Jamie Smith |
1:aac28ffd63ed | 716 | /** |
Jamie Smith |
3:197ad972fb7c | 717 | * Write a record to the FRS (Flash Record System) on the IMU. FRS records are composed of 32-bit words, |
Jamie Smith |
3:197ad972fb7c | 718 | * with the size of each record determined by the record type. |
Jamie Smith |
3:197ad972fb7c | 719 | * |
Jamie Smith |
3:197ad972fb7c | 720 | * Will block until the entire record has been written. |
Jamie Smith |
3:197ad972fb7c | 721 | * @param recordID Record ID to write. See SH-2 figures 28 and 29 for a list of these. Sometimes also called |
Jamie Smith |
3:197ad972fb7c | 722 | * the "FRS Type" by the datasheet (???). |
Jamie Smith |
3:197ad972fb7c | 723 | * @param buffer Buffer to write data into. |
Jamie Smith |
3:197ad972fb7c | 724 | * @param length Amount of words to write to the record. Must be <= the length of the record. |
Jamie Smith |
3:197ad972fb7c | 725 | * |
Jamie Smith |
3:197ad972fb7c | 726 | * @return whether the request succeeded |
Jamie Smith |
3:197ad972fb7c | 727 | */ |
Jamie Smith |
3:197ad972fb7c | 728 | bool writeFRSRecord(uint16_t recordID, uint32_t* buffer, uint16_t length); |
Jamie Smith |
3:197ad972fb7c | 729 | |
Jamie Smith |
3:197ad972fb7c | 730 | /** |
Jamie Smith |
1:aac28ffd63ed | 731 | * Reads a packet from the IMU and stores it in the class variables. |
Jamie Smith |
1:aac28ffd63ed | 732 | * |
Jamie Smith |
1:aac28ffd63ed | 733 | * @param timeout how long to wait for there to be a packet |
Jamie Smith |
1:aac28ffd63ed | 734 | * |
Jamie Smith |
1:aac28ffd63ed | 735 | * @return whether a packet was recieved. |
Jamie Smith |
1:aac28ffd63ed | 736 | */ |
Jamie Smith |
1:aac28ffd63ed | 737 | bool receivePacket(float timeout=.2f); |
Jamie Smith |
1:aac28ffd63ed | 738 | |
Jamie Smith |
1:aac28ffd63ed | 739 | /** |
Jamie Smith |
1:aac28ffd63ed | 740 | * Sends the current shtpData contents to the BNO. It's a good idea to disable interrupts before you call this. |
Jamie Smith |
1:aac28ffd63ed | 741 | * |
Jamie Smith |
1:aac28ffd63ed | 742 | * @param channelNumber the channel to send on |
Jamie Smith |
1:aac28ffd63ed | 743 | * @param dataLength How many bits of shtpData to send |
Jamie Smith |
1:aac28ffd63ed | 744 | * @return |
Jamie Smith |
1:aac28ffd63ed | 745 | */ |
Jamie Smith |
1:aac28ffd63ed | 746 | bool sendPacket(uint8_t channelNumber, uint8_t dataLength); |
Jamie Smith |
1:aac28ffd63ed | 747 | |
Jamie Smith |
1:aac28ffd63ed | 748 | /** |
Jamie Smith |
1:aac28ffd63ed | 749 | * Prints the current shtp packet stored in the buffer. |
Jamie Smith |
1:aac28ffd63ed | 750 | * @param length |
Jamie Smith |
1:aac28ffd63ed | 751 | */ |
Jamie Smith |
1:aac28ffd63ed | 752 | void printPacket(); |
Jamie Smith |
1:aac28ffd63ed | 753 | |
Jamie Smith |
1:aac28ffd63ed | 754 | /** |
Jamie Smith |
1:aac28ffd63ed | 755 | * Erases the current SHTP packet buffer so new data can be written |
Jamie Smith |
1:aac28ffd63ed | 756 | */ |
Jamie Smith |
1:aac28ffd63ed | 757 | void zeroBuffer(); |
Jamie Smith |
1:aac28ffd63ed | 758 | |
Jamie Smith |
1:aac28ffd63ed | 759 | /** |
Jamie Smith |
1:aac28ffd63ed | 760 | * Loads the metadata for this report into the metadata buffer. |
Jamie Smith |
1:aac28ffd63ed | 761 | * @param report |
Jamie Smith |
1:aac28ffd63ed | 762 | * @return Whether the operation succeeded. |
Jamie Smith |
1:aac28ffd63ed | 763 | */ |
Jamie Smith |
1:aac28ffd63ed | 764 | bool loadReportMetadata(Report report); |
Jamie Smith |
1:aac28ffd63ed | 765 | |
Jamie Smith |
1:aac28ffd63ed | 766 | }; |
Jamie Smith |
1:aac28ffd63ed | 767 | |
Jamie Smith |
1:aac28ffd63ed | 768 | |
Jamie Smith |
1:aac28ffd63ed | 769 | #endif //HAMSTER_BNO080_H |