Mpu6050 body position

Hi everyone! I'm working on a project using MPU6050 IMU Sensors. For my project focused on spine positioning. I attached the sensors from the cervical down to lumbar. I need data showing the results of the XYZ position of the patient's spine in meters. Any help or references would be greatly appreciated!

#include <Wire.h>
#include <MPU6050.h>  // Include the MPU6050 library

MPU6050 imu;  // Create an instance of the MPU6050

// Constants
const float dt = 0.1;  // Time step in seconds
const float threshold = 0.05;  // Threshold to detect if the sensor is stationary

// Variables
int16_t raw_ax, raw_ay, raw_az;  // Raw acceleration values
float ax, ay, az;  // Converted acceleration values
float vx = 0, vy = 0, vz = 0;  // Velocity values
float x = 0, y = 0, z = 0;  // Position values

void setup() {
  Serial.begin(9600);
  Wire.begin();
  imu.initialize();
  
  if (!imu.testConnection()) {
    Serial.println("IMU connection failed");
    while (1);  // Stop if IMU is not connected
  }
}

void loop() {
  // Read raw accelerometer data
  imu.getAcceleration(&raw_ax, &raw_ay, &raw_az);
  
  // Convert raw data to g's (1 g = 9.8 m/s²)
  ax = raw_ax / 16384.0;
  ay = raw_ay / 16384.0;
  az = raw_az / 16384.0;
  
  // Subtract gravity if the sensor is relatively stationary
  if (abs(ax) < threshold && abs(ay) < threshold && abs(az - 1.0) < threshold) {
    ax = 0;
    ay = 0;
    az = 0;
    vx = 0; vy = 0; vz = 0;  // Reset velocity if nearly stationary
  } else {
    // Update velocity by integrating acceleration
    vx += ax * dt;
    vy += ay * dt;
    vz += (az - 1.0) * dt;  // Subtract gravity component (assuming z-axis is vertical)
  }
  
  // Update position by integrating velocity
  x += vx * dt;
  y += vy * dt;
  z += vz * dt;
  
  // Print position data to Serial Monitor
  Serial.print("Position X: ");
  Serial.print(x);
  Serial.print(" m, ");
  Serial.print("Y: ");
  Serial.print(y);
  Serial.print(" m, ");
  Serial.print("Z: ");
  Serial.print(z);
  Serial.println(" m");
  
  // Delay for a while before the next loop iteration
  delay(100);  // Delay in milliseconds (100 ms = 0.1 s)
}

I used this code before but the results show the values keep increasing with large numbers. I want it to be start with 0 when its not moving and reset to 0 when its stop moving. But I didn't get it.

To my best knowledge, the 6050 is an accelerometer, not a position sensor.

2 Likes

If you don't mind, can you suggest what sensors are suitable for positioning purposes? Or if I still want to use the 6050, is there any other way to determine the difference in the position or posture of the spine whether it is normal or not.

Welcome to the forum.

The LiDAR scanner from a iPhone can scan the back of a person, but you need X-ray photos to say something about the posture of the spine.

To calculate the position from a single MPU-6050 is possible for maybe a few minutes. It is done by trying to keep track of the sensor values with calculations. But the motions may not be too fast and not too small.
However, for your project it will probably be impossible. I see too many problems: wrong counterfeit sensor, wrong library, motions are too small, the I2C bus is too slow, initializing the starting position might take half an hour, and so on.

Fun fact: It is possible to connect many MPU-6050 sensors to a single board: https://wokwi.com/projects/394777263927329793 But that is a completely different world than your project.

1 Like

Not sure to understand your problem, but if you want to attach this sensor to 'the spine of a person' and analyze the posture while waking ( for example ) I think you need an inclinometer ( 3 axis ).
Also having more than one sensor to help reconstruct the whole spine movements...
But then I don't understand why it should auto zero when the person stops, so what is the goal?