Hi all!
I have been trying to build my own PID library to control a RC airplane. After troubleshooting for a little bit I simplified my code so I could magnify my issue.
Essentially, I want to set up pointers in the class constructor so I do not have to constantly feed new values to different functions within the class.
My .h looks like this:
#ifndef PID_h
#define PID_h
class PID {
public:
PID(double);
void calculate();
double var;
private
double *pointer;
};
#endif
//My .cpp looks like this:
#include "Arduino.h"
#include "PID.h"
PID::PID(double input){
pointer = &input;
}
void PID::calculate(){
var = *pointer;
}
//And finally my arduino code:
#include <PID.h>
double pitch;
PID pid(pitch);
void loop (){
accel();
convert();
pid.calculate();
Serial.print(pitch);
Serial.print(" ");
Serial.println(pid.var);
}
When the PID instance "pid" is created in the beginning at global scope, it should pass "pitch" to the header file. Within the constructor the private double "*pointer" should store the location of "pitch".
The function accel() grabs accelerometer values over the i2c bus. The convert () function takes the accelerometer value and puts into degress, this is stored the double "pitch." This works just fine.
Once the arduino calls "pid.calculate ()" the public double "var" should be redirected and then store the value "pitch"
The serial bus should then print the value pitch, and the value pid.var. These should be the same.
The problem is: the value pitch is live accelerometer data, but the "pid.var" only ever reads "-0.00" instead of the value pitch.
Any help on this one guys? I have been working on this one for a while but I cannot see what I am missing....