View Single Post
  #6   Spotlight this post!  
Unread 16-03-2014, 17:02
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,043
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: PIDController causing code to fail silently

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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote