float mistakes

This commit is contained in:
Tim Zeuner 2023-01-04 21:47:54 +01:00
parent f55ebb6b3f
commit 8150850bb6
2 changed files with 31 additions and 8 deletions

View File

@ -1,10 +1,15 @@
#include "control_module.h" #include "control_module.h"
ControlModule::ControlModule(){ ControlModule::ControlModule(): ControlModule(1.0, 1.0, 1.0)
{
} }
ControlModule::ControlModule(float forwardSpeed, float rotateSpeed, float moveSideSpeed) ControlModule::ControlModule(float forwardSpeed, float rotateSpeed, float moveSideSpeed)
{ {
motors[0] = 0.0;
motors[1] = 0.0;
motors[2] = 0.0;
motors[3] = 0.0;
this->forwardSpeed = forwardSpeed; this->forwardSpeed = forwardSpeed;
this->rotateSpeed = rotateSpeed; this->rotateSpeed = rotateSpeed;
this->moveSideSpeed = moveSideSpeed; this->moveSideSpeed = moveSideSpeed;
@ -15,13 +20,13 @@ ControlModule::~ControlModule()
} }
void ControlModule::moveForward(){ void ControlModule::moveForward(){
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){ for(int i = 0; i < 4; i++){
motors[i] += forwardSpeed; motors[i] += forwardSpeed;
} }
}; };
void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){ void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
float speed = moveSideSpeed * (contourColumsMiddle - imageColumsMiddle)/imageColumsMiddle; float speed = moveSideSpeed * static_cast<float>(contourColumsMiddle - imageColumsMiddle)/static_cast<float>(imageColumsMiddle);
motors[0] += speed; motors[0] += speed;
motors[1] -= speed; motors[1] -= speed;
motors[2] -= speed; motors[2] -= speed;
@ -29,7 +34,7 @@ void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
} }
void ControlModule::rotate(int angle){ void ControlModule::rotate(int angle){
float speed = rotateSpeed * (angle + 90)/90; float speed = rotateSpeed * (static_cast<float>(angle) + 90.0f)/90.0f;
motors[0] += speed; motors[0] += speed;
motors[1] -= speed; motors[1] -= speed;
motors[2] += speed; motors[2] += speed;
@ -37,19 +42,32 @@ void ControlModule::rotate(int angle){
} }
void ControlModule::unit(){ void ControlModule::unit(){
float max = 10E-12; float 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];
} }
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
motors[i] /= max; //Avoid dividing by zero
if (max > 0.001)
{
for(int i = 0; i < 4; i++){
motors[i] /= max;
}
} }
} }
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);
moveForward(); moveForward();
moveSide(imageColumsMiddle, contourColumsMiddle); moveSide(imageColumsMiddle, contourColumsMiddle);
rotate(angle); rotate(angle);
unit(); unit();
} }
std::vector<float> ControlModule::readMotors()
{
std::unique_lock<std::mutex> lock(mtx);
return std::vector<float> {motors[0], motors[1], motors[2], motors[3]};
}

View File

@ -1,8 +1,12 @@
#pragma once #pragma once
#include <mutex>
#include <vector>
#include <iostream>
class ControlModule class ControlModule
{ {
private: private:
mutable std::mutex mtx;
float motors[4]; //LeftFront; RightFront; LeftBack; RightBack float motors[4]; //LeftFront; RightFront; LeftBack; RightBack
float forwardSpeed; float forwardSpeed;
float rotateSpeed; float rotateSpeed;
@ -17,4 +21,5 @@ public:
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 void calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle); //Funktion to be called
std::vector<float> readMotors();
}; };