beautypg.com

Pololu Maestro User Manual

Page 58

background image

Using the subroutine brings the script down to 31 bytes: 4 per position and 11 bytes of overhead for the loop and
to define FRAME. We can go further: inspecting the compiled code shows that putting each number on the stack
requires 3 bytes: one byte as a command, and two for the two-byte number. Numbers from 0 to 255 can be loaded
onto the stack with just two bytes. Suppose in our application we do not need the full 0.25 μs resolution of the device,
since all of our settings are multiples of 100. Then we can use smaller numbers to save another byte:

# Move servo 0 to five different positions, in a loop.

begin

40 frame

50 frame

60 frame

70 frame

80 frame

repeat

# loads a frame specified in 25 us units

sub frame

100 times

0 servo

500 delay

return

This program is 29 bytes long, with 3 bytes used per position and 14 bytes of overhead. Note that we could get the
same efficiency if we used the SERVO_8BIT command, which takes a one-byte argument from 0 to 254. We can go
even smaller by putting all of the numbers together:

# Move servo 0 to five different positions, in a loop.

begin

80 70 60 50 40

frame frame frame frame frame

repeat

# loads a frame specified in 25 us units

sub frame

100 times

0 servo

500 delay

return

If you step through this version program, you will also notice that all five numbers are placed on the stack in a single
step: this is because the compiler can use a single command to put multiple numbers on the stack. Using a single
command for multiple numbers saves space: we are now down to just 26 bytes. Only 12 bytes are used for the 5
frames, for an average of 2.4 bytes per frame. This is probably compact enough – by duplicating this structure we
could fit 420 different positions into the 1024-byte program memory of the Micro Maestro. However, the code can
get even smaller. Consider this script, which uses 31 frames to make a smooth back-and-forth motion:

# Moves servo in a sine wave between 1 and 2 ms.

begin

60 64 68 71 74 77 79 80 80 79 78 76 73 70 66 62

58 54 50 47 44 42 41 40 40 41 43 46 49 52 56

all_frames

repeat

sub all_frames

begin

depth

while

100 times

0 servo

100 delay

repeat

return

In this version of the code, we have rewritten the FRAME subroutine, using the DEPTH command to automatically
load frames from the stack until there are none left. This program uses 34 bytes to store 31 frames, for an average of

Pololu Maestro Servo Controller User's Guide

© 2001–2014 Pololu Corporation

6. The Maestro Scripting Language

Page 58 of 73