# Servo
[Libraries]

The Servo Motor subsystem controls speed and position control for up to 4 standard servo motors. More...

## Functions
int | Servo_SetActive (int index, int state)
| Lock or unlock the I/O lines used by the servos.
int | Servo_GetActive (int index)
| Check whether the I/O lines used by the servos are locked.
int | Servo_SetPosition (int index, int position)
| Set the position of the specified servo motor.
int | Servo_SetSpeed (int index, int speed)
| Set the speed at which a servo will move in response to a call to Servo_SetPosition().
int | Servo_GetPosition (int index)
| Read the current position of a servo motor.
int | Servo_GetSpeed (int index)
| Get the speed at which a servo will move in response to a call to Servo_SetPosition().

## Detailed Description

The Servo Motor subsystem controls speed and position control for up to 4 standard servo motors.

Standard servos have a range of motion of approximately 180 degrees, although this varies from motor to motor. Be sure to plug in the connector with the correct orientation with regard to the GND/5V signals on the board.

Because not all servo motors are created equal, and not all of them can be safely driven across all 180 degrees, the default (safe) range of motion is from values 0 to 1023. The full range of motion is from -512 to 1536, but use this extra range cautiously if you don't know how your motor can handle it. A little gentle experimentation should do the trick.

You can also specify the speed with which the motors will respond to new position commands - a high value will result in an immediate response, while a lower value can offer some smoothing when appropriate.

See the servo section in the Application Board overview for more detailed info.

## Function Documentation

int Servo_GetActive | ( | int | index | ) |

Check whether the I/O lines used by the servos are locked.

Parameters:

  | index | An integer specifying which servo (0-3).

Returns:
  State - 1 (active) or 0 (inactive).

Example

      if( Servo_GetActive(2) )
      {
        // Servo 2 is active
      }
      else
      {
        // Servo 2 is inactive
      }

Definition at line 176 of file servo.c.

int Servo_GetPosition | ( | int | index | ) |

Read the current position of a servo motor.

Parameters:

  | index | An integer specifying which servo (0 - 3).

Returns:
  The position (0 - 1023), or 0 on error.

Example

      int srv0_pos = Servo_GetPosition(0);
      // now srv0_pos is the current position

Definition at line 271 of file servo.c.

int Servo_GetSpeed | ( | int | index | ) |

Get the speed at which a servo will move in response to a call to Servo_SetPosition().

Read the value previously set for the speed parameter.

Parameters:

  | index | An integer specifying which servo (0 - 3).

Returns:
  The speed (0 - 1023), or 0 on error.

Example

      int srv0_speed = Servo_GetSpeed(0);
      // now srv0_speed is the current speed

Definition at line 292 of file servo.c.

int Servo_SetActive | ( | int | index,
|  | int | state |
| ) |  |  |

Lock or unlock the I/O lines used by the servos.

Sets whether the specified Servo I/O is active.

Parameters:

  | index | An integer specifying which servo (0 - 3).
  | state | An integer specifying the active state - 1 (active) or 0 (inactive).

Returns:
  Zero on success.

Example

      // enable servo 2
      Servo_SetActive(2, 1);

Definition at line 128 of file servo.c.

int Servo_SetPosition | ( | int | index,
|  | int | position |
| ) |  |  |

Set the position of the specified servo motor.

Most servos like to be driven within a "safe range" which usually ends up being somewhere around 110-120 degrees range of motion, or thereabouts. Some servos don't mind being driven all the way to their 180 degree range of motion limit. With this in mind, the range of values you can send to servos connected to the Make Controller Kit is as follows:

- Values from 0-1023 correspond to the normal, or "safe", range of motion.
- You can also send values from -512 all the way up to 1536 to drive the servo through its full range of motion.

Note that it is sometimes possible to damage your servo by driving it too far, so proceed with a bit of caution when using the extended range until you know your servos can handle it.

Parameters:

  | index | An integer specifying which servo (0 - 3).
  | position | An integer specifying the servo position (0 - 1023).

Returns:
  status (0 = OK).

Example

      // set servo 1 to midway through the "safe" range
      Servo_SetPosition(1, 512);

Definition at line 207 of file servo.c.

int Servo_SetSpeed | ( | int | index,
|  | int | speed |
| ) |  |  |

Set the speed at which a servo will move in response to a call to Servo_SetPosition().

Higher values will result in a more immediate response, while lower values will more slowly step through all the intermediate values, achieving a smoother motion.

Parameters:

  | index | An integer specifying which servo (0 - 3).
  | speed | An integer specifying the servo speed (0 - 1023).

Returns:
  status (0 = OK).

Example

      // set servo 1 half speed
      Servo_SetSpeed(1, 512);

Definition at line 242 of file servo.c.