I’m trying to get basic pid control working on my robot. I followed the guide here: Guide
In order to keep calculating error, I have the robot calculating the average of the revolutions of the left and right motors and subtract that number from the initial error. The issue that pops up is that both motors continously print 0 for the total rotations no matter what I try.
At first I tried creating a constant variable to add up all of the rotations but that stayed 0
I then printed out the rotation at every loop and it was also always 0.
The drivetrain moves the specified distance but the loop never ends because the error stays what it initially was. This is because the motor.rotation(rev) command always prints 0. In driverControl, the command works perfectly fine but fails to in auton. Why is this?
Not sure why it isn’t printing (deg and rev should work) . You might try using rotationUnits::rev - I have found some cases where the natural language or brief version did not work.
Couple things about your PID. You have no wait in the while loop, so it will count to 500 very fast and do almost nothing. You are trying to get to 0 error in a calculation that will have a large decimal place precision, so 0 has very little chance of ever being true. There is no filter to cap speed at a reasonable number, so the first values will be max velocity and likely will make your wheels slip at the start of your motion. GL.
@Omerktosi, please, be so kind to include your full code wrapped in the [code]...[/code] tags next time instead of the screenshot.
Your kD and kI are zero and fast cycle should not affect result but, please, follow @DougMoyers advice and include vex::task::sleep(10) into your loop. Without timeout you overflow cout buffers even before robot starts to move.
After you add timeout , please, check first that your motors are not returning same size but opposite sign rotation values that always add up to zero.
That’s why I have the time variable. Once the error reaches 0 (which I should change to rounded to 0), the time variable begins counting up. As long as the time variable is less than 500, which it is for as long as the error exists, the loop will run.
The loop runs multiple times, the drive train moves where it should, but it never exits that loop because the error never decreases. This is because the right drive and left drive both print 0 the whole time for motor.revolution(rev) for some unknown reason.
Ok, I guess I missed what you were doing with the time variable. Time is the way you want to exit… as it is written, the loop exits 10 seconds after error < .005
As for not exiting, your .005 revolution tolerance is 1/16 of an inch (if my math is right), which is pretty optimistic. Might set a threshold global, put it in spots you want to check it, and dial it up until the loop exits. As it is, .0051 error will never exit.
A point that is not relevant to your problem, you are setting integral to 0 when error is > 40. Because your error is based on revolutions, the maximum error you could have on a field is ~10. That 40 represents the number of revolutions to target.