#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; | ||||
} | } | ||||
{ | { | ||||
} | } | ||||
void ControlModule::moveForward(){ | |||||
for(int i = 0; i < 4; i++){ | |||||
motors[i] += forwardSpeed; | |||||
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; | |||||
} | } | ||||
}; | }; | ||||
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() |
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 |
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; |
#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) |
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(); |
} | } | ||||
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; |
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; | ||||