Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 78cc710f

History | View | Annotate | Download (19.834 KB)

1 4bc1c0c1 Kevin Townsend
/***************************************************************************
2
  This is a library for the BNO055 orientation sensor
3

4
  Designed specifically to work with the Adafruit BNO055 Breakout.
5

6
  Pick one up today in the adafruit shop!
7
  ------> http://www.adafruit.com/products
8

9
  These sensors use I2C to communicate, 2 pins are required to interface.
10

11
  Adafruit invests time and resources providing this open source code,
12
  please support Adafruit andopen-source hardware by purchasing products
13
  from Adafruit!
14

15
  Written by KTOWN for Adafruit Industries.
16

17
  MIT license, all text above must be included in any redistribution
18 463eabf7 Wetmelon
 ***************************************************************************/
19 4bc1c0c1 Kevin Townsend
20
#if ARDUINO >= 100
21 463eabf7 Wetmelon
 #include "Arduino.h"
22 4bc1c0c1 Kevin Townsend
#else
23 463eabf7 Wetmelon
 #include "WProgram.h"
24 4bc1c0c1 Kevin Townsend
#endif
25
26
#include <math.h>
27
#include <limits.h>
28
29
#include "Adafruit_BNO055.h"
30
31
/***************************************************************************
32
 CONSTRUCTOR
33
 ***************************************************************************/
34 40f91f6f Tony DiCola
35 4bc1c0c1 Kevin Townsend
/**************************************************************************/
36
/*!
37 463eabf7 Wetmelon
    @brief  Instantiates a new Adafruit_BNO055 class
38
*/
39 4bc1c0c1 Kevin Townsend
/**************************************************************************/
40
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address)
41
{
42 463eabf7 Wetmelon
  _sensorID = sensorID;
43
  _address = address;
44 4bc1c0c1 Kevin Townsend
}
45
46
/***************************************************************************
47
 PUBLIC FUNCTIONS
48
 ***************************************************************************/
49
50
/**************************************************************************/
51
/*!
52 463eabf7 Wetmelon
    @brief  Sets up the HW
53
*/
54 4bc1c0c1 Kevin Townsend
/**************************************************************************/
55
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode)
56
{
57 463eabf7 Wetmelon
  /* Enable I2C */
58
  Wire.begin();
59
60 78cc710f ladyada
  // BNO055 clock stretches for 500us or more!
61
#ifdef ESP8266
62
  Wire.setClockStretchLimit(1000); // Allow for 1000us of clock stretching
63
#endif
64
65 463eabf7 Wetmelon
  /* Make sure we have the right device */
66
  uint8_t id = read8(BNO055_CHIP_ID_ADDR);
67
  if(id != BNO055_ID)
68
  {
69
    delay(1000); // hold on for boot
70
    id = read8(BNO055_CHIP_ID_ADDR);
71
    if(id != BNO055_ID) {
72
      return false;  // still not? ok bail
73
    }
74
  }
75
76
  /* Switch to config mode (just in case since this is the default) */
77
  setMode(OPERATION_MODE_CONFIG);
78
79
  /* Reset */
80
  write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
81
  while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID)
82
  {
83
    delay(10);
84
  }
85
  delay(50);
86
87
  /* Set to normal power mode */
88
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
89
  delay(10);
90
91
  write8(BNO055_PAGE_ID_ADDR, 0);
92
93
  /* Set the output units */
94
  /*
95
  uint8_t unitsel = (0 << 7) | // Orientation = Android
96
                    (0 << 4) | // Temperature = Celsius
97
                    (0 << 2) | // Euler = Degrees
98
                    (1 << 1) | // Gyro = Rads
99
                    (0 << 0);  // Accelerometer = m/s^2
100
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
101
  */
102
103 378858ec Shunya Sato
  /* Configure axis mapping (see section 3.4) */
104
  /*
105
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
106
  delay(10);
107
  write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
108
  delay(10);
109
  */
110
  
111 463eabf7 Wetmelon
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
112
  delay(10);
113
  /* Set the requested operating mode (see section 3.3) */
114
  setMode(mode);
115
  delay(20);
116
117
  return true;
118 4bc1c0c1 Kevin Townsend
}
119
120
/**************************************************************************/
121
/*!
122 463eabf7 Wetmelon
    @brief  Puts the chip in the specified operating mode
123
*/
124 4bc1c0c1 Kevin Townsend
/**************************************************************************/
125
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
126
{
127 463eabf7 Wetmelon
  _mode = mode;
128
  write8(BNO055_OPR_MODE_ADDR, _mode);
129
  delay(30);
130 4bc1c0c1 Kevin Townsend
}
131
132
/**************************************************************************/
133
/*!
134 463eabf7 Wetmelon
    @brief  Use the external 32.768KHz crystal
135
*/
136 c4f272e1 ladyada
/**************************************************************************/
137
void Adafruit_BNO055::setExtCrystalUse(boolean usextal)
138
{
139 463eabf7 Wetmelon
  adafruit_bno055_opmode_t modeback = _mode;
140
141
  /* Switch to config mode (just in case since this is the default) */
142
  setMode(OPERATION_MODE_CONFIG);
143
  delay(25);
144
  write8(BNO055_PAGE_ID_ADDR, 0);
145
  if (usextal) {
146
    write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
147
  } else {
148
    write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
149
  }
150
  delay(10);
151
  /* Set the requested operating mode (see section 3.3) */
152
  setMode(modeback);
153
  delay(20);
154 c4f272e1 ladyada
}
155
156
157
/**************************************************************************/
158
/*!
159 463eabf7 Wetmelon
    @brief  Gets the latest system status info
160
*/
161 4bc1c0c1 Kevin Townsend
/**************************************************************************/
162 3b2655dc ladyada
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
163 4bc1c0c1 Kevin Townsend
{
164 463eabf7 Wetmelon
  write8(BNO055_PAGE_ID_ADDR, 0);
165
166
  /* System Status (see section 4.3.58)
167
     ---------------------------------
168
     0 = Idle
169
     1 = System Error
170
     2 = Initializing Peripherals
171
     3 = System Iniitalization
172
     4 = Executing Self-Test
173
     5 = Sensor fusio algorithm running
174
     6 = System running without fusion algorithms */
175
176
  if (system_status != 0)
177
    *system_status    = read8(BNO055_SYS_STAT_ADDR);
178
179
  /* Self Test Results (see section )
180
     --------------------------------
181
     1 = test passed, 0 = test failed
182

183
     Bit 0 = Accelerometer self test
184
     Bit 1 = Magnetometer self test
185
     Bit 2 = Gyroscope self test
186
     Bit 3 = MCU self test
187

188
     0x0F = all good! */
189
190
  if (self_test_result != 0)
191
    *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
192
193
  /* System Error (see section 4.3.59)
194
     ---------------------------------
195
     0 = No error
196
     1 = Peripheral initialization error
197
     2 = System initialization error
198
     3 = Self test result failed
199
     4 = Register map value out of range
200
     5 = Register map address out of range
201
     6 = Register map write error
202
     7 = BNO low power mode not available for selected operat ion mode
203
     8 = Accelerometer power mode not available
204
     9 = Fusion algorithm configuration error
205
     A = Sensor configuration error */
206
207
  if (system_error != 0)
208
    *system_error     = read8(BNO055_SYS_ERR_ADDR);
209
210
  delay(200);
211 4bc1c0c1 Kevin Townsend
}
212
213
/**************************************************************************/
214
/*!
215 463eabf7 Wetmelon
    @brief  Gets the chip revision numbers
216
*/
217 4bc1c0c1 Kevin Townsend
/**************************************************************************/
218
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
219
{
220 463eabf7 Wetmelon
  uint8_t a, b;
221 4bc1c0c1 Kevin Townsend
222 463eabf7 Wetmelon
  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));
223 4bc1c0c1 Kevin Townsend
224 463eabf7 Wetmelon
  /* Check the accelerometer revision */
225
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
226 67f3cff5 Kevin Townsend
227 463eabf7 Wetmelon
  /* Check the magnetometer revision */
228
  info->mag_rev   = read8(BNO055_MAG_REV_ID_ADDR);
229 67f3cff5 Kevin Townsend
230 463eabf7 Wetmelon
  /* Check the gyroscope revision */
231
  info->gyro_rev  = read8(BNO055_GYRO_REV_ID_ADDR);
232 67f3cff5 Kevin Townsend
233