Skip to content

Commit d2c0895

Browse files
authored
Merge leads_vec_power/Pedal and leads_vec_wsc/Accelerometer into LEADS-Arduino (#484)
* Removed `Pedal`. (#482) * Replaced `Accelerometer` with a more specific derived class `BNO08x`. (#482) * Created a local copy of `Adafruit_BNO08x_RVC`. (#482)
1 parent 72e16dc commit d2c0895

File tree

8 files changed

+213
-61
lines changed

8 files changed

+213
-61
lines changed

arduino/leads_vec_power/Pedal.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

arduino/leads_vec_power/Pedal.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

arduino/leads_vec_wsc/Accelerometer.h

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*!
2+
* @file Adafruit_BNO08x_RVC.cpp
3+
*
4+
* @mainpage Adafruit BNO08x RVC A simple library to use the UART-RVC mode of
5+
* the BNO08x sensors from Hillcrest Laboratories
6+
*
7+
* @section intro_sec Introduction
8+
*
9+
* I2C Driver for the Library for the BNO08x_RVC A simple library to use
10+
* the UART-RVC mode of the BNO08x sensors from Hillcrest Laboratories
11+
*
12+
* This is a library for the Adafruit BNO08x_RVC breakout:
13+
* https://www.adafruit.com/product/4754
14+
*
15+
* Adafruit invests time and resources providing this open source code,
16+
* please support Adafruit and open-source hardware by purchasing products from
17+
* Adafruit!
18+
*
19+
* @section dependencies Dependencies
20+
* This library depends on the Adafruit BusIO library
21+
*
22+
* This library depends on the Adafruit Unified Sensor library
23+
*
24+
* @section author Author
25+
*
26+
* Bryan Siepert for Adafruit Industries
27+
*
28+
* @section license License
29+
*
30+
* BSD (see license.txt)
31+
*
32+
* @section HISTORY
33+
*
34+
* v1.0 - First release
35+
*/
36+
37+
#include "Arduino.h"
38+
#include <Wire.h>
39+
40+
#include "Adafruit_BNO08x_RVC.h"
41+
42+
/**
43+
* @brief Construct a new Adafruit_BNO08x_RVC::Adafruit_BNO08x_RVC object
44+
*
45+
*/
46+
Adafruit_BNO08x_RVC::Adafruit_BNO08x_RVC(void) {}
47+
48+
/**
49+
* @brief Destroy the Adafruit_BNO08x_RVC::Adafruit_BNO08x_RVC object
50+
*
51+
*/
52+
Adafruit_BNO08x_RVC::~Adafruit_BNO08x_RVC(void) {}
53+
54+
/*!
55+
* @brief Setups the hardware
56+
* @param theSerial
57+
* Pointer to Stream (HardwareSerial/SoftwareSerial) interface
58+
* @return True
59+
*/
60+
bool Adafruit_BNO08x_RVC::begin(Stream *theSerial) {
61+
serial_dev = theSerial;
62+
return true;
63+
}
64+
65+
/**
66+
* @brief Get the next available heading and acceleration data from the sensor
67+
*
68+
* @param heading pointer to a BNO08x_RVC_Data struct to hold the measurements
69+
* @return true: success false: failure
70+
*/
71+
bool Adafruit_BNO08x_RVC::read(BNO08x_RVC_Data *heading) {
72+
if (!heading) {
73+
return false;
74+
}
75+
76+
if (!serial_dev->available()) {
77+
return false;
78+
}
79+
if (serial_dev->peek() != 0xAA) {
80+
serial_dev->read();
81+
return false;
82+
}
83+
// Now read all 19 bytes
84+
85+
if (serial_dev->available() < 19) {
86+
return false;
87+
}
88+
// at this point we know there's at least 19 bytes available and the first
89+
if (serial_dev->read() != 0xAA) {
90+
// shouldn't happen baecause peek said it was 0xAA
91+
return false;
92+
}
93+
// make sure the next byte is the second 0xAA
94+
if (serial_dev->read() != 0xAA) {
95+
return false;
96+
}
97+
uint8_t buffer[19];
98+
// ok, we've got our header, read the actual data+crc
99+
if (!serial_dev->readBytes(buffer, 17)) {
100+
return false;
101+
};
102+
103+
uint8_t sum = 0;
104+
// get checksum ready
105+
for (uint8_t i = 0; i < 16; i++) {
106+
sum += buffer[i];
107+
}
108+
if (sum != buffer[16]) {
109+
return false;
110+
}
111+
112+
// The data comes in endian'd, this solves it so it works on all platforms
113+
int16_t buffer_16[6];
114+
115+
for (uint8_t i = 0; i < 6; i++) {
116+
117+
buffer_16[i] = (buffer[1 + (i * 2)]);
118+
buffer_16[i] += (buffer[1 + (i * 2) + 1] << 8);
119+
}
120+
heading->yaw = (float)buffer_16[0] * DEGREE_SCALE;
121+
heading->pitch = (float)buffer_16[1] * DEGREE_SCALE;
122+
heading->roll = (float)buffer_16[2] * DEGREE_SCALE;
123+
124+
heading->x_accel = (float)buffer_16[3] * MILLI_G_TO_MS2;
125+
heading->y_accel = (float)buffer_16[4] * MILLI_G_TO_MS2;
126+
heading->z_accel = (float)buffer_16[5] * MILLI_G_TO_MS2;
127+
128+
return true;
129+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*!
2+
* @file Adafruit_BNO08x_RVC.h
3+
*
4+
* I2C Driver for the Adafruit BNO08x RVC A simple library to use the
5+
*UART-RVC mode of the BNO08x sensors from Hillcrest Laboratories
6+
*
7+
* This is a library for use with thethe Adafruit BNO08x breakout:
8+
* https://www.adafruit.com/products/4754
9+
*
10+
* Adafruit invests time and resources providing this open source code,
11+
* please support Adafruit and open-source hardware by purchasing products from
12+
* Adafruit!
13+
*
14+
*
15+
* BSD license (see license.txt)
16+
*/
17+
18+
#ifndef _ADAFRUIT_BNO08x_RVC_H
19+
#define _ADAFRUIT_BNO08x_RVC_H
20+
21+
#define MILLI_G_TO_MS2 0.0098067 ///< Scalar to convert milli-gs to m/s^2
22+
#define DEGREE_SCALE 0.01 ///< To convert the degree values
23+
24+
#include "Arduino.h"
25+
26+
/*!
27+
* @brief Class that stores state and functions for interacting with
28+
* the BNO08x_RVC A simple library to use the UART-RVC mode of the
29+
* BNO08x sensors from Hillcrest Laboratories
30+
*/
31+
/**! Struct to hold a UART-RVC packet **/
32+
typedef struct BNO08xRVCData {
33+
float yaw, ///< Yaw in Degrees
34+
pitch, ///< Pitch in Degrees
35+
roll; ///< Roll in Degrees
36+
float x_accel, ///< The X acceleration value in m/s^2
37+
y_accel, ///< The Y acceleration value in m/s^2
38+
z_accel; ///< The Z acceleration value in m/s^2
39+
40+
} BNO08x_RVC_Data;
41+
42+
/**
43+
* @brief A class to interact with the BNO08x sensors from Hillcrest
44+
* Laboritories using the UART-RVC mode
45+
*
46+
*/
47+
class Adafruit_BNO08x_RVC {
48+
public:
49+
Adafruit_BNO08x_RVC();
50+
~Adafruit_BNO08x_RVC();
51+
52+
bool begin(Stream *theStream);
53+
bool read(BNO08x_RVC_Data *heading);
54+
55+
private:
56+
Stream *serial_dev;
57+
};
58+
59+
#endif
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
#include "Accelerometer.h"
1+
#include "BNO08x.h"
22

3-
String Acceleration::toString() {
4-
return String(yaw) + "," + pitch + "," + roll + "," + forwardAcceleration + "," + lateralAcceleration + "," + verticalAcceleration;
5-
}
6-
Accelerometer::Accelerometer(OnAccelerometerUpdate onUpdate) : _onUpdate(onUpdate) {}
7-
void Accelerometer::initialize(const ArrayList<String> &parentTags) {
8-
Device<Acceleration>::initialize(parentTags);
3+
BNO08x::BNO08x(OnAccelerometerUpdate onUpdate) : Accelerometer(onUpdate) {}
4+
void BNO08x::initialize(const ArrayList<String> &parentTags) {
5+
Accelerometer::initialize(parentTags);
96
Serial1.begin(115200);
107
while (!Serial1) delay(10);
118
if (!_rvc.begin(&Serial1)) delay(10);
129
}
13-
Acceleration Accelerometer::read() {
10+
Acceleration BNO08x::read() {
1411
BNO08x_RVC_Data heading;
1512
Acceleration r = Acceleration();
1613
if (!_rvc.read(&heading)) return r;

arduino/leads_vec_wsc/BNO08x.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef BNO08X_H
2+
#define BNO08X_H
3+
4+
5+
#include "Adafruit_BNO08x_RVC.h"
6+
#include "LEADS.h"
7+
8+
class BNO08x : public Accelerometer {
9+
protected:
10+
Adafruit_BNO08x_RVC _rvc = Adafruit_BNO08x_RVC();
11+
public:
12+
explicit BNO08x(OnAccelerometerUpdate onUpdate);
13+
void initialize(const ArrayList<String> &parentTags) override;
14+
Acceleration read() override;
15+
};
16+
17+
18+
#endif // BNO08X_H

arduino/leads_vec_wsc/leads_vec_wsc.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "Accelerometer.h"
1+
#include "BNO08x.h"
22

33
const int PIN_LFWSS[] = {2};
44
const int PIN_RFWSS[] = {3};
@@ -12,7 +12,7 @@ WheelSpeedSensor RFWSS{ArrayList<int>(PIN_RFWSS, 1), [](float ws) {returnFloat(P
1212
WheelSpeedSensor LRWSS{ArrayList<int>(PIN_LRWSS, 1), [](float ws) {returnFloat(P, LEFT_REAR_WHEEL_SPEED_SENSOR, ws);}};
1313
WheelSpeedSensor RRWSS{ArrayList<int>(PIN_RRWSS, 1), [](float ws) {returnFloat(P, RIGHT_REAR_WHEEL_SPEED_SENSOR, ws);}};
1414
WheelSpeedSensor CRWSS{ArrayList<int>(PIN_CRWSS, 1), [](float ws) {returnFloat(P, CENTER_REAR_WHEEL_SPEED_SENSOR, ws);}};
15-
Accelerometer ACCL{[](Acceleration acceleration) {returnString(P, ACCELEROMETER, acceleration.toString());}};
15+
BNO08x ACCL{[](Acceleration acceleration) {returnString(P, ACCELEROMETER, acceleration.toString());}};
1616

1717
void setup() {
1818
P.initializeAsRoot();

0 commit comments

Comments
 (0)