Programming Reference

The programming language used in Robot Simualtor is a simple version of BASIC ( Beginner's All-purpose Symbolic Instruction Code ) with a few exceptions to make it suitable for programming robots.

The following table shows the available commands and operators:

goto call return stop load >>> <<<
for to next step while continue break
if then else end true false and or not
== != > < >= <=      
( ) + - * / ^ %
& | << >> ! ~    
min max lerp abs int sqrt rand randx
sin cos tan asin acos atan2 atan  
++ -- = += -= *= /=  
time reset wait ticks   target    
beep print var # , . axis button joystick



beep

Sounds a tone through the computer's speaker.

Example

beep


reset

Sets run time to zero, each subsequent use of
"time" variable will return time measured since
last reset or program start.

Example

start

while time < 2
print time
next

reset
print "done"
goto start


>>>

Starts a block of lines that are executed at once
each step the program makes, to excute each of
these lines individually use debug mode ( Shift +
F5 ).

Excuting a block of lines at once is useful for
making several motors move at once, such as 
walking or coordinated move of some kind.

Program with blocks executes faster because each
block takes only one time step to complete, motors
and sensors are updated each time step and not
each line inside a block.

Example

>>>
for i = 1 to 100
print i
next
<<<


<<<

Ends a block of lines that are executed at once
each step the program makes, to excute each of
these lines individually use debug mode ( Shift +
F5 ).

Excuting a block of lines at once is useful for
making several motors move at once, such as
walking or coordinated move of some kind.

Program with blocks executes faster because each
block takes only one time step to complete, motors
and sensors are updated each time step and not
each line inside a block.

Example

>>>
for i = 1 to 100
print i
next
<<<


print

Prints a number of a variable or a string onto the
output buffer, only simple printing is allowed,
and when extressions are used they must be
surrouned with parenthesis.

Example

print (2+3*a)
print a
print "this is the start"


load

Loads and executes a program segment, the new
program completely replaces current program and
all variables are set to zero.

This command is useful for executing a few non
related program segments in sequence or some
logical order.

Example

for i = 1 to 10
if i > 5 then
load "new program.txt"
end
next


if

Starts an if block, if condition following the if
is true the program executes following the then
keyword, if not the program skips to the nearest
else or end command.

If blocks can be contained to a single line, but
it is recommended to use the full if then end block
for better program consitancy and clarity.

Example

a = 5
if a > 4 then
b = a
else
b = 4
end

for i=1 to 10
print i
if i>5 then print "bigger than 5"
else print "not bigger"
next

if i == 10 then goto start


then

Continues an if block, if condition following the if
is true the program executes following the then
keyword, if not the program skips to the nearest
else or end command.

If blocks can be contained to a single line, but
it is recommended to use the full if then end block
for better program consitancy and clarity.

Example

a = 5
if a > 4 then
b = a
else
b = 4
end

for i=1 to 10
print i
if i>5 then print "bigger than 5"
else print "not bigger"
next

if i == 10 then goto start


else

Continues an if block, if condition following the if
is true the program executes following the then
keyword, if not the program skips to the nearest
else or end command.

If blocks can be contained to a single line, but
it is recommended to use the full if then end block
for better program consitancy and clarity.

Example

a = 5
if a > 4 then
b = a
else
b = 4
end

for i=1 to 10
print i
if i>5 then print "bigger than 5"
else print "not bigger"
next

if i == 10 then goto start


end

Ends an if block, if condition following the if
is true the program executes following the then
keyword, if not the program skips to the nearest
else or end command.

If blocks can be contained to a single line, but
it is recommended to use the full if then end block
for better program consitancy and clarity.

Example

a = 5
if a > 4 then
b = a
else
b = 4
end

for i=1 to 10
print i
if i>5 then print "bigger than 5"
else print "not bigger"
next

if i == 10 then goto start


for

Starts a for next loop, any paramter can be used
inside, any step value can be used, default step
value is 1.

Example

for i = 1 to 10
print i
next

for t = 0 to 3.15 step 0.1
y = sin(y)
next


to

Starts a for next loop, any paramter can be used
inside, any step value can be used, default step
value is 1.

Example

for i = 1 to 10
print i
next

for t = 0 to 3.15 step 0.1
y = sin(y)
next


next

Ends a for next loop, any paramter can be used
inside, any step value can be used, default step
value is 1.

Example

for i = 1 to 10
print i
next

for t = 0 to 3.15 step 0.1
y = sin(y)
next



step

Starts a for next loop, any paramter can be used
inside, any step value can be used, default step
value is 1.

Example

for i = 1 to 10
print i
next

for t = 0 to 3.15 step 0.1
y = sin(y)
next


while

Starts a while next loop, checks a logical condition
and if true resumes execution, if not skips to next
command.

Example

x = 0
y = 0

while y < 100
x++
y=x^2
next


goto

This command instructs the program skip to the line
with label name matching the goto statement.

Label names are treated as variable names, the first
occurance of a name in the program is also a place
marker for which to jump.

Example

start_here
a++
if a < 10 then goto start_here


stop

This command stops program execution, and positions
the program excution point beyond the last line.

Example

for i = 1 to 100
if i = 55 then stop
next

wait

This command pauses execution for a specified amount
of time in seconds.

Example

motor1 = 10
wait 5.5
motor1 = 0

call

This command jumps to a block of lines starting
with label name specified by the call command and
resumes execution on the next line after a return
statement is met.

Example

// this is the main program

for i = 1 to 100
call do_something
next
stop

// this is a subroutine

do_something
x=i^3
print x
return


return

This command returns to next line after last call
statement was executed.

Any block of lines between label and return is
treated as a subroutine.

Example

// this is the main program

for i = 1 to 100
call do_something
next
stop

// this is a subroutine

do_something
x=i^3
print x
return


continue

This command skips to following next statement
skipping all commands in between.

Example

for i = 1 to 100
if i > 50 then continue
print i
next


break

This command skips beyond the following next statement
terminating the for or while loop which surrounds it.

Example

for i = 1 to 100
if (i^3 > 50) then break
print i
next


==

Logical equal operator, used to compare two numbers
and return 1 if equal (within premissible error)
or 0 if not.

Do not confuse this logical operator with the =
which is the assigmnet operator used to assign values.

Example

a = 7
b = 5
if a == b then print "strange indeed !"
else print "ok that figures.."

!=

Logical not equal operator, used to compare two numbers
and return 0 if equal (within premissible error)
or 1 if not.

Example

a = 7
b = 5
if a != b then print "ok that figures.."
else print "starnage indeed"


>

Logical greater than operator, used to compare two
numbers and return 1 if first is greater than the
other, and 0 if not.

Example

If 7 > 5 then print "great !"


<

Logical smaller than operator, used to compare two
numbers and return 1 if first is smaller than the
other, and 0 if not.

Example

if 5 < 7 then print "great !"

>=

Logical greater or equal operator, used to compare two
numbers and return 1 if first is greater or equals the
other, and 0 if not.

Example

if 7 >= 7 then print "great !"


<=

Logical smaller or equal operator, used to compare two
numbers and return 1 if first is smaller or equals the
other, and 0 if not.

Example

if 7 <= 7 then print "great !"


and

Logical and operator, used to compare two logical
expressions and return 1 if both are true, and 0 if not.

Example

a = 3
b = 4
c = 5

if (b>a) and (b<c) then print "ordered"
if a and b and c then print "all non zero"


or

Logical or operator, used to compare two logical
expressions and return 1 if at least one of them
are true, and 0 if not.

Example

a = 3
b = 4
c = 0

if (b>a) or (b<c) then print "one is true"
if a or b or c then print "one of them is non zero"


not

Logical not operator, used to negate a logical
expressions and return 1 if zero and 0 if nonzero.

Example

if not (3<2) then print "then 2 <= 3"
if not 0 then print "zero is false"
if not (2>3) and not (2>4) then print "great!"


(

Left parenthesis mark, used to enclose mathematical
or logical expressions, any number of parenthesis
can be used, or nested, and they allow better
evaluation and clarity in your expressions.

Example

a = (2+3)*4
b = ((2+3)*3 - 7)
if (a + b) > (c*d - 7) then goto start


)

Right parenthesis mark, used to enclose mathematical
or logical expressions, any number of parenthesis
can be used, or nested, and they allow better
evaluation and clarity in your expressions.

Example

a = (2+3)*4
b = ((2+3)*3 - 7)
if (a + b) > (c*d - 7) then goto start


,

Comma separator, used to seprarate between arguments
of a mathematic function.

Example

a = min(18, 24)
b = lerp(0.5, 12, 24)
if a == b then print "i knew that !"


+

Mathematical addition operator, used to add two
numbers together and return the sum.

Example

a = 2 + 3
if a == 5 then print "great !"


-

Mathematical substraction operator, used to substract
one number from the other and return the result.

Example

a = 8 - 3
if a == 5 then print "great !"


*

Mathematical multiplication operator, used to multiply
one number by the other and return the result.

Example

a = 8*3
if a == 24 then print "great !"


/

Mathematical division operator, used to divide
one number by the other and return the result.

Example

a = 24/3
if a == 8 then print "great !"


^

Mathematical exponent operator, used to raise
power of one number by the other and return the result.

Example

a = 2^3
if a == 8 then print "great !"


%

Mathematical modulus operator, returns the remainder
(modulus) obtained by dividing one numeric expression
into another, can be used from integers as well as
floating point numbers.


Example

a = 25 % 8
if a == 1 then print "great !"

b = 8 % 3.5
if b == 1 then print "great !"


.

Property operator, used to retrive the property
of an object or to attach or detach objects from it.
When used inside a number used as decimal point.
When used between two variables used as property
operator.

Available properties:

vx - returns the speed of the object along the x axis
vy - returns the speed of the object along the y axis
vz - returns the speed of the object along the z axis

rx - returns the rotation speed of the object around the x axis
ry - returns the rotation speed of the object around the y axis
rz - returns the rotation speed of the object around the z axis

rvx - returns the relative speed of the object along its x axis
rvy - returns the relative speed of the object along its y axis
rvz - returns the relative speed of the object along its z axis

rrx - returns the relative rotation speed of the object around its x axis
rry - returns the relative rotation speed of the object around its y axis
rrz - returns the relative rotation speed of the object around its z axis

alfa - returns the elevation angle of the object in radians.
beta - returns the yaw angle of the object in radians.
gama - returns the roll angle of the object in radians.

m11, m12, m13 - return the first line of the object matrix
m21, m22, m23 - return the second line of the object matrix
m31, m32, m33 - return the third line of the object matrix

attach - attaches a near object to the selected object.
detach - detached an attached object from the selected object.

For attachment to work the object must be declared as an
output of type magnet.

Example

dx = robot.x - 100
dy = robot.y - 100
dz = robot.z - 100

dist = sqrt(dx*dx + dy*dy + dz*dz)

magnet1.attach
if magnet1 then
print "we got something !'
magnet1.detach
end


&

Bitwise and operator, used to perform a bitwise
and operation between two numbers and return the
result.

Example

a = 0xff & 0x7f
if a == 0x7f then print "great!"


|

Bitwise or operator, used to perform a bitwise
or operation between two numbers and return the
result.

Example

a = 0xff | 0x7f
if a == 0xff then print "great!"


<<

Bitwise left shift operator, shifts one number
by specified number of bits to the left.

Example

a = 1 << 3
if a == 8 then print "great!"


>>

Bitwise right shift operator, shifts one number
by specified number of bits to the right.

Example

a = 8 >> 3
if a == 1 then print "great!"


!

Bitwise not operator, return 1 if number is zero
and 0 if number is nonzero.

Example

a = 0
if not a then print "great!"

b = 7
if not b then print "impossible!"


~

Bitwise two's complement operator, return the bitwise
complement of a number.

Example

a = 0xff
if (a & 0xf) then print "great!"

b = ~a
if (b & 0xf) then print "impossible!"


abs

Returns the absolute value of a number, number can
be integer or floating point.

Example

a = -3.5
if abs(a) == 3.5 then print "great!"


int

Returns the integer value of a floating point,
keeping the real number and dropping the decimal
fraction.

Example

a = int(1.5)
if (a == 1) then print "great!"


sqrt

Returns the square root of a number, if negative
the funtion returns the square root of the absolute
value of the number.

Example

a = sqrt(4)
if (a==2) print "great!"

b = sqrt(-4)
if (a==3) print "cheat!'


sin

Returns the sine of the number (angle in radians).

Example

a = sin(0.5235988)
if (a == 0.5) then print "this is 30 degress"


cos

Returns the cosine of the number (angle in radians).

Example

a = cos(1.0471976)
if (a == 0.5) then print "this is 60 degress"



tan

Returns the tangent of the number (angle in radians).

Example

a = tan(0.7853982)
if (a == 1) then print "this is 45 degress"


asin

Calculates the arcsine of a number in radians.

Example

y = asin( x )


acos

Calculates the arccosine of a number in radians.

Example

y = acos( x )


atan2

Calculates the arctangent of of y/x.

Example

alpha = atan2(dy, dx)


atan

Calculates the arctangent of of x ( k = dy/dx ).

Example

alpha = atan(k)


min

Calculates the minimal value of two numbers.

Example

a = min(5, 7)
if (a==5) then print "great!"


max

Calculates the maximal value of two numbers.

Example

a = max(5, 7)
if (a==7) then print "great!"


lerp

Calculates a value which is a linear interpolation
between two other values, with a factor given in
first value.

lerp(t, a1, a2) = t*a2 + (1-t)*a1

t must be between 0 and 1, a1 and a2 are the values
to interpolate btween.

Example

a1 = -10
a2 = 10

for t = 0 to 1 step 0.1
print lerp(t, a1, a2)
next


var

Variable operator, allows access to 1024 values in
a sequential order, making for array functionality,
and sequential control of inputs and outputs.

Basicly all variables, labels, inputs and outputs,
have a number between 0 and 1023 which can be accessed
using the var command.

Example

for i = 100 to 150
var(i) = i
next

for i = 100 to 150
print var(i)
next


#

Address operator, returning the numerical value
of a variable, label, input or output, so that
it can be accessed using the variable operator.

Example

a = 7
b = 8

if var(#a) == 7 then print "this is a"
if var(#a+1) == 8 then print "this is b"



++

Increment operator, adds 1 to the variable preceding
it, this operation has meaning upon variables only.

Example

i = 0
while i < 10
i++
print i
next


--

Decrement operator, subtracts 1 from the variable
preceding it, this operation has meaning if done
upon variables only.

Example

i = 10
while i > 0
i--
print i
next


=

Assigment operator, used to assign value to variables.
do not confuse with the logical equal operator.

Example

a = 7
b = 5
if a == b then print "strange indeed !"
else print "ok that figures.."


+=

Add and assign operator, adds a number to the value
and assigns it to the variable for which it was added.

Example

a = 0
a += 5
a += 5
if (a==10) then print "good!"


-=

Subtract and assign operator, subtracts a number from
the value and assigns it to the variable from which
it was subtracted.

Example

a = 10
a -= 5
a -= 5
if (a==0) then print "good!"


*=

Multiply and assign operator, multiply a number by
the value and assigns it to the variable by which
it was multiplied.

Example

a = 5
a *= 2
if (a==10) then print "good!"


/=

Divide and assign operator, divides a number by
the value and assigns it to the variable which
was divided.

Example

a = 10
a /= 2
if (a==5) then print "good!"


rand

Generates a random value between 0 and 1.

Example

a = 10*rand
if (a>5) then print "greater than average"


randx

Generates a random value between -1 and 1.

Example

a = 10*randx
if (a>0) then print "greater than average"


time

Returns time in seconds since the program started.

Example

start

while time < 2
print time
next

reset
print "done"
goto start


ticks

Returns time in miliseconds since system was started.

Example

end_tick = ticks + 1000
while ticks < end_tick
print ticks
next


true

Returns 1, used to express logical expressions
in a more natural way.

Example

a = true
if a then print "so true!"


false

Returns 0, used to express logical expressions
in a more natural way.

Example

a = false
if not a then print "not true!"


axis

Returns the value of joystick axis specified by
the paramter that follows.

Example

pitch1 = axis(1)
roll1 = axis(0)
grab1 = axis(3)
yaw1 = axis(2)


button

Returns the value of joystick button specified by
the paramter that follows.

Example

if button(0) then
magnet1.attach
end

if button(1) then
magnet1.detach
end


joystick

Returns 1, if joystick is enabled, 0 if not.

Example

if joystick then
car1 = axis(2)
else
car1 = 0
end


target

Returns 1 is tagret is found, 0 if not.
Accepts three input variables and returns
the relative distance and angle to the target.

Example

if target(#distance, #beta, #alpha) then
yaw1 = beta
last1 = true
end