Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LSM9DS1 RangeFinder FastPWM
Dependents: PM2_Example_PES_board PM2_Example_PES_board PM2_Example_PES_board PM2_Example_PES_board ... more
Servo.cpp
- Committer:
- pmic
- Date:
- 2022-03-21
- Revision:
- 16:69911e81dfd4
- Parent:
- 10:fe74e8909d3f
- Child:
- 18:21c0a669d6ef
File content as of revision 16:69911e81dfd4:
#include "Servo.h"
Servo::Servo(PinName Pin) : ServoPin(Pin) {}
/**
* Sets the desired angle.
* @_Input a value between 0...1.
*/
void Servo::SetPosition(float _Input)
{
Position = static_cast<int>(_Input * static_cast<float>(Period));
}
void Servo::StartPulse()
{
ServoPin = 1;
PulseStop.attach(callback(this, &Servo::EndPulse), std::chrono::microseconds{static_cast<long int>(Position)});
}
void Servo::EndPulse()
{
ServoPin = 0;
}
/**
* Enables the servo with start angle and period.
* @_StartInput a value between 0...1.
* @_Period period in mus.
*/
void Servo::Enable(float _StartInput, int _Period)
{
Period = _Period;
Position = static_cast<int>(_StartInput * static_cast<float>(Period));
Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)});
}
/**
* Disables the servo.
*/
void Servo::Disable()
{
Pulse.detach();
}
