LSM9DS1-Adafruit-AHRS/calibration/calibration_data_collection.cpp

152 lines
No EOL
4.2 KiB
C++

/*
This code is a modified version of the standard code from
the Adafruit LSM9DS1 library.
Example I2C hardware setup with the TinyS3:
LSM9DS1 -------- ESP32
3V ------------- 3.3V
G -------------- GND or -
SCL ------------ 9
SDA ------------ 8
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h> // not used in this demo but required!
// i2c
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
#define LSM9DS1_SCK A5
#define LSM9DS1_MISO 12
#define LSM9DS1_MOSI A4
#define LSM9DS1_XGCS 6
#define LSM9DS1_MCS 5
// You can also use software SPI
//Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_SCK, LSM9DS1_MISO, LSM9DS1_MOSI, LSM9DS1_XGCS, LSM9DS1_MCS);
// Or hardware SPI! In this case, only CS pins are passed in
//Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_XGCS, LSM9DS1_MCS);
#define DELAY 100 // 100 ms between prints
int printCount = 0; // Counts the number of times we performed measurements
bool acc = true; // Switches between the accelerometer and magnetometer
void setupSensor()
{
// IMPORTANT: Accuracy is inversely proportional to the sensor range!
// Ringinator works with
// 1.) Set the accelerometer range
lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G, lsm.LSM9DS1_ACCELDATARATE_10HZ);
//lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_4G, lsm.LSM9DS1_ACCELDATARATE_119HZ);
//lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_8G, lsm.LSM9DS1_ACCELDATARATE_476HZ);
//lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_16G, lsm.LSM9DS1_ACCELDATARATE_952HZ);
// 2.) Set the magnetometer sensitivity
lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
//lsm.setupMag(lsm.LSM9DS1_MAGGAIN_8GAUSS);
//lsm.setupMag(lsm.LSM9DS1_MAGGAIN_12GAUSS);
//lsm.setupMag(lsm.LSM9DS1_MAGGAIN_16GAUSS);
// 3.) Setup the gyroscope
lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
//lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_500DPS);
//lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_2000DPS);
}
void setup()
{
Serial.begin(115200);
while (!Serial) {
delay(1); // will pause Zero, Leonardo, etc until serial console opens
}
Serial.println("LSM9DS1 data read demo");
// Try to initialise and warn if we couldn't detect the chip
if (!lsm.begin())
{
Serial.println("Oops ... unable to initialize the LSM9DS1. Check your wiring!");
while (1);
}
Serial.println("Found LSM9DS1 9DOF");
// helper to just set the default scaling we want, see above!
setupSensor();
delay(2000); // 2 Second delay because the TinyS3 has to be reset and the button press shakes the device
Serial.println("Collecting gyro data, hold still");
// get gyro offset
float gxa = 0, gya = 0, gza = 0; //change to double if number is big
for (int i = 0; i < 300; i++) {
lsm.read(); /* ask it to read in the data */
sensors_event_t a, m, g, temp;
lsm.getEvent(&a, &m, &g, &temp);
gxa += g.gyro.x;
gya += g.gyro.y;
gza += g.gyro.z;
}
Serial.println(F("gyro offsets"));
Serial.print(gxa / 300);
Serial.print(", ");
Serial.print(gya / 300);
Serial.print(", ");
Serial.println(gza / 300);
Serial.println();
Serial.println(F("rotate slowly and carefully in 3D"));
delay(3000);
Serial.println("starting");
}
void loop()
{
lsm.read(); /* ask it to read in the data */
/* Get a new sensor event */
sensors_event_t a, m, g, temp;
lsm.getEvent(&a, &m, &g, &temp);
if (printCount < 300 && acc) {
Serial.print(a.acceleration.x);
Serial.print(", ");
Serial.print(a.acceleration.y);
Serial.print(", ");
Serial.println(a.acceleration.z);
printCount++;
} else if (printCount < 300 && !acc) {
Serial.print(m.magnetic.x);
Serial.print(", ");
Serial.print(m.magnetic.y);
Serial.print(", ");
Serial.println(m.magnetic.z);
printCount++;
} else {
Serial.println("Done.");
delay(4000);
printCount = 0;
if (acc) {
Serial.println(F("rotate slowly and carefully in 3D"));
delay(3000);
Serial.println("starting");
acc = false;
} else {
while(1); // HALT
}
}
delay(DELAY);
}