Subroutines, B. command reference – Pololu Maestro User Manual

Page 52

Advertising
background image

goto mylabel

# ...any code here is skipped...

4000 1 servo

mylabel: # the program continues here

4000 2 servo

In this example, only servo 2 will get set to 4000, while servo 1 will be unchanged.

Subroutines

It can be useful to use the same sequence of commands many times throughout your program. Subroutines are used
to make this easier and more space-efficient. For example, suppose you often need to set servo 1 and servo 2 back to
their neutral positions of 6000 (1.5 ms) using the sequence “6000 1 servo 6000 2 servo”. You can encapsulate this in
a subroutine as follows:

sub neutral

6000 1 servo

6000 2 servo

return

Then, whenever you want send these two servos back to neutral, you can just use “neutral” as a command. More
advanced subroutines take values off of the stack. For example, the subroutine

sub set_servos

2 servo 1 servo

return

will set channel 2 to the value on the top of the stack and channel 1 to the next value. So, if you write “4000 6000
set_servos”, your script will set channel 1 to 4000 and channel 2 to 6000.

Subroutines can call other subroutines, up to a limit of 10 levels of recursion. For example, the subroutine “neutral”
above can be implemented by calling set_servos:

sub neutral

6000 6000 set_servos

return

6.b. Command Reference

The following is the entire list of commands and keywords in the Maestro script language. The “stack effect” column
specifies how many numbers are consumed and added to the stack. For example, the PLUS command takes off two
numbers and returns one; so it has a stack effect of -2,+1. Commands with special effects will be marked with a *.

Pololu Maestro Servo Controller User's Guide

© 2001–2014 Pololu Corporation

6. The Maestro Scripting Language

Page 52 of 73

Advertising