55 lines
1.3 KiB
C++
Raw Normal View History

2022-11-03 08:54:35 +01:00
#include "control_module.h"
2022-12-14 12:30:54 +01:00
ControlModule::ControlModule(){
}
ControlModule::ControlModule(float forwardSpeed, float rotateSpeed, float moveSideSpeed)
2022-11-03 08:54:35 +01:00
{
2022-12-14 12:30:54 +01:00
this->forwardSpeed = forwardSpeed;
this->rotateSpeed = rotateSpeed;
this->moveSideSpeed = moveSideSpeed;
2022-11-03 08:54:35 +01:00
}
ControlModule::~ControlModule()
{
2022-12-14 12:30:54 +01:00
}
void ControlModule::moveForward(){
2022-12-14 16:14:00 +01:00
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
motors[i] += forwardSpeed;
2022-12-14 12:30:54 +01:00
}
};
2022-12-14 16:14:00 +01:00
void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
float speed = moveSideSpeed * (contourColumsMiddle - imageColumsMiddle)/imageColumsMiddle;
motors[0] += speed;
motors[1] -= speed;
motors[2] -= speed;
motors[3] += speed;
}
2022-12-14 12:30:54 +01:00
2022-12-14 16:14:00 +01:00
void ControlModule::rotate(int angle){
float speed = rotateSpeed * (angle + 90)/90;
motors[0] += speed;
motors[1] -= speed;
motors[2] += speed;
motors[3] -= speed;
2022-12-14 12:30:54 +01:00
}
2022-12-14 16:14:00 +01:00
void ControlModule::unit(){
float max = 10E-12;
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
if(motors[i] > max)
max = motors[i];
}
for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
motors[i] /= max;
}
}
2022-12-14 12:30:54 +01:00
2022-12-14 16:14:00 +01:00
void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, int angle){
moveForward();
moveSide(imageColumsMiddle, contourColumsMiddle);
rotate(angle);
unit();
2022-11-03 08:54:35 +01:00
}