Retour Clement
This commit is contained in:
@ -1,22 +1,29 @@
|
||||
#include "ServoMotorComponent.h"
|
||||
|
||||
ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod) {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
// Functions for setting up the ServoMotor
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Prepares the ServoMotor.
|
||||
*/
|
||||
ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod, float step) {
|
||||
this->PIN = PIN;
|
||||
this->myservo.attach(PIN);
|
||||
this->desiredposition = Position::MIDDLE;
|
||||
this->currentPosition = 0;
|
||||
this->currentPosition = 32;
|
||||
this->lastUpTime = millis();
|
||||
this->updatePeriod = updatePeriod;
|
||||
this->step = step;
|
||||
this->myservo.write(this->PIN, this->currentPosition);
|
||||
}
|
||||
|
||||
int ServoMotorComponent::getCurrentPosition() {
|
||||
return this->myservo.read(this->PIN);
|
||||
}
|
||||
|
||||
void ServoMotorComponent::setCurrentPosition() {
|
||||
this->currentPosition = this->myservo.read(this->PIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the desired position
|
||||
* @desiredPosition: Give desired position
|
||||
*/
|
||||
void ServoMotorComponent::setDesiredPosition(Position desiredPosition) {
|
||||
switch (desiredPosition) {
|
||||
case Position::LEFT:
|
||||
@ -34,6 +41,9 @@ void ServoMotorComponent::setDesiredPosition(Position desiredPosition) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write a new servoMotor position when it's necessary
|
||||
*/
|
||||
void ServoMotorComponent::refresh() {
|
||||
if (this->desiredposition == this->currentPosition
|
||||
|| millis() - this->lastUpTime <= this->updatePeriod) return;
|
||||
@ -46,11 +56,4 @@ void ServoMotorComponent::refresh() {
|
||||
}
|
||||
this->lastUpTime = millis();
|
||||
this->myservo.write(this->PIN, this->currentPosition);
|
||||
}
|
||||
|
||||
|
||||
void ServoMotorComponent::setPin(int PIN) {
|
||||
this->PIN = PIN;
|
||||
this->myservo.attach(PIN);
|
||||
return;
|
||||
}
|
@ -12,21 +12,18 @@ enum Position {
|
||||
class ServoMotorComponent
|
||||
{
|
||||
public:
|
||||
ServoMotorComponent(int PIN, unsigned long updatePeriod);
|
||||
~ServoMotorComponent() = default;
|
||||
int getCurrentPosition();
|
||||
void setCurrentPosition();
|
||||
ServoMotorComponent(int PIN, unsigned long updatePeriod, float step = 1);
|
||||
void setDesiredPosition(Position desiredPosition);
|
||||
void setPin(int PIN);
|
||||
void refresh();
|
||||
|
||||
private:
|
||||
int PIN;
|
||||
int currentPosition;
|
||||
int desiredposition;
|
||||
float currentPosition;
|
||||
float desiredposition;
|
||||
Servo myservo;
|
||||
unsigned long lastUpTime;
|
||||
unsigned long updatePeriod;
|
||||
float step;
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user