Also, I've had very odd problems in the past using the PIDController, so I've tried to avoid using it for most things. When we've needed that level of control, we tend to use the PID control available on the Jaguar, instead of using the PIDController class.
One thing worth noting too is you can get more consistent control on your main thread if you control the amount of delay more precisely than using wpilib.Wait() at the end of each loop. We use this object to do it.
Code:
try:
import wpilib
except ImportError:
from pyfrc import wpilib
class PreciseDelay(object):
'''
Used to synchronize a timing loop.
Usage:
delay = PreciseDelay(time_to_delay)
while something:
# do things here
delay.wait()
TODO: Does this add unwanted overhead?
'''
def __init__(self, delay_period):
'''
:param delay_period: The amount of time to do a delay
'''
self.timer = wpilib.Timer()
self.delay_period = delay_period
self.timer.Start()
def wait(self):
'''Waits until the delay period has passed'''
# we must *always* yield here, so other things can run
wpilib.Wait(0.001)
while not self.timer.HasPeriodPassed(self.delay_period):
wpilib.Wait(0.001)
With something like this controlling your delay, you can implement PID-like control in your main loop without too many problems.