Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 510d0f10

History | View | Annotate | Download (22.681 KB)

1 5d8d461e Jan Hoffmann
/*!
2
 * @file Adafruit_BNO055.cpp
3
 *
4
 *  @mainpage Adafruit BNO055 Orientation Sensor
5 75f03d2e Szymon Kaliski
 *
6 5d8d461e Jan Hoffmann
 *  @section intro_sec Introduction
7
 *
8 75f03d2e Szymon Kaliski
 *  This is a library for the BNO055 orientation sensor
9 5d8d461e Jan Hoffmann
 *
10 75f03d2e Szymon Kaliski
 *  Designed specifically to work with the Adafruit BNO055 Breakout.
11 5d8d461e Jan Hoffmann
 *
12 75f03d2e Szymon Kaliski
 *  Pick one up today in the adafruit shop!
13
 *  ------> https://www.adafruit.com/product/2472
14 5d8d461e Jan Hoffmann
 *
15 75f03d2e Szymon Kaliski
 *  These sensors use I2C to communicate, 2 pins are required to interface.
16 5d8d461e Jan Hoffmann
 *
17 75f03d2e Szymon Kaliski
 *  Adafruit invests time and resources providing this open source code,
18
 *  please support Adafruit andopen-source hardware by purchasing products
19
 *  from Adafruit!
20 5d8d461e Jan Hoffmann
 *
21
 *  @section author Author
22
 *
23
 *  K.Townsend (Adafruit Industries)
24
 *
25 75f03d2e Szymon Kaliski
 *  @section license License
26
 *
27
 *  MIT license, all text above must be included in any redistribution
28 5d8d461e Jan Hoffmann
 */
29 4bc1c0c1 Kevin Townsend
30 55e2f6d1 Jan Hoffmann
#include "Arduino.h"
31 4bc1c0c1 Kevin Townsend
32
#include <limits.h>
33 55e2f6d1 Jan Hoffmann
#include <math.h>
34 4bc1c0c1 Kevin Townsend
35
#include "Adafruit_BNO055.h"
36
37
/*!
38 5d8d461e Jan Hoffmann
 *  @brief  Instantiates a new Adafruit_BNO055 class
39 9a8e8b26 Jan Hoffmann
 *  @param  sensorID
40
 *          sensor ID
41
 *  @param  address
42
 *          i2c address
43 dbc34b4d Jan Hoffmann
 *  @param  *theWire
44
 *          Wire object
45 5d8d461e Jan Hoffmann
 */
46 9a8e8b26 Jan Hoffmann
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address,
47
                                 TwoWire *theWire) {
48
  _sensorID = sensorID;
49
  _address = address;
50 83911fa6 Jan Hoffmann
  _wire = theWire;
51 9a8e8b26 Jan Hoffmann
}
52 f7556b0d Jan Hoffmann
53
/*!
54
 *  @brief  Sets up the HW
55
 *  @param  mode
56
 *          mode values
57
 *           [OPERATION_MODE_CONFIG,
58
 *            OPERATION_MODE_ACCONLY,
59
 *            OPERATION_MODE_MAGONLY,
60
 *            OPERATION_MODE_GYRONLY,
61
 *            OPERATION_MODE_ACCMAG,
62
 *            OPERATION_MODE_ACCGYRO,
63
 *            OPERATION_MODE_MAGGYRO,
64
 *            OPERATION_MODE_AMG,
65
 *            OPERATION_MODE_IMUPLUS,
66
 *            OPERATION_MODE_COMPASS,
67
 *            OPERATION_MODE_M4G,
68
 *            OPERATION_MODE_NDOF_FMC_OFF,
69
 *            OPERATION_MODE_NDOF]
70
 *  @return true if process is successful
71
 */
72 9a8e8b26 Jan Hoffmann
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode) {
73
#if defined(ARDUINO_SAMD_ZERO) && (_address == BNO055_ADDRESS_A)
74 f7556b0d Jan Hoffmann
#error                                                                         \
75
    "On an arduino Zero, BNO055's ADR pin must be high. Fix that, then delete this line."
76
  _address = BNO055_ADDRESS_B;
77
#endif
78
79 463eabf7 Wetmelon
  /* Enable I2C */
80 f7556b0d Jan Hoffmann
  _wire->begin();
81 463eabf7 Wetmelon
82 9a8e8b26 Jan Hoffmann
  // BNO055 clock stretches for 500us or more!
83 78cc710f ladyada
#ifdef ESP8266
84 9a8e8b26 Jan Hoffmann
  _wire->setClockStretchLimit(1000); // Allow for 1000us of clock stretching
85 78cc710f ladyada
#endif
86
87 463eabf7 Wetmelon
  /* Make sure we have the right device */
88
  uint8_t id = read8(BNO055_CHIP_ID_ADDR);
89 55e2f6d1 Jan Hoffmann
  if (id != BNO055_ID) {
90 9a8e8b26 Jan Hoffmann
    delay(1000); // hold on for boot
91 463eabf7 Wetmelon
    id = read8(BNO055_CHIP_ID_ADDR);
92 55e2f6d1 Jan Hoffmann
    if (id != BNO055_ID) {
93 9a8e8b26 Jan Hoffmann
      return false; // still not? ok bail
94 463eabf7 Wetmelon
    }
95
  }
96
97
  /* Switch to config mode (just in case since this is the default) */
98
  setMode(OPERATION_MODE_CONFIG);
99
100
  /* Reset */
101
  write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
102 55e2f6d1 Jan Hoffmann
  while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID) {
103 463eabf7 Wetmelon
    delay(10);
104
  }
105
  delay(50);
106
107
  /* Set to normal power mode */
108
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
109
  delay(10);
110
111
  write8(BNO055_PAGE_ID_ADDR, 0);
112
113 9a8e8b26 Jan Hoffmann
  /* Set the output units */
114
  /*
115
  uint8_t unitsel = (0 << 7) | // Orientation = Android
116
                    (0 << 4) | // Temperature = Celsius
117
                    (0 << 2) | // Euler = Degrees
118
                    (1 << 1) | // Gyro = Rads
119
                    (0 << 0);  // Accelerometer = m/s^2
120
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
121
  */
122 463eabf7 Wetmelon
123 378858ec Shunya Sato
  /* Configure axis mapping (see section 3.4) */
124 9a8e8b26 Jan Hoffmann
  /*
125
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
126
  delay(10);
127
  write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
128
  delay(10);
129
  */
130
131 463eabf7 Wetmelon
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
132
  delay(10);
133
  /* Set the requested operating mode (see section 3.3) */
134
  setMode(mode);
135
  delay(20);
136
137
  return true;
138 4bc1c0c1 Kevin Townsend
}
139
140
/*!
141 5d8d461e Jan Hoffmann
 *  @brief  Puts the chip in the specified operating mode
142
 *  @param  mode
143 f7556b0d Jan Hoffmann
 *          mode values
144
 *           [OPERATION_MODE_CONFIG,
145
 *            OPERATION_MODE_ACCONLY,
146
 *            OPERATION_MODE_MAGONLY,
147
 *            OPERATION_MODE_GYRONLY,
148
 *            OPERATION_MODE_ACCMAG,
149
 *            OPERATION_MODE_ACCGYRO,
150
 *            OPERATION_MODE_MAGGYRO,
151
 *            OPERATION_MODE_AMG,
152
 *            OPERATION_MODE_IMUPLUS,
153
 *            OPERATION_MODE_COMPASS,
154
 *            OPERATION_MODE_M4G,
155
 *            OPERATION_MODE_NDOF_FMC_OFF,
156
 *            OPERATION_MODE_NDOF]
157 5d8d461e Jan Hoffmann
 */
158 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode) {
159 463eabf7 Wetmelon
  _mode = mode;
160
  write8(BNO055_OPR_MODE_ADDR, _mode);
161
  delay(30);
162 4bc1c0c1 Kevin Townsend
}
163
164
/*!
165 5d8d461e Jan Hoffmann
 *  @brief  Changes the chip's axis remap
166
 *  @param  remapcode
167 f7556b0d Jan Hoffmann
 *          remap code possible values
168
 *          [REMAP_CONFIG_P0
169
 *           REMAP_CONFIG_P1 (default)
170
 *           REMAP_CONFIG_P2
171
 *           REMAP_CONFIG_P3
172
 *           REMAP_CONFIG_P4
173
 *           REMAP_CONFIG_P5
174
 *           REMAP_CONFIG_P6
175
 *           REMAP_CONFIG_P7]
176 5d8d461e Jan Hoffmann
 */
177 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setAxisRemap(
178
    adafruit_bno055_axis_remap_config_t remapcode) {
179 a97e0fe1 kA®0šhî
  adafruit_bno055_opmode_t modeback = _mode;
180
181
  setMode(OPERATION_MODE_CONFIG);
182
  delay(25);
183
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, remapcode);
184
  delay(10);
185
  /* Set the requested operating mode (see section 3.3) */
186
  setMode(modeback);
187
  delay(20);
188
}
189
190
/*!
191 5d8d461e Jan Hoffmann
 *  @brief  Changes the chip's axis signs
192
 *  @param  remapsign
193 f7556b0d Jan Hoffmann
 *          remap sign possible values
194
 *          [REMAP_SIGN_P0
195
 *           REMAP_SIGN_P1 (default)
196
 *           REMAP_SIGN_P2
197
 *           REMAP_SIGN_P3
198
 *           REMAP_SIGN_P4
199
 *           REMAP_SIGN_P5
200
 *           REMAP_SIGN_P6
201
 *           REMAP_SIGN_P7]
202 5d8d461e Jan Hoffmann
 */
203 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setAxisSign(adafruit_bno055_axis_remap_sign_t remapsign) {
204 a97e0fe1 kA®0šhî
  adafruit_bno055_opmode_t modeback = _mode;
205
206
  setMode(OPERATION_MODE_CONFIG);
207
  delay(25);
208
  write8(BNO055_AXIS_MAP_SIGN_ADDR, remapsign);
209
  delay(10);
210
  /* Set the requested operating mode (see section 3.3) */
211
  setMode(modeback);
212
  delay(20);
213
}
214
215
/*!
216 5d8d461e Jan Hoffmann
 *  @brief  Use the external 32.768KHz crystal
217 f7556b0d Jan Hoffmann
 *  @param  usextal
218
 *          use external crystal boolean