Added BNO080Wheelchair.h

Dependents:   BNO080_program wheelchaircontrol8 Version1-9 BNO080_program

Committer:
MultipleMonomials
Date:
Sun Dec 23 05:23:21 2018 +0000
Revision:
0:f677e13975d0
Child:
1:aac28ffd63ed
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MultipleMonomials 0:f677e13975d0 1 /*
MultipleMonomials 0:f677e13975d0 2 * This is USC RPL's ARM MBed BNO080 IMU driver, by Jamie Smith.
MultipleMonomials 0:f677e13975d0 3 *
MultipleMonomials 0:f677e13975d0 4 * It is based on SparkFun and Nathan Seidle's Arduino driver for this chip, but is substantially rewritten and adapted.
MultipleMonomials 0:f677e13975d0 5 * It also supports some extra features, such as setting the mounting orientation and
MultipleMonomials 0:f677e13975d0 6 * enabling some additional data reports.
MultipleMonomials 0:f677e13975d0 7 *
MultipleMonomials 0:f677e13975d0 8 * This driver uses no dynamic allocation, but does allocate a couple hundred bytes of class variables as buffers.
MultipleMonomials 0:f677e13975d0 9 * This should allow you to monitor its memory usage using MBed's size printout.
MultipleMonomials 0:f677e13975d0 10 *
MultipleMonomials 0:f677e13975d0 11 * The BNO080 is a very complex chip; it's capable of monitoring and controlling other sensors and making
MultipleMonomials 0:f677e13975d0 12 * intelligent decisions and calculations using its data. Accordingly, the protocol for communicating with it
MultipleMonomials 0:f677e13975d0 13 * is quite complex, and it took me quite a while to wrap my head around it. If you need to modify or debug
MultipleMonomials 0:f677e13975d0 14 * this driver, look at the CPP file for an overview of the chip's communication protocol.
MultipleMonomials 0:f677e13975d0 15 *
MultipleMonomials 0:f677e13975d0 16 * Note: this driver only supports I2C. I attempted to create an SPI version, but as far as I can tell,
MultipleMonomials 0:f677e13975d0 17 * the BNO's SPI interface has a bug that causes you to be unable to wake the chip from sleep in some conditions.
MultipleMonomials 0:f677e13975d0 18 * Until this is fixed, SPI on it is virtually unusable.
MultipleMonomials 0:f677e13975d0 19 */
MultipleMonomials 0:f677e13975d0 20
MultipleMonomials 0:f677e13975d0 21 #ifndef HAMSTER_BNO080_H
MultipleMonomials 0:f677e13975d0 22 #define HAMSTER_BNO080_H
MultipleMonomials 0:f677e13975d0 23
MultipleMonomials 0:f677e13975d0 24 #include <mbed.h>
MultipleMonomials 0:f677e13975d0 25 #include <quaternion.h>
MultipleMonomials 0:f677e13975d0 26
MultipleMonomials 0:f677e13975d0 27 #include "BNO080Constants.h"
MultipleMonomials 0:f677e13975d0 28
MultipleMonomials 0:f677e13975d0 29 class BNO080
MultipleMonomials 0:f677e13975d0 30 {
MultipleMonomials 0:f677e13975d0 31 /**
MultipleMonomials 0:f677e13975d0 32 * Serial stream to print debug info to. Used for errors, and debugging output if debugging is enabled.
MultipleMonomials 0:f677e13975d0 33 */
MultipleMonomials 0:f677e13975d0 34 Serial * _debugPort;
MultipleMonomials 0:f677e13975d0 35
MultipleMonomials 0:f677e13975d0 36 /**
MultipleMonomials 0:f677e13975d0 37 * I2C port object
MultipleMonomials 0:f677e13975d0 38 */
MultipleMonomials 0:f677e13975d0 39 I2C _i2cPort;
MultipleMonomials 0:f677e13975d0 40
MultipleMonomials 0:f677e13975d0 41 /// user defined port speed
MultipleMonomials 0:f677e13975d0 42 int _i2cPortSpeed;
MultipleMonomials 0:f677e13975d0 43
MultipleMonomials 0:f677e13975d0 44 /// i2c address of IMU (7 bits)
MultipleMonomials 0:f677e13975d0 45 uint8_t _i2cAddress;
MultipleMonomials 0:f677e13975d0 46
MultipleMonomials 0:f677e13975d0 47 DigitalIn _int;
MultipleMonomials 0:f677e13975d0 48 DigitalOut _rst;
MultipleMonomials 0:f677e13975d0 49
MultipleMonomials 0:f677e13975d0 50 DigitalOut _scope;
MultipleMonomials 0:f677e13975d0 51
MultipleMonomials 0:f677e13975d0 52 // packet storage
MultipleMonomials 0:f677e13975d0 53 //-----------------------------------------------------------------------------------------------------------------
MultipleMonomials 0:f677e13975d0 54
MultipleMonomials 0:f677e13975d0 55 #define SHTP_HEADER_SIZE 4
MultipleMonomials 0:f677e13975d0 56 #define STORED_PACKET_SIZE 128
MultipleMonomials 0:f677e13975d0 57
MultipleMonomials 0:f677e13975d0 58 /// Each packet has a header of 4 uint8_ts
MultipleMonomials 0:f677e13975d0 59 uint8_t shtpHeader[SHTP_HEADER_SIZE];
MultipleMonomials 0:f677e13975d0 60
MultipleMonomials 0:f677e13975d0 61 /// Stores data contained in each packet. Packets can contain an arbitrary amount of data, but we shouldn't need to read more than a few hundred bytes of them.
MultipleMonomials 0:f677e13975d0 62 /// The only long packets we actually care about are batched sensor data packets, and with how this driver handles batching, we *should* only have to deal
MultipleMonomials 0:f677e13975d0 63 /// with at most 9 reports at a time = ~90 bytes + a few bytes of padding
MultipleMonomials 0:f677e13975d0 64 uint8_t shtpData[STORED_PACKET_SIZE];
MultipleMonomials 0:f677e13975d0 65
MultipleMonomials 0:f677e13975d0 66 /// Length of packet that was received into buffer. Does NOT include header bytes.
MultipleMonomials 0:f677e13975d0 67 uint16_t packetLength;
MultipleMonomials 0:f677e13975d0 68
MultipleMonomials 0:f677e13975d0 69 /// There are 6 com channels. Each channel has its own seqnum
MultipleMonomials 0:f677e13975d0 70 uint8_t sequenceNumber[6] = {0, 0, 0, 0, 0, 0};
MultipleMonomials 0:f677e13975d0 71
MultipleMonomials 0:f677e13975d0 72 /// Commands have a seqNum as well. These are inside command packet, the header uses its own seqNum per channel
MultipleMonomials 0:f677e13975d0 73 uint8_t commandSequenceNumber = 0;
MultipleMonomials 0:f677e13975d0 74
MultipleMonomials 0:f677e13975d0 75 // data storage
MultipleMonomials 0:f677e13975d0 76 //-----------------------------------------------------------------------------------------------------------------
MultipleMonomials 0:f677e13975d0 77
MultipleMonomials 0:f677e13975d0 78 // 1 larger than the largest sensor report ID
MultipleMonomials 0:f677e13975d0 79 #define STATUS_ARRAY_LEN 0x1A
MultipleMonomials 0:f677e13975d0 80
MultipleMonomials 0:f677e13975d0 81 /// stores status of each sensor, indexed by report ID
MultipleMonomials 0:f677e13975d0 82 uint8_t reportStatus[STATUS_ARRAY_LEN] = {};
MultipleMonomials 0:f677e13975d0 83
MultipleMonomials 0:f677e13975d0 84 public:
MultipleMonomials 0:f677e13975d0 85
MultipleMonomials 0:f677e13975d0 86 /// List of all sensor reports that the IMU supports.
MultipleMonomials 0:f677e13975d0 87 enum class Report : uint8_t
MultipleMonomials 0:f677e13975d0 88 {
MultipleMonomials 0:f677e13975d0 89 /**
MultipleMonomials 0:f677e13975d0 90 * Total acceleration of the IMU in world space.
MultipleMonomials 0:f677e13975d0 91 * See BNO datasheet section 2.1.1
MultipleMonomials 0:f677e13975d0 92 */
MultipleMonomials 0:f677e13975d0 93 TOTAL_ACCELERATION = SENSOR_REPORTID_ACCELEROMETER,
MultipleMonomials 0:f677e13975d0 94
MultipleMonomials 0:f677e13975d0 95 /**
MultipleMonomials 0:f677e13975d0 96 * Acceleration of the IMU not including the acceleration of gravity.
MultipleMonomials 0:f677e13975d0 97 * See BNO datasheet section 2.1.1
MultipleMonomials 0:f677e13975d0 98 */
MultipleMonomials 0:f677e13975d0 99 LINEAR_ACCELERATION = SENSOR_REPORTID_LINEAR_ACCELERATION,
MultipleMonomials 0:f677e13975d0 100
MultipleMonomials 0:f677e13975d0 101 /**
MultipleMonomials 0:f677e13975d0 102 * Acceleration of gravity felt by the IMU.
MultipleMonomials 0:f677e13975d0 103 * See BNO datasheet section 2.1.1
MultipleMonomials 0:f677e13975d0 104 */
MultipleMonomials 0:f677e13975d0 105 GRAVITY_ACCELERATION = SENSOR_REPORTID_GRAVITY,
MultipleMonomials 0:f677e13975d0 106
MultipleMonomials 0:f677e13975d0 107 /**
MultipleMonomials 0:f677e13975d0 108 * (calibrated) gyroscope reading of the rotational speed of the IMU.
MultipleMonomials 0:f677e13975d0 109 * See BNO datasheet section 2.1.2
MultipleMonomials 0:f677e13975d0 110 */
MultipleMonomials 0:f677e13975d0 111 GYROSCOPE = SENSOR_REPORTID_GYROSCOPE_CALIBRATED,
MultipleMonomials 0:f677e13975d0 112
MultipleMonomials 0:f677e13975d0 113 /**
MultipleMonomials 0:f677e13975d0 114 * (calibrated) reading of magnetic field levels.
MultipleMonomials 0:f677e13975d0 115 * See BNO datasheet section 2.1.3
MultipleMonomials 0:f677e13975d0 116 */
MultipleMonomials 0:f677e13975d0 117 MAG_FIELD = SENSOR_REPORTID_MAGNETIC_FIELD_CALIBRATED,
MultipleMonomials 0:f677e13975d0 118
MultipleMonomials 0:f677e13975d0 119 /**
MultipleMonomials 0:f677e13975d0 120 * Fused reading of the IMU's rotation in space using all three sensors. This is the most accurate reading
MultipleMonomials 0:f677e13975d0 121 * of absolute orientation that the IMU can provide.
MultipleMonomials 0:f677e13975d0 122 * See BNO datasheet section 2.2.4
MultipleMonomials 0:f677e13975d0 123 */
MultipleMonomials 0:f677e13975d0 124 ROTATION = SENSOR_REPORTID_ROTATION_VECTOR,
MultipleMonomials 0:f677e13975d0 125
MultipleMonomials 0:f677e13975d0 126 /**
MultipleMonomials 0:f677e13975d0 127 * Fused reading of rotation from accelerometer and magnetometer readings. This report is designed to decrease
MultipleMonomials 0:f677e13975d0 128 * power consumption (by turning off the gyroscope) in exchange for reduced responsiveness.
MultipleMonomials 0:f677e13975d0 129 */
MultipleMonomials 0:f677e13975d0 130 GEOMAGNETIC_ROTATION = SENSOR_REPORTID_GEOMAGNETIC_ROTATION_VECTOR,
MultipleMonomials 0:f677e13975d0 131
MultipleMonomials 0:f677e13975d0 132 /**
MultipleMonomials 0:f677e13975d0 133 * Fused reading of the IMU's rotation in space. Unlike the regular rotation vector, the Game Rotation Vector
MultipleMonomials 0:f677e13975d0 134 * is not referenced against the magnetic field and the "zero yaw" point is arbitrary.
MultipleMonomials 0:f677e13975d0 135 * See BNO datasheet section 2.2.2
MultipleMonomials 0:f677e13975d0 136 */
MultipleMonomials 0:f677e13975d0 137 GAME_ROTATION = SENSOR_REPORTID_GAME_ROTATION_VECTOR,
MultipleMonomials 0:f677e13975d0 138
MultipleMonomials 0:f677e13975d0 139 /**
MultipleMonomials 0:f677e13975d0 140 * Detects a user tapping on the device containing the IMU.
MultipleMonomials 0:f677e13975d0 141 * See BNO datasheet section 2.4.2
MultipleMonomials 0:f677e13975d0 142 */
MultipleMonomials 0:f677e13975d0 143 TAP_DETECTOR = SENSOR_REPORTID_TAP_DETECTOR,
MultipleMonomials 0:f677e13975d0 144
MultipleMonomials 0:f677e13975d0 145 /**
MultipleMonomials 0:f677e13975d0 146 * Detects whether the device is on a table, being held stably, or being moved.
MultipleMonomials 0:f677e13975d0 147 * See BNO datasheet section 2.4.1
MultipleMonomials 0:f677e13975d0 148 */
MultipleMonomials 0:f677e13975d0 149 STABILITY_CLASSIFIER = SENSOR_REPORTID_STABILITY_CLASSIFIER,
MultipleMonomials 0:f677e13975d0 150
MultipleMonomials 0:f677e13975d0 151 /**
MultipleMonomials 0:f677e13975d0 152 * Detects a user taking a step with the IMU worn on their person.
MultipleMonomials 0:f677e13975d0 153 * See BNO datasheet section 2.4.3
MultipleMonomials 0:f677e13975d0 154 */
MultipleMonomials 0:f677e13975d0 155 STEP_DETECTOR = SENSOR_REPORTID_STEP_DETECTOR,
MultipleMonomials 0:f677e13975d0 156
MultipleMonomials 0:f677e13975d0 157 /**
MultipleMonomials 0:f677e13975d0 158 * Detects how many steps a user has taken.
MultipleMonomials 0:f677e13975d0 159 * See BNO datasheet section 2.4.4
MultipleMonomials 0:f677e13975d0 160 */
MultipleMonomials 0:f677e13975d0 161 STEP_COUNTER = SENSOR_REPORTID_STEP_COUNTER,
MultipleMonomials 0:f677e13975d0 162
MultipleMonomials 0:f677e13975d0 163 /**
MultipleMonomials 0:f677e13975d0 164 * Detects when the IMU has made a "significant" motion, defined as moving a few steps and/or accelerating significantly.
MultipleMonomials 0:f677e13975d0 165 * See BNO datasheet section 2.4.6
MultipleMonomials 0:f677e13975d0 166 */
MultipleMonomials 0:f677e13975d0 167 SIGNIFICANT_MOTION = SENSOR_REPORTID_SIGNIFICANT_MOTION,
MultipleMonomials 0:f677e13975d0 168
MultipleMonomials 0:f677e13975d0 169 /**
MultipleMonomials 0:f677e13975d0 170 * Detects when the IMU is being shaken.
MultipleMonomials 0:f677e13975d0 171 * See BNO datasheet section 2.4.7
MultipleMonomials 0:f677e13975d0 172 */
MultipleMonomials 0:f677e13975d0 173 SHAKE_DETECTOR = SENSOR_REPORTID_SHAKE_DETECTOR
MultipleMonomials 0:f677e13975d0 174 };
MultipleMonomials 0:f677e13975d0 175
MultipleMonomials 0:f677e13975d0 176 // data variables to read reports from
MultipleMonomials 0:f677e13975d0 177 //-----------------------------------------------------------------------------------------------------------------
MultipleMonomials 0:f677e13975d0 178
MultipleMonomials 0:f677e13975d0 179 // @{
MultipleMonomials 0:f677e13975d0 180 /// Version info read from the IMU when it starts up
MultipleMonomials 0:f677e13975d0 181 uint8_t majorSoftwareVersion;
MultipleMonomials 0:f677e13975d0 182 uint8_t minorSoftwareVersion;
MultipleMonomials 0:f677e13975d0 183 uint16_t patchSoftwareVersion;
MultipleMonomials 0:f677e13975d0 184 uint32_t partNumber;
MultipleMonomials 0:f677e13975d0 185 uint32_t buildNumber;
MultipleMonomials 0:f677e13975d0 186 // @}
MultipleMonomials 0:f677e13975d0 187
MultipleMonomials 0:f677e13975d0 188
MultipleMonomials 0:f677e13975d0 189 /**
MultipleMonomials 0:f677e13975d0 190 * Readout from Accleration report.
MultipleMonomials 0:f677e13975d0 191 * Represents total acceleration in m/s^2 felt by the BNO's accelerometer.
MultipleMonomials 0:f677e13975d0 192 */
MultipleMonomials 0:f677e13975d0 193 TVector3 totalAcceleration;
MultipleMonomials 0:f677e13975d0 194
MultipleMonomials 0:f677e13975d0 195 /**
MultipleMonomials 0:f677e13975d0 196 * Readout from Linear Acceleration report.
MultipleMonomials 0:f677e13975d0 197 * Represents acceleration felt in m/s^2 by the BNO's accelerometer not including the force of gravity.
MultipleMonomials 0:f677e13975d0 198 */
MultipleMonomials 0:f677e13975d0 199 TVector3 linearAcceleration;
MultipleMonomials 0:f677e13975d0 200
MultipleMonomials 0:f677e13975d0 201 /**
MultipleMonomials 0:f677e13975d0 202 * Readout from Gravity report.
MultipleMonomials 0:f677e13975d0 203 * Represents the force of gravity in m/s^2 felt by the BNO's accelerometer.
MultipleMonomials 0:f677e13975d0 204 */
MultipleMonomials 0:f677e13975d0 205 TVector3 gravityAcceleration;
MultipleMonomials 0:f677e13975d0 206
MultipleMonomials 0:f677e13975d0 207 /**
MultipleMonomials 0:f677e13975d0 208 * Readout from Calibrated Gyroscope report
MultipleMonomials 0:f677e13975d0 209 * Represents the angular velocities of the chip in rad/s in the X, Y, and Z axes
MultipleMonomials 0:f677e13975d0 210 */
MultipleMonomials 0:f677e13975d0 211 TVector3 gyroRotation;
MultipleMonomials 0:f677e13975d0 212
MultipleMonomials 0:f677e13975d0 213 /**
MultipleMonomials 0:f677e13975d0 214 * Readout from the Magnetic Field Calibrated report.
MultipleMonomials 0:f677e13975d0 215 * Represents the magnetic field read by the chip in uT in the X, Y, and Z axes
MultipleMonomials 0:f677e13975d0 216 */
MultipleMonomials 0:f677e13975d0 217 TVector3 magField;
MultipleMonomials 0:f677e13975d0 218
MultipleMonomials 0:f677e13975d0 219 /**
MultipleMonomials 0:f677e13975d0 220 * Readout from the Rotation Vector report.
MultipleMonomials 0:f677e13975d0 221 * Represents the rotation of the IMU (relative to magnetic north) in radians.
MultipleMonomials 0:f677e13975d0 222 */
MultipleMonomials 0:f677e13975d0 223 Quaternion rotationVector;
MultipleMonomials 0:f677e13975d0 224
MultipleMonomials 0:f677e13975d0 225 /**
MultipleMonomials 0:f677e13975d0 226 * Auxillary accuracy readout from the Rotation Vector report.
MultipleMonomials 0:f677e13975d0 227 * Represents the estimated accuracy of the rotation vector in radians.
MultipleMonomials 0:f677e13975d0 228 */
MultipleMonomials 0:f677e13975d0 229 float rotationAccuracy;
MultipleMonomials 0:f677e13975d0 230
MultipleMonomials 0:f677e13975d0 231 /**
MultipleMonomials 0:f677e13975d0 232 * Readout from the Game Rotation Vector report.
MultipleMonomials 0:f677e13975d0 233 * Represents the rotation of the IMU in radians. Unlike the regular rotation vector, the Game Rotation Vector
MultipleMonomials 0:f677e13975d0 234 * is not referenced against the magnetic field and the "zero yaw" point is arbitrary.
MultipleMonomials 0:f677e13975d0 235 */
MultipleMonomials 0:f677e13975d0 236 Quaternion gameRotationVector;
MultipleMonomials 0:f677e13975d0 237
MultipleMonomials 0:f677e13975d0 238 /**
MultipleMonomials 0:f677e13975d0 239 * Readout from the Geomagnetic Rotation Vector report.
MultipleMonomials 0:f677e13975d0 240 * Represents the geomagnetic rotation of the IMU (relative to magnetic north) in radians.
MultipleMonomials 0:f677e13975d0 241 */
MultipleMonomials 0:f677e13975d0 242 Quaternion geomagneticRotationVector;
MultipleMonomials 0:f677e13975d0 243
MultipleMonomials 0:f677e13975d0 244 /**
MultipleMonomials 0:f677e13975d0 245 * Auxillary accuracy readout from the Geomagnetic Rotation Vector report.
MultipleMonomials 0:f677e13975d0 246 * Represents the estimated accuracy of the rotation vector in radians.
MultipleMonomials 0:f677e13975d0 247 */
MultipleMonomials 0:f677e13975d0 248 float geomagneticRotationAccuracy;
MultipleMonomials 0:f677e13975d0 249
MultipleMonomials 0:f677e13975d0 250 /**
MultipleMonomials 0:f677e13975d0 251 * Tap readout from the Tap Detector report. This flag is set to true whenever a tap is detected, and you should
MultipleMonomials 0:f677e13975d0 252 * manually clear it when you have processed the tap.
MultipleMonomials 0:f677e13975d0 253 */
MultipleMonomials 0:f677e13975d0 254 bool tapDetected;
MultipleMonomials 0:f677e13975d0 255
MultipleMonomials 0:f677e13975d0 256 /**
MultipleMonomials 0:f677e13975d0 257 * Whether the last tap detected was a single or double tap.
MultipleMonomials 0:f677e13975d0 258 */
MultipleMonomials 0:f677e13975d0 259 bool doubleTap;
MultipleMonomials 0:f677e13975d0 260
MultipleMonomials 0:f677e13975d0 261
MultipleMonomials 0:f677e13975d0 262 // Management functions
MultipleMonomials 0:f677e13975d0 263 //-----------------------------------------------------------------------------------------------------------------
MultipleMonomials 0:f677e13975d0 264
MultipleMonomials 0:f677e13975d0 265 /**
MultipleMonomials 0:f677e13975d0 266 * Construct a BNO080, providing pins and parameters.
MultipleMonomials 0:f677e13975d0 267 *
MultipleMonomials 0:f677e13975d0 268 * NOTE: while some schematics tell you to connect the BOOTN and WAKEN pins to the processor, this driver does not use or require them.
MultipleMonomials 0:f677e13975d0 269 * Just tie them both to VCC per the datasheet.
MultipleMonomials 0:f677e13975d0 270 *
MultipleMonomials 0:f677e13975d0 271 * @param debugPort Serial port to write output to. Cannot be nullptr.
MultipleMonomials 0:f677e13975d0 272 * @param user_SDApin Hardware SPI MOSI pin
MultipleMonomials 0:f677e13975d0 273 * @param user_SCLpin Hardware SPI MISO pin
MultipleMonomials 0:f677e13975d0 274 * @param user_SCLKPin Hardware SPI SCLK pin
MultipleMonomials 0:f677e13975d0 275 * @param user_CSPin SPI CS pin. Can be any IO pin, no restrictions.
MultipleMonomials 0:f677e13975d0 276 * @param user_INTPin Input pin connected to HINTN
MultipleMonomials 0:f677e13975d0 277 * @param user_RSTPin Output pin connected to NRST
MultipleMonomials 0:f677e13975d0 278 * @param i2cPortSpeed SPI frequency. The BNO's max is 3Mhz, we default to 300Khz for safety.
MultipleMonomials 0:f677e13975d0 279 */
MultipleMonomials 0:f677e13975d0 280 BNO080(Serial *debugPort, PinName user_SDApin, PinName user_SCLpin, PinName user_INTPin, PinName user_RSTPin,
MultipleMonomials 0:f677e13975d0 281 uint8_t i2cAddress=0x4a, int i2cPortSpeed=400000);
MultipleMonomials 0:f677e13975d0 282
MultipleMonomials 0:f677e13975d0 283 /**
MultipleMonomials 0:f677e13975d0 284 * Resets and connects to the IMU.
MultipleMonomials 0:f677e13975d0 285 *
MultipleMonomials 0:f677e13975d0 286 * If this function is failing, it would be a good idea to turn on BNO_DEBUG in the cpp file to get detailed output
MultipleMonomials 0:f677e13975d0 287 *
MultipleMonomials 0:f677e13975d0 288 * @return whether or not initialization was successful
MultipleMonomials 0:f677e13975d0 289 */
MultipleMonomials 0:f677e13975d0 290 bool begin();
MultipleMonomials 0:f677e13975d0 291
MultipleMonomials 0:f677e13975d0 292 /**
MultipleMonomials 0:f677e13975d0 293 * Tells the IMU to use its current rotation vector as the "zero" rotation vector and to reorient
MultipleMonomials 0:f677e13975d0 294 * all outputs accordingly.
MultipleMonomials 0:f677e13975d0 295 *
MultipleMonomials 0:f677e13975d0 296 * @param zOnly If true, only the rotation about the Z axis (the heading) will be tared.
MultipleMonomials 0:f677e13975d0 297 */
MultipleMonomials 0:f677e13975d0 298 void tare(bool zOnly = false);
MultipleMonomials 0:f677e13975d0 299
MultipleMonomials 0:f677e13975d0 300 /**
MultipleMonomials 0:f677e13975d0 301 * Tells the IMU to begin a dynamic sensor calibration. To calibrate the IMU, call this function and move
MultipleMonomials 0:f677e13975d0 302 * the IMU according to the instructions in the "BNO080 Sensor Calibration Procedure" app note
MultipleMonomials 0:f677e13975d0 303 * (http://www.hillcrestlabs.com/download/59de9014566d0727bd002ae7).
MultipleMonomials 0:f677e13975d0 304 *
MultipleMonomials 0:f677e13975d0 305 * To tell when the calibration is complete, look at the status bits for Game Rotation Vector (for accel and gyro)
MultipleMonomials 0:f677e13975d0 306 * and Magnetic Field (for the magnetometer).
MultipleMonomials 0:f677e13975d0 307 *
MultipleMonomials 0:f677e13975d0 308 * The gyro and accelerometer should only need to be calibrated once, but the magnetometer will need to be recalibrated
MultipleMonomials 0:f677e13975d0 309 * every time the orientation of ferrous metals around the IMU changes (e.g. when it is put into a new enclosure).
MultipleMonomials 0:f677e13975d0 310 *
MultipleMonomials 0:f677e13975d0 311 * The new calibration will not be saved in flash until you call saveCalibration().
MultipleMonomials 0:f677e13975d0 312 *
MultipleMonomials 0:f677e13975d0 313 * @param calibrateAccel Whether to calibrate the accelerometer.
MultipleMonomials 0:f677e13975d0 314 * @param calibrateGyro Whether to calibrate the gyro.
MultipleMonomials 0:f677e13975d0 315 * @param calibrateMag Whether to calibrate the magnetometer.
MultipleMonomials 0:f677e13975d0 316 */
MultipleMonomials 0:f677e13975d0 317 void startCalibration(bool calibrateAccel, bool calibrateGyro, bool calibrateMag);
MultipleMonomials 0:f677e13975d0 318
MultipleMonomials 0:f677e13975d0 319 /**
MultipleMonomials 0:f677e13975d0 320 * Saves the calibration started with startCalibration() and ends the calibration.
MultipleMonomials 0:f677e13975d0 321 * You will want to call this once the status bits read as "accuracy high".
MultipleMonomials 0:f677e13975d0 322 *
MultipleMonomials 0:f677e13975d0 323 * WARNING: if you paid for a factory calibrated IMU, then this WILL OVERWRITE THE FACTORY CALIBRATION in whatever sensors
MultipleMonomials 0:f677e13975d0 324 * are being calibrated. Use with caution!
MultipleMonomials 0:f677e13975d0 325 */
MultipleMonomials 0:f677e13975d0 326 void saveCalibration();
MultipleMonomials 0:f677e13975d0 327
MultipleMonomials 0:f677e13975d0 328 // Report functions
MultipleMonomials 0:f677e13975d0 329 //-----------------------------------------------------------------------------------------------------------------
MultipleMonomials 0:f677e13975d0 330
MultipleMonomials 0:f677e13975d0 331 /**
MultipleMonomials 0:f677e13975d0 332 * Checks for new data packets queued on the IMU.
MultipleMonomials 0:f677e13975d0 333 * If there are packets queued, receives all of them and updates
MultipleMonomials 0:f677e13975d0 334 * the class variables with the results.
MultipleMonomials 0:f677e13975d0 335 *
MultipleMonomials 0:f677e13975d0 336 * @return true iff new data was received
MultipleMonomials 0:f677e13975d0 337 */
MultipleMonomials 0:f677e13975d0 338 bool updateData();
MultipleMonomials 0:f677e13975d0 339
MultipleMonomials 0:f677e13975d0 340
MultipleMonomials 0:f677e13975d0 341 /**
MultipleMonomials 0:f677e13975d0 342 * Gets the status of a report as a 2 bit number.
MultipleMonomials 0:f677e13975d0 343 * per SH-2 section 6.5.1, this is interpreted as: <br>
MultipleMonomials 0:f677e13975d0 344 * 0 - unreliable <br>
MultipleMonomials 0:f677e13975d0 345 * 1 - accuracy low <br>
MultipleMonomials 0:f677e13975d0 346 * 2 - accuracy medium <br>
MultipleMonomials 0:f677e13975d0 347 * 3 - accuracy high <br>
MultipleMonomials 0:f677e13975d0 348 * of course, these are only updated if a given report is enabled.
MultipleMonomials 0:f677e13975d0 349 * @param report
MultipleMonomials 0:f677e13975d0 350 * @return
MultipleMonomials 0:f677e13975d0 351 */
MultipleMonomials 0:f677e13975d0 352 uint8_t getReportStatus(Report report);
MultipleMonomials 0:f677e13975d0 353
MultipleMonomials 0:f677e13975d0 354
MultipleMonomials 0:f677e13975d0 355 /**
MultipleMonomials 0:f677e13975d0 356 * Enable a data report from the IMU. Look at the comments above to see what the reports do.
MultipleMonomials 0:f677e13975d0 357 *
MultipleMonomials 0:f677e13975d0 358 * @param timeBetweenReports time in milliseconds between data updates.
MultipleMonomials 0:f677e13975d0 359 */
MultipleMonomials 0:f677e13975d0 360 void enableReport(Report report, uint16_t timeBetweenReports);
MultipleMonomials 0:f677e13975d0 361
MultipleMonomials 0:f677e13975d0 362 private:
MultipleMonomials 0:f677e13975d0 363
MultipleMonomials 0:f677e13975d0 364 // internal utility functions
MultipleMonomials 0:f677e13975d0 365 //-----------------------------------------------------------------------------------------------------------------
MultipleMonomials 0:f677e13975d0 366
MultipleMonomials 0:f677e13975d0 367 /**
MultipleMonomials 0:f677e13975d0 368 * Processes the packet currently stored in the buffer, and updates class variables to reflect the data it contains
MultipleMonomials 0:f677e13975d0 369 */
MultipleMonomials 0:f677e13975d0 370 void processPacket();
MultipleMonomials 0:f677e13975d0 371
MultipleMonomials 0:f677e13975d0 372 /**
MultipleMonomials 0:f677e13975d0 373 * Processes the sensor data packet currently stored in the buffer.
MultipleMonomials 0:f677e13975d0 374 * Only called from processPacket()
MultipleMonomials 0:f677e13975d0 375 */
MultipleMonomials 0:f677e13975d0 376 void parseSensorDataPacket();
MultipleMonomials 0:f677e13975d0 377
MultipleMonomials 0:f677e13975d0 378 /**
MultipleMonomials 0:f677e13975d0 379 * Call to wait for a packet with the given parameters to come in.
MultipleMonomials 0:f677e13975d0 380 *
MultipleMonomials 0:f677e13975d0 381 * @param channel Channel of the packet
MultipleMonomials 0:f677e13975d0 382 * @param reportID Report ID (first data byte) of the packet
MultipleMonomials 0:f677e13975d0 383 * @param timeout how long to wait for the packet
MultipleMonomials 0:f677e13975d0 384 * @return true if the packet has been received, false if it timed out
MultipleMonomials 0:f677e13975d0 385 */
MultipleMonomials 0:f677e13975d0 386 bool waitForPacket(int channel, uint8_t reportID, float timeout = .125f);
MultipleMonomials 0:f677e13975d0 387
MultipleMonomials 0:f677e13975d0 388 /**
MultipleMonomials 0:f677e13975d0 389 * Given a Q value, converts fixed point floating to regular floating point number.
MultipleMonomials 0:f677e13975d0 390 * @param fixedPointValue
MultipleMonomials 0:f677e13975d0 391 * @param qPoint
MultipleMonomials 0:f677e13975d0 392 * @return
MultipleMonomials 0:f677e13975d0 393 */
MultipleMonomials 0:f677e13975d0 394 float qToFloat(int16_t fixedPointValue, uint8_t qPoint);
MultipleMonomials 0:f677e13975d0 395
MultipleMonomials 0:f677e13975d0 396 /**
MultipleMonomials 0:f677e13975d0 397 * Given a floating point value and a Q point, convert to Q
MultipleMonomials 0:f677e13975d0 398 * See https://en.wikipedia.org/wiki/Q_(number_format)
MultipleMonomials 0:f677e13975d0 399 * @param qFloat
MultipleMonomials 0:f677e13975d0 400 * @param qPoint
MultipleMonomials 0:f677e13975d0 401 * @return
MultipleMonomials 0:f677e13975d0 402 */
MultipleMonomials 0:f677e13975d0 403 int16_t floatToQ(float qFloat, uint8_t qPoint);
MultipleMonomials 0:f677e13975d0 404
MultipleMonomials 0:f677e13975d0 405 /**
MultipleMonomials 0:f677e13975d0 406 * Tell the sensor to do a command.
MultipleMonomials 0:f677e13975d0 407 * See SH-2 Reference Manual section 6.3.8 page 42, Command request
MultipleMonomials 0:f677e13975d0 408 * The caller is expected to set shtpData 3 though 11 prior to calling
MultipleMonomials 0:f677e13975d0 409 */
MultipleMonomials 0:f677e13975d0 410 void sendCommand(uint8_t command);
MultipleMonomials 0:f677e13975d0 411
MultipleMonomials 0:f677e13975d0 412 /**
MultipleMonomials 0:f677e13975d0 413 * Given a sensor's report ID, this tells the BNO080 to begin reporting the values.
MultipleMonomials 0:f677e13975d0 414 *
MultipleMonomials 0:f677e13975d0 415 * @param reportID
MultipleMonomials 0:f677e13975d0 416 * @param timeBetweenReports
MultipleMonomials 0:f677e13975d0 417 * @param specificConfig the specific config word. Useful for personal activity classifier.
MultipleMonomials 0:f677e13975d0 418 */
MultipleMonomials 0:f677e13975d0 419 void setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports, uint32_t specificConfig = 0);
MultipleMonomials 0:f677e13975d0 420
MultipleMonomials 0:f677e13975d0 421 /**
MultipleMonomials 0:f677e13975d0 422 * Reads a packet from the IMU and stores it in the class variables.
MultipleMonomials 0:f677e13975d0 423 *
MultipleMonomials 0:f677e13975d0 424 * @param timeout how long to wait for there to be a packet
MultipleMonomials 0:f677e13975d0 425 *
MultipleMonomials 0:f677e13975d0 426 * @return whether a packet was recieved.
MultipleMonomials 0:f677e13975d0 427 */
MultipleMonomials 0:f677e13975d0 428 bool receivePacket(float timeout=.2f);
MultipleMonomials 0:f677e13975d0 429
MultipleMonomials 0:f677e13975d0 430 /**
MultipleMonomials 0:f677e13975d0 431 * Sends the current shtpData contents to the BNO. It's a good idea to disable interrupts before you call this.
MultipleMonomials 0:f677e13975d0 432 *
MultipleMonomials 0:f677e13975d0 433 * @param channelNumber the channel to send on
MultipleMonomials 0:f677e13975d0 434 * @param dataLength How many bits of shtpData to send
MultipleMonomials 0:f677e13975d0 435 * @return
MultipleMonomials 0:f677e13975d0 436 */
MultipleMonomials 0:f677e13975d0 437 bool sendPacket(uint8_t channelNumber, uint8_t dataLength);
MultipleMonomials 0:f677e13975d0 438
MultipleMonomials 0:f677e13975d0 439 /**
MultipleMonomials 0:f677e13975d0 440 * Prints the current shtp packet stored in the buffer.
MultipleMonomials 0:f677e13975d0 441 * @param length
MultipleMonomials 0:f677e13975d0 442 */
MultipleMonomials 0:f677e13975d0 443 void printPacket();
MultipleMonomials 0:f677e13975d0 444
MultipleMonomials 0:f677e13975d0 445 /**
MultipleMonomials 0:f677e13975d0 446 * Erases the current SHTP packet buffer so new data can be written
MultipleMonomials 0:f677e13975d0 447 */
MultipleMonomials 0:f677e13975d0 448 void zeroBuffer();
MultipleMonomials 0:f677e13975d0 449
MultipleMonomials 0:f677e13975d0 450 };
MultipleMonomials 0:f677e13975d0 451
MultipleMonomials 0:f677e13975d0 452
MultipleMonomials 0:f677e13975d0 453 #endif //HAMSTER_BNO080_H