Log in

View Full Version : Python virtual speed controllers!


Robototes2412
25-11-2010, 23:19
Hello all.

I know this should go in the python forum, but this can affect everyone.

here is the code, just put it all in one directory, running directions below:

debug_test.py
#!/usr/bin/python

import cmd

test = None
var = []

def do_args(args):
print args.split()

def do_driver(args):
arg = args.split()
test_passed = False

if len(arg) > 0:
var1 = float(arg[0])
var2 = float(arg[1])
var3 = float(arg[2])
else:
var1 = 0.5
var2 = var1
var3 = var1
try:
import outputs, testing
test = outputs.Driver(1,2,3,4)
print
print "Doing tankDriveRaw:"
test.tankDriveRaw(var1, var2)
print
print "Doing holonomicDriveRaw:"
test.holonomicDriveRaw(var1, var2, var3)
print
print "testing the fancy list-based versions, tankDrive then holonomicDrive"
test.tankDrive([0, var1], [0, var2])
test.holonomicDrive([var1, var2, 0, var3])
print
print "Don\'t forget to stop the robot"
test.stop()
test_passed = True

except:
print "fail"

if test_passed:
print "The robot is able to move, test passed"

class tests(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = "2412_debug# "

def do_driver(self, args):
"""Actually tests the driver class I wrote"""
return do_driver(args)

def do_EOF(self, args):
import sys
print
sys.exit(0)

def do_args(self, args):
"""A dummy to test variable handling"""
return do_args(args)

def do_show(self, args):
"""A failed show command"""
arg = args.split()

if arg[0] == "test":
print test

if arg[0] == "ver":
print "Version 0.2, Touko"

else:
for n in var:
if arg[0] == n:
print var[n][arg[0]]

def do_var(self, args):
"""Sets variables, can't do much else :("""
args = args.split()

if args[0] == "test":
test = args[1]
print "test case"
else:
var.append({args[0]:args[1]})
print "other case"
print "variable " + args[0] + " set to " + args[1]

def do_showVars(self, args):
"""Shows all raw variables"""
args = args.split()

for n in var:
print n

def do_prompt(self, args):
"""Allows you to change your prompt"""
args = args.split()

self.prompt = args[0]

def do_cure_cancer(self, args):
"""the obligatory easter egg"""
args = args.split()

print "sorry, my mentor told me to not make programs cure cancer :P"

main = tests()

main.cmdloop()

outputs.py
#not the main class

try:
import wpilib as wpi
except:
import testing as wpi

class Driver:
def __init__(self, port1, port2, port3, port4):
self.FL = wpi.Victor(port1)
self.FR = wpi.Victor(port2)
self.RL = wpi.Victor(port3)
self.RR = wpi.Victor(port4)
self.vicList = [self.FL, self.FR, self.RL, self.RR]

def stop(self):
"""Stops the robot, useful for everything"""
self.tankDriveRaw(0, 0)
print "Robot Stopped"

def tankDrive(self, stick, stick2):
left = stick[1]
right = stick2[1]

try:
self.vicList[0].Set(left)
self.vicList[1].Set(-right)
self.vicList[2].Set(left)
self.vicList[3].Set(-right)
except:
print "tankDrive at " + str(left) + " and " + str(right)

def holonomicDrive(self, xboxController):
power = xboxController[1] #y-axis, first stick
strafe = xboxController[0] #x-axis, first stick
spin = xboxController[3] #x-axis, second stick (x...[2] is the trigger axis)

fl = power + strafe + spin
fr = power - strafe - spin
rl = power - strafe + spin
rr = power + strafe - spin

print "holonomicDrive called"

self.FL.Set(fl)
self.FR.Set(fr)
self.RL.Set(rl)
self.RR.Set(rr)

def tankDriveRaw(self, left, right):
self.vicList[0].Set(left)
self.vicList[1].Set(-right)
self.vicList[2].Set(left)
self.vicList[3].Set(-right)
print "tankDriveRaw at " + str(left) + " and " + str(right)

def holonomicDriveRaw(self, power, strafe, spin):
fl = power + strafe + spin
fr = power - strafe - spin
rl = power - strafe + spin
rr = power + strafe - spin

print 'holonomicDriveRaw called'

self.FL.Set(fl)
self.FR.Set(fr)
self.RL.Set(rl)
self.RR.Set(rr)


inputs.py
#inputs.py

def system():
pass

try:
import wpilib as wpi
system.runPlace = 0 # robot
except:
import testing as wpi
system.runPlace = 1 # laptop


class Attack3:
def __init__(self, port):
self.port = port
self.joy = wpi.Joystick(self.port)

print "New Attack3 configured at port " + str(port)

def getAxies(self, x_axis, y_axis):
"""Returns a list in the format of [arg1, arg2]"""
resultant = list()
resultant.append(self.joy.GetRawAxis(x_axis))
resultant.append(self.joy.GetRawAxis(y_axis))

return resultant

def getStickAxes(self):
return self.getAxies(2,1)

def getThrottle(self):
return self.joy.GetRawAxis(3)

def getButtons(self):
r = [] #r is of type list

for n in range(1,12):
r.append(self.joy.GetRawButton(n))

#To the tune of ghostbusters:

#When there's an annoying problem, that you need to solve?
#whatcha gonna use?
#A FOR LOOP!
#when there's a lot of data
#that you need to parse
#whatcha gonna use?
#A FOR LOOP!

#I ain't afraid of no macs

#XD

return r

def getTrigger(self):
return self.getButtons()[0]

class Xbox():
def __init__(self, port):
self.port = port
self.joy = wpi.Joystick(self.port)
self.a = 1
self.b = 2
self.x = 4
self.y = 3
self.lb = 6
self.rb = 5
self.back = 7
self.start = 8

print "New Attack3 configured at port " + str(port)

def bufferIt(n):
if abs(n) < 0.2:
n = 0
return n

def getAxies(self, x_axis, y_axis):
"""Returns a list in the format of [arg1, arg2]"""
resultant = list()
resultant.append(bufferIt(self.joy.GetRawAxis(x_ax is)))
resultant.append(bufferIt(self.joy.GetRawAxis(y_ax is)))

return resultant

def getAllAxes(self):
r = list()

for n in range(1,6):
r.append(bufferIt(self.joy.GetRawAxis(n)))

return r

def getButton(self, button):
return self.joy.GetRawButton(button)

def getButtonList(self):
r = list()

for n in range(1,9):
r.append(self.joy.getRawButton(n))

return r

testing.py
#This file should emulate enough of wpilib to make it usable for testing
#TODO: use pygame to get joystick inputs from actual joysticks


def limit(n):
if n > 1.0:
return 1.0
elif n < -1.0:
return -1.0
else:
return n

class Victor:
def __init__(self, port):
self.port = port
print "Virtual Victor set up on port " + str(port)
self.speed = float()

def Set(self, speed):
"""***FOR TESTING PURPOSES ONLY***
This "sets" the virtual Victor to any arbitrary speed"""
speed = limit(speed)
print "on port " + str(self.port) + " the speed is now " + str(speed)
self.speed = speed

def Get(self):
"""***FOR TESTING PURPOSES ONLY***
This emulates the wpilib.Victor.Get function"""
return self.speed

class Joystick:
def __init__(self, port):
self.port = port
print "New joystick set up at port "+str(port)

def Get(self):
return 0.5

def speedReport(list_obj):
"""Use this for the driver object"""
for n in list_obj:
n.Get()

def IsDisabled():
return False

def IsAutonomous():
return False

def IsOperatorControl():
return True

def IsEnabled():
return True

class dummyDog:
def SetEnabled(self, var):
pass

def SetExpiration(self, var):
pass

def Feed(self):
pass

def GetWatchdog():
return dummyDog()


run debug_test.py
its a shell interface to my tests, you can throughly ignore show and var, it was a part of another project i attempted (and failed at)
driver does the cool stuff

have fun!

virtuald
30-11-2010, 10:21
It should be noted that the python interpreter ported to the cRio is python3, not python2. So, if you were to run the robot.py on there, it would have some problems, particularly because of the print statement is now a function in python3.

Bot190
30-11-2010, 16:13
I'm pretty sure this code was NOT intended to be run on a robot, and is meant to be a simulation run on a computer.

virtuald
30-11-2010, 16:31
I'm pretty sure this code was NOT intended to be run on a robot, and is meant to be a simulation run on a computer.

Referring to the top of the file:


try:
import wpilib as wpi
except:
import testing as wpi


That import statement seems to refer to the wpilib that is available in the python interpreter ported to the cRio. So, it could theoretically be run on the robot, if the bugs were fixed. Might not be useful, but it could work.

Also refer to inputs.py, it has the same thing that switches the input device from a joystick to predetermined values.

Bot190
30-11-2010, 16:56
Ok, now I've confused myself, Looking at what you said, then looking at the code, and what the OP said, I have no clue what its meant to do, and how its meant to be used. Can the OP clarify this?

Robototes2412
30-11-2010, 21:47
sorry,

I didn't realize it was py3 on the robot, i will fix the prints.

What you can do with this is using the cmd framework I have set up, you can essentially have your laptop run any code the robot would (when this is done, of course) and get a result you can use a whiteboard to draw out.

This code will also (hopefully) run on robotPy.

Can I please have feedback on things such as my coding style? It would help a lot.

Sorry for the confusion.