Announcement

Collapse
No announcement yet.

PID Control

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PID Control

    I've been seeing code in which teams have their own PID code to make sure that the robot moves accurately each time. Is it more accurate that the built-in function in the Modern Robotics Controllers? If so, practically how would I go about implementing it?

    Thanks,
    Lockdown 8564

  • #2
    It depends on what you are using PID for? If you are using PID to control a single motor, I would recommend using the built-in PID. If you are going to do PID controlled drive, for example, you may need to coordinate with multiple motors, then built-in PID may not be sufficient because it only knows about one motor. For example, let's say you decided to make the robot drive forward 5 ft. You calculated 5 ft is equivalent to say 10,000 encoder counts, you program the left motor encoder target to be 10,000 count and do the same to the right. Now, both motor controllers will try to achieve 10,000 encoder counts but it could be at different speeds (due to friction, weight distribution, motor differences... etc). So at the end, you sort of achieve 5 ft, but probably in an S-curve and ended up, drifting stopping left or right of the target. With PID library code, you can distribute power between the wheels to make sure it's travelling straight and more.

    Comment


    • #3
      My team is planning to use 2 or 4 motors for our drive train and 2 for our pulley system. I'd like to use PID to make sure they're both working at the same speeds, etc.

      Comment


      • #4
        Originally posted by FTC8564 View Post
        My team is planning to use 2 or 4 motors for our drive train and 2 for our pulley system. I'd like to use PID to make sure they're both working at the same speeds, etc.
        Then you need to incorporate gyro in addition to the encoders for your drive base PID control. That's why we use our PID library because it will take all encoders as well as the gyro as input to calculate what power should be distributed to each motor.

        Comment


        • #5
          How do you calculate the power? And do you have the power change over time if certain motors are going different speeds? And do you use the Modern Robotics gyro?

          Comment


          • #6
            You have two components when driving forward: 1) Overall forward power; 2) Differential power between left and right. You calculate the overall forward power by checking your encoder against its target. So if the target is far away, the overall forward power is large. If you are almost there, the overall forward power would be small. Then the differential power is controlled by the gyro. If the robot is deviated from the center line in a big way (i.e. the error is large), the differential power is big. If you are right on straight, the differential power is zero. With these two components, the wheel powers are:
            Code:
            leftPower = drivePower + differentialPower;
            rightPower = drivePower - differentialPower;

            Comment


            • #7
              Would the deviated angle be proportional to differentialPower? Like 10 degrees off is equivalent to adding 10 to the drivePower?

              Thanks,
              Lockdown 8564

              Comment


              • #8
                Originally posted by FTC8564 View Post
                Would the deviated angle be proportional to differentialPower? Like 10 degrees off is equivalent to adding 10 to the drivePower?

                Thanks,
                Lockdown 8564
                Yes, the differential power is "proportional" to the deviated angle. That's the "Proportional" part of PID. The actual number would have to be "tuned". The pseudo-code for a Proportional-only PID is:
                Code:
                driveError = distanceTarget - currentEncoder;
                turnError = angleTarget - currentGyroAngle;
                drivePower = driveKp*driveError;
                turnPower = turnKp*turnError;
                leftPower = drivePower + turnPower;
                rightPower = driverPower - turnPower;

                Comment


                • #9
                  How would I calculate the tuning factor for each part of the algorithm? Like Kp, Ki, Kd.

                  Thanks,
                  Lockdown 8564

                  Comment


                  • #10
                    Originally posted by FTC8564 View Post
                    How would I calculate the tuning factor for each part of the algorithm? Like Kp, Ki, Kd.

                    Thanks,
                    Lockdown 8564
                    It's a lot of tweaking and testing. You'll find that it's a very thorough art as evidenced by the piles of textbooks. For a quick table summarizing the effects of each parameter (and thus the parameter to be changed for any given desired result) see https://en.wikipedia.org/wiki/PID_co...#Manual_tuning.
                    FTC6460 mentor (software+computer vision+electronics), FPGA enthusiast. In favor of allowing custom electronics on FTC bots.
                    Co-founder of ##ftc live chat for FTC programming--currently you may need to join and wait some time for help--volunteer basis only.

                    Comment


                    • #11
                      Originally posted by FTC8564 View Post
                      How would I calculate the tuning factor for each part of the algorithm? Like Kp, Ki, Kd.

                      Thanks,
                      Lockdown 8564
                      If you can tolerate some small amount of "steady state error" (i.e. tolerance), proportional-only PID is the easiest to tuned. You can just set Ki and Kd to zero. Otherwise, Kp and Kd aren't too bad to tune. Introducing Ki is a bit of work, I would try avoiding it.

                      Comment


                      • #12
                        Originally posted by FTC8564 View Post
                        How do you calculate the power? And do you have the power change over time if certain motors are going different speeds? And do you use the Modern Robotics gyro?
                        Hi

                        There is a sample of driving using the gyro in the STK samples folder. Look at PushbotAutoDriveByGyro_Linear

                        This code illustrates a simple way to use a gyro to drive straight, or to turn to a certain angle.
                        It uses a simple Proportional control algorithm (Just the P of a PID).
                        It also assumes that the gyro is providing continuous rotation values (rather than just 0-360) so I may have to be adapted to be used on other gyros.

                        Check it out, and read the comments (they try to explain how the code is structured, and how how the angles and distances work.)
                        The general idea is that a complex path can be built from a series of simple moves constructed from three options:
                        gyroDrive() Drive in a straight line, along a given heading for a set distance.
                        gyroTurn() Turn to a specific angle (Absolute angle, set to zero at startup)
                        gyroHold() Hold the specific angle for a specific time interval.

                        Note: This is just one way that the gyro could be used. There are many other ways that are much more sophisticated.

                        Note: There are actually a series of Auto Examples.... PushbotAutoDriveToLine, PushbotAutoDriveByTime, PushbotAutoDriveByEncoder that are somewhat of a progression.

                        If you read through them all, you will see several concepts that may help you with your own autonomous driving.

                        Phil.

                        Comment


                        • #13
                          In PushbotAutoDriveByGyro_Linear, to keep a constant heading while moving forward a set distance, variable speed is used in conjunction with Run To Position on each of the motors individually. Does this work better/worse/comparably to running a P or PD or PID controlled drive by reading the encoder values and running in Run Without Encoders mode?
                          Programmer for Team 4997 Masquerade -- 2012 World Champions, 2014 - 2016 Division Finalists
                          Founding Member of Team 6433 Neutrinos -- 2015 World Champions

                          Check out my intro video to the new tech platform
                          Check out my team's Robot Reveal for Res-Q

                          Comment


                          • #14
                            For us, we think doing explicit PID control is still more flexible than built-in motor controller PID. For the motor controller PID, it can only deal with the drive distance. You still need to do external PID calculations for the differential power of left and right wheels. I am not familiar with RunToPosition mode. Does it allow tuning the PID coefficients? If it does, you can probably do either. One could argue that built-in PID offloads part of the calculations to the microcontroller in the motor controller and may be doing it at a higher rate. But if it does not allow PID tuning, I prefer doing our own for optimal control. Besides, in our application, we need to distribute power to different wheels for many other reasons such as keeping a specific heading while doing mecanum drive, stall detection etc, so built-in PID doesn't allow that kind of flexibility.
                            In summary, if you are not doing anything other than keeping the robot driving straight in autonomous, using built-in PID is probably much easier to implement.

                            Comment


                            • #15
                              Originally posted by mikets View Post
                              In summary, if you are not doing anything other than keeping the robot driving straight in autonomous, using built-in PID is probably much easier to implement.
                              I would agree with mikets summary. If you REALLLLLLY know what you are doing with closed loop control and PID's, then you can probably roll your own "better" solution. However, if you don't have much experience, or don't have the tools to analyse the performance of the system you create, you are probably better off using the built-in Run-to-position. (Unless you want to use the full speed capabilities of your motors... but that's another thread

                              The RunToPosition mode takes care of ramping speeds up and down, and automatically stopping the motors when they reach their target, so you will typically get a more accurate stop then by simply running at constant speed and reading the encoder values.

                              Comment

                              Working...
                              X