OpenTechSchool Artificial Intelligence for robots and swarms

Reference Lua


Home About

Course by Roman for OTS Brussels

The table below contains a short Lua reference, with examples. It covers just the essential parts of the language you need to program the robots.

Comments
-- This is a single-line comment

--[[ This is a multiline
     comment ]]
                    
Variables
-- assignment (no need to declare a variable)
x = 2.55   -- number
x = "ciao" -- string

-- printing on the ARGoS logger
log("INFO: x = " .. x)
logerr("ERROR: x = " .. x)

-- NOTE: in ARGoS, the standard Lua 'print()' function does not work
                    
Conditionals
-- simple if/then
if x > 3 then
   logerr("x is too big")
end

-- if/then/elseif/else
if x > 3 then
   logerr("x is too big")
elseif x < 3 then
   logerr("x is too small")
else
   logerr("maybe I just don't like x")
end

-- checking for equality
if x == 3 then
   log("x is equal to 3")
end

-- checking for inequality
if x ~= 4 then
   log("x is different from 4")
end

-- combining conditions with OR
if (x > 3) or (x < 3) then
   logerr("x is not 3")
end

-- combining conditions with AND
if (x > 3) and (y > 3) then
   logerr("x and y are too big")
end

-- negating a condition
if not (x > 3) then
   logerr("x is <= 3")
end
                    
Loops
-- 'while' loop to print 1 2 3 4 5
x = 0
while x < 5 do
   x = x + 1
   log(x)
end

-- 'repeat until' loop to print 1 2 3 4 5
x = 0
repeat
   x = x + 1
   log(x)
until x == 5

--[[ 'for' loop with increase/decrease of a variable:
     for x = init_value, end_value, step do ... end
     where:
        x = init_value initializes the counting variable x to init_value
        end_value      is the stopping value
        step           is the (optional) value to sum to x at each iteration
     the loop continues:
        - when x <= end_value, if step > 0
        - when x >= end_value, if step < 0
  ]]

-- 'for' loop to print 1 2 3 4 5
for x = 1, 5 do
   log(x)
end

-- 'for' loop to print 1 3 5
for x = 1, 5, 2 do
   log(x)
end

-- 'for' loop to print 5 4 3 2 1
for x = 5, 1, -1 do
   log(x)
end
                    
Tables
-- creating an empty table
t = {}

-- creating a table with some initial value
t = { x=3 }

-- using the contents of a table: two equivalent ways
log("t.x = " .. t.x)       -- dot syntax
log("t['x'] = " .. t["x"]) -- string syntax

-- printing the contents of a table: a custom function
function table.print(t)
   for key,value in pairs(t) do
      log(key .. " -> " .. value)
   end
end

-- tables are always passed by reference!
t1 = { x=3 }
t2 = t1      -- now t2 points to the contents of t1 -> no deep copy
t2.x = 5
log(t1.x)    -- prints 5, not 3!

-- copying tables the right way
function table.copy(t)
   local t2 = {}
   for key,value in pairs(t) do
      t2[key] = value
   end
   return t2
end
t1 = { x=3 }
t2 = table.copy(t1)
t2.x = 5
log(t1.x)    -- prints 3
log(t2.x)    -- prints 5

-- tables can be used as arrays
a = { "Huey", "Dewey", "Louie" } -- indices are assigned automatically starting from 1
log(a[1])                        -- prints "Huey"
log(a[2])                        -- prints "Dewey"
log(a[3])                        -- prints "Louie"
log(#a)                          -- prints the number of elements in a

-- sorting the contents of a simple table
a = { "Huey", "Dewey", "Louie" }
table.sort(a)  -- this operation modifies a!
table.print(a) -- prints "1 -> Dewey", "2 -> Huey", "3 -> Louie"

--[[ To sort the contents of a nested table, such as those in the robot,
     you need to specify a comparation function to tell the sort algorithm
     how to compare two table elements ]]

-- Take a nested table
t = {
   { x=4, label="Huey" },
   { x=56, label="Dewey" },
   { x=0.6, label="Louie" }
}

-- Sort it by x in increasing order
table.sort(t, function(a,b) return a.x < b.x end)

-- Sort it by x in decreasing order
table.sort(t, function(a,b) return a.x > b.x end)

-- Sort it by label in increasing order
table.sort(t, function(a,b) return a.label < b.label end)

-- Sort it by label in decreasing order
table.sort(t, function(a,b) return a.label > b.label end)

                    
Functions
-- defining a function
function my_fun(p)
   log("Called my_fun(" .. p .. ")")
end

-- returning a value
function my_add(a, b)
   return a + b
end
                    
Math
-- All the math functions are part of the 'math' table

-- Setting a 2D vector from length and angle
function vec2_new_polar(length, angle) {
   local vec2 = {
      x = length * math.cos(angle)
      y = length * math.sin(angle)
   }
   return vec2
}
v = vec2_new_polar(2, math.pi/3)

-- Summing two 2D vectors (v1 = v1 + v2)
function vec2_sum(v1, v2) {
   v1.x = v1.x + v2.x
   v1.y = v1.y + v2.y
}
v1 = { x=1, y=2 }
v2 = { x=3, y=1 }
vec2_sum(v1, v2)
table.print(v1)   -- prints "x -> 4", "y -> 3"

-- Getting the angle of a 2D vector
function vec2_angle(v) {
   return math.atan2(v.y, v.x)
}

--[[ NOTE: to get a random number, DO NOT use math.random(). Rather,
     use robot.random, described below. ]]