Pid

double kP=0.0;

double ki=0.0;

double kd=0.0;

int desiredValue=360;

int error;

int prevErrror=0;

int derivitive;

int totalError=0;

bool enablemypid=true;

int mypid(){

while(enablemypid);

int LrwPosition=Lrw.position(degrees);

int LfwPosition=Lfw.position(degrees);

int RrwPosition=Rrw.position(degrees);

int RfwPosition=Rfw.position(degrees);

int averagePosition= (LrwPosition+RrwPosition+RfwPosition+LfwPosition)/4;

prevErrror=error;

derivitive=error-prevErrror;

error=averagePosition-desiredValue;

totalError+=error;

double motorPower = (errorkP+derivitivekd+prevErrror*ki)/12.0;

Lrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Lfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

vex::task::sleep(20);

return 1;

};

does anyone know how i can modify this pid for auto
balancing.

double kP=0.0;

double ki=0.0;

double kd=0.0;

int desiredValue=360;

int error;

int prevErrror=0;

int derivitive;

int totalError=0;

bool enablemypid=true;

int mypid(){

while(enablemypid);

int LrwPosition=Lrw.position(degrees);

int LfwPosition=Lfw.position(degrees);

int RrwPosition=Rrw.position(degrees);

int RfwPosition=Rfw.position(degrees);

int averagePosition= (LrwPosition+RrwPosition+RfwPosition+LfwPosition)/4;

prevErrror=error;

derivitive=error-prevErrror;

error=averagePosition-desiredValue;

totalError+=error;

double motorPower = (error*kP+derivitive*kd+prevErrror*ki)/12.0;

Lrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Lfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rrw.spin(directionType::fwd,motorPower,velocityUnits::pct);

Rfw.spin(directionType::fwd,motorPower,velocityUnits::pct);

vex::task::sleep(20);

return 1;

};

(formatted for readability)

7 Likes

First, ask yourself:

  • What does PID do?
  • What are the inputs into a PID controller?
  • What are the outputs of a PID controller?

Then ask yourself:

  • What should autobalancing do when condition X is present?
  • What should autobalancing do when condition Y is present?
10 Likes

I don’t know what you mean about inputs .Anyways I would also like to know how to pair it with an inertial sensor

The function can’t run without any inputs; what is going into the function that the function then uses? What does the function spit out, and what does the output do?

I suggest you look at the inertial sensor documentation to see how the inertial sensor works, that might help with figuring out what you should have as one of your inputs.

5 Likes

thanks could you give me an example of an auto balancing input

So, looking at the inertial sensor, you want the value that can tell how off-balance the robot is on the y-axis. This would be the pitch value. So you need to have the inertial pitch value as one of your inputs. When looking at a PID, your inputs are your kP, kI, kD, desired value, and error. Since kP, kI, and kD are used for tuning, it wouldn’t go there. Your desired value is what you want to end at, so you wouldn’t set it there either. Therefore, it would go with the error, looking like this:

double error = inertial6.pitch(degrees);

You would want to make sure that your error is constantly being updated, otherwise it would take the original pitch measurement and never check to see if it’s getting closer.

3 Likes

have you ever learned about recursive formulas for sequences in math?

2 Likes

no im a rising 7th grader.

1 Like