Moved ControlledMotor to branch

This commit is contained in:
Pascal Serrarens 2023-12-05 11:18:38 +01:00
parent f4b0eb1743
commit 1b36f97e18
3 changed files with 7 additions and 106 deletions

View File

@ -1,44 +0,0 @@
#include "ControlledMotor.h"
#include <Arduino.h>
ControlledMotor::ControlledMotor() {
this->type = Thing::ControlledMotorType;
}
ControlledMotor::ControlledMotor(Motor* motor, Encoder* encoder)
: ControlledMotor() {
this->motor = motor;
this->encoder = encoder;
}
void ControlledMotor::SetTargetSpeed(float velocity) {
this->targetVelocity = velocity;
this->rotationDirection =
(targetVelocity < 0) ? Direction::Reverse : Direction::Forward;
}
void ControlledMotor::Update(float currentTimeMs) {
actualVelocity =
(int)rotationDirection * encoder->GetRevolutionsPerSecond(currentTimeMs);
float error = targetVelocity - velocity;
float timeStep = currentTimeMs - lastUpdateTime;
float acceleration =
error * timeStep * pidP; // Just P is used at this moment
motor->SetSpeed(targetVelocity + acceleration); // or something like that
this->lastUpdateTime = currentTimeMs;
}
float ControlledMotor::GetActualSpeed() {
return actualVelocity;
}
bool ControlledMotor::Drive(float distance) {
if (!driving) {
targetDistance = distance;
startDistance = encoder->GetDistance();
driving = true;
}
float totalDistance = encoder->GetDistance() - startDistance;
bool completed = totalDistance > targetDistance;
return completed;
}

View File

@ -1,59 +0,0 @@
#pragma once
#include "Encoder.h"
#include "Motor.h"
namespace Passer {
namespace RoboidControl {
/// @brief A motor with speed control
/// It uses a feedback loop from an encoder to regulate the speed
/// The speed is measured in revolutions per second.
class ControlledMotor : public Thing {
public:
ControlledMotor();
ControlledMotor(Motor* motor, Encoder* encoder);
inline static bool CheckType(Thing* thing) {
return (thing->type & (int)Thing::Type::ControlledMotor) != 0;
}
float velocity;
float pidP = 1;
float pidD = 0;
float pidI = 0;
void Update(float currentTimeMs);
/// @brief Set the target speed for the motor controller
/// @param speed the target in revolutions per second.
void SetTargetSpeed(float speed);
/// @brief Get the actual speed from the encoder
/// @return The speed in revolutions per second
float GetActualSpeed();
bool Drive(float distance);
Motor* motor;
Encoder* encoder;
protected:
float lastUpdateTime;
float targetVelocity;
float actualVelocity;
float netDistance = 0;
float startDistance = 0;
enum Direction { Forward = 1, Reverse = -1 };
Direction rotationDirection;
bool driving = false;
float targetDistance = 0;
float lastEncoderPosition = 0;
};
} // namespace RoboidControl
} // namespace Passer
using namespace Passer::RoboidControl;

View File

@ -1,9 +1,13 @@
#include "Motor.h"
#include <time.h>
Motor::Motor() { type = (int)Thing::UncontrolledMotorType; }
Motor::Motor() {
type = (int)Thing::UncontrolledMotorType;
}
float Motor::GetSpeed() { return this->currentTargetSpeed; }
float Motor::GetSpeed() {
return this->currentTargetSpeed;
}
void Motor::SetSpeed(float targetSpeed) {
this->currentTargetSpeed = targetSpeed;
@ -21,6 +25,6 @@ bool Motor::Drive(float distance) {
return true;
}
SetSpeed(distance < 0 ? -1 : 1); // max speed
SetSpeed(distance < 0 ? -1 : 1); // max speed
return false;
}