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-11-03
Revision:
12:92dc63d24bf8
Parent:
10:f0df63e99a96
Child:
14:0eff26aa0d17

File content as of revision 12:92dc63d24bf8:

#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) : signalPin(pin)
{
    // Set default calibration
    calibrate(2000, 1000, 60.f, -60.f);
    
    pulseWidth = center;
    
    if (numServos == 0)
    {
        refreshTicker = new Ticker();
        
        // Start the ticker that refreshes all servos
        refreshTicker->attach_us(&Servo::refresh, period);
    }
    
    servos[numServos++] = this;
}



bool Servo::calibrate(unsigned int plus60, unsigned int minus60, float upperLimit, float lowerLimit)
{
    // Check if given parameters are valid
    if (upperLimit > lowerLimit)
    {
        center = (plus60 + minus60) / 2;
        usPerDegree = ((int)plus60 - (int)center) / 60.0f;
        this->upperLimit = upperLimit;
        this->lowerLimit = lowerLimit;   
        return true;
    }
    else
    {
        return false;
    }
}



void Servo::write(float degrees)
{
    // Limit to the valid angle range
    degrees = (degrees > upperLimit ? upperLimit : (degrees < lowerLimit ? lowerLimit : degrees));
    
    pulseWidth = center + (int)(degrees * usPerDegree);
}



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



float Servo::read()
{
    return ((int)pulseWidth - (int)center) / usPerDegree;
}



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



void Servo::operator=(float degrees)
{
    write(degrees);
}



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



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



void Servo::refresh()
{
    // Start all of the individual servo width timeouts and write a logical 1 to their signal pins
    for (unsigned 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;
}