A class to control a model R/C servo, using a PwmOut

Dependents:   Brute_TS_Controller_2018_11

Embed: (wiki syntax)

« Back to documentation index

Servo Class Reference

Servo Class Reference

Servo control class, based on a PwmOut. More...

#include <Servo.h>

Public Member Functions

 Servo (PinName pin)
 Create a servo object connected to the specified PwmOut pin.
void write (double percent)
 Set the servo position, normalised to it's full range.
double read ()
 Read the servo motors current position.
void position (double degrees)
 Set the servo position.
void calibrate (double range=0.0005, double degrees=45.0)
 Allows calibration of the range and angles for a particular servo.
Servooperator= (double percent)
 Shorthand for the write and read functions.

Detailed Description

Servo control class, based on a PwmOut.

Example:

 // Continuously sweep the servo through it's full range
 #include "mbed.h"
 #include "Servo.h"
 
 Servo myservo(p21);
 
 int main() {
     while(1) {
         for(int i=0; i<100; i++) {
             myservo = i/100.0;
             wait(0.01);
         }
         for(int i=100; i>0; i--) {
             myservo = i/100.0;
             wait(0.01);
         }
     }
 }

Definition at line 52 of file Servo.h.


Constructor & Destructor Documentation

Servo ( PinName  pin )

Create a servo object connected to the specified PwmOut pin.

Parameters:
pinPwmOut pin to connect to

Definition at line 37 of file Servo.cpp.


Member Function Documentation

void calibrate ( double  range = 0.0005,
double  degrees = 45.0 
)

Allows calibration of the range and angles for a particular servo.

Parameters:
rangePulsewidth range from center (1.5ms) to maximum/minimum position in seconds
degreesAngle from centre to maximum/minimum position in degrees

Definition at line 53 of file Servo.cpp.

Servo & operator= ( double  percent )

Shorthand for the write and read functions.

Definition at line 62 of file Servo.cpp.

void position ( double  degrees )

Set the servo position.

Parameters:
degreesServo position in degrees

Definition at line 48 of file Servo.cpp.

double read (  )

Read the servo motors current position.

Parameters:
returnsA normalised number 0.0-1.0 representing the full range.

Definition at line 58 of file Servo.cpp.

void write ( double  percent )

Set the servo position, normalised to it's full range.

Parameters:
percentA normalised number 0.0-1.0 to represent the full range.

Definition at line 42 of file Servo.cpp.