114 lines
2.5 KiB
C++
Raw Normal View History

2022-11-03 08:54:35 +01:00
#include "control_module.h"
2023-01-31 12:45:08 +01:00
ControlModule::ControlModule(): ControlModule(0.0, 1.0, 1.0)
2023-01-04 21:47:54 +01:00
{
2022-12-14 12:30:54 +01:00
}
2023-01-31 21:05:04 +01:00
ControlModule::ControlModule(double maxSpeed, double rotateSpeed, double moveSideSpeed)
2022-11-03 08:54:35 +01:00
{
2023-01-04 21:47:54 +01:00
motors[0] = 0.0;
motors[1] = 0.0;
motors[2] = 0.0;
motors[3] = 0.0;
2023-01-31 12:45:08 +01:00
this->maxSpeed = maxSpeed;
2022-12-14 12:30:54 +01:00
this->rotateSpeed = rotateSpeed;
this->moveSideSpeed = moveSideSpeed;
2022-11-03 08:54:35 +01:00
}
ControlModule::~ControlModule()
{
2022-12-14 12:30:54 +01:00
}
2023-01-31 12:45:08 +01:00
void ControlModule::adjustSpeed(){
double factor = 0.0;
if(maxSpeed > 1.0)
{
factor = 1.0;
}
else if(maxSpeed < -1.0)
{
factor = -1.0;
}
else
{
factor = maxSpeed;
}
for(int i = 0; i < 4; i++)
{
motors[i] *= maxSpeed;
2022-12-14 12:30:54 +01:00
}
};
2022-12-14 16:14:00 +01:00
void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
2023-01-31 21:05:04 +01:00
double speed = moveSideSpeed * static_cast<double>(contourColumsMiddle - imageColumsMiddle)/static_cast<double>(imageColumsMiddle);
2022-12-14 16:14:00 +01:00
motors[0] += speed;
motors[1] -= speed;
motors[2] -= speed;
motors[3] += speed;
}
2022-12-14 12:30:54 +01:00
2023-02-01 17:26:35 +01:00
void ControlModule::drive(double ratio)
{
motors[0] += ratio;
motors[1] += ratio;
motors[2] += ratio;
motors[3] += ratio;
}
2023-01-31 22:03:25 +01:00
2023-02-01 17:26:35 +01:00
void ControlModule::rotate(double ratio)
{
motors[0] += ratio;
motors[1] -= ratio;
motors[2] += ratio;
motors[3] -= ratio;
2022-12-14 12:30:54 +01:00
}
2022-12-14 16:14:00 +01:00
void ControlModule::unit(){
2023-01-31 21:05:04 +01:00
double max = 1.0E-6;
2022-12-14 16:14:00 +01:00
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
if(motors[i] > max)
max = motors[i];
}
2023-01-04 21:47:54 +01:00
//Avoid dividing by zero
if (max > 0.001)
{
for(int i = 0; i < 4; i++){
motors[i] /= max;
}
2022-12-14 16:14:00 +01:00
}
2023-01-04 21:47:54 +01:00
2022-12-14 16:14:00 +01:00
}
2022-12-14 12:30:54 +01:00
2023-01-31 21:05:04 +01:00
void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, double angle){
2023-01-04 21:47:54 +01:00
std::unique_lock<std::mutex> lock(mtx);
2023-02-01 17:26:35 +01:00
double rotationRatio = ratio(angle);
rotate(rotationRatio);
drive(1.0-rotationRatio);
2022-12-14 16:14:00 +01:00
unit();
2023-01-31 12:45:08 +01:00
adjustSpeed();
2023-01-04 21:47:54 +01:00
}
2023-01-31 21:05:04 +01:00
std::vector<double> ControlModule::readMotors()
2023-01-04 21:47:54 +01:00
{
std::unique_lock<std::mutex> lock(mtx);
2023-01-31 21:05:04 +01:00
return std::vector<double> {motors[0], motors[1], motors[2], motors[3]};
2023-02-01 17:26:35 +01:00
}
double ControlModule::ratio(double angle)
{
double minAngularSpeed = -1.0;
double maxAngularSpeed = 1.0;
double speedRange = maxAngularSpeed - minAngularSpeed;
double minAngle = -90.0;
double maxAngle = 90.0;
double angularRange = maxAngle - minAngle;
double progress = angle - minAngle;
double progressPercent = progress/angularRange;
double speed = minAngularSpeed + progressPercent*speedRange;
return abs(speed);
}