Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino_Helpers.h>
- // Basic demo for accelerometer/gyro readings from Adafruit ISM330DHCX
- #include <Adafruit_ISM330DHCX.h>
- #include "SensorFusion.h" //SF
- #include <Arduino_Helpers.h>
- #include <AH/Math/Quaternion.hpp>
- #include <AH/Math/Vector.hpp>
- // For SPI mode, we need a CS pin
- #define LSM_CS 10
- // For software-SPI mode we need SCK/MOSI/MISO pins
- #define LSM_SCK 13
- #define LSM_MISO 12
- #define LSM_MOSI 11
- Adafruit_ISM330DHCX ism330dhcx;
- Adafruit_ISM330DHCX ism330dhcx2;
- SF fusion;
- float pitch, roll, yaw;
- float *quat;
- float deltat;
- Quaternion quat1;
- Quaternion quat2;
- void setup(void) {
- Serial.begin(9600);
- while (!Serial)
- delay(10); // will pause Zero, Leonardo, etc until serial console opens
- Serial.println("Adafruit ISM330DHCX test!");
- if (!ism330dhcx.begin_I2C(0x6A)) {
- // if (!ism330dhcx.begin_SPI(LSM_CS)) {
- // if (!ism330dhcx.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
- Serial.println("Failed to find ISM330DHCX 1 chip");
- while (1) {
- delay(10);
- }
- }
- Serial.println("ISM330DHCX 1 Found!");
- if (!ism330dhcx2.begin_I2C(0x6B)) {
- // if (!ism330dhcx.begin_SPI(LSM_CS)) {
- // if (!ism330dhcx.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
- Serial.println("Failed to find ISM330DHCX 2 chip");
- while (1) {
- delay(10);
- }
- }
- Serial.println("ISM330DHCX 2 Found!");
- // ism330dhcx.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
- Serial.print("Accelerometer range set to: ");
- switch (ism330dhcx.getAccelRange()) {
- case LSM6DS_ACCEL_RANGE_2_G:
- Serial.println("+-2G");
- break;
- case LSM6DS_ACCEL_RANGE_4_G:
- Serial.println("+-4G");
- break;
- case LSM6DS_ACCEL_RANGE_8_G:
- Serial.println("+-8G");
- break;
- case LSM6DS_ACCEL_RANGE_16_G:
- Serial.println("+-16G");
- break;
- }
- // ism330dhcx.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS);
- Serial.print("Gyro range set to: ");
- switch (ism330dhcx.getGyroRange()) {
- case LSM6DS_GYRO_RANGE_125_DPS:
- Serial.println("125 degrees/s");
- break;
- case LSM6DS_GYRO_RANGE_250_DPS:
- Serial.println("250 degrees/s");
- break;
- case LSM6DS_GYRO_RANGE_500_DPS:
- Serial.println("500 degrees/s");
- break;
- case LSM6DS_GYRO_RANGE_1000_DPS:
- Serial.println("1000 degrees/s");
- break;
- case LSM6DS_GYRO_RANGE_2000_DPS:
- Serial.println("2000 degrees/s");
- break;
- case ISM330DHCX_GYRO_RANGE_4000_DPS:
- Serial.println("4000 degrees/s");
- break;
- }
- // ism330dhcx.setAccelDataRate(LSM6DS_RATE_12_5_HZ);
- Serial.print("Accelerometer data rate set to: ");
- switch (ism330dhcx.getAccelDataRate()) {
- case LSM6DS_RATE_SHUTDOWN:
- Serial.println("0 Hz");
- break;
- case LSM6DS_RATE_12_5_HZ:
- Serial.println("12.5 Hz");
- break;
- case LSM6DS_RATE_26_HZ:
- Serial.println("26 Hz");
- break;
- case LSM6DS_RATE_52_HZ:
- Serial.println("52 Hz");
- break;
- case LSM6DS_RATE_104_HZ:
- Serial.println("104 Hz");
- break;
- case LSM6DS_RATE_208_HZ:
- Serial.println("208 Hz");
- break;
- case LSM6DS_RATE_416_HZ:
- Serial.println("416 Hz");
- break;
- case LSM6DS_RATE_833_HZ:
- Serial.println("833 Hz");
- break;
- case LSM6DS_RATE_1_66K_HZ:
- Serial.println("1.66 KHz");
- break;
- case LSM6DS_RATE_3_33K_HZ:
- Serial.println("3.33 KHz");
- break;
- case LSM6DS_RATE_6_66K_HZ:
- Serial.println("6.66 KHz");
- break;
- }
- // ism330dhcx.setGyroDataRate(LSM6DS_RATE_12_5_HZ);
- Serial.print("Gyro data rate set to: ");
- switch (ism330dhcx.getGyroDataRate()) {
- case LSM6DS_RATE_SHUTDOWN:
- Serial.println("0 Hz");
- break;
- case LSM6DS_RATE_12_5_HZ:
- Serial.println("12.5 Hz");
- break;
- case LSM6DS_RATE_26_HZ:
- Serial.println("26 Hz");
- break;
- case LSM6DS_RATE_52_HZ:
- Serial.println("52 Hz");
- break;
- case LSM6DS_RATE_104_HZ:
- Serial.println("104 Hz");
- break;
- case LSM6DS_RATE_208_HZ:
- Serial.println("208 Hz");
- break;
- case LSM6DS_RATE_416_HZ:
- Serial.println("416 Hz");
- break;
- case LSM6DS_RATE_833_HZ:
- Serial.println("833 Hz");
- break;
- case LSM6DS_RATE_1_66K_HZ:
- Serial.println("1.66 KHz");
- break;
- case LSM6DS_RATE_3_33K_HZ:
- Serial.println("3.33 KHz");
- break;
- case LSM6DS_RATE_6_66K_HZ:
- Serial.println("6.66 KHz");
- break;
- }
- ism330dhcx.configInt1(false, false, true); // accelerometer DRDY on INT1
- ism330dhcx.configInt2(false, true, false); // gyro DRDY on INT2
- ism330dhcx2.configInt1(false, false, true); // accelerometer DRDY on INT1
- ism330dhcx2.configInt2(false, true, false); // gyro DRDY on INT2
- }
- void rotationsFromQuat(Quaternion quat) {
- roll = atan2f(quat.w * quat.x + quat.y * quat.z, 0.5f - quat.x * quat.x - quat.y * quat.y);
- Serial.print("roll: "); Serial.println(roll * (180 / 3.1459));
- pitch = asinf(-2.0f * (quat.x * quat.z - quat.w * quat.y));
- Serial.print("pitch: "); Serial.println(pitch * (180 / 3.1459));
- yaw = atan2f(quat.x * quat.y + quat.w * quat.z, 0.5f - quat.y * quat.y - quat.z * quat.z);
- Serial.print("yaw: "); Serial.println(yaw * (180 / 3.1459));
- }
- void sensor1() {
- sensors_event_t accel;
- sensors_event_t gyro;
- sensors_event_t temp;
- ism330dhcx.getEvent(&accel, &gyro, &temp);
- deltat = fusion.deltatUpdate(); //this have to be done before calling the fusion update
- fusion.MahonyUpdate(gyro.gyro.x, gyro.gyro.y, gyro.gyro.z, accel.acceleration.x, accel.acceleration.y, accel.acceleration.z, deltat); //mahony is suggested if there isn't the mag and the mcu is slow
- /*
- Serial.print("Pitch:\t"); Serial.println(pitch);
- Serial.print("Roll:\t"); Serial.println(roll);
- Serial.print("Yaw:\t"); Serial.println(yaw);
- Serial.println();
- */
- quat = fusion.getQuat();
- quat1 = Quaternion(quat[0], quat[1], quat[2], quat[3]);
- Vec3f vec = Vec3f(accel.acceleration.x, accel.acceleration.y, accel.acceleration.z);
- Quaternion invertedQuat = quat1.conjugated();
- Vec3f invertedVec = invertedQuat.rotate(vec);
- Serial.println("---- 1 ----");
- Serial.print("x: "); Serial.println(vec.x);
- Serial.print("y: "); Serial.println(vec.y);
- Serial.print("z: "); Serial.println(vec.z);
- Serial.println("rotated");
- Serial.print("x: "); Serial.println(invertedVec.x);
- Serial.print("y: "); Serial.println(invertedVec.y);
- Serial.print("z: "); Serial.println(invertedVec.z);
- Serial.println("");
- }
- void sensor2() {
- sensors_event_t accel;
- sensors_event_t gyro;
- sensors_event_t temp;
- ism330dhcx2.getEvent(&accel, &gyro, &temp);
- deltat = fusion.deltatUpdate(); //this have to be done before calling the fusion update
- fusion.MahonyUpdate(gyro.gyro.x, gyro.gyro.y, gyro.gyro.z, accel.acceleration.x, accel.acceleration.y, accel.acceleration.z, deltat); //mahony is suggested if there isn't the mag and the mcu is slow
- quat = fusion.getQuat();
- quat2 = Quaternion(quat[0], quat[1], quat[2], quat[3]);
- Vec3f vec = Vec3f(accel.acceleration.x, accel.acceleration.y, accel.acceleration.z);
- Vec3f rotatedVector = quat2.conjugated().rotate(vec);
- rotatedVector = quat2.rotate(rotatedVector);
- Serial.println("---- 1 ----");
- Serial.print("x: "); Serial.println(vec.x);
- Serial.print("y: "); Serial.println(vec.y);
- Serial.print("z: "); Serial.println(vec.z);
- Serial.println("rotated");
- Serial.print("x: "); Serial.println(rotatedVector.x);
- Serial.print("y: "); Serial.println(rotatedVector.y);
- Serial.print("z: "); Serial.println(rotatedVector.z);
- Serial.println("");
- }
- void loop() {
- // /* Get a new normalized sensor event */
- Serial.println("-----------");
- sensor1();
- //sensor2();
- delay(25);
- // // serial plotter friendly format
- // Serial.print(temp.temperature);
- // Serial.print(",");
- // Serial.print(accel.acceleration.x);
- // Serial.print(","); Serial.print(accel.acceleration.y);
- // Serial.print(","); Serial.print(accel.acceleration.z);
- // Serial.print(",");
- // Serial.print(gyro.gyro.x);
- // Serial.print(","); Serial.print(gyro.gyro.y);
- // Serial.print(","); Serial.print(gyro.gyro.z);
- // Serial.println();
- // delayMicroseconds(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment