Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Python (http://www.chiefdelphi.com/forums/forumdisplay.php?f=187)
-   -   RobotPy: Python for FRC (http://www.chiefdelphi.com/forums/showthread.php?t=87260)

Peter Johnson 26-10-2010 00:49

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.

Robototes2412 26-10-2010 01:18

Re: RobotPy: Python for FRC
 
thank you so much :)

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

Peter Johnson 26-10-2010 01:42

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by Robototes2412 (Post 978421)
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).

Robototes2412 26-10-2010 01:47

Re: RobotPy: Python for FRC
 
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

AustinSchuh 26-10-2010 02:09

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by Peter Johnson (Post 978423)
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).

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.

Peter Johnson 26-10-2010 02:22

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by AustinSchuh (Post 978425)
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?

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.

AustinSchuh 26-10-2010 02:49

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by Peter Johnson (Post 978427)
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.

Robototes2412 26-10-2010 09:44

Re: RobotPy: Python for FRC
 
where does print go?

Peter Johnson 26-10-2010 12:39

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by Robototes2412 (Post 978451)
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/sh...ad.php?t=80146).

Robototes2412 26-10-2010 18:41

Re: RobotPy: Python for FRC
 
I have basic driving functions and inputs programmed:

output.py
Code:

#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:
Code:

#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)


Robototes2412 26-10-2010 19:43

Re: RobotPy: Python for FRC
 
1 Attachment(s)
Code Zipped up and is attatched.

First Release, nothing fancy

Peter Johnson 26-10-2010 23:29

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by AustinSchuh (Post 978429)
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.

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.

AustinSchuh 27-10-2010 00:58

Re: RobotPy: Python for FRC
 
Quote:

Originally Posted by Peter Johnson (Post 978550)
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.

virtuald 27-10-2010 09:14

Re: RobotPy: Python for FRC
 
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.

Zach O 27-10-2010 19:42

Re: RobotPy: Python for FRC
 
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.


All times are GMT -5. The time now is 17:20.

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