PID controller & Accelerometer & Brushless motor help needed!

Hey people,

Recently I have build an arm with a brushless motor on one end and a 10dof sensor on the other end.
Using a PID controller, i have attempted to take the accelerometer Pitch axis data an use that as input and have the brushless motor spin up until the arm levels its self out and stays there when the accelerometer pitch axis reads 0.

My biggest problem is that the motor doesn't seem to respond properly to the commands that the PID is giving it.

After a long few days, and it smashing its self into the ground countless times i have decided to turn the to experts and see if you guys can help me.. Any help, feedback or comments would be appreciated no matter how positive or negative :stuck_out_tongue_winking_eye: lol


Hardware is:
Arduino Nano,
GY-80 (10dof)
12amp afro esc
980kv brushless motor

Here is the code i am running..

//Basic PID controller for stabiliaztion on 1 axis
//Hayden Woodger

#include <PID_v1.h>
#include <Wire.h>
#include <ADXL345.h>
#include <Servo.h>
const float alpha = 0.5;

//ESC startup signals
int Max = 2000;
int Min = 1000;

double fXg = 0;
double fYg = 0;
double fZg = 0;

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp = 2, Ki = 1, Kd = 1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
ADXL345 acc;
Servo motor;
void setup()
{
acc.begin();
motor.attach(9);
motor.writeMicroseconds(Max);
motor.writeMicroseconds(Min);
delay(1000);
Setpoint = 0;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}

void loop()
{
double pitch, Xg, Yg, Zg;
acc.read(&Xg, &Yg, &Zg);

//Low Pass Filter
fXg = Xg * alpha + (fXg * (1.0 - alpha));
fYg = Yg * alpha + (fYg * (1.0 - alpha));
fZg = Zg * alpha + (fZg * (1.0 - alpha));

//Pitch Equation
pitch = (atan2(fXg, sqrt(fYg * fYg + fZg * fZg)) * 180.0) / M_PI;

Input = (pitch);
myPID.Compute();
motor.write(Output);
}

Have mercy in my coding abilities

PID_Controller.ino (1.07 KB)

WIN_20150530_011110.JPG

 Input = (pitch);

(The) (useless) (parentheses) (are) (for) (?);

What IS the value of pitch?

motor.write(Output);

What IS the value of Output?

Are you using an ESC designed for closed loop control? You need active braking for a start, and
rapid linear response to commanded input.