Hey guys! First time poster, and a semi-noob here. I'm currently working on a posture trainer, where I aim to have two gyroscopes placed on the user's spine, comparing their values to determine whether or not he/she is slouching.
So, I've been trawling around forums and guides in order to get pitch data along the y axis of two gyroscopes working. Finally, I've been able to get it to work somewhat, but it's got issues.
First and foremost, after running for some time (10-30 seconds), the console starts printing not a number(nan).
Second, it's not very accurate. Small, slow changes don't register at all. I need it to be accurate since just a degree or two can screw it up for my application of choice.
I am using SparkFun LSM6DS3 6DOF boards (Datasheet (PDF download)).
This is my test code (cred to racquemis here on the forums for the complementary filter code).
Since I am using two gyros, this code also implements SparkFun's example for hooking up multiple units over I2C. I am only measuring one for this test.
#include "SparkFunLSM6DS3.h"
#include "Wire.h"
#include "SPI.h"
//Create two instances of the driver class
LSM6DS3 SensorOne( I2C_MODE, 0x6A );
LSM6DS3 SensorTwo( I2C_MODE, 0x6B );
long timer = micros();
int delta_t;
float pitch = 0;
float pitchAcc;
float P_CompCoeff= 0.98;
float gyro1Rotation;
float gyro2Rotation;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(5000); //relax...
Serial.println("Processor came out of reset.\n");
//Call .begin() to configure the IMUs
if( SensorOne.begin() != 0 )
{
Serial.println("Problem starting the sensor at 0x6A.");
}
else
{
Serial.println("Sensor at 0x6A started.");
}
if( SensorTwo.begin() != 0 )
{
Serial.println("Problem starting the sensor at 0x6B.");
}
else
{
Serial.println("Sensor at 0x6B started.");
}
}
void loop()
{
ComplementaryFilter(SensorTwo.readFloatAccelX(), SensorTwo.readFloatAccelY(), SensorTwo.readFloatAccelZ(), SensorTwo.readFloatGyroY());
timer=micros();
Serial.println(pitch);
delay(20);
}
void ComplementaryFilter(float ax,float ay,float az,float gy) {
delta_t = micros() - timer;
long squaresum=(long)ay*ay+(long)az*az;
pitch+=((-gy/32.8f)*(delta_t/1000000.0f));
pitchAcc =atan(ax/sqrt(squaresum))*RAD_TO_DEG;
pitch =P_CompCoeff*pitch + (1.0f-P_CompCoeff)*pitchAcc;
}
The gyroscopes he uses are different, so I guess that the inaccuracy could have something to do with his gyro having a different sensitivity?
Would be eternally grateful for any help! If I've missed providing you with some piece of information, feel free to gimme a holler and I'll do my best to fill you in!
Cheers!