ready to test capture Route and Autopiloz

This commit is contained in:
2022-02-03 19:50:48 +01:00
parent 5ca21b09b2
commit bc37abcb66
20 changed files with 226 additions and 39 deletions
+7 -7
View File
@@ -4,14 +4,14 @@ ESP32 Rover Pinbelegung
x 23 DirR1 x 23 DirR1
x 22 PWMR x 22 PWMR
x TX PC x TX PC
x RX PC x RX PC Connector I2C
AKKU 35 21 SDA AKKU 35 21 SDA SDA
INTL1 32 GND INTL1 32 GND GND
INTL2 33 19 SCL INTL2 33 19 SCL SCL
INTB1 25 18 INTB1 25 18 VCC
INTB2 26 5 INTB2 26 5
DirL1 27 17 GPS TX DirL1 27 17 GPS RX
DirR2 14 16 GPS RX DirR2 14 16 GPS TX
DirL2 12 4 SD-Card DirL2 12 4 SD-Card
GND x GND x
PWML 13 2 SD-Card PWML 13 2 SD-Card
+13 -1
View File
@@ -11,13 +11,19 @@
#include "navigation.h" #include "navigation.h"
Navigation::Navigation(uint8_t rx, uint8_t tx) { Navigation::Navigation(uint8_t rx, uint8_t tx, Route* route) {
this->gps = new TinyGPSPlus; this->gps = new TinyGPSPlus;
Serial1.begin(9600, SERIAL_8N1, rx, tx); Serial1.begin(9600, SERIAL_8N1, rx, tx);
if (route)
this->route = route;
else
this->route = new Route();
} }
Navigation::~Navigation() { Navigation::~Navigation() {
delete this->gps; delete this->gps;
if (this->route)
delete this->route;
} }
void Navigation::loop() { void Navigation::loop() {
@@ -25,6 +31,12 @@ void Navigation::loop() {
gps->encode(Serial1.read()); gps->encode(Serial1.read());
} }
void Navigation::newRoute() {
if (this->route)
delete this->route;
this->route = new Route();
}
bool Navigation::startNavigation() { bool Navigation::startNavigation() {
Point newTargetPoint = this->route->startRoute(); Point newTargetPoint = this->route->startRoute();
this->navigationStarted = this->setTargetPoint(newTargetPoint); this->navigationStarted = this->setTargetPoint(newTargetPoint);
+3 -2
View File
@@ -26,7 +26,7 @@ struct CourseCorrection {
}; };
class Navigation { class Navigation {
public: public:
Navigation(uint8_t rx, uint8_t tx); Navigation(uint8_t rx, uint8_t tx, Route* route = nullptr);
~Navigation(); ~Navigation();
void loop(); void loop();
@@ -38,6 +38,7 @@ class Navigation {
bool addCurrentPosToRoute(); bool addCurrentPosToRoute();
TinyGPSPlus* getGPS() { return this->gps; } TinyGPSPlus* getGPS() { return this->gps; }
RouteInfo getRouteInfo() const { return this->route->getRouteInfo(); }
private: private:
Point currentLocation(); Point currentLocation();
@@ -47,7 +48,7 @@ class Navigation {
static double courseTo(Point p1, Point p2); static double courseTo(Point p1, Point p2);
TinyGPSPlus* gps; TinyGPSPlus* gps;
Route* route; Route* route = nullptr;
Point lastPoint; Point lastPoint;
Point targetPoint; Point targetPoint;
+9
View File
@@ -23,14 +23,23 @@ Point Route::startRoute() {
if (this->points.size() < 1) if (this->points.size() < 1)
return Point(0, 0); return Point(0, 0);
*this->it = this->points.begin(); *this->it = this->points.begin();
this->currentPoint = 1;
return **this->it; return **this->it;
} }
Point Route::getNextPoint() { Point Route::getNextPoint() {
if (*this->it != this->points.end()) { if (*this->it != this->points.end()) {
this->it++; this->it++;
this->currentPoint++;
return **this->it; return **this->it;
} }
else else
return Point(0, 0); return Point(0, 0);
}
RouteInfo Route::getRouteInfo() {
RouteInfo info;
info.totalPoints = this->points.size();
info.currentPoint = this->currentPoint;
return info;
} }
+7 -2
View File
@@ -31,6 +31,11 @@ class Point{
bool isValid() const { return this->lat + this->lon; } bool isValid() const { return this->lat + this->lon; }
}; };
struct RouteInfo{
uint16_t currentPoint;
uint16_t totalPoints;
};
class Route { class Route {
public: public:
Route(); Route();
@@ -38,10 +43,10 @@ class Route {
Point startRoute(); Point startRoute();
Point getNextPoint(); Point getNextPoint();
uint16_t getNumberOfPoints() { return this->count_points; } RouteInfo getRouteInfo();
private: private:
uint16_t count_points = 0; uint16_t currentPoint = 0;
std::list<Point> points; std::list<Point> points;
std::list<Point>::iterator* it; std::list<Point>::iterator* it;
@@ -1,24 +1,25 @@
/** /**
* @file menuCaptureRoute.cpp * @file menuAutopilot.cpp
* @author Alexander Klein (alex@kleiax.de) * @author Alexander Klein (alex@kleiax.de)
* @brief * @brief
* @version 0.1 * @version 0.1
* @date 2022-01-31 * @date 2022-02-03
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
* *
*/ */
#include "menuCaptureRoute.h"
void MenuCaptureRoute::printMenu() { #include "menuAutopilot.h"
this->driveManager->changeModus(Modi::CaptureRoute);
void MenuAutopilot::printMenu() {
this->driveManager->changeModus(Modi::Autopilot);
if (this->lcd) { if (this->lcd) {
this->lcd->clear(); this->lcd->clear();
this->lcd->setCursor(0, 0); this->lcd->setCursor(0, 0);
this->lcd->print("Jetzt kannst"); this->lcd->print("Autopilot");
this->lcd->setCursor(0, 1); this->lcd->setCursor(0, 1);
this->lcd->print("du fahren. :-)"); this->lcd->print("du fahren. :-)");
} else } else
std::cout << "Jetzt kannst du\n fahren. :-)" << std::endl; std::cout << "Autopilot du\n fahren. :-)" << std::endl;
} }
@@ -0,0 +1,23 @@
/**
* @file menuAutopilot.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2022-02-03
*
* @copyright Copyright (c) 2022
*
*/
#ifndef MENU_AUTOPILOT_DRIVE_H
#define MENU_AUTOPILOT_DRIVE_H
#include "SpecialMenus/menuDriveMode.h"
class MenuAutopilot : public MenuDriveMode {
public:
MenuAutopilot(DriveManager* driveManager) : MenuDriveMode(driveManager) {}
void printMenu();
};
#endif // MENU_AUTOPILOT_DRIVE_H
@@ -0,0 +1,46 @@
/**
* @file menuCaptureRoute.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2022-01-31
*
* @copyright Copyright (c) 2022
*
*/
#include "menuCaptureRoute.h"
MenuCaptureRoute::MenuCaptureRoute(DriveManager* driveManager)
: MenuDriveMode(driveManager) {
}
void MenuCaptureRoute::printMenu() {
if (this->firstPrint)
this->init();
if (this->lcd) {
this->lcd->clear();
this->lcd->setCursor(0, 0);
this->lcd->printf("Points: %u", this->routeInfo.totalPoints);
this->lcd->setCursor(0, 1);
this->lcd->print("du fahren. :-)");
} else
std::cout << "Capture du\n fahren. :-)" << std::endl;
}
void MenuCaptureRoute::update() {
if (millis() - this->last_millis < this->delay && !this->captureRoute->shouldUpdate())
return;
this->routeInfo = this->captureRoute->getRouteInfo();
this->printMenu();
this->last_millis = millis();
}
void MenuCaptureRoute::init() {
this->firstPrint = false;
this->driveManager->changeModus(Modi::CaptureRoute);
this->captureRoute = (CaptureRoute*) this->driveManager->getDriveModiPtr();
this->routeInfo = this->captureRoute->getRouteInfo();
}
@@ -11,12 +11,22 @@
#ifndef MENU_CAPTURE_ROUTE_H #ifndef MENU_CAPTURE_ROUTE_H
#define MENU_MANUAL_DRIVE_H #define MENU_MANUAL_DRIVE_H
#include "menuDriveMode.h" #include "SpecialMenus/menuDriveMode.h"
class MenuCaptureRoute : public MenuDriveMode { class MenuCaptureRoute : public MenuDriveMode {
public: public:
MenuCaptureRoute(DriveManager* driveManager) : MenuDriveMode(driveManager) {} MenuCaptureRoute(DriveManager* driveManager);
void printMenu(); void printMenu();
void update();
private:
void init();
CaptureRoute* captureRoute;
RouteInfo routeInfo;
uint32_t last_millis = 0;
const uint16_t delay = 5000;
}; };
#endif // MENU_MANUAL_DRIVE_H #endif // MENU_MANUAL_DRIVE_H
@@ -12,7 +12,7 @@
#ifndef MENU_MANUAL_DRIVE_H #ifndef MENU_MANUAL_DRIVE_H
#define MENU_MANUAL_DRIVE_H #define MENU_MANUAL_DRIVE_H
#include "menuDriveMode.h" #include "SpecialMenus/menuDriveMode.h"
class MenuManualControl : public MenuDriveMode { class MenuManualControl : public MenuDriveMode {
public: public:
+1
View File
@@ -16,6 +16,7 @@ MenuDriveMode::MenuDriveMode(DriveManager* driveManager) {
} }
void MenuDriveMode::left() { void MenuDriveMode::left() {
this->firstPrint = true;
this->driveManager->changeModus(Modi::Off); this->driveManager->changeModus(Modi::Off);
if (parentMenu) if (parentMenu)
this->parentMenu->printMenu(); this->parentMenu->printMenu();
+1
View File
@@ -30,6 +30,7 @@ class MenuDriveMode : public MenuControl {
protected: protected:
DriveManager* driveManager; DriveManager* driveManager;
bool firstPrint = true;
}; };
#endif // MENU_DRIVE_MODI_H #endif // MENU_DRIVE_MODI_H
+52 -1
View File
@@ -11,10 +11,61 @@
#include "driveModi/Modi/Autopilot/autopilot.h" #include "driveModi/Modi/Autopilot/autopilot.h"
Autopilot::Autopilot() { Autopilot::Autopilot(MoveControl* moveControl, Navigation* navigation)
: ManualControl(moveControl) {
this->navigation = navigation;
this->navigationStarted = this->navigation->startNavigation();
this->courseCorrection.correction = 0;
this->courseCorrection.distance = 0;
}
void Autopilot::loop() {
if (!this->selfDriving)
ManualControl::loop();
if (millis() - this->last_millis < delay) {
return;
}
this->runAutopilot();
this->last_millis = millis();
} }
void Autopilot::runAutopilot() { void Autopilot::runAutopilot() {
this->courseCorrection = this->navigation->getCourseCorrection();
if (selfDriving) {
this->setSpeedInRelToDistance(this->courseCorrection.distance);
this->setRotInRelToDistance(this->courseCorrection.correction);
}
}
void Autopilot::setSelfDriving(bool val) {
if (!this->navigationStarted)
return;
this->selfDriving = val;
this->moveControl->setSpeed(0);
this->moveControl->setRotationspeed(0);
}
void Autopilot::setSpeedInRelToDistance(double distance) {
// TODO: Delte Magic Numbers
if (distance > 2)
this->moveControl->setSpeed(1.5);
else if (distance < 0.5)
this->moveControl->setSpeed(1.5);
else
this->moveControl->setSpeed(0);
}
void Autopilot::setRotInRelToDistance(int16_t course) {
// TODO: Delte Magic Numbers
int8_t steps = course / 30;
if (steps == 0 && abs(course) >= 5)
steps = 1;
if (course == 0)
this->moveControl->setRotationspeed(0);
else
this->moveControl->setRotationspeed(steps);
} }
+14 -5
View File
@@ -15,20 +15,29 @@
#include "navigation.h" #include "navigation.h"
#include "moveControl.h" #include "moveControl.h"
#include "driveModi/driveModi.h" #include "driveModi/Modi/ManualControl/manualControl.h"
class Autopilot : DriveModi{ class Autopilot : public ManualControl {
public: public:
Autopilot(); Autopilot(MoveControl* moveControl, Navigation* navigation);
void loop(); void loop();
void runAutopilot(); void runAutopilot();
private: private:
MoveControl* moveControl; void setSelfDriving(bool val);
Navigation* navigation; void setSpeedInRelToDistance(double distance);
void setRotInRelToDistance(int16_t course);
Navigation* navigation;
CourseCorrection courseCorrection;
bool navigationStarted = false;
bool selfDriving = false;
uint16_t last_millis = 0;
uint8_t delay = 40;
}; };
#endif // AUTOPILOT_H #endif // AUTOPILOT_H
@@ -3,6 +3,7 @@
CaptureRoute::CaptureRoute(MoveControl* moveControl, Navigation* navigation) CaptureRoute::CaptureRoute(MoveControl* moveControl, Navigation* navigation)
: ManualControl(moveControl) { : ManualControl(moveControl) {
this->navigation = navigation; this->navigation = navigation;
this->routeInfo = RouteInfo{0, 0};
} }
void CaptureRoute::loop() { void CaptureRoute::loop() {
@@ -18,5 +19,15 @@ void CaptureRoute::loop() {
void CaptureRoute::runCaptureRoute() { void CaptureRoute::runCaptureRoute() {
if (Ps3.data.button.triangle) { if (Ps3.data.button.triangle) {
this->navigation->addCurrentPosToRoute(); this->navigation->addCurrentPosToRoute();
this->routeInfo.totalPoints++;
this->updateDisplay = true;
} }
}
bool CaptureRoute::shouldUpdate() {
if (this->updateDisplay) {
this->updateDisplay = false;
return true;
}
return false;
} }
@@ -15,14 +15,17 @@ class CaptureRoute : public ManualControl {
void runCaptureRoute(); void runCaptureRoute();
Navigation* getNavigation() const { return this->navigation; } Navigation* getNavigation() const { return this->navigation; }
RouteInfo getRouteInfo() const { return this->routeInfo; }
bool shouldUpdate();
private: private:
Navigation* navigation; Navigation* navigation;
RouteInfo routeInfo;
uint16_t savedPoints = 0;
uint32_t last_millis = 0; uint32_t last_millis = 0;
uint16_t delay = 200; uint16_t delay = 200;
bool updateDisplay = false;
}; };
#endif // CAPTURE_ROUTE_H #endif // CAPTURE_ROUTE_H
@@ -69,9 +69,10 @@ class ManualControl : public DriveModi{
*/ */
void setMaxRotation(double maxRotation) { max_rotation = maxRotation; } void setMaxRotation(double maxRotation) { max_rotation = maxRotation; }
private: protected:
MoveControl *moveControl; MoveControl *moveControl;
private:
uint32_t last_millis = 0; uint32_t last_millis = 0;
uint8_t delay = 10; uint8_t delay = 10;
double max_speed = 1; double max_speed = 1;
+3 -2
View File
@@ -60,8 +60,9 @@ void DriveManager::changeModus(Modi modus) {
} }
break; break;
case Modi::Autopilot: case Modi::Autopilot: {
this->currentModusPtr = nullptr; this->currentModusPtr = new Autopilot(this->moveControl, this->navigation);
}
break; break;
case Modi::ConsolControl: case Modi::ConsolControl:
+8 -6
View File
@@ -15,8 +15,9 @@
#include "menu.h" #include "menu.h"
#include "menuAction.h" #include "menuAction.h"
#include "SpecialMenus/menuPidSettings.h" #include "SpecialMenus/menuPidSettings.h"
#include "SpecialMenus/menuManualDrive.h" #include "SpecialMenus/ManualDrive/menuManualDrive.h"
#include "SpecialMenus/menuCaptureRoute.h" #include "SpecialMenus/CaptureRoute/menuCaptureRoute.h"
#include "SpecialMenus/Autopilot/menuAutopilot.h"
#include "SpecialMenus/menuGPS.h" #include "SpecialMenus/menuGPS.h"
MoveControl moveController; MoveControl moveController;
@@ -34,12 +35,11 @@ void controllerPrintBattery();
void i2cScanner(); void i2cScanner();
void restart(); void restart();
void exitDriveMode() {driveManager.changeModus(Modi::Off);}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
std::cout << "Welcome to Kleiax-Rover" << std::endl; std::cout << "Welcome to Kleiax-Rover" << std::endl;
std::cout << "All actions from the main programm run on" << xPortGetCoreID() << std::endl; std::cout << "All actions from the main programm run on Core -> " << xPortGetCoreID() << std::endl;
Wire.begin(21, 19); Wire.begin(21, 19);
// i2cScanner(); // i2cScanner();
@@ -67,6 +67,7 @@ void setup() {
MenuPidSettings* pidr_m = new MenuPidSettings(moveController.getPID(1)); MenuPidSettings* pidr_m = new MenuPidSettings(moveController.getPID(1));
MenuManualControl* man_m = new MenuManualControl(&driveManager); MenuManualControl* man_m = new MenuManualControl(&driveManager);
MenuCaptureRoute* cap_m = new MenuCaptureRoute(&driveManager); MenuCaptureRoute* cap_m = new MenuCaptureRoute(&driveManager);
MenuAutopilot* auto_m = new MenuAutopilot(&driveManager);
MenuGPS* gps_m = new MenuGPS(driveManager.getNavigation()->getGPS()); MenuGPS* gps_m = new MenuGPS(driveManager.getNavigation()->getGPS());
// Set Menus on LCD // Set Menus on LCD
@@ -77,6 +78,7 @@ void setup() {
pidr_m->setLcd(lcd); pidr_m->setLcd(lcd);
man_m->setLcd(lcd); man_m->setLcd(lcd);
cap_m->setLcd(lcd); cap_m->setLcd(lcd);
auto_m->setLcd(lcd);
gps_m->setLcd(lcd); gps_m->setLcd(lcd);
@@ -91,7 +93,7 @@ void setup() {
main_m->addEntry(restart_e); main_m->addEntry(restart_e);
// Entry for the mode Menu // Entry for the mode Menu
MenuAction* autopilot_e = new MenuAction("Autopilot", dummy); MenuAction* autopilot_e = new MenuAction("Autopilot", auto_m);
MenuAction* captureRoute_e = new MenuAction("Capture Route", cap_m); MenuAction* captureRoute_e = new MenuAction("Capture Route", cap_m);
MenuAction* consolControl_e = new MenuAction("Consol Control", dummy); MenuAction* consolControl_e = new MenuAction("Consol Control", dummy);
MenuAction* manualControl_e = new MenuAction("Manual Control", man_m); MenuAction* manualControl_e = new MenuAction("Manual Control", man_m);
@@ -153,7 +155,7 @@ void callbackControllerAction() {
void callbackControllerConnect() { void callbackControllerConnect() {
std::cout << "Controller connected to ESP32" << std::endl; std::cout << "Controller connected to ESP32" << std::endl;
std::cout << "All actions from the Controller run on" << xPortGetCoreID() << std::endl; std::cout << "All actions from the Controller run on Core -> " << xPortGetCoreID() << std::endl;
// TODO: Display is not functional without this seconde init, first init is in setup() // TODO: Display is not functional without this seconde init, first init is in setup()
lcd->init(); lcd->init();