Introduce maxSpeed

This commit is contained in:
Tim Zeuner 2023-01-31 12:45:08 +01:00
parent 59ce30a828
commit 450bb76d7d
7 changed files with 33 additions and 16 deletions

View File

@ -1,16 +1,16 @@
#include "control_module.h" #include "control_module.h"
ControlModule::ControlModule(): ControlModule(1.0, 1.0, 1.0) ControlModule::ControlModule(): ControlModule(0.0, 1.0, 1.0)
{ {
} }
ControlModule::ControlModule(float forwardSpeed, float rotateSpeed, float moveSideSpeed) ControlModule::ControlModule(float maxSpeed, float rotateSpeed, float moveSideSpeed)
{ {
motors[0] = 0.0; motors[0] = 0.0;
motors[1] = 0.0; motors[1] = 0.0;
motors[2] = 0.0; motors[2] = 0.0;
motors[3] = 0.0; motors[3] = 0.0;
this->forwardSpeed = forwardSpeed; this->maxSpeed = maxSpeed;
this->rotateSpeed = rotateSpeed; this->rotateSpeed = rotateSpeed;
this->moveSideSpeed = moveSideSpeed; this->moveSideSpeed = moveSideSpeed;
} }
@ -19,9 +19,24 @@ ControlModule::~ControlModule()
{ {
} }
void ControlModule::moveForward(){ void ControlModule::adjustSpeed(){
for(int i = 0; i < 4; i++){ double factor = 0.0;
motors[i] += forwardSpeed; 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;
} }
}; };
@ -60,10 +75,10 @@ void ControlModule::unit(){
void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle){ void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle){
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);
moveForward(); //moveSide(imageColumsMiddle, contourColumsMiddle);
moveSide(imageColumsMiddle, contourColumsMiddle);
rotate(angle); rotate(angle);
unit(); unit();
adjustSpeed();
} }
std::vector<float> ControlModule::readMotors() std::vector<float> ControlModule::readMotors()

View File

@ -8,14 +8,14 @@ class ControlModule
private: private:
mutable std::mutex mtx; mutable std::mutex mtx;
float motors[4]; //LeftFront; RightFront; LeftBack; RightBack float motors[4]; //LeftFront; RightFront; LeftBack; RightBack
float forwardSpeed; float maxSpeed;
float rotateSpeed; float rotateSpeed;
float moveSideSpeed; float moveSideSpeed;
public: public:
ControlModule(/* args */); ControlModule(/* args */);
ControlModule(float forwardSpeed, float rotateSpeed, float moveSideSpeed); ControlModule(float maxSpeed, float rotateSpeed, float moveSideSpeed);
~ControlModule(); ~ControlModule();
void moveForward(); void adjustSpeed();
void moveSide(int imageColumsMiddle, int contourColumsMiddle); void moveSide(int imageColumsMiddle, int contourColumsMiddle);
void rotate(int angle); void rotate(int angle);
void unit(); //Brings the max Value to 1.0 void unit(); //Brings the max Value to 1.0

View File

@ -10,10 +10,11 @@ int main(void)
const int videoHeight = 720; const int videoHeight = 720;
const int videoWidth = 1280; const int videoWidth = 1280;
const int gaussKernelSize = 11; const int gaussKernelSize = 11;
const double maxSpeed = 0.5;
std::mutex mutex; std::mutex mutex;
LFR lfr(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, [&](std::exception const &ex) LFR lfr(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, maxSpeed, [&](std::exception const &ex)
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
std::cerr<<"camera exception:"<<ex.what()<<std::endl; std::cerr<<"camera exception:"<<ex.what()<<std::endl;

View File

@ -3,11 +3,11 @@
#define right false #define right false
#define left true #define left true
LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, ExceptionCallback cb): LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, double maxSpeed, ExceptionCallback cb):
stop(false), stop(false),
input(videoHeight, videoWidth), input(videoHeight, videoWidth),
processing(), processing(),
controlModule(), controlModule(maxSpeed, 1.0, 1.0),
interpreter(), interpreter(),
intersectionHandler(), intersectionHandler(),
cb(cb) cb(cb)

View File

@ -57,7 +57,7 @@ private:
public: public:
LFR() = delete; LFR() = delete;
LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, ExceptionCallback cb); LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, double maxSpeed, ExceptionCallback cb);
~LFR(); ~LFR();
void startLoop(); void startLoop();

View File

@ -73,7 +73,7 @@ void LFR_StateMachine::parseString(string s)
} }
LFR_StateMachine::LFR_StateMachine(): LFR_StateMachine::LFR_StateMachine():
autonomousMode(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, [&](std::exception const &ex) autonomousMode(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, maxSpeed [&](std::exception const &ex)
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
std::cerr<<"camera exception:"<<ex.what()<<std::endl; std::cerr<<"camera exception:"<<ex.what()<<std::endl;

View File

@ -16,6 +16,7 @@ class LFR_StateMachine
const int videoHeight = 720; const int videoHeight = 720;
const int videoWidth = 1280; const int videoWidth = 1280;
const int gaussKernelSize = 11; const int gaussKernelSize = 11;
const double maxSpeed = 0.5;
std::mutex mutex; std::mutex mutex;