Servo Motor Driver

High-level driver for servo motors. More...

Detailed Description

High-level driver for servo motors.

Files

file  servo.h
 High-level driver for easy handling of servo motors.
 

Data Structures

struct  servo_t
 Descriptor struct for a servo. More...
 

Functions

int servo_init (servo_t *dev, pwm_t pwm, int pwm_channel, unsigned int min, unsigned int max)
 Initialize a servo motor by assigning it a PWM device and channel. More...
 
void servo_set (const servo_t *dev, unsigned int pos)
 Set the servo motor to a specified position. More...
 

Function Documentation

◆ servo_init()

int servo_init ( servo_t dev,
pwm_t  pwm,
int  pwm_channel,
unsigned int  min,
unsigned int  max 
)

Initialize a servo motor by assigning it a PWM device and channel.

Digital servos are controlled by regular pulses sent to them. The width of a pulse determines the position of the servo. A pulse width of 1.5ms puts the servo in the center position, a pulse width of about 1.0ms and about 2.0ms put the servo to the maximum angles. These values can however differ slightly from servo to servo, so the min and max values are parameterized in the init function.

The servo is initialized with default PWM values:

  • frequency: 100Hz (10ms interval)
  • resolution: 10000 (1000 steps per ms)

These default values can be changed by setting SERVO_RESOLUTION and SERVO_FREQUENCY macros. Caution: When initializing a servo, the PWM device will be reconfigured to new frequency/resolution values. It is however fine to use multiple servos with the same PWM device, just on different channels.

Parameters
[out]devstruct describing the servo
[in]pwmthe PWM device the servo is connected to
[in]pwm_channelthe PWM channel the servo is connected to
[in]minminimum pulse width (in the resolution range)
[in]maxmaximum pulse width (in the resolution range)
Returns
0 on success
<0 on error

◆ servo_set()

void servo_set ( const servo_t dev,
unsigned int  pos 
)

Set the servo motor to a specified position.

The position of the servo is specified in the pulse width that controls the servo. With default configurations, a value of 1500 means a pulse width of 1.5 ms, which is the center position on most servos.

In case pos is larger/smaller then the max/min values, pos will be set to these values.

Parameters
[in]devthe servo to set
[in]posthe position to set the servo (in the resolution range)