Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Python (http://www.chiefdelphi.com/forums/forumdisplay.php?f=187)
-   -   Announcing a new RobotPy tool: fake-wpilib! (http://www.chiefdelphi.com/forums/showthread.php?t=109678)

virtuald 26-11-2012 02:11

Announcing a new RobotPy tool: fake-wpilib!
 
This is a library designed to emulate parts of WPILib (we have not implemented all of it, since we didn't need it) so you can more easily do unit testing of your robot code on any platform that supports python3, without having to have a cRio around for testing. Our team used it last year with our robot code, this is a better packaged version of that same code.

In particular, we use this to quickly run our robot code in all modes before uploading it to the robot, to ensure that there aren't any obvious indentation or syntax errors that might not have been caught otherwise. An example that does this is included, called 'import_test'

Please let us know if you use it, and how we can make it better -- and submit patches to add/fix functionality! We will be updating it as we use it.

Probably during the regular season this will be released in a separate package on the RobotPy website. For now, you can download it via the RobotPy github site at https://github.com/robotpy/fake-wpilib

One thing that we have implemented with it is a pyTkinter-based game for our students to code against -- using this library and a GUI implementation, they can drive their robot code around on a virtual field shown on the screen. We're planning on implementing objects and stuff on the field and interactive sensors and stuff, so that they can learn robot programming even easier without a robot. We have not yet released this code, but probably in the future when it's more complete.

virtuald 07-12-2012 18:01

Re: Announcing a new RobotPy tool: fake-wpilib!
 
I've updated fake-wpilib to include a working version of SmartDashboard (which is identical to the version contained in RobotPy), which you can use with the latest versions of SmartDashboard to test your SmartDashboard ideas on your PC.

By running 'C:\Program Files\SmartDashboard\SmartDashboard.jar ip 127.0.0.1', it will tell the SmartDashboard to connect to your code running on the PC. This is only supported in the SVN version of SmartDashboard, and does not work in the binaries currently released by FRC. This should work in 2013 releases, however.

tux 03-02-2013 22:52

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!

virtuald 04-02-2013 14:29

Re: Announcing a new RobotPy tool: fake-wpilib!
 
Glad to hear you've found it useful. I am curious why you're not using a class-based approach, however.

nightpool 04-02-2013 15:36

Re: Announcing a new RobotPy tool: fake-wpilib!
 
Ehh. I'm not the OP, but everyone has different approaches to programming. Python is built to be multiparadigm and support both object-oriented and functional programming styles equally. In practice Python is mainly OO, but there are still a lot of cases where classes are just not needed.

nightpool 04-02-2013 15:44

Re: Announcing a new RobotPy tool: fake-wpilib!
 
Quote:

Originally Posted by tux (Post 1227164)
Code:

    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...

Doesn't fake_wpilib already do this? Looking at __init__.py Wait is already tied to fake_time.Wait

virtuald 04-02-2013 17:13

Re: Announcing a new RobotPy tool: fake-wpilib!
 
Quote:

Originally Posted by nightpool (Post 1227418)
Ehh. I'm not the OP, but everyone has different approaches to programming. Python is built to be multiparadigm and support both object-oriented and functional programming styles equally. In practice Python is mainly OO, but there are still a lot of cases where classes are just not needed.

Yes, I am aware that it is not necessary to do it that way -- that's why RobotPy has support for both ways (and perhaps at some point, fake-wpilib will support it too. Maybe someone can submit patches :) ).

However, it doesn't answer the question of why the OP decided to do it that way, which was the original question.

tux 04-02-2013 17:53

Re: Announcing a new RobotPy tool: fake-wpilib!
 
Quote:

Originally Posted by nightpool (Post 1227422)
Doesn't fake_wpilib already do this? Looking at __init__.py Wait is already tied to fake_time.Wait

Right, but when I was trying to figure out what was going on I wanted things to go slower.

If you uncomment the import and the time.sleep() you can slow down the whole process...

tux 04-02-2013 18:02

Re: Announcing a new RobotPy tool: fake-wpilib!
 
Quote:

Originally Posted by virtuald (Post 1227385)
I am curious why you're not using a class-based approach, however.

None of our students have any programming experience. I think it is simpler to start with a function-based system.

I have introduced a decorator that sets up static variables (sort of like in C) which helps to alleviate the need for global variables that the functional style sometimes requires.


Code:

def static(**kw):
    '''
    Used to create a decorator function that will add an
    attribute to a function and initialize it.

    >>> @static(foo=5)
    ... def bar():
    ...    print(bar.foo)
    ...    bar.foo += 1
    ...
    >>> bar()
    5
    >>> bar()
    6
    '''

    def decorator(f):
        f.__dict__.update(kw)
        return f
    return decorator



All times are GMT -5. The time now is 19:49.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi