diff options
author | HampusM <hampus@hampusmat.com> | 2021-12-22 20:04:18 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-12-22 20:04:18 +0100 |
commit | 379f727926836dcc71dcbb4341d6b19cff18dd0f (patch) | |
tree | 649bcf06489fe6c0e1fd4de669eff2107f9b8c61 /src/gyronardo.cpp | |
parent | 365ff332362822a2742a5d6653310f6e9eb4f96d (diff) |
feat: add automatic sensor calibration
Diffstat (limited to 'src/gyronardo.cpp')
-rw-r--r-- | src/gyronardo.cpp | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/src/gyronardo.cpp b/src/gyronardo.cpp index 1a81850..025bb7d 100644 --- a/src/gyronardo.cpp +++ b/src/gyronardo.cpp @@ -4,6 +4,14 @@ Sensor sensor(0x68, &Wire); +/** + * Stops code execution. + */ +void stop() +{ + while (true) {} +} + void setup() { Serial.begin(9600); @@ -21,18 +29,33 @@ void setup() sensor.setGyroSensitivity(1); // 500 degrees/s sensor.setThrottleEnabled(true); - Serial.println("start..."); - // Calibration values - sensor.accel_cal_x = 0.198; - sensor.accel_cal_y = -0.018; - sensor.gyro_cal_x = 0.780; - sensor.gyro_cal_y = -1.495; + Serial.println("Automatically calibrating sensor..."); + bool cal_status = sensor.autoCalibrate(); + + if (!cal_status) + { + Serial.print("Error: Automatic calibration timed out after "); + Serial.print(CALIBRATION_TIMEOUT); + Serial.println(" milliseconds"); + + stop(); + } + + Serial.println("Finished calibrating"); + + Serial.println("Starting..."); } void loop() { + if (!sensor.is_calibrated) + { + return; + } + sensor.read(); + float pitch = sensor.getPitch(); float roll = sensor.getRoll(); |