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.h

Committer:
pclary
Date:
2013-01-17
Revision:
15:55221f9de707
Parent:
14:0eff26aa0d17

File content as of revision 15:55221f9de707:

#ifndef SERVO_H
#define SERVO_H

#include "mbed.h"



class Servo
{
public:
    Servo(PinName pin, bool start = true);
    bool calibrate(unsigned int plus45, unsigned int minus45, float upperLimit, float lowerLimit);
    void write(float degrees);
    void writeWidth(unsigned int width);
    float read();
    int readWidth();
    void disable();
    void enable();
    void operator=(float degrees);
    operator float();
    operator int();
    
    unsigned int center;
    float usPerDegree;
    float upperLimit;
    float lowerLimit;
    
protected:
    static unsigned int numServos;
    static Servo *servos[];
    static const unsigned int period = 20000;
    static Ticker* refreshTicker;
    Timeout servoTimeout;
    unsigned int pulseWidth;
    DigitalOut signalPin;
    bool enabled;
    
    
    
    static void refresh();
    void timeout();
};

#endif // SERVO_H