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
         disabled()
         repeat wpilib.Wait(0.01) until not wpilib.IsDisabled()
      elseif wpilib.IsAutonomous() then
         autonomous()
         repeat wpilib.Wait(0.01) until not wpilib.IsAutonomous() or not wpilib.IsEnabled()
      else
         teleop()
         repeat wpilib.Wait(0.01) until not wpilib.IsOperatorControl() or not wpilib.IsEnabled()
      end
   end
end

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...
      wpilib.Wait(0.01)
   end
end

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

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

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.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.