PID Printing Variables

  1. last week

    Kselva20

    Nov 8 5249Z
    Edited last week by Kselva20

    Hello, I am currently working on some PID code for my team and recently ran into some problems. I am using c++ pro on vex coding studio. I am a little newer to c++, so please bare with me. I am currently using the 2nd Ziegler-Nichols method to tune my PID values, and I am having trouble finding the period of critical gain. I am trying to use the built in timer in vex coding studio and printing the value of it to the screen of the brain. However, when I do this, the robot repeatedly prints 0 for every value. If you notice, I also tried multiplying the value by a large number to see if the result would change, but I got the same repeating 0s. Could anybody suggest what may be wrong? Also, I was wondering if there might be a way to simply record these values in an array and then somehow save this array in an SD card. If somebody could also explain the SD card utility as well, that would be much appreciated. Thank you very much.

    void PIDChassis(double dist){
        RightChassis.resetRotation();
        LeftChassis.resetRotation();
        
        double goal = (180*dist)/(2*M_PI);
        
        double pGain = 30.0;
        double iGain = 0.0;
        double dGain = 0.0;
        
        double errorR = goal - RightChassis.rotation(vex::rotationUnits::deg);
        double errorL = goal - LeftChassis.rotation(vex::rotationUnits::deg);
        double errorSum = 0.0;
        bool isFirst = true;
        int counter = 0;
        double oldError = RightChassis.rotation(vex::rotationUnits::deg);
        int i = 0;
        Brain.resetTimer();
        double oldSpeed = 0.0;
        
        while(true){
            
            errorR = goal - RightChassis.rotation(vex::rotationUnits::deg);
            errorL = goal - LeftChassis.rotation(vex::rotationUnits::deg);
            errorSum += ((errorR + errorL)/2);
            if(errorR == 0.0 || errorR < goal || errorL == 0.0 || errorL < goal){
                errorSum = 0.0;
            }
            double change = ((errorR + errorL)/2) - oldError;
            double avgError = ((errorR + errorL)/2);
            double difference = RightChassis.rotation(vex::rotationUnits::deg) - LeftChassis.rotation(vex::rotationUnits::deg);
            double motorSpeed = (avgError*pGain) + (errorSum*iGain) + (change*dGain);
            RightChassis.spin(vex::directionType::fwd, motorSpeed, vex::velocityUnits::dps);
            LeftChassis.spin(vex::directionType::fwd, motorSpeed+ difference,vex::velocityUnits::dps);
            
            oldError = avgError;
            
            if(oldSpeed >= 0.0 && motorSpeed < 0.0 && isFirst == true){
                isFirst = false;
                oldSpeed = motorSpeed;
                Brain.resetTimer();
                continue;
            }
            else if(oldSpeed < 0.0 && motorSpeed > 0.0 && isFirst == false){
                counter++;
            }
            else if(oldSpeed > 0.0 && motorSpeed < 0.0 && isFirst == false){
                counter++;
                
            }
            else{
                oldSpeed = motorSpeed;
                continue;
            }
    
            if(counter % 2 != 0 && counter > 1){
                double currentTime = Brain.timer(vex::timeUnits::msec);
                //currentTime *= 100000000000000000000.0;
                int add = (i-1)*10;
                char str[20];
                Brain.Screen.printAt(1,40 + add, true, str, "%lf", currentTime);
                Brain.resetTimer();
            }
            oldSpeed = motorSpeed;
            i++;
        }
        
        LeftChassis.stop(vex::brakeType::brake);
        RightChassis.stop(vex::brakeType::brake);
        
        RightChassis.resetRotation();
        LeftChassis.resetRotation();
    }
 

or Sign Up to reply!