No announcement yet.

PID & Gyro with Encoders

  • Filter
  • Time
  • Show
Clear All
new posts

  • PID & Gyro with Encoders

    We tried to make an autonomous code that used PID and the gyro sensor to align with the current heading and all that. We wanted to make the program work in such a way that it would run a while loop to align and adjust the powers for a certain specified distance. So we would tell the program to move forward say 5 rotations. While it was moving forward it would run this while loop to align and correct itself. However, whenever we try making it move forward would make it move backward infinitely even though the correction is calculated. It for some reason does not correct and I do not know why. We haven't been able to test our moveBackward function but it should be pretty similar to the moveForward function. We haven't used encoders before so we do not know what might be going wrong. It seems to be going backwards infinitely and doesn't work. The code which is supposed to use PID in the instance of driving using the encoders seems to drive infinitely either forward or backward depending on the powers. It doesn't drive exactly one rotation or the specified distance at any point. The code was designed so that it would be given a target position and it would run the PID loop until it reaches the target position and then break the loop and stop. In this case, I would think that the target position would be set and it would be using the the loop to keep changing the power level during the motion until it reaches the target position. Correct me if I am wrong, as I haven't used encoders for this purpose in the past. Please help me out if possible.

  • #2
    Your link to google drive isn't working. You could try posting the link again, or copy and paste your code.


    • #3
      That's weird, I don't think the full link was posted. Here is the right link:


      • #4
        I haven't looked at your link, but it sounds like it may be something in your configuration rather than your code. If you haven't used encoders before, you may want to start with something simpler to understand how they work. Have you tried the sample program ""?

        Are you printing telemetry? I would show the current count along with target for each drive motor. Some things to check:

        Do you have the correct motor type selected in the configuration?
        Do you have the correct DcMotor.setDirection() for your motors and physical setup on the robot?
        Does your teleop program drive/turn in the direction you expect?
        Are the encoder wires connected to the same port as their corresponding motor power cables?
        If you are using a REV module, do you have the logic level shifters connected securely and in the proper orientation?