-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <lucas.guo.cn@gmail.com> Co-authored-by: Ali Jahangiri <75624145+aliphys@users.noreply.github.com>
- Loading branch information
1 parent
337855d
commit 8c97fed
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
Arduino_BHY2/examples/IMURangeSettings/IMURangeSettings.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* IMURangeSettings | ||
* | ||
* This sketch demonstrates how to configure the acceleration (SENSOR_ID_ACC) and gyroscope (SENSOR_ID_GYRO) range values. | ||
* In the setup function, the default ranges are printed to the serial for the acceleration and gravity (+/-8g). | ||
* Then, the range is modified to +/-4g, and is confirmed by printing the value of .range method to the Serial monitor. Finally, | ||
* the setup function prints the default range value for the gyroscope (+/-2000 dps) and then modifies this to +/-1000 dps. | ||
* | ||
* Every second, the loop function prints out acceleration and gyroscope values to the Serial port. | ||
* | ||
* | ||
*/ | ||
|
||
#include "Arduino_BHY2.h" | ||
|
||
// declare 3D sensor instances | ||
// default ADC range is -32768 to 32767 (16 bit) | ||
SensorXYZ accel(SENSOR_ID_ACC); | ||
SensorXYZ gyro(SENSOR_ID_GYRO); | ||
SensorXYZ gravity(SENSOR_ID_GRA); | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
while(!Serial); | ||
|
||
BHY2.begin(); | ||
|
||
accel.begin(); | ||
gyro.begin(); | ||
|
||
delay(1000); | ||
|
||
SensorConfig cfg = accel.getConfiguration(); | ||
|
||
Serial.println("Default Range values:"); | ||
Serial.println(String("range of accel: +/-") + cfg.range + String("g")); | ||
cfg = gravity.getConfiguration(); | ||
Serial.println(String("range of gravity: +/-") + cfg.range + String("g")); | ||
|
||
accel.setRange(4); //this sets the range of accel to +/-4g, | ||
Serial.println("Modified Range values:"); | ||
cfg = accel.getConfiguration(); | ||
Serial.println(String("range of accel: +/-") + cfg.range + String("g")); | ||
cfg = gravity.getConfiguration(); | ||
Serial.println(String("range of gravity: +/-") + cfg.range + String("g")); | ||
|
||
cfg = gyro.getConfiguration(); | ||
Serial.println(String("range of gyro: +/-") + cfg.range + String("dps")); | ||
gyro.setRange(1000); //this sets the range of gyro to +/-1000dps, | ||
cfg = gyro.getConfiguration(); | ||
Serial.println(String("range of gyro: +/-") + cfg.range + String("dps")); | ||
} | ||
|
||
void loop() { | ||
static auto printTime = millis(); | ||
|
||
// Update function should be continuously polled | ||
BHY2.update(); | ||
|
||
// print gyroscope/acceleration data once every second | ||
if (millis() - printTime >= 1000) { | ||
printTime = millis(); | ||
Serial.print(String("acceleration (raw): ") + accel.toString()); | ||
|
||
float accelX = ((float)accel.x() / 32768.0) *4; | ||
float accelY = ((float)accel.y() / 32768.0) *4; | ||
float accelZ = ((float)accel.z() / 32768.0) *4; | ||
String accelInG = String("X: ") + String(accelX) + String(" Y: ") + String(accelY) + String(" Z: ") + String(accelZ); | ||
|
||
Serial.println(String("acceleration (g): ") + accelInG); | ||
Serial.println(String("gyroscope: ") + gyro.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters