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+ }
0 commit comments