I'm using an MPU-9250 with a teensy 4.0 and am using my accelerometer to calculate the velocity of a rocket flight computer (and ideally the height reached any help on this would also be helpful) and i'm having issues.
I'm using the Suvat equation V = u + (a*dt) to calculate the velocity of the computer, however by leaving the computer/sensor upright and not moving the velocity consistently increases whether positive or negative acceleration/velocity making me think that the timing is affecting my calculated velocity as I assume it should stay or fluctuate around 0 m/s when stationary.
Here's the code i'm using
#include <Wire.h>
#include <SD.h>
#include "MPU9250.h"
MPU9250 IMU(Wire,0x68);
File DataFile;
int status;
float acceleration;
float constant;
float ACCX;
uint32_t Time;
float t;
float dt;
void setup() {
Wire.setSCL(19);
Wire.setSDA(18);
Wire.begin();
Serial.begin(9600);
delay(1000);
SD.begin(10);
if (SD.begin(10))
{
Serial.println("SD card is present");
}
else
{
Serial.println("SD card missing or failure");
while(1);
} if (SD.exists("IMU.txt"))
{
Serial.println("Clearing file");
SD.remove("IMU.txt");
Serial.print("Done");
Serial.println(" ");
}
Serial.println("Accelerometer readings");
status = IMU.begin();
if (status < 0) {
Serial.println("IMU initialization unsuccessful");
Serial.println("Check IMU wiring or try cycling power");
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
//Take 10 g readings and find an average for g
//constant
int j;
constant = 0;
for (j = 0; j<100; j++) {
if (j>=0){
ACCX = IMU.getAccelX_mss();
//HH = bmp.readAltitude(SEALEVELPRESSURE_HPA);
constant = (constant + ACCX);
}
}
constant = constant/(j);
Serial.print("Average : "); Serial.println(constant, 16);
delay(500);
Time = millis();
}
int i=0;
float u = 0;
float V;
float a;
float to = 0;
void loop() {
uint32_t now = (millis()-Time);
uint32_t secs = now / 1000;
uint32_t ms = now - (secs * 1000);
String TimeString;
TimeString = String(secs);
TimeString = TimeString + ".";
if (ms < 100) {
TimeString = TimeString + "0";
}
if (ms < 10) {
TimeString = TimeString + "0";
}
TimeString = TimeString + String(ms);
t = TimeString.toFloat();
IMU.readSensor(),16;
acceleration = IMU.getAccelX_mss(),16;
a = acceleration - constant;
Serial.print(TimeString); Serial.print(","); Serial.println(a);
dt = t-to;
V = u + (a*dt);
//Flag to show that u is calculating correctly
Serial.print(V); Serial.print(" m/s");Serial.print(u); Serial.print(" m/s"); Serial.print(" "); Serial.println(dt);
u = V;
to = t;
V=0;
/*
DataFile = SD.open("IMU.txt", FILE_WRITE);
if (DataFile){
DataFile.print(t); DataFile.print(",");
DataFile.println(a);
DataFile.close();
} else
{
Serial.println("error opening IMU.txt");
}
*/
delay(100);
}
Any help would be appreciated, I know it's most likely poor coding from me but I just can't seem to get my head around it
-Harvey ![]()