Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust sensitivity for startWakeOnMotion and startTiltDetection #15

Closed
chinswain opened this issue Nov 27, 2024 · 3 comments
Closed

Adjust sensitivity for startWakeOnMotion and startTiltDetection #15

chinswain opened this issue Nov 27, 2024 · 3 comments

Comments

@chinswain
Copy link

Is it possible to adjust the sensitivity of startWakeOnMotion and startTiltDetection functions?
I see the attached in the datasheet, are you able to add this to the library?

image

@tdk-opensource
Copy link
Collaborator

Thank you for opening this issue! We will look into it shortly.

Notifying @tdk-invn-oss/motion-maintainers @tdk-invn-oss/arduino-maintainers

@rbuisson-invn
Copy link
Contributor

Hello Chinswain,

The registers you mention control the full scale when reading accel data.

For WakeOnMotion or TiltDetection features you should better have a look to the APEX_CONFIG registers.
For Tilt you can configure the duration to detect a tilt using this register:
image

For WakeOnMotion, you can set a threshold for detection as shown in following picture:
image

You can write these registers through I2C or SPI depending on your hardware.

Regards,

@chinswain
Copy link
Author

Thanks for the prompt reply, I'll take a look. My use case is inside a beehive, I wish to determine if the beehive has fallen over, startWakeOnMotion seems to be the best for this.

#include "ICM42670P.h"

ICM42670 IMU(Wire, 0);

volatile bool wake_up = false;

void irq_handler(void) {
  wake_up = true;
}

void setup() {
  int ret;
  Serial.begin(115200);
  while (!Serial) {}
  pinMode(14, INPUT_PULLUP);
  // Initializing the ICM42670
  ret = IMU.begin();
  if (ret != 0) {
    Serial.print("ICM42670 initialization failed: ");
    Serial.println(ret);
    while (1)
      ;
  }

  IMU.startWakeOnMotion(14, irq_handler);
  Serial.println("Going to sleep");
}

void loop() {
  if (wake_up) {
    Serial.println("Wake-up");
    IMU.startAccel(100, 16);   // 100 Hz, ±16g
    IMU.startGyro(100, 2000);  // 100 Hz, ±2000 dps
    delay(100);
    inv_imu_sensor_event_t imu_event;
    int ret = IMU.getDataFromRegisters(imu_event);
    if (ret == 0) {
      // Convert acceleration to g
      float accelX = imu_event.accel[0] / 2048.0;
      float accelY = imu_event.accel[1] / 2048.0;
      float accelZ = imu_event.accel[2] / 2048.0;
      // Convert gyroscope to dps
      float gyroX = imu_event.gyro[0] / 16.4;
      float gyroY = imu_event.gyro[1] / 16.4;
      float gyroZ = imu_event.gyro[2] / 16.4;

      // Determine beehive position based on Z-axis acceleration
      String position;
      if (accelZ < -0.8) {
        position = "Upright";
      } else if (accelZ > 0.8) {
        position = "Upside down";
      } else {
        position = "On its side";
      }

      // Report position if not upright
      if (position != "Upright") {
        Serial.println("Beehive is NOT upright.");
        Serial.print("Position: ");
        Serial.println(position);
        Serial.print("Acceleration: ");
        Serial.print("X: ");
        Serial.print(accelX);
        Serial.print(", Y: ");
        Serial.print(accelY);
        Serial.print(", Z: ");
        Serial.println(accelZ);
      } else {
        Serial.println("Beehive is upright.");
      }
    }
    delay(2000);
    wake_up = false;
    Serial.println("Going to sleep");
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants