A simple library for driving RC servos without using the mbed's PWM functions. This allows the mbed to drive as many servos as there are DigitalOut pins, and additionally allows for the PWM functions to be used at a different frequency than the 50Hz used for servos.

Servo.cpp

Committer:
pclary
Date:
2012-10-03
Revision:
8:131785ed96fb
Parent:
7:ff85ac12e11b
Child:
9:6bfea9af4dcb

File content as of revision 8:131785ed96fb:

#include "Servo.h"
#include "mbed.h"



// The next line determines the maximum number of servo objects 
// that can be successfully created.
// By default, this is set to 26, as there are only 26 unique 
// DigitalOut pins provided by the mbed.
Servo *Servo::servos[26];
unsigned int Servo::numServos = 0;
Ticker* Servo::refreshTicker;



// The pin must be specified when the object is initialized, but 
// the initial pulse width can be omitted, giving a default of 
// 1500 us. 
// This should be at about half the range of most servos.
Servo::Servo(PinName pin, unsigned int width) : signalPin(pin)
{
    pulseWidth = width;
    
    if (numServos == 0)
    {
        refreshTicker = new Ticker();
        
        // Start the ticker that refreshes all servos
        refreshTicker->attach_us(&Servo::refresh, period);
    }
    
    servos[numServos++] = this;
}



void Servo::write(int width)
{
    // Make sure that the pulse width is less than the refresh period
    pulseWidth = width < period ? width : period;
}



int Servo::read()
{
    return pulseWidth;
}



void Servo::operator=(int width)
{
    write(width);
}



Servo::operator int()
{
    return read();
}



void Servo::refresh()
{
    // Start all of the individual servo width timeouts and write a logical 1 to their signal pins
    for (int i = 0; i < numServos; i++)
    {
        if (servos[i]->pulseWidth > 0)
        {
            servos[i]->servoTimeout.attach_us(servos[i], &Servo::timeout, servos[i]->pulseWidth);
            Servo::servos[i]->signalPin.write(1);
        }
    }
}



void Servo::timeout()
{
    // Write a logical zero to the servo's signal pin
    signalPin = 0;
}