Pid_q&a

double kP=0.0;

double ki=0.0;

double kd=0.0;

int desiredValue=400,(deg);

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;
double error=n1.position(rotationUnits::deg);
totalError+=error;
//double error = inertial9.pitch(degrees);
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 get this pid to run a simple autonomous program with a shaft encoder?

i’m using c++
20 char

Based on the way you’ve phrased the question, I think it might be better if you take a step back. Start simple and then layer in complexity.

First, write an autonomous without PID and without using encoders. Just drive forward, either by motor rotations or time, etc.

Then modify the program to use the encoders to stop after a specified distance.

Finally, learn how you may control the motors using a PID loop.

so i have an autonomous that works well looking like this in pre auton

void f(int speed, float rotation) // Go back at “speed” for “rotation”

// wheel rotations

{Lrw.resetRotation();

Lfw.resetRotation();

Rrw.resetRotation();

Rfw.resetRotation();

Lrw.startRotateTo(rotation, rotationUnits::rev, speed, velocityUnits::pct);

Lfw.startRotateTo(rotation, rotationUnits::rev, speed, velocityUnits::pct);

Rfw.startRotateTo(rotation, rotationUnits::rev, speed, velocityUnits::pct);

Rrw.rotateTo(rotation, rotationUnits::rev, speed, velocityUnits::pct);

}

and this is the actual command in autonomuos

f(30,5);
//so I would like to take the next step to pid and odometry.

The PID function that you copied at the start uses a while loop. Next, think about how you would change your f function to move the input distance using a while loop.

Side note: while functions can be named anything, a big part of writing good code is to communicate to other humans (which may be a future you!) what everything does. A generic function named f conveys no meaning to anyone. You may wish to consider renaming it to something more descriptive like moveAtSpeedForDistance

would that be similar to a bang bang algorithm?

void forward1(int speed, float rotation)

{encoder1.resetRotation();

int desiredValue=400;

while(1);

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;

do you think this will work?

thanks very much for this it helped me get alot more creative

@46b_toxic, first, you will make the experience much easier for everyone if you enclose your code into [code]...[/code] tags, like this:

Second, the proper syntax of the while() loop is:

while( condition == true )
{
    x = function1();
    if( x>y )
    { 
       function2(x,y);
    }
    vex::task::sleep(10);
}

No semicolon right after while(1) because it will get stuck there forever, if you put semicolon there.

Third, you need to look up motor functions at VEX Help

For example, the last argument to motor.rotateTo() is a boolean waitForCompletion that is assumed true by default. That means that, unless you explicitly set it to false, the program will not advance to the next rotateTo() command, until the first motor gets to the requested position.

Since you are calling multiple rotateTo() in a row, you need to pass false to all of them, except for the last one.

or you can use startRotateTo

yeah, i’m trying that .are you on the seton team?

#include "robot-config.h"
#include <cmath>
#include <iostream>
using namespace vex;


competition Competition;



void calibrateInertial(inertial& Gyro){
  if(Gyro.installed()){
    Gyro.startCalibration();
    task::sleep(200);
    Gyro.calibrate();
    std::cout << "Calibrating Inertial Sensor..." << std::flush;
    while(Gyro.isCalibrating()){
      task::sleep(100);
      std::cout << "." << std::flush;
    }
    std::cout << std::endl;
    task::sleep(100);
  }
  else {
    std::cout << "No Inertial Sensor" << std::endl;
  }
}

void pre_auton(void) {

  
} 


double posNeg180(double angle){
  while(angle > 180){
    angle -= 360;
  }
  while(angle <= -180){
    angle += 360;
  }
  return angle;
}
double mypid(double desiredValue){
  double kp=0.5; //This number gets the bot to move

  double ki=0.000005; //This prevents it from stopping

  double kd=0.1; // This stops it at the end

  double prevErrror = 0;

  double derivitive = 0.0;

  double totalError=0;

  double error=posNeg180(desiredValue - inertials.yaw());

  while(std::abs(error) > 4){
    double angle=(inertials.yaw());

    prevErrror=error;

    error=posNeg180(desiredValue - angle);

    derivitive=error-prevErrror;

    totalError+=error;
    //double error = inertial9.pitch(degrees);
    double motorPower = error*kp + derivitive*kd + totalError*ki;

    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);
  }
  lrw.stop(hold);
  lfw.stop(hold);
  rrw.stop(hold);
  rfw.stop(hold);

  return 1;

};



void autonomous(void) {
  calibrateInertial(inertials);

mypid(-90);
wait(500,msec);
mypid(90);

} 

void usercontrol(void) {
  
  while (1) {
    
    wait(20, msec); 
                    
  }  
}    


int main() {
  calibrateInertial(inertials);
  
  
  std::cout << inertials.heading() << std::endl;

edit by mods:
@46b_toxic Please try and use code tags when posting code. I have added them to this post for you. It would also be helpful if you also asked a question rather than posting code and expecting the forum members to guess what that might be.

oh sorry I was transferring that pid from a school computer to a home computer

@46b_toxic
Please do not use vexforum as a pastebin, there are plenty of free options for that, for example, google drive, dropbox or github.

4 Likes