Baran Yasar 1 year ago
parent
commit
ea33e0b065

+ 8
- 8
AutonomousMode/ControlModule/control_module.cpp View File

{ {
} }


ControlModule::ControlModule(float maxSpeed, float rotateSpeed, float moveSideSpeed)
ControlModule::ControlModule(double maxSpeed, double rotateSpeed, double moveSideSpeed)
{ {
motors[0] = 0.0; motors[0] = 0.0;
motors[1] = 0.0; motors[1] = 0.0;
}; };


void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){ void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
float speed = moveSideSpeed * static_cast<float>(contourColumsMiddle - imageColumsMiddle)/static_cast<float>(imageColumsMiddle);
double speed = moveSideSpeed * static_cast<double>(contourColumsMiddle - imageColumsMiddle)/static_cast<double>(imageColumsMiddle);
motors[0] += speed; motors[0] += speed;
motors[1] -= speed; motors[1] -= speed;
motors[2] -= speed; motors[2] -= speed;
motors[3] += speed; motors[3] += speed;
} }


void ControlModule::rotate(int angle){
float speed = rotateSpeed * (static_cast<float>(angle) + 90.0f)/90.0f;
void ControlModule::rotate(double angle){
double speed = rotateSpeed * (angle + 90.0)/90.0;
motors[0] += speed; motors[0] += speed;
motors[1] -= speed; motors[1] -= speed;
motors[2] += speed; motors[2] += speed;
} }


void ControlModule::unit(){ void ControlModule::unit(){
float max = 1.0E-6;
double max = 1.0E-6;
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){ for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
if(motors[i] > max) if(motors[i] > max)
max = motors[i]; max = motors[i];


} }


void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle){
void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, double angle){
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);
//moveSide(imageColumsMiddle, contourColumsMiddle); //moveSide(imageColumsMiddle, contourColumsMiddle);
rotate(angle); rotate(angle);
adjustSpeed(); adjustSpeed();
} }


std::vector<float> ControlModule::readMotors()
std::vector<double> ControlModule::readMotors()
{ {
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);
return std::vector<float> {motors[0], motors[1], motors[2], motors[3]};
return std::vector<double> {motors[0], motors[1], motors[2], motors[3]};
} }

+ 8
- 8
AutonomousMode/ControlModule/control_module.h View File

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


void calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle); //Funktion to be called
std::vector<float> readMotors();
void calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, double angle); //Funktion to be called
std::vector<double> readMotors();
}; };

+ 1
- 1
AutonomousMode/Utils/CMakeLists.txt View File

find_package( OpenCV REQUIRED ) find_package( OpenCV REQUIRED )


add_library(Utils SHARED utils.cpp)
add_library(Utils utils.cpp)
set_target_properties(Utils PROPERTIES VERSION ${PROJECT_VERSION}) set_target_properties(Utils PROPERTIES VERSION ${PROJECT_VERSION})


target_include_directories(Utils PRIVATE ${OpenCV_INCLUDE_DIRS}) target_include_directories(Utils PRIVATE ${OpenCV_INCLUDE_DIRS})

+ 17
- 3
AutonomousMode/Utils/utils.cpp View File

#include "utils.h" #include "utils.h"


#define PI 3.14159265359

LFRPoint::LFRPoint(/* args */) : x(0.0), y(0.0) LFRPoint::LFRPoint(/* args */) : x(0.0), y(0.0)
{ {
} }
{ {
} }


double LFRVector::angle(const LFRVector& other) const
{
return acos(dot(other)/(this->norm()*other.norm())) * (180.0/M_PI);
}

double LFRVector::dot(const LFRVector& other) const
{
return x*other.x + y*other.y;
}

double LFRVector::norm() const
{
return sqrt(x*x+y*y);
}


LFRLine::LFRLine(/* args */) : start(), dir() LFRLine::LFRLine(/* args */) : start(), dir()
{ {
} }
int refAngle = 0; int refAngle = 0;


if(deltaX > 10E-12){ if(deltaX > 10E-12){
refAngle = (int)((atan(deltaY/deltaX) * 180.0/PI) + 0.5 - (refAngle<0)); //Here 0.5 (or -0.5) is added to round a float number to int right
refAngle = (int)((atan(deltaY/deltaX) * 180.0/M_PI) + 0.5 - (refAngle<0)); //Here 0.5 (or -0.5) is added to round a float number to int right


// convert from img coordinates to regbot coordinates // convert from img coordinates to regbot coordinates
refAngle = -(refAngle); refAngle = -(refAngle);

+ 8
- 1
AutonomousMode/Utils/utils.h View File

#pragma once #pragma once


#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <cmath>
#define _USE_MATH_DEFINES
#include <math.h>


using namespace cv; using namespace cv;
using namespace std; using namespace std;
LFRVector(const LFRPoint& pt); LFRVector(const LFRPoint& pt);
~LFRVector(); ~LFRVector();


double angle(const LFRVector& other) const;
double dot(const LFRVector& other) const;
double norm() const;

}; };


class LFRLine class LFRLine
std::vector<cv::Rect> boundingBoxes; std::vector<cv::Rect> boundingBoxes;
std::vector<cv::Point> leftEdges; std::vector<cv::Point> leftEdges;
std::vector<cv::Point> middlePoints; std::vector<cv::Point> middlePoints;
int angle; //Angle of the contour the robot has to follow to
double angle; //Angle of the contour the robot has to follow to
int index; //Index of the contour the robot has to follow to int index; //Index of the contour the robot has to follow to


FrameData(): contours(), boundingBoxes(), leftEdges(), middlePoints() {} FrameData(): contours(), boundingBoxes(), leftEdges(), middlePoints() {}

+ 1
- 1
AutonomousMode/lfr.h View File

cv::Mat rawImage; cv::Mat rawImage;
cv::Mat processedImage; cv::Mat processedImage;
FrameData data; FrameData data;
std::vector<float> motorSignals;
std::vector<double> motorSignals;
}; };


class LFR class LFR

Loading…
Cancel
Save