Implemented first Hangar-Service
Dependencies: CalibrateMagneto QuaternionMath
Fork of SML2 by
Accelerometer.cpp@11:d21275e60ebb, 2015-03-12 (annotated)
- Committer:
- pvaibhav
- Date:
- Thu Mar 12 10:16:01 2015 +0000
- Revision:
- 11:d21275e60ebb
- Parent:
- 9:a0ae9e68f95d
- Child:
- 12:1632d7391453
Magnetometer driver implemented including hall-compensation. Reading registers for all sensors uses big-endian sequential read.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pvaibhav | 1:c279bc3af90c | 1 | #include "Accelerometer.h" |
pvaibhav | 1:c279bc3af90c | 2 | #define DEBUG "BMX055-Acc" |
pvaibhav | 1:c279bc3af90c | 3 | #include "Logger.h" |
pvaibhav | 1:c279bc3af90c | 4 | |
pvaibhav | 8:cba37530d480 | 5 | #include "Vector3.h" |
pvaibhav | 8:cba37530d480 | 6 | |
pvaibhav | 7:604a8369b801 | 7 | Accelerometer::Accelerometer(I2C &i2c) : I2CPeripheral(i2c, 0x18 << 1 /* address */) |
pvaibhav | 7:604a8369b801 | 8 | { |
pvaibhav | 7:604a8369b801 | 9 | if (powerOn()) { |
pvaibhav | 5:b9f2f62a8f90 | 10 | INFO("Bosch Sensortec BMX055-Accel found"); |
pvaibhav | 7:604a8369b801 | 11 | powerOff(); |
pvaibhav | 1:c279bc3af90c | 12 | } else { |
pvaibhav | 7:604a8369b801 | 13 | WARN("Bosch Sensortec BMX055-Accel not found"); |
pvaibhav | 1:c279bc3af90c | 14 | } |
pvaibhav | 1:c279bc3af90c | 15 | } |
pvaibhav | 1:c279bc3af90c | 16 | |
pvaibhav | 7:604a8369b801 | 17 | bool Accelerometer::powerOn() |
pvaibhav | 7:604a8369b801 | 18 | { |
pvaibhav | 5:b9f2f62a8f90 | 19 | write_reg(0x14, 0xB6); // reset |
pvaibhav | 5:b9f2f62a8f90 | 20 | wait_ms(2); // page 11 says only 1.3ms, nothing for startup time, so assuming 2ms |
pvaibhav | 5:b9f2f62a8f90 | 21 | write_reg(0x11, 0); // set power normal mode |
pvaibhav | 7:604a8369b801 | 22 | write_reg(0x10, 15); // set bandwidth = 250 Hz |
pvaibhav | 7:604a8369b801 | 23 | return read_reg(0x00) == 0xfa; // verify chip ID |
pvaibhav | 5:b9f2f62a8f90 | 24 | } |
pvaibhav | 5:b9f2f62a8f90 | 25 | |
pvaibhav | 7:604a8369b801 | 26 | void Accelerometer::powerOff() |
pvaibhav | 7:604a8369b801 | 27 | { |
pvaibhav | 5:b9f2f62a8f90 | 28 | write_reg(0x11, 1); // deep suspend mode |
pvaibhav | 6:c12cea26842d | 29 | LOG("deep sleep"); |
pvaibhav | 5:b9f2f62a8f90 | 30 | } |
pvaibhav | 7:604a8369b801 | 31 | |
pvaibhav | 7:604a8369b801 | 32 | void Accelerometer::start() |
pvaibhav | 7:604a8369b801 | 33 | { |
pvaibhav | 8:cba37530d480 | 34 | // nothing to do right now |
pvaibhav | 7:604a8369b801 | 35 | } |
pvaibhav | 7:604a8369b801 | 36 | |
pvaibhav | 7:604a8369b801 | 37 | void Accelerometer::stop() |
pvaibhav | 7:604a8369b801 | 38 | { |
pvaibhav | 7:604a8369b801 | 39 | // nothing to do right now |
pvaibhav | 7:604a8369b801 | 40 | } |
pvaibhav | 7:604a8369b801 | 41 | |
pvaibhav | 8:cba37530d480 | 42 | Vector3 Accelerometer::read() |
pvaibhav | 7:604a8369b801 | 43 | { |
pvaibhav | 7:604a8369b801 | 44 | // Check comments in the read() function of the Gyroscope for more info why we read bytes one by one. |
pvaibhav | 7:604a8369b801 | 45 | union { |
pvaibhav | 7:604a8369b801 | 46 | uint8_t bytes[6]; |
pvaibhav | 7:604a8369b801 | 47 | struct { |
pvaibhav | 11:d21275e60ebb | 48 | int16_t z; |
pvaibhav | 7:604a8369b801 | 49 | int16_t y; |
pvaibhav | 11:d21275e60ebb | 50 | int16_t x; |
pvaibhav | 11:d21275e60ebb | 51 | } val; |
pvaibhav | 11:d21275e60ebb | 52 | } buffer; |
pvaibhav | 7:604a8369b801 | 53 | |
pvaibhav | 11:d21275e60ebb | 54 | size_t i = 6; |
pvaibhav | 11:d21275e60ebb | 55 | while (i --> 0) { |
pvaibhav | 11:d21275e60ebb | 56 | buffer.bytes[i] = read_reg(0x02 + i); |
pvaibhav | 11:d21275e60ebb | 57 | }; |
pvaibhav | 7:604a8369b801 | 58 | |
pvaibhav | 11:d21275e60ebb | 59 | const int16_t x = buffer.val.x / 16; |
pvaibhav | 11:d21275e60ebb | 60 | const int16_t y = buffer.val.y / 16; |
pvaibhav | 11:d21275e60ebb | 61 | const int16_t z = buffer.val.z / 16; |
pvaibhav | 9:a0ae9e68f95d | 62 | |
pvaibhav | 8:cba37530d480 | 63 | const float accel_resolution = 0.0009765625; |
pvaibhav | 8:cba37530d480 | 64 | |
pvaibhav | 11:d21275e60ebb | 65 | return Vector3(x, y, z) * accel_resolution; |
pvaibhav | 7:604a8369b801 | 66 | } |