Sorry if this is in the wrong folder, I'm very new to arduino and havent got a lot of experience in this! I'm trying to create a self balancing platform for a project and ive come across a problem with my code. Im trying to implement a complementary filter with the MPU6050 that im using to read the orientation of the platform. Im using a library to try and implement this(<six_axis_comp_filter.h>) and im continually getting either the "not declared in this scope or "expected constructor, destructor, or type conversion before '(' token" error for the "CompFilter" function. If anyone knows anything about this library or if there is another way of implementing the complementary filter then it would be very much appreciated! Sorry if the format of my question is also poor, since this is my first ever forum post any criticism is welcome however please be gentle!
#include <six_axis_comp_filter.h>
#include <six_axis_comp_filtercpp.h>
#include <I2Cdev.h>
#include <PID_v1.h>
#include <Servo.h>
#include <MPU6050.h>
//Angle variable setup
float accelx, accely, accelz, gyrox, gyroy, gyroz; //create variables for the filter
float anglex, angley; //create angle values
float comanglex, comangley; // angles from complementary filter
//MPU setup
MPU6050 sensor;
int16_t ax, ay, az, gx, gy, gz;
//Servo setup
Servo servox;
Servo servoy;
int Outx;
int Outy;
int servoypin =5;
int servoxpin = 2;
//PID setup
int sampletime=20;
double xSetpoint, xInput, xOutput; //Set the variables for the controller
double ySetpoint, yInput, yOutput; //Set the variables for the controller
double aKp=2.5, aKi=0, aKd=0; //set the aggresive pid values
double cKp=4, cKi=0, cKd=0; //set the conservative values
PID xPID(&xInput, &xOutput, &xSetpoint, cKp, cKi, cKd, DIRECT);
PID yPID(&yInput, &yOutput, &ySetpoint, cKp, cKi, cKd, DIRECT);
//Filter setup
float deltaTime = 0.1;
float tau = 3;
CompSixAxis(deltaTime, tau);
void setup() {
//Serial monitor setup
Serial.begin(112000);
Serial.println ( "Initializing the sensor" );
sensor.initialize ( );
Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed");
//PID variables
xInput = map(ax, -14000, 14000, 0, 255);
yInput = map(ay, -14000, 14000, 0, 255);
xSetpoint = 2100;
yInput = ay;
ySetpoint = -150;
xPID.SetOutputLimits (-17000, 17000);
yPID.SetOutputLimits (-17000, 17000);
xPID.SetMode(AUTOMATIC);
yPID.SetMode(AUTOMATIC);
}
void loop() {
//servo write variables
int SRx = 0;
int SRy = 0;
// imu raw data read
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
CompFilter.CompAccelUpdate(accelX, accelY, accelZ); // takes arguments in m/s^2
CompFilter.CompGyroUpdate(gyroX, gyroY, gyroZ); // takes arguments un rad/s
CompFilter.CompUpdate();
CompFilter.CompStart();
//input variables
xInput = map(ax, -17000, 17000, 0, 255);
yInput = map(ay, -17000, 17000, 0, 255);
//errors for PID controller
double xerror = abs(xSetpoint-xInput);
double yerror = abs(ySetpoint-yInput);
Serial.println (xerror);
Serial.println (yerror);
//define if loop for aggressive or conservative PID values
//XLOOP
if(xerror<500000000)
{
xPID.SetTunings(cKp, cKi, cKd);
}
else
{
xPID.SetTunings(aKp, aKi, aKd);
}
//YLOOP
if(yerror<1000000)
{
yPID.SetTunings(cKp, cKi, cKd);
}
else
{
yPID.SetTunings(aKp, aKi, aKd);
}
xPID.Compute();
yPID.Compute();
//WRITE VALUES TO SERVOS
Outx = map (xOutput, 8370, 7380, 180, 0) ; //map the attained MPU values to the servo position 0-180
servox.write (Outx); //write the position of the mpu to the position of the servo
Outy = map (yOutput, 15000, -15000, 180, 0) ; //map the y values to the other servo
//sg100.write (yOutput); //write the value to the other servo
//serial print values at the end, grouped for easier reading and editing
//Serial.print ("outputs \n");
//Serial.println (Outx);
//Serial.println (Outy);
Serial.println (xOutput);
//Serial.println (yOutput);
//Serial.println (ax);
//Serial.println (ay);
//Serial.print ("inputs \n");
//Serial.println (xInput);
//Serial.println (yInput);
}