Blog Post

Programming with SD540 and Python

Programming with SD540 and Python

3226

Connecting the SD540

For proper connection of the SD540, read the 'Connecting the SD540' section of the 'How to Use SD540' blog post.

 

Getting Started with Python

If you are starting a new FIRST robotics competition team or you are already involved with a team and are considering using a new programming language for your robot, you may want to consider python, a simple, powerful language perfect for beginners and experts alike. While FIRST does not provide official python-compatible software for the roboRIO, the WPI robotics library has been ported to python, and is legal to use on your competition robot.

  

Installing RobotPy

First, make sure your computer has Python 3.5 installed (do not forget to check the box that says to add to PATH), as well as pip (download and open the executable, type “upgrade” into the “Command” box, and click “run”).

 

Next, make sure your roboRIO is connected to your computer (if this is a new roboRIO, you will have to image it first). Methods of verifying connection include opening the driver station and checking the connection status light, or opening your browser and typing “roboio-XXX.local”, where XXX is your team number. If you use the latter method, and you do not have Microsoft Silverlight, a notification will appear asking you to install it, which is enough to verify connection to the roboRIO.

 

Next, download RobotPy here, and unzip the file by right-clicking on it (not opening it) and selecting “extract all.” Open windows command prompt and change the directory to the location of the extracted files (type “cd” followed by a space and then the file path). Click here for more information about how to use the windows command prompt. Once you have changed the directory to that location, type the following command:

  

py installer.py install-robotpy

 

It will prompt several times for information, and will proceed to install all of the necessary files on your roboRIO.

 

Once you have done this, install the python libraries on your computer by typing the following command (using pip we installed above).

  

pip install pyfrc

 

That's it! Once pyfrc installs, you are ready to write robot code!

 

For more information about RobotPy installation, as well as helpful python tutorials, click here.

 

For more information about PyFRC installation, click here.

   

Sample Code and Deployment

Paste the following code into a plain-text document (a new document created in notepad) which is saved with the name “robot.py”. This code will run two motors in PWM slots zero and one with respect to the input from a joystick designated to USB 1, and is compatible with the SD540 PWM capability. For instructions on how to wire the SD540 to the PWM slots on the roboRIO, click here. To run this code after deployment, you will need to run the robot driver station application (info here), installed with the FRC Labview update.

  

#!/usr/bin/env python3
import wpilib
class MyRobot(wpilib.IterativeRobot):
    def robotInit(self):
        self.robot_drive = wpilib.RobotDrive(0,1) #initialize PWM slots 0 and 1.  Plug SD 540 in those
						     # slots on the RoboRIO
        self.stick = wpilib.Joystick(1) #initialize joystick
    def teleopPeriodic(self):
        self.robot_drive.arcadeDrive(self.stick) #move motors based on joystick input
    def testPeriodic(self):
        wpilib.LiveWindow.run()
if __name__ == "__main__":
    wpilib.run(MyRobot)

  

Then, open command prompt again and change directory to the folder containing that file. Enter the following command:

 

py robot.py deploy --builtin

 

This will test the robot code and then download it to the roboRIO. If you have already run this command, have made changes to the code, and are attempting to re-deploy, then it may be necessary to reboot the roboRIO before beginning deployment. If you wish to see the result of print statements in your code, then type the following command as well:

 

netconsole

 

This command was installed with pyfrc, and allows the user to monitor the status of the roboRIO, as well as read the print statements written into the code. If you wish to exit the python netconsole, type CTRL-C into the command prompt, and it should exit without closing the command prompt window.

 

For more information about code deployment, click here.

Comments (0)

No comments at this moment
Please Login to add your comments

Featured Posts

The mindsensors Grove Sensor Adapter makes it easy to use a variety of unique Grove sensors with the...

Read more

The mindsensors Grove Sensor Adapter makes it easy to use a variety of unique Grove sensors with the...

Read more

Poll

  • What do you use to program your FRC Robot?
    Total: