Refacto servoMotor
This commit is contained in:
parent
9a8a84a59e
commit
dea01d3e84
@ -1,50 +1,56 @@
|
|||||||
#include "ServoMotorComponent.h"
|
#include "ServoMotorComponent.h"
|
||||||
|
|
||||||
ServoMotorComponent::ServoMotorComponent(int PIN) {
|
ServoMotorComponent::ServoMotorComponent(int PIN, unsigned long updatePeriod) {
|
||||||
this->PIN = PIN;
|
this->PIN = PIN;
|
||||||
this->position = 30;
|
|
||||||
this->speed = 100;
|
|
||||||
this->easing = 0.5;
|
|
||||||
this->myservo.attach(PIN);
|
this->myservo.attach(PIN);
|
||||||
|
this->desiredposition = Position::MIDDLE;
|
||||||
|
this->currentPosition = 0;
|
||||||
|
this->lastUpTime = millis();
|
||||||
|
this->updatePeriod = updatePeriod;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServoMotorComponent::goLeft() {
|
int ServoMotorComponent::getCurrentPosition() {
|
||||||
this->myservo.write(this->PIN, 52, this->speed, this->easing);
|
return this->myservo.read(this->PIN);
|
||||||
this->position = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServoMotorComponent::goRight() {
|
void ServoMotorComponent::setCurrentPosition() {
|
||||||
this->myservo.write(this->PIN, 18, this->speed, this->easing);
|
this->currentPosition = this->myservo.read(this->PIN);
|
||||||
|
|
||||||
this->position = 180;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServoMotorComponent::goMiddle() {
|
void ServoMotorComponent::setDesiredPosition(Position desiredPosition) {
|
||||||
this->myservo.write(this->PIN, 32, this->speed, this->easing);
|
switch (desiredPosition) {
|
||||||
this->position = 90;
|
case Position::LEFT:
|
||||||
|
this->desiredposition = 52;
|
||||||
|
break;
|
||||||
|
case Position::MIDDLE:
|
||||||
|
this->desiredposition = 32;
|
||||||
|
break;
|
||||||
|
case Position::RIGHT:
|
||||||
|
this->desiredposition = 18;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ServoMotorComponent::getPosition() {
|
|
||||||
return this->position;
|
void ServoMotorComponent::refresh() {
|
||||||
|
if (this->desiredposition == this->currentPosition
|
||||||
|
|| millis() - this->lastUpTime <= this->updatePeriod) return;
|
||||||
|
|
||||||
|
if (this->currentPosition > this->desiredposition) {
|
||||||
|
this->currentPosition--;
|
||||||
}
|
}
|
||||||
|
if (this->currentPosition < this->desiredposition) {
|
||||||
|
this->currentPosition++;
|
||||||
|
}
|
||||||
|
this->lastUpTime = millis();
|
||||||
|
this->myservo.write(this->PIN, this->currentPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ServoMotorComponent::setPin(int PIN) {
|
void ServoMotorComponent::setPin(int PIN) {
|
||||||
this->PIN = PIN;
|
this->PIN = PIN;
|
||||||
this->myservo.attach(PIN);
|
this->myservo.attach(PIN);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServoMotorComponent::setSpeed(int speed) {
|
|
||||||
this->speed = speed;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServoMotorComponent::pause() {
|
|
||||||
this->myservo.pause();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServoMotorComponent::setEasing(float easing) {
|
|
||||||
this->easing = easing;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
@ -3,26 +3,30 @@
|
|||||||
|
|
||||||
#include <Servo.h>
|
#include <Servo.h>
|
||||||
|
|
||||||
|
enum Position {
|
||||||
|
LEFT,
|
||||||
|
MIDDLE,
|
||||||
|
RIGHT
|
||||||
|
};
|
||||||
|
|
||||||
class ServoMotorComponent
|
class ServoMotorComponent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ServoMotorComponent(int PIN);
|
ServoMotorComponent(int PIN, unsigned long updatePeriod);
|
||||||
~ServoMotorComponent() = default;
|
~ServoMotorComponent() = default;
|
||||||
void goLeft();
|
int getCurrentPosition();
|
||||||
void goRight();
|
void setCurrentPosition();
|
||||||
void goMiddle();
|
void setDesiredPosition(Position desiredPosition);
|
||||||
int getPosition();
|
|
||||||
void setPin(int PIN);
|
void setPin(int PIN);
|
||||||
void pause();
|
void refresh();
|
||||||
void setSpeed(int speed);
|
|
||||||
void setEasing(float easing);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int PIN;
|
int PIN;
|
||||||
int position;
|
int currentPosition;
|
||||||
int speed;
|
int desiredposition;
|
||||||
float easing;
|
|
||||||
Servo myservo;
|
Servo myservo;
|
||||||
|
unsigned long lastUpTime;
|
||||||
|
unsigned long updatePeriod;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user