Advertisement
pleasedontcode

Kalman Filter code

Aug 23rd, 2024
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 0.83 KB | Source Code | 0 0
  1. #include <Kalman.h> // Include Kalman Filter Library
  2.  
  3. Kalman kalmanX; // Create Kalman filter for X-axis
  4. Kalman kalmanY; // Create Kalman filter for Y-axis
  5.  
  6. float angleX, angleY;  // Filtered angle output
  7.  
  8. void setup() {
  9.   Serial.begin(115200);
  10.   kalmanX.setAngle(0); // Initialize the filter angle
  11.   kalmanY.setAngle(0);
  12. }
  13.  
  14. void loop() {
  15.   float accelX, accelY, gyroX, gyroY; // Simulated sensor readings
  16.  
  17.   // Replace these with actual sensor readings
  18.   accelX = getAccelX();
  19.   accelY = getAccelY();
  20.   gyroX = getGyroX();
  21.   gyroY = getGyroY();
  22.  
  23.   // Apply Kalman filter
  24.   angleX = kalmanX.getAngle(accelX, gyroX, 0.01);
  25.   angleY = kalmanY.getAngle(accelY, gyroY, 0.01);
  26.  
  27.   Serial.print("Angle X: "); Serial.print(angleX); Serial.print("\t");
  28.   Serial.print("Angle Y: "); Serial.println(angleY);
  29.  
  30.   delay(10);
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement