complementary filter object

code_midgen
EmaMaker 2020-02-26 18:56:39 +01:00
parent e0c7c569f6
commit 33bdb4462a
4 changed files with 28 additions and 1 deletions

View File

@ -0,0 +1,10 @@
#include "vars.h"
class ComplementaryFilter{
public:
ComplementaryFilter(float k);
float calculate(float f);
private:
int K;
float oldVal;
};

View File

@ -3,6 +3,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "motor.h" #include "motor.h"
#include "PID_v2.h" #include "PID_v2.h"
#include "complementary_filter.h"
//PID Constants //PID Constants
#define KP 1.5 #define KP 1.5
@ -41,6 +42,7 @@ class DriveController{
Motor* m3; Motor* m3;
Motor* m4; Motor* m4;
PID* pid; PID* pid;
ComplementaryFilter* speedFilter;
int pDir, pSpeed, pTilt, oldSpeed; int pDir, pSpeed, pTilt, oldSpeed;
float x, y, vx, vy, speed1, speed2, speed3, speed4, pidfactor, delta; float x, y, vx, vy, speed1, speed2, speed3, speed4, pidfactor, delta;

View File

@ -0,0 +1,12 @@
#include "complementary_filter.h"
ComplementaryFilter::ComplementaryFilter(float k){
this->K = k;
}
float result;
float ComplementaryFilter::calculate(float val){
result = val*K + oldVal*(1-K);
oldVal = val;
return result;
}

View File

@ -38,6 +38,9 @@ DriveController::DriveController(Motor* m1_, Motor* m2_, Motor* m3_, Motor* m4_)
pid->SetOutputLimits(-255,255); pid->SetOutputLimits(-255,255);
pid->SetMode(AUTOMATIC); pid->SetMode(AUTOMATIC);
// Complementary filter for speed
speedFilter = new ComplementaryFilter(0.3);
canUnlock = true; canUnlock = true;
unlockTime = 0; unlockTime = 0;
@ -63,7 +66,7 @@ float DriveController::torad(float f){
void DriveController::drive(int dir, int speed, int tilt){ void DriveController::drive(int dir, int speed, int tilt){
speed = (speed * KSPD + oldSpeed * (1-KSPD))*GLOBAL_SPD_MULT; speed = speedFilter->calculate(speed)*GLOBAL_SPD_MULT;
tilt = tilt > 180 ? tilt - 360 : tilt; tilt = tilt > 180 ? tilt - 360 : tilt;
vx = ((speed * cosins[dir])); vx = ((speed * cosins[dir]));