Skip to content

Commit 4120b1f

Browse files
bstbudlucasguocnaliphys
authored
example: add example for IMU sensor range settings (#65)
* example: add example for IMU sensor range settings * Reduce Serial transfer -> Solve garble serial out * Add comments to `IMURangeSettings.ino` * Add missing `;` * Add option to print values in g --------- Co-authored-by: zg guo <[email protected]> Co-authored-by: Ali Jahangiri <[email protected]>
1 parent 49ea46e commit 4120b1f

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* IMURangeSettings
3+
*
4+
* This sketch demonstrates how to configure the acceleration (SENSOR_ID_ACC) and gyroscope (SENSOR_ID_GYRO) range values.
5+
* In the setup function, the default ranges are printed to the serial for the acceleration and gravity (+/-8g).
6+
* Then, the range is modified to +/-4g, and is confirmed by printing the value of .range method to the Serial monitor. Finally,
7+
* the setup function prints the default range value for the gyroscope (+/-2000 dps) and then modifies this to +/-1000 dps.
8+
*
9+
* Every second, the loop function prints out acceleration and gyroscope values to the Serial port.
10+
*
11+
*
12+
*/
13+
14+
#include "Arduino_BHY2.h"
15+
16+
// declare 3D sensor instances
17+
// default ADC range is -32768 to 32767 (16 bit)
18+
SensorXYZ accel(SENSOR_ID_ACC);
19+
SensorXYZ gyro(SENSOR_ID_GYRO);
20+
SensorXYZ gravity(SENSOR_ID_GRA);
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
while(!Serial);
25+
26+
BHY2.begin();
27+
28+
accel.begin();
29+
gyro.begin();
30+
31+
delay(1000);
32+
33+
SensorConfig cfg = accel.getConfiguration();
34+
35+
Serial.println("Default Range values:");
36+
Serial.println(String("range of accel: +/-") + cfg.range + String("g"));
37+
cfg = gravity.getConfiguration();
38+
Serial.println(String("range of gravity: +/-") + cfg.range + String("g"));
39+
40+
accel.setRange(4); //this sets the range of accel to +/-4g,
41+
Serial.println("Modified Range values:");
42+
cfg = accel.getConfiguration();
43+
Serial.println(String("range of accel: +/-") + cfg.range + String("g"));
44+
cfg = gravity.getConfiguration();
45+
Serial.println(String("range of gravity: +/-") + cfg.range + String("g"));
46+
47+
cfg = gyro.getConfiguration();
48+
Serial.println(String("range of gyro: +/-") + cfg.range + String("dps"));
49+
gyro.setRange(1000); //this sets the range of gyro to +/-1000dps,
50+
cfg = gyro.getConfiguration();
51+
Serial.println(String("range of gyro: +/-") + cfg.range + String("dps"));
52+
}
53+
54+
void loop() {
55+
static auto printTime = millis();
56+
57+
// Update function should be continuously polled
58+
BHY2.update();
59+
60+
// print gyroscope/acceleration data once every second
61+
if (millis() - printTime >= 1000) {
62+
printTime = millis();
63+
Serial.print(String("acceleration (raw): ") + accel.toString());
64+
65+
float accelX = ((float)accel.x() / 32768.0) *4;
66+
float accelY = ((float)accel.y() / 32768.0) *4;
67+
float accelZ = ((float)accel.z() / 32768.0) *4;
68+
String accelInG = String("X: ") + String(accelX) + String(" Y: ") + String(accelY) + String(" Z: ") + String(accelZ);
69+
70+
Serial.println(String("acceleration (g): ") + accelInG);
71+
Serial.println(String("gyroscope: ") + gyro.toString());
72+
}
73+
}

examples/Standalone/Standalone.ino

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ void setup()
2626
temp.begin();
2727
gas.begin();
2828
rotation.begin();
29+
30+
SensorConfig cfg = accel.getConfiguration();
31+
Serial.println(String("range of accel: +/-") + cfg.range + String("g"));
32+
accel.setRange(2); //this sets the range of accel to +/-4g,
33+
cfg = accel.getConfiguration();
34+
Serial.println(String("range of accel: +/-") + cfg.range + String("g"));
35+
36+
cfg = gyro.getConfiguration();
37+
Serial.println(String("range of gyro: +/-") + cfg.range + String("dps"));
38+
gyro.setRange(1000);
39+
cfg = gyro.getConfiguration();
40+
Serial.println(String("range of gyro: +/-") + cfg.range + String("dps"));
2941
}
3042

3143
void loop()

src/sensors/SensorClass.h

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ class SensorClass {
2121
*/
2222
bool begin(float rate = 1000, uint32_t latency = 1);
2323
void configure(float rate, uint32_t latency);
24+
/*
25+
* parameter range:
26+
* for accelerometer, the range parameter is in the units of g (1g = ~9.80665 m/s^2)
27+
* valid values:
28+
* 2 (+/-2g), 4 (+/-4g), 8 (+/-8g), 16 (+/-16g),
29+
*
30+
* for gyroscope, the range parameter is in the units of dps (degrees per second)
31+
* valid values:
32+
* 125 (+/-125 dps)
33+
* 250 (+/-250 dps)
34+
* 500 (+/-500 dps)
35+
* 1000 (+/-1000 dps)
36+
* 2000 (+/-2000 dps)
37+
*
38+
* for other sensors, the range is defined based on the physical driver implementation
39+
* please note that: changing the range of one virtual sensor might affect the other virtual sensors which share the same underlying physical sensor, e.g.:
40+
* changing the accelerometer range will at the same time impact the sensing range for the gravity virtual sensor,
41+
* for more details please refer to the datasheet of BHI260AP, section "Change Sensor Dynamic Range"
42+
*/
2443
int setRange(uint16_t range);
2544
const SensorConfig getConfiguration();
2645
void end();

0 commit comments

Comments
 (0)