refactor autopilot

This commit is contained in:
2023-05-25 19:59:16 +02:00
parent 1faab632d5
commit 98b65c4aec
3 changed files with 24 additions and 79 deletions
+17 -22
View File
@@ -72,8 +72,10 @@ void Autopilot::runAutopilot() {
} }
if (this->state == State::SelfDriving) { if (this->state == State::SelfDriving) {
this->setSpeedInRelToDistance(this->courseCorrection.distance); if (abs(this->courseCorrection.correction) >= this->maxCourseDeviationBerforeAct)
this->setRotInRelToDistance(this->courseCorrection.correction); this->rotate();
else
this->drive();
} }
} }
@@ -127,25 +129,18 @@ void Autopilot::checkNtrip() {
} }
} }
void Autopilot::setSpeedInRelToDistance(double distance) { void Autopilot::drive() {
if (distance >= MIN_DISTANCE_TO_NEXT_POINT) this->moveControl->setRotationSpeed(0);
this->moveControl->setSpeed(AUTOPILOT_MAX_SPEED); if (this->courseCorrection.distance >= this->minRemainingDistance)
else if (distance < MIN_DISTANCE_TO_NEXT_POINT) this->moveControl->setSpeed(this->drivingSpeed);
this->moveControl->setSpeed(AUTOPILOT_SPEED_2);
else {
this->moveControl->setSpeed(0);
std::cout << "Error in Autopilot::setSpeedInRelToDistance" << std::endl;
}
}
void Autopilot::setRotInRelToDistance(int16_t course) {
int8_t steps = course / COURSE_CORRECTION_FACTOR;
if (steps == 0 && abs(course) >= MIN_COURSE_CORRECTION_TO_ACT)
steps = 1;
if (course == 0)
this->moveControl->setRotationSpeed(0);
else else
this->moveControl->setRotationSpeed(steps); this,moveControl->setSpeed(0);
}
void Autopilot::rotate() {
this->moveControl->setSpeed(0);
if (this->courseCorrection.correction > 0)
this->moveControl->setRotationSpeed(this->rotationSpeed);
else
this->moveControl->setRotationSpeed(-this->rotationSpeed);
} }
+7 -3
View File
@@ -15,7 +15,6 @@
#include "navigation.h" #include "navigation.h"
#include "moveControl.h" #include "moveControl.h"
#include "driveModi/Modi/Autopilot/autopilotConfig.h"
#include "driveModi/Modi/ManualControl/manualControl.h" #include "driveModi/Modi/ManualControl/manualControl.h"
/** /**
@@ -110,8 +109,8 @@ class Autopilot : public ManualControl {
private: private:
void init(); void init();
void checkNtrip(); void checkNtrip();
void setSpeedInRelToDistance(double distance); void drive();
void setRotInRelToDistance(int16_t course); void rotate();
Navigation* navigation; Navigation* navigation;
CourseCorrection courseCorrection; CourseCorrection courseCorrection;
@@ -124,6 +123,7 @@ class Autopilot : public ManualControl {
uint8_t loopDelayMillis = 40; uint8_t loopDelayMillis = 40;
uint8_t ntripReconnectAttemps = 0; uint8_t ntripReconnectAttemps = 0;
uint8_t ntripReconnectMaxAttemps = 10; uint8_t ntripReconnectMaxAttemps = 10;
uint8_t maxCourseDeviationBerforeAct = 5;
uint16_t displayUpdateDelayMillis = 1000; uint16_t displayUpdateDelayMillis = 1000;
uint16_t reconnectNtripDelayMillis = 1000; uint16_t reconnectNtripDelayMillis = 1000;
uint16_t autopilotChangeDelayMillis = 500; uint16_t autopilotChangeDelayMillis = 500;
@@ -131,6 +131,10 @@ class Autopilot : public ManualControl {
uint32_t loopLastMillis = 0; uint32_t loopLastMillis = 0;
uint32_t reconnectNtripLastMillis = 0; uint32_t reconnectNtripLastMillis = 0;
uint32_t lastAutopilotChangeMillis = 0; uint32_t lastAutopilotChangeMillis = 0;
double drivingSpeed = 1;
double rotationSpeed = 7;
double minRemainingDistance = 0.25;
}; };
#endif // AUTOPILOT_H #endif // AUTOPILOT_H
@@ -1,54 +0,0 @@
/**
* @file autopilotConfig.h
* @author Alexander Klein (alex@kleiax.de)
* @brief Some defines to configure the behaviour of the autopilot
* @version 0.1
* @date 2022-03-02
*
* @copyright Copyright (c) 2022
*
*/
#ifndef AUTOPILOT_CONFIG_H
#define AUTOPILOT_CONFIG_H
/**
* @brief
*
*/
#define AUTOPILOT_MAX_SPEED 0.5
/**
* @brief Max speed in the near of target point
*/
#define AUTOPILOT_SPEED_2 0.25
/**
* @brief Distance in meters
*
* If this distance to the next point is reached the autopilot will drive
* now with the speed set by AUTOPILOT_SPEED_2
*/
#define MIN_DISTANCE_FOR_SPEED_2 4
/**
* @brief Needed distance to the next point to choose the next point as target.
*/
#define MIN_DISTANCE_TO_NEXT_POINT 0.5
/**
* @brief Minimal course correction
* If the needed correction in degree is lower than this value the rover will
* be drive straight on.
*/
#define MIN_COURSE_CORRECTION_TO_ACT 5
/**
* @brief Course correction factor
* This value is used by the setRotInRelToDistance function and is used to calculate
* the value for setRotationSpeed.
*/
#define COURSE_CORRECTION_FACTOR 60
#endif // AUTOPILOT_CONFIG_H