About Kalman filter

#include <Wire.h>
#include "Kalman.h"
#include "MPU6050.h"
#include "I2Cdev.h"

Kalman kalmanX, kalmanY, kalmanZ;

MPU6050 accelgyro;

int16_t ax, ay, az, gx, gy, gz;
double accX, accY, accZ, gyroX, gyroY, gyroZ;
double pitch, roll, yaw, AngleX, AngleY, AngleZ, Xrate, Yrate, Zrate;
uint32_t timer;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  
  accelgyro.initialize();
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  accX = ax;
  accY = ay;
  accZ = az;
  gyroX = gx;
  gyroY = gy;
  gyroZ = gz;
  
  pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
  roll = atan2(accY, accZ) * RAD_TO_DEG;
  yaw = -atan2(accX, accY) * RAD_TO_DEG;
  
  kalmanX.setAngle(roll);
  kalmanY.setAngle(pitch);
  kalmanZ.setAngle(yaw);
  
  timer = micros();
}

void loop() {
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  accX = ax;
  accY = ay;
  accZ = az;
  gyroX = gx;
  gyroY = gy;
  gyroZ = gz;
  
  double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
  timer = micros();
  
  pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
  roll = atan2(accY, accZ) * RAD_TO_DEG;
  yaw = -atan2(accX, accY) * RAD_TO_DEG;
  
  Yrate = gyroY / 131.0; // Convert to deg/s
  Xrate = gyroX / 131.0;
  Zrate = gyroZ / 131.0;
  
  AngleY = kalmanY.getAngle(pitch, Yrate, dt);
  AngleX = kalmanX.getAngle(roll, Xrate, dt);
  AngleZ = kalmanZ.getAngle(yaw, Zrate, dt);
  
  Serial.print(AngleX);
  Serial.print(',');
  Serial.print(AngleY);
  Serial.print(',');
  Serial.println(AngleZ);
}

There was an error during the compilation of the Arduino program.

'kalmanX' does not name a type; did you mean 'Kalman_h'?
I have confirmed that the Kalman library has been imported.

Which Kalman library are you using? There are several.

When trying out a new library, it is always a good idea to run the library examples. Make sure that it works as expected, and that you understand how to use the library.

1 Like

This error is telling you that the compiler does not recognize the constructor you are using. Sadly, it is a rather indirect message.
The compiler does not know what you are trying to do on that line. So it makes a guess that you are declaring a variable, and that you didn't tell it the type of the variable (e.g int, float, etc). It then takes another stab at it and guesses that you are trying to include a header.

The solution is to look at the header file to see what the correct incantations that are needed are!
According to the first Kalman library that comes up on a search, the class name is KALMAN, and not Kalman. A look at the constructor shows how the name is spelled and that certain parameters are needed.

In addition, as mentioned in the post above, a look at the provided examples is really helpful.

This library

Where can I find example references?

The examples are in the library you linked.

I have tried to understand the examples, but I still can't find the reason.

Have you studied the theory of Kalman filters? If you have, the library examples are very straightforward. Run the "full" example and see what it does.

The full example shows how you could instantiate your objects. Look at this section:

//------------------------------------
/****  MODELIZATION PARAMETERS  ****/
//------------------------------------
.... stuff deleted

KALMAN<Nstate,Nobs> K; // your Kalman filter

You would name yours kalmanX, instead of just K.

Do you mean to say this?

#include <Wire.h>
#include "Kalman.h"
#include "MPU6050.h"
#include "I2Cdev.h"

KALMAN<Nstate,Nobs> kalmanX, kalmanY, kalmanZ ;

MPU6050 accelgyro;

int16_t ax, ay, az, gx, gy, gz;
double accX, accY, accZ, gyroX, gyroY, gyroZ;
double pitch, roll, yaw, AngleX, AngleY, AngleZ, Xrate, Yrate, Zrate;
uint32_t timer;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  
  accelgyro.initialize();
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  accX = ax;
  accY = ay;
  accZ = az;
  gyroX = gx;
  gyroY = gy;
  gyroZ = gz;
  
  pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
  roll = atan2(accY, accZ) * RAD_TO_DEG;
  yaw = -atan2(accX, accY) * RAD_TO_DEG;
  
  kalmanX.setAngle(roll);
  kalmanY.setAngle(pitch);
  kalmanZ.setAngle(yaw);
  
  timer = micros();
}

void loop() {
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  accX = ax;
  accY = ay;
  accZ = az;
  gyroX = gx;
  gyroY = gy;
  gyroZ = gz;
  
  double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
  timer = micros();
  
  pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
  roll = atan2(accY, accZ) * RAD_TO_DEG;
  yaw = -atan2(accX, accY) * RAD_TO_DEG;
  
  Yrate = gyroY / 131.0; // Convert to deg/s
  Xrate = gyroX / 131.0;
  Zrate = gyroZ / 131.0;
  
  AngleY = kalmanY.getAngle(pitch, Yrate, dt);
  AngleX = kalmanX.getAngle(roll, Xrate, dt);
  AngleZ = kalmanZ.getAngle(yaw, Zrate, dt);
  
  Serial.print(AngleX);
  Serial.print(',');
  Serial.print(AngleY);
  Serial.print(',');
  Serial.println(AngleZ);
}

Yes.
And, of course, you will need to define the number of states and obs before the line that uses them:
e.g
#define Nstate 2

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.