Clone wiki

Greyhound Lua / Tutorials / Basic

Basic Tutorial

If you haven't done so already, please see the Installation instructions.

Writing your first program

Open up your favorite editor, and type:

-- robot.lua
module(..., package.seeall)

The first line isn't strictly necessary: it's a comment. Lua ignores any text after the -- to the end of the line, so you can keep notes on what the code does. The second line tells Lua that you want to start a new module. Continuing on:

function run()
   -- Main loop
   while true do
      if wpilib.IsDisabled() then
         repeat wpilib.Wait(0.01) until not wpilib.IsDisabled()
      elseif wpilib.IsAutonomous() then
         repeat wpilib.Wait(0.01) until not wpilib.IsAutonomous() or not wpilib.IsEnabled()
         repeat wpilib.Wait(0.01) until not wpilib.IsOperatorControl() or not wpilib.IsEnabled()

This is a lot of code, and the details aren't too important, but it's basically making sure that when the robot changes modes (e.g. autonomous to teleoperated), the right part of your code is called.

So what happens what teleoperated period starts? That's what we're going to write next:

function teleop()
   while wpilib.IsOperatorControl() and wpilib.IsEnabled() do
      -- Run one cycle of teleoperated here...

Basically, this just waits for the robot to disable. You'll change that later. And to complete our example:

function autonomous()
   -- Run autonomous code here...

function disabled()
   -- Do something in disabled mode here...

Downloading to the robot

You download your code to the robot using an FTP client (like Cyberduck). Your computer's IP address must be statically set to 10.XX.YY.6, where XXYY is your team number. Inside the FTP program, connect to ftp://10.XX.YY.2/##, again, substituting XXYY for your team number.

On the robot, create a folder called lua (case is important), copy samples/boot.lua into it, along with your new robot.lua file.

Power cycle, and you are running Lua!

Further Reading

For more examples, see the samples/ directory.