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

Add frequency input to Mahony AHRS algortihm #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ void IMU::getAhrsData(float *pitch,float *roll,float *yaw){
getGyroData(&gyroX,&gyroY,&gyroZ);
getAccelData(&accX,&accY,&accZ);

MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ,pitch,roll,yaw);
MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ,pitch,roll,yaw,0.0f); // 0 frequency internal defined value

}

void IMU::getAhrsData(float *pitch,float *roll,float *yaw, float samplefrequency){

float accX = 0;
float accY = 0;
float accZ = 0;

float gyroX = 0;
float gyroY = 0;
float gyroZ = 0;


getGyroData(&gyroX,&gyroY,&gyroZ);
getAccelData(&accX,&accY,&accZ);

MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ,pitch,roll,yaw,samplefrequency);

}
1 change: 1 addition & 0 deletions src/IMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class IMU {
void getTempData(float *t);

void getAhrsData(float *pitch,float *roll,float *yaw);
void getAhrsData(float *pitch,float *roll,float *yaw, float samplefrequency);

ImuType imuType;
float aRes, gRes;
Expand Down
20 changes: 19 additions & 1 deletion src/utility/MPU6886.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,25 @@ void MPU6886::getAhrsData(float *pitch,float *roll,float *yaw){
getGyroData(&gyroX,&gyroY,&gyroZ);
getAccelData(&accX,&accY,&accZ);

MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ,pitch,roll,yaw);
MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ,pitch,roll,yaw,0.0f); // 0 frequency internal defined value

}

void MPU6886::getAhrsData(float *pitch,float *roll,float *yaw, float samplefrequency){

float accX = 0;
float accY = 0;
float accZ = 0;

float gyroX = 0;
float gyroY = 0;
float gyroZ = 0;


getGyroData(&gyroX,&gyroY,&gyroZ);
getAccelData(&accX,&accY,&accZ);

MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ,pitch,roll,yaw,samplefrequency);

}

Expand Down
1 change: 1 addition & 0 deletions src/utility/MPU6886.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class MPU6886 {
void SetAccelFsr(Ascale scale);

void getAhrsData(float *pitch,float *roll,float *yaw);
void getAhrsData(float *pitch,float *roll,float *yaw,float samplefrequency);

public:
float aRes, gRes;
Expand Down
15 changes: 8 additions & 7 deletions src/utility/MahonyAHRS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ void MahonyAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az
//---------------------------------------------------------------------------------------------------
// IMU algorithm update

void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az,float *pitch,float *roll,float *yaw) {
void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az,float *pitch,float *roll,float *yaw, float samplefrequency) {
float recipNorm;
float halfvx, halfvy, halfvz;
float halfex, halfey, halfez;
float qa, qb, qc;

if (samplefrequency == 0.0f) samplefrequency = sampleFreq;

// Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
Expand All @@ -180,9 +181,9 @@ void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float

// Compute and apply integral feedback if enabled
if(twoKi > 0.0f) {
integralFBx += twoKi * halfex * (1.0f / sampleFreq); // integral error scaled by Ki
integralFBy += twoKi * halfey * (1.0f / sampleFreq);
integralFBz += twoKi * halfez * (1.0f / sampleFreq);
integralFBx += twoKi * halfex * (1.0f / samplefrequency); // integral error scaled by Ki
integralFBy += twoKi * halfey * (1.0f / samplefrequency);
integralFBz += twoKi * halfez * (1.0f / samplefrequency);
gx += integralFBx; // apply integral feedback
gy += integralFBy;
gz += integralFBz;
Expand All @@ -200,9 +201,9 @@ void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float
}

// Integrate rate of change of quaternion
gx *= (0.5f * (1.0f / sampleFreq)); // pre-multiply common factors
gy *= (0.5f * (1.0f / sampleFreq));
gz *= (0.5f * (1.0f / sampleFreq));
gx *= (0.5f * (1.0f / samplefrequency)); // pre-multiply common factors
gy *= (0.5f * (1.0f / samplefrequency));
gz *= (0.5f * (1.0f / samplefrequency));
qa = q0;
qb = q1;
qc = q2;
Expand Down
2 changes: 1 addition & 1 deletion src/utility/MahonyAHRS.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extern volatile float twoKi; // 2 * integral gain (Ki)

void MahonyAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
//void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az);
void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az,float *pitch,float *roll,float *yaw);
void MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az,float *pitch,float *roll,float *yaw, float samplefrequency);
float invSqrt(float x);
#endif
//=====================================================================================================
Expand Down