View Single Post
  #3   Spotlight this post!  
Unread 03-02-2013, 22:52
tux tux is offline
Registered User
AKA: Lee Harr
FRC #3842 (Shock-a-Bots)
Team Role: Mentor
 
Join Date: Apr 2005
Rookie Year: 2005
Location: Rochester, NY
Posts: 91
tux is an unknown quantity at this point
Re: Announcing a new RobotPy tool: fake-wpilib!

I am using fake-wpilib as a handy test before committing or uploading code. Saves me a lot of time since I tend to do dumb things like forgetting to import a function that I started using and mistyping names.

Our code is not class based, so I monkey-patch _wpilib to enable the robot, switch to autonomous mode, then switch to teleoperated.

I have a module called
Code:
get_wpilib.py
All modules that need wpilib say:
Code:
from get_wpilib import wpilib

Code:
try:
    import wpilib
except ImportError:
    import sys
    sys.path.append('../fake-wpilib/lib/')
    import fake_wpilib as wpilib

    import _wpilib
    _wpilib.internal.enabled = True
    def start_auto(tm):
        #print(tm)
        return tm > 2 and tm < 17
    _wpilib.internal.on_IsAutonomous = start_auto

    def start_tele(tm):
        #print(tm)
        return tm > 17 and tm < 30
    _wpilib.internal.on_IsOperatorControl = start_tele


    def wwwait(t):
        #import time
        #time.sleep(0.01)
        _wpilib.fake_time.Wait(t)
    wpilib.Wait = wwwait
wwwait can be used to slow things down if needed...


I also modified the joystick code to return random values. I think of it as "fuzz testing" the robot code.

Code:
    def GetRawAxis(self, axis):
        import random
        return random.uniform(-1, 1)

    def GetRawButton(self, number):
        import random
        return random.choice([True, False])
Works pretty well so far. Saves many reboots!

Thanks for fake-wpilib!
Reply With Quote