RobotPy: Python for FRC

After being inspired by Ross Light and Team 973’s success in porting Lua to the cRIO (see the “Announcing FIRSTLua” thread), I have completed a port of Python to the cRIO, called RobotPy.

RobotPy is a fully functioning port of Python 3.1.2 that can be used for programming your FRC robot. The full set of WPILib functions, including CANJaguar, is available in Python via “import wpilib”.

As with the Lua port,

  • Python is easier to learn than C
  • Live code reloads
  • Errors won’t crash the robot
  • Python provides automatic garbage collection
  • WindRiver isn’t needed after the base code is installed. Just a text editor and an FTP client.

I’ve performed some initial testing on Team 294’s 2010 robot, including running CAN bus Jaguars and compressors and pneumatics. Examples and installation documentation are available on the website and are also included in the download. Also, the download .zip file includes a prebuilt FRC_UserProgram.out, so you don’t need to have any programming software installed to get up and running (just FTP a bunch of files to your robot)! Please report any bugs you find via the RobotPy website’s ticket system (note: registration is required).

RobotPy website: http://www.tortall.net/projects/robotpy/

While the download .zip doesn’t include the source code, it is available via a git repository. See the Download page on the website for details.

With the exception of modules with large dependencies (e.g. databases), almost all of the Python standard library is included. So yes, you can parse XML files with expat on the robot if you so choose :). Much of this is untested however (I have not tried to run the Python test suite on the robot).

Major thanks go to Ross Light and Team 973; this project would not have happened without them creating FIRSTLua first to inspire me to port Python. RobotPy uses the WPILib SWIG wrapper created by Ross, with the addition of CANJaguar. The idea of using a boot script to auto-reload the user code is also taken from FIRSTLua.

1 Like

thank you so much :slight_smile:

I’m already porting my team’s high level motor grouper and pneumatics classes, I’m hoping these can be included with your permission, of course

Of course! I’d love to build up and include (maybe as a separate download) a library of team-built robot-oriented Python modules. And if anyone has suggestions on any other common/useful modules to include, I’ll look at adding them as well (NumPy and SciPy come to mind, for example).

I have most of the common inputs that my team used coded in python for prototyping, so I will be done with the whole thing by tomorrow

If you don’t get to it first, I’m going to try to port NumPy and SciPy to the cRIO. I’ve been using those for prototyping up control loops on my desktop, and they are really nice.

Do sockets work? That would make it easy to add a telnet server to the robot, and allow remote execution of code to aid testing. Also, does print work correctly?

Thanks again for doing this! I’m really excited.

I haven’t tested sockets yet. Theoretically they should work, but a lot has to be correct for them to work, and I might have missed something.

Yes, print works correctly.

Feel free to tackle NumPy and SciPy. FYI, to date, I’ve been integrating C/C++ modules directly into Python/Lib and Python/Modules/ (and updating Modules/config.c to add them to _PyImport_Inittab), because it’s all a static executable anyway.

Thanks for the information. Unfortunately my course load is quite high this semester, so it might be quite a number of weeks until I get to NumPy and SciPy. But they are definitely on the list of things to do, and I’ll let you know when I get time and get somewhere with them.

where does print go?

The cRIO console. There’s a couple different ways to see it; NetConsole is probably the best (see http://www.chiefdelphi.com/forums/showthread.php?t=80146).

I have basic driving functions and inputs programmed:

output.py

#Made by Sam Dodrill for Team 2412, WTFPL

from wpilib import Victor, Solenoid
import wpilib
import math

class Driver:
    def __init__(self, FL, FR, RL = None, RR = None):
        self.FLvictor = Victor(FL)
        self.FRvictor = Victor(FR)
        
        if RL != None and RR != None:become standard for the interpreter :)
            self.RLvictor = Victor(RL)
            self.RRvictor = Victor(RR)
            self.fourWheel = True
            
        
    def drive(self, speed):
        self.FLvictor.Set(speed)
        self.FRvictor.Set(-speed)
        
        if fourWheel:
            self.RLvictor.Set(speed)
            self.RRvictor.Set(-speed)
    
    def tankDrive(self, left, right):
        self.FLvictor.Set(left)
        self.FRvictor.Set(-right)
        
        if fourWheel:
            self.RLvictor.Set(left)
            self.FRvictor.Set(-right)
    
    def limit(self, n):
        if n > 1:
            return 1
        elif n < -1:
            return -1
        else:
            return n
    
    def arcadeDrive(self, speed, direction):
        left  = speed - direction
        right = speed + direction
        
        maxi = max(left, right)
        
        if maxi > 1:
            left = left/maxi
            right = right/maxi
        
        left = limit(left)
        right = limit(right)
        
        self.tankDrive(left, right)
        
    def holonomicDrive(self, power, slide, spin):
        fl = power + slide + spin
        fr = power - slide - spin
        rl = power - slide + spin
        rr = power + slide - spin
        
        fl = limit(fl);
        fr = limit(fr);
        rl = limit(rl);
        rr = limit(rr);
        
        self.setSpeed(fl, fr, rl, rr)
        
    def setSpeed(self, fl, fr, rl, rr):
        self.FLvictor.Set(fl)
        self.FRvictor.Set(-fr)
        self.RLvictor.Set(rl)
        self.RRvictor.Set(-rr)
        
    def goLeft(self):
        self.holonomicDrive(0, 0.5, 0)
    
    def goRight(self):
        self.holonomicDrive(0, -0.5, 0)
    
    def goForward(self):
        self.holonomicDrive(0.5, 0, 0)
    
    def goBackward(self):
        self.holonomicDrive(-0.5, 0, 0)
    
    def turnLeft(self):
        self.holonomicDrive(0, 0, -0.5)
        
    def turnRight(self):
        self.holonomicDrive(0, 0, 0.5)
    
    def stop(self):
        print("OH CRAP")
        self.drive(0)

input.py:

#made by Sam Dodrill for team 2412, but you can use it too

import wpilib, math

class Attack3:
    def __init__(self, port)
        self.joy = wpilib.Joystick(port)
    
    def getX(self):
        return self.joy.GetRawAxis(2)
    
    def getY(self):
        return self.joy.GetRawAxis(1)
    
    def getThrottle(self):
        return self.joy.GetRawAxis(3)
    
    def getTrigger(self):
        return self.joy.GetRawButton(1)
    
    def get2(self):
        return self.joy.GetRawButton(2)
    
    def get3(self):
        return self.joy.GetRawButton(3)
    
    def get4(self):
        return self.joy.GetRawButton(4)
    
    #yada yada yada, TODO: finish with all 11 buttons
    
def calcBuffer(self, n):
    buffers = 0.2
    if math.abs(n) < buffers:
        n = 0
        
    return n
    
class Xbox:
    def __init__(self, port):
        self.joy = wpilib.Joystick(port)
    
    def getStick(self, n1, n2): #returns a list in the form of [x,y]
        r = list()
        r.append(calcBuffer(self.joy.GetRawAxis(n1)))
        r.append(calcBuffer(self.joy.GetRawAxis(n2)))
        return r
    
    def getLeftStick(self):
        return self.getStick(1,2)
    
    def getRightStick(self):
        return self.getStick(4,5)
    
    def getButtons(self, n1, n2, n3, n4): #returns a list in the form of [b1, b2, b3, b4]
        r = list()
        r.append(self.joy.GetRawButton(n1))
        r.append(self.joy.GetRawButton(n2))
        r.append(self.joy.GetRawButton(n3))
        r.append(self.joy.GetRawButton(n4))
        
        return r
        
    def getMainButtons(self): #returns [a,b,x,y]
        return self.getButtons(1, 2, 3, 4)

Code Zipped up and is attatched.

First Release, nothing fancy

py.zip (3.02 KB)


py.zip (3.02 KB)

I glanced at NumPy/SciPy. NumPy should be straightforward. SciPy, however, requires a Fortran compiler, which isn’t included with WindRiver. It should be possible to build a cross-compiler but it will definitely take more work.

Hmm. That does complicate things. Something else to add to the list of prereqs for SciPy. It looks like some of what I assumed was in SciPy is also in NumPy (matricies, for example) so NumPy might be enough. At the very least, it’ll help quite a bit.

This is totally awesome. When I saw the Lua thing, I was thinking the same thing, so I’m glad someone took the time to do it. I’ll have to try it on the bot later.

I’m super excited to see this! I love Python as a langue, and I can’t wait to maybe load this on a practice robot.

I’ve implemented dynamic loading of C modules. The next release of RobotPy will break out many of the C modules into separate loadable object files, the same way that normal Python does. These separate .out files are located in /lib/python3.1/dyn-load on the robot, so they won’t clutter up the /ni-rt/system directory.

This should also make distributing other modules (e.g. NumPy) easier, as they can be distributed as separate .zip packages without having to integrate them into the RobotPy executable.

I’m also going to make the wpilib module dynamically loaded. This will make it easier to test with different/beta/custom versions of WPILib. This reduces the size of the basic Python executable down to ~3.5M; the wpilib module is ~5M!

After they were blown away with WindRiver/C++ last season, I introduced the controls and software team to Python by taking part in Team 342’s summerpygames. They got the hang of it and are asking for more. This is perfect! :cool:
We’ll give it a try.

Thanks,
Eric

…just a thought. You might be able to f2c the FORTRAN. I’ve had success with this with older versions of vxWorks.

A bit off topic, but the book “Learn Python The Hard Way” seems like a great guide to learning how to program, using Python. (Different than learning how to use Python :p)

This might be good supplement to RobotPy.

http://learnpythonthehardway.org/index

I built the most recent version from source. The make_dist.bat file copies the loadable modules to “dyn-load”, but Python looks for the modules in the “lib-dynload” directory. I fixed the bat file to reflect this, and it works fine now. Before, boot.py couldn’t find the time library and aborted.

I post this here because the discussion boards on FIRST Forge are limited to project members (total project members: 1).