Go to Post Ship day is a sweet and sour day. You're happy to see your robot ship, but then you start going through robot withdrawl - Schneidie [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-11-2010, 23:19
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Python virtual speed controllers!

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
Code:
#!/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
Code:
#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
Code:
#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_axis)))
	resultant.append(bufferIt(self.joy.GetRawAxis(y_axis)))
	
	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
Code:
#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!
  #2   Spotlight this post!  
Unread 30-11-2010, 10:21
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,102
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Python virtual speed controllers!

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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
  #3   Spotlight this post!  
Unread 30-11-2010, 16:13
Bot190's Avatar
Bot190 Bot190 is offline
Registered User
FRC #0166 (ChopShop)
Team Role: Programmer
 
Join Date: Sep 2009
Rookie Year: 2009
Location: Merrimack NH
Posts: 105
Bot190 will become famous soon enough
Re: Python virtual speed controllers!

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

  #4   Spotlight this post!  
Unread 30-11-2010, 16:31
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,102
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Python virtual speed controllers!

Quote:
Originally Posted by Bot190 View Post
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:

Code:
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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
  #5   Spotlight this post!  
Unread 30-11-2010, 16:56
Bot190's Avatar
Bot190 Bot190 is offline
Registered User
FRC #0166 (ChopShop)
Team Role: Programmer
 
Join Date: Sep 2009
Rookie Year: 2009
Location: Merrimack NH
Posts: 105
Bot190 will become famous soon enough
Re: Python virtual speed controllers!

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?
__________________

  #6   Spotlight this post!  
Unread 30-11-2010, 21:47
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Re: Python virtual speed controllers!

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


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed Controllers Liu_yiang General Forum 11 07-02-2009 20:12
Speed controllers.... Denman Technical Discussion 14 07-10-2004 15:19
speed controllers archiver 2000 5 24-06-2002 00:19
Speed Controllers archiver 2000 1 23-06-2002 23:01
Speed Controllers... Tom Schindler Technical Discussion 4 13-12-2001 17:26


All times are GMT -5. The time now is 11:43.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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