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