Making smooth sequences with get_moving_state, Using an analog input to control servos – Pololu Maestro User Manual

Page 59

Advertising
background image

just 1.1 bytes per frame. We could store a sequence containing 900 different positions in the memory of the Micro
Maestro using this kind of script.

Making smooth sequences with GET_MOVING_STATE

Speed and acceleration settings can be used to make smooth motion sequences with the Maestro. However, a common
problem is that you do not know how much you need to delay between frames to allow the servo to reach its final
position. Here is an example of how to use the built-in function GET_MOVING_STATE to make a smooth sequence,
instead of DELAY:

# This example uses speed and acceleration to make a smooth

# motion back and forth between 1 and 2 ms.

3 0 acceleration

30 0 speed

begin

4000 0 servo # set servo 0 to 1.00 ms

moving_wait

8000 0 servo # 2.00 ms

moving_wait

repeat

sub moving_wait

begin

get_moving_state

while

# wait until it is no longer moving

repeat

return

GET_MOVING_STATE returns a 1 as long as there is at least one servo that is limited by a speed or acceleration
setting still moving, so you can use it whenever you want to wait for all motion to stop before proceeding to the next
step of a script.

Using an analog input to control servos

An important feature of the Maestro is that it can be used to read inputs from sensors, switches, or other devices. As a
simple example, suppose we want to use a potentiometer to control the position of a servo. For this example, connect
the potentiometer to form a voltage divider between 5V and 0, with the center tap connected to channel 1. Configure
channel 1 to be an input, and examine the signal on the Status tab of the Maestro Control Center. You should see the
position indicator vary from 0 to 255 μs as you turn the potentiometer from one side to the other. In your script, this
range corresponds to numbers from 0 to 1023. We can scale this number up to approximately the full range of a servo,
then set the servo position to this number, all in a loop:

# Sets servo 0 to a position based on an analog input.

begin

1 get_position # get the value of the pot, 0-1023

4 times 4000 plus # scale it to 4000-8092, approximately 1-2 ms

0 servo # set servo 0 based to the value

repeat

Alternatively, you might want the servo to go to discrete positions depending on the input value:

# Set the servo to 4000, 6000, or 8000 depending on an analog input.

begin

1 get_position # get the value of the pot, 0-1023

dup 300 less_than

if

4000 # go to 4000 for values 0-299

else

dup 600 less_than

if

6000 # go to 6000 for values 300-599

else

Pololu Maestro Servo Controller User's Guide

© 2001–2014 Pololu Corporation

6. The Maestro Scripting Language

Page 59 of 73

Advertising