refactore
all components now have a base class compnent for loop functions
This commit is contained in:
2023-08-18 10:47:35 +02:00
parent 25d37e3970
commit 11d21e2e89
35 changed files with 303 additions and 511 deletions
+14 -7
View File
@@ -16,28 +16,27 @@ Battery::Battery(uint8_t pin, uint32_t r1, uint32_t r2) {
this->r1 = r1;
this->r2 = r2;
this->initBuffer();
this->loopDelay = 100;
}
Battery::Battery(uint8_t pin) {
this->pin = pin;
this->initBuffer();
this->loopDelay = 100;
}
bool Battery::loop() {
if (millis() - this->lastMillis < this->delay)
return false;
void Battery::run() {
this->readAdcToBuf();
this->loopCounter++;
this->lastMillis = millis();
if (this->loopCounter == this->calulationDelayMultiplier) {
this->calculateBatteryVoltage();
this->calculateBatteryPercent();
return true;
this->loopCounter = 0;
this->calculatetNewValues = true;
}
return false;
return;
}
double Battery::getBatteryVoltage() {
@@ -51,6 +50,14 @@ bool Battery::isBatteryLow(double voltage) {
return false;
}
bool Battery::isNewValue() {
if (!this->calculatetNewValues)
return false;
this->calculatetNewValues = false;
return true;
}
double Battery::calculateInputVoltage() {
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
double reading = this->getBufAvg();
+8 -25
View File
@@ -17,6 +17,8 @@
#include <math.h>
#include <Arduino.h>
#include "component.h"
/**
* @brief A class for battery monitoring
*
@@ -25,7 +27,7 @@
* to be after a voltage diveder, so that maximum voltage for the
* microcontroller is 3.3 Volt.
*/
class Battery {
class Battery : public Component {
public:
/**
* @brief Construct a new Battery object
@@ -41,27 +43,6 @@ class Battery {
Battery(uint8_t pin, uint32_t r1, uint32_t r2);
Battery(uint8_t pin);
/**
* @brief Read new values if the delay is reached.
*
* @return bool true if new values are calculated
*/
bool loop();
/**
* @brief Set the delay to wait befor new values are read
*
* @param delay in milliseconds
*/
void setDelay(uint16_t delay) {this->delay = delay; }
/**
* @brief Get the delay to wait befor new values are read
*
* @return uint16_t delay in milliseconds
*/
uint16_t getDelay() {return this->delay; }
/**
* @brief Get the battery voltage
*
@@ -74,7 +55,7 @@ class Battery {
*
* @return uint8_t charge level in percent
*/
uint8_t getBatteryPercent() {return this->batteryPercent;}
uint8_t getBatteryPercent() { return this->batteryPercent; }
/**
* @brief Checks if the battery is low.
@@ -89,7 +70,10 @@ class Battery {
*/
bool isBatteryLow(double voltage);
bool isNewValue();
private:
void run() override;
double calculateInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
@@ -104,13 +88,12 @@ class Battery {
uint8_t batteryPercent = 0;
uint8_t batteryLowPercent = 10;
uint8_t bufferPos = 0;
uint8_t delay = 100;
uint8_t calulationDelayMultiplier = 10;
uint8_t loopCounter = 0;
uint16_t adcBuffer[bufferSize];
uint32_t r1 = 0;
uint32_t r2 = 0;
uint64_t lastMillis = 0;
bool calculatetNewValues = false;
double batteryVoltage = 0;
const float capacityVoltages[21] = {9.82, 10.83, 11.06, 11.12, // 0 5 10 15
+40
View File
@@ -0,0 +1,40 @@
#include "component.h"
Component::Component(uint16_t loopDelay) {
this->loopDelay = loopDelay;
}
void Component::loop() {
if (!this->active)
return;
if (this->childComponents.size()){
std::list<Component*>::iterator it;
for (it = this->childComponents.begin(); it != this->childComponents.end(); it++)
(*it)->loop();
}
this->runAsChild();
if (this->onlyChilds)
return;
if (this->loopDelay && millis() - this->lastMillis < this->loopDelay)
return;
this->lastMillis = millis();
this->beforeRun();
this->run();
this->afterRun();
if (this->timeUpdateAfter)
this->lastMillis = millis();
}
void Component::addChildComponent(Component* child) {
this->childComponents.push_back(child);
}
void Component::removeChildComponent(Component* child) {
this->childComponents.remove(child);
}
+51
View File
@@ -0,0 +1,51 @@
/**
* @file component.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2023-08-16
*
* @copyright Copyright (c) 2023
*
*/
#pragma once
#include <Arduino.h>
#include <list>
class Component {
public:
Component() {}
Component(uint16_t loopDelay);
void loop();
void deactivate() { this->active = false; }
void activate() { this->active = false; }
protected:
virtual void runAsChild() {}
virtual void beforeRun() {}
virtual void run() = 0;
virtual void afterRun() {}
void addChildComponent(Component* child);
void removeChildComponent(Component* child);
void activateOnlyChilds() { this->onlyChilds = true; }
void deactivateOnlyChilds() { this->onlyChilds = false; }
void setTimerAfterTask() { this->timeUpdateAfter = true; }
uint16_t loopDelay = 0;
private:
std::list<Component*> childComponents;
bool active = true;
bool onlyChilds = false;
bool timeUpdateAfter = false;
uint32_t lastMillis = 0;
};
+6 -10
View File
@@ -12,23 +12,19 @@
#include "controlPad.h"
ControlPad::ControlPad() {
this->loopDelay = 5;
}
bool ControlPad::loop() {
if (millis() - this->lastLoopMillis < this->loopDelay)
return false;
this->lastLoopMillis = millis();
void ControlPad::run() {
if(!this->connected)
return false;
return;
if (millis() - this->lastMessageReceive > this->disconnectTime) {
this->connected = false;
this->controlInput.buttons = 0;
this->controlInput.x = 127;
this->controlInput.y = 127;
return false;
return;
}
if (this->lastButtons != controlInput.buttons)
@@ -39,7 +35,7 @@ bool ControlPad::loop() {
this->menuControl->printMenu();
this->firstButtonPress = false;
std::cout << "ControlPad::loop - First menu print" << std::endl;
return true;
return;
}
if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Left))
@@ -58,7 +54,7 @@ bool ControlPad::loop() {
this->updated = false;
this->lastButtons = controlInput.buttons;
}
return true;
return;
}
void ControlPad::insertData(const uint8_t *data) {
+5 -7
View File
@@ -13,14 +13,13 @@
#include <menuControl.h>
#include "controlPadInput.h"
#include <controlPadInput.h>
#include <component.h>
class ControlPad {
class ControlPad : public Component {
public:
ControlPad();
bool loop();
void insertData(const uint8_t *data);
void setMenuControl(MenuControl* menuControl) { this->menuControl = menuControl; }
@@ -30,6 +29,8 @@ class ControlPad {
bool isControlPadConnected() const { return this->connected; }
private:
void run() override;
MenuControl* menuControl = nullptr;
ControlPadInput controlInput;
@@ -42,9 +43,6 @@ class ControlPad {
uint8_t lastButtons = 0;
uint16_t disconnectTime = 100;
uint16_t loopDelay = 5;
uint32_t lastMessageReceive = 0;
uint32_t lastLoopMillis = 0;
};
+1 -1
View File
@@ -17,7 +17,7 @@ LcdWrapper::LcdWrapper(LiquidCrystal_I2C* lcd) {
this->changed = false;
}
void LcdWrapper::loop() {
void LcdWrapper::run() {
if (!this->changed)
return;
+4 -7
View File
@@ -15,6 +15,7 @@
#include <iostream>
#include <LiquidCrystal_I2C.h>
#include <displayWrapper.h>
#include <component.h>
#define DISPLAY_WRAPPER_ROWS 16
#define DISPLAY_WRAPPER_LINES 2
@@ -28,7 +29,7 @@ typedef void (*LcdWrapperCallback) (const char data[][DISPLAY_WRAPPER_ROWS], uin
* The class takes the information to print from any thread. The
* loop function has to be called to print the data.
*/
class LcdWrapper : public DisplayWrapper {
class LcdWrapper : public DisplayWrapper, public Component {
public:
/**
* @brief Construct a new Lcd Wrapper object
@@ -37,12 +38,6 @@ class LcdWrapper : public DisplayWrapper {
*/
LcdWrapper(LiquidCrystal_I2C* lcd);
/**
* @brief Print the saved data
*
*/
void loop();
/**
* @brief Empty the buffer
*
@@ -67,6 +62,8 @@ class LcdWrapper : public DisplayWrapper {
private:
void run() override;
LiquidCrystal_I2C* lcd;
LcdWrapperCallback callback = nullptr;
char data[DISPLAY_WRAPPER_LINES][DISPLAY_WRAPPER_ROWS];
+3 -20
View File
@@ -15,6 +15,7 @@
MotorControl::MotorControl() {
this->setMinPwm(PWMMIN);
this->setMaxPwm(PWMMAX);
this->loopDelay = DELAY;
}
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2) {
@@ -34,20 +35,7 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
ledcWrite(this->pwmChannel, 0);
}
uint16_t MotorControl::loop() {
uint32_t time = millis();
uint16_t elapsed_time = time - this->lastMillis;
//Cancel if delayLoop is not reached
if (elapsed_time < delayLoop)
return elapsed_time;
runMotorControl();
this->lastMillis = time;
return elapsed_time;
}
void MotorControl::runMotorControl() {
void MotorControl::run() {
// Absolute difference between targetPower and power
uint8_t abs_difference = abs(this->targetPower - this->power);
@@ -108,7 +96,7 @@ void MotorControl::setMaxPwm(uint8_t max) {
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
this->powersteps = increment;
return (uint16_t) (delayLoop * ( 100 / powersteps ));
return (uint16_t) (this->loopDelay * ( 100 / powersteps ));
}
void MotorControl::setTargetPower(int8_t power) {
@@ -118,11 +106,6 @@ void MotorControl::setTargetPower(int8_t power) {
std::cout << " MotorControl::setTargetPower: Invalid Argument - Power: " << power << std::endl;
}
uint16_t MotorControl::setDelay(uint8_t delayLoop) {
this->delayLoop = delayLoop;
return (uint16_t) (delayLoop * ( 100 / powersteps ));
}
void MotorControl::stop() {
this->targetPower = 0;
}
+4 -37
View File
@@ -16,6 +16,8 @@
#include <iostream>
#include <Arduino.h>
#include <component.h>
#define DELAY 10
#define PWMFREQ 16000
#define PWMRES 8
@@ -28,7 +30,7 @@
* You can control the acceleration of the motor, for example to
* prevent a damage on your H-Bridge.
*/
class MotorControl {
class MotorControl : public Component {
public:
MotorControl();
@@ -42,26 +44,6 @@ class MotorControl {
*/
void init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2);
/**
* @brief Calls runMotorControl() to update the pwm signal
*
* This function should be called every mainloop. If the delayLoop is not reached, than the
* functions returns immediately.
* @see runMotorControl()
* @see DELAY
* @return time since the last call in Milliseconds
*/
uint16_t loop();
/**
* @brief Normally called repeatedly by loop() to update the pwm signal.
*
* Checks the difference between target power and current power to
* increase or decrease the duty cycle. The amount of decrease or increase
* is set by setPowerSetps() (default = 2).
*/
void runMotorControl();
/**
* @brief Set the minimum duty cycle
*
@@ -104,19 +86,6 @@ class MotorControl {
*/
void setTargetPower(int8_t power);
/**
* @brief Set the min delayLoop between each loop
*
* Note the dependency between delayLoop and
* setPowerSteps().
*
* @see setPowerSteps()
*
* @param delayLoop time in Milliseconds
* @return time from 0% power to 100% power in Milliseconds
*/
uint16_t setDelay(uint8_t delayLoop);
/**
* @brief Stops the motor like setTargetPower() to 0
*
@@ -149,6 +118,7 @@ class MotorControl {
bool isAccelerationNegative();
private:
void run() override;
void setRealPower(int8_t power);
void increasePower(int8_t power);
@@ -164,10 +134,7 @@ class MotorControl {
uint8_t dutycycleMax;
uint8_t dir_1;
uint8_t dir_2;
uint8_t delayLoop = DELAY;
uint8_t powersteps = POWERSTEPS;
uint32_t lastMillis = 0;
};
+13 -20
View File
@@ -39,11 +39,6 @@ void Navigation::init(Route* route) {
uint8_t versionLow = this->gps->getProtocolVersionLow();
std::cout << "u-blox protocol version: " << unsigned(versionHigh) << "." << unsigned(versionLow) << std::endl;
// std::cout << "Set GNSS Module to factory settings... ";
// this->gps->factoryReset();
// delay(5000);
// std::cout << "Complete" << std::endl;
this->gps->setSPIOutput(COM_TYPE_UBX);
this->gps->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10);
this->gps->setUSBOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
@@ -67,14 +62,16 @@ void Navigation::init(Route* route) {
CalibrateCompass caliCompass(this->compass);
caliCompass.loadData();
caliCompass.useData();
// std::cout << "Navigation::init compass correction data: " << caliCompass << std::endl;
this->loopDelay =AZIMUTH_UPDATE_DELAY;
}
Navigation::~Navigation() {
delete this->gps;
delete this->compass;
delete this->ntripClient;
delete this->route;
if (this->ntripClient)
delete this->ntripClient;
}
void Navigation::initNtrip(String host, uint16_t port, String mountPoint, String user, String password) {
@@ -83,9 +80,17 @@ void Navigation::initNtrip(String host, uint16_t port, String mountPoint, String
this->ntripClient->loop();
this->ntripClient->setActivated(false);
this->isNtripInit = true;
this->addChildComponent(this->ntripClient);
}
void Navigation::loop() {
void Navigation::run() {
this->compass->read();
this->realAzimuth = this->compass->getAzimuth();
this->updateMagneticDeclination();
}
void Navigation::runAsChild() {
this->gps->checkUblox();
this->gps->checkCallbacks();
@@ -93,18 +98,6 @@ void Navigation::loop() {
this->updateCurrentLocation();
Navigation::newData = false;
}
if (this->ntripClient)
this->ntripClient->loop();
if (millis() - this->lastMillis > AZIMUTH_UPDATE_DELAY) {
this->compass->read();
this->realAzimuth = this->compass->getAzimuth();
this->updateMagneticDeclination();
this->lastMillis = millis();
}
}
void Navigation::newRoute() {
+4 -8
View File
@@ -21,6 +21,7 @@
#include "route.h"
#include "NTRIPClient.h"
#include "calibrateCompass.h"
#include "component.h"
/**
* @brief The minimal distance between Points
@@ -54,7 +55,7 @@ struct CourseCorrection {
* be drive and the distance to the next checkpoint.
*
*/
class Navigation {
class Navigation : public Component {
public:
enum Status {
InsufficientAccuracy,
@@ -103,13 +104,6 @@ class Navigation {
*/
void initNtrip(String host, uint16_t port, String mountPoint, String user, String password);
/**
* @brief Decodes new GPS information
*
* Should be called ever main loop.
*/
void loop();
/**
* @brief creates a new empty route
*
@@ -218,6 +212,8 @@ class Navigation {
static int16_t fixDegree(int16_t degree);
private:
void run() override;
void runAsChild() override;
void updateCurrentLocation();
void updateMagneticDeclination();
void init(Route* route);
+7 -7
View File
@@ -22,19 +22,15 @@ NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS* gps, const char* host, uint16_t port, c
this->ntripClient = new WiFiClient;
this->state = NTRIPClientStates::closeConnection;
this->loopDelay = 20;
}
NTRIPClient::~NTRIPClient() {
delete this->ntripClient;
}
void NTRIPClient::loop() {
this->pushGPGGA();
if (millis() - this->lastLoopTime < this->delayTime)
return;
this->lastLoopTime = millis();
void NTRIPClient::run() {
switch (this->state) {
case NTRIPClientStates::openConnection:
if (!this->activated) {
@@ -81,6 +77,10 @@ void NTRIPClient::loop() {
}
}
void NTRIPClient::runAsChild() {
this->pushGPGGA();
}
void NTRIPClient::gpsConfiguration() {
this->gps->setSPIOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
this->gps->setPortInput(COM_PORT_SPI, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3);
+4 -11
View File
@@ -19,6 +19,7 @@
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>
#include "debugTimes.h"
#include "component.h"
/**
* @brief States for the state machine.
@@ -39,7 +40,7 @@ enum NTRIPClientStates {
* data and push it to a given gnss module. This module have to be compatible
* with the SparkFun u-blox GNSS Arduino Library.
*/
class NTRIPClient {
class NTRIPClient : public Component {
public:
/**
* @brief Construct a new NTRIPClient object
@@ -54,14 +55,6 @@ class NTRIPClient {
NTRIPClient(SFE_UBLOX_GNSS* gps, const char* host, uint16_t port, const char* mountPoint, const char* user, const char* password);
~NTRIPClient();
/**
* @brief Runs the state machine.
*
* See NTRIPClientStates for more information.
* Should be called ever main loop.
*/
void loop();
/**
* @brief Configure the Gnss module to accept correction data
*
@@ -100,6 +93,8 @@ class NTRIPClient {
NTRIPClientStates getClientState() { return this->state; }
private:
void run() override;
void runAsChild() override;
void pushGPGGA();
bool beginClient();
void closeConnection();
@@ -119,14 +114,12 @@ class NTRIPClient {
uint32_t lastReceivedRtcmTime = 0;
// uint32_t lastNtripConnectTime = 0; // can deleted?
uint32_t lastGPGGAPushTime = 0;
uint32_t lastLoopTime = 0;
uint32_t lastReconnectTime = 0;
char host[128];
char mountPoint[128];
char user[128];
char password[128];
const uint8_t delayTime = 20;
const uint8_t maxReconnectAttemps = 10;
const uint16_t reconnectDelayTime = 1000;
const uint16_t timeOut = 10000;
+3 -15
View File
@@ -14,6 +14,7 @@
Speedometer::Speedometer(uint8_t pin, double diameter, uint16_t steps, uint8_t numOfValForAvg) {
this->init(pin, diameter, steps);
this->bufSize = numOfValForAvg;
this->loopDelay = DELAY_SPEEDOMETER;
}
Speedometer::Speedometer(uint8_t pin, double diameter, uint16_t steps) {
@@ -25,24 +26,11 @@ Speedometer::~Speedometer() {
delete this->pulseCounter;
}
uint16_t Speedometer::loop() {
void Speedometer::run() {
if (this->calibrationRunning)
return -1;
return;
uint32_t time = millis();
uint16_t elapsedTime = time - this->lastMillisLoop;
//Cancel if delayLoop is not reached
if (elapsedTime < this->delayLoop)
return elapsedTime;
runSpeedometer();
this->lastMillisLoop = time;
return elapsedTime;
}
void Speedometer::runSpeedometer() {
uint32_t time = millis();
uint16_t elapsedTime = time - this->lastMillisCalc;
this->lastMillisCalc = time;
+3 -28
View File
@@ -17,6 +17,7 @@
#include <iostream>
#include "counter.h"
#include "component.h"
/**
* @brief The default size of numbers to be taken in account for the average.
@@ -38,7 +39,7 @@
* The calculated speed is the average of an amount of last measurements.
*
*/
class Speedometer {
class Speedometer : public Component {
public:
/**
* @brief Enum to control the direction.
@@ -66,24 +67,6 @@ class Speedometer {
~Speedometer();
/**
* @brief Calls runSpeedometer() to update all Values.
*
* This function should be called every mainloop. If the delayLoop is not reached, than the
* functions returns immediately.
* @see runSpeedometer()
* @see DELAY_SPEEDOMETER
* @return time since the last call in Milliseconds
*/
uint16_t loop();
/**
* @brief Normally called repeatedly by loop() to calculate new values.
*
* Add a new Value to the average and update the speed.
*/
void runSpeedometer();
/**
* @brief Set the direction
*
@@ -107,13 +90,6 @@ class Speedometer {
*/
void setEncFilter(uint16_t val);
/**
* @brief Set the min delayLoop between each loop
*
* @param delayLoop time in Milliseconds
*/
void setDelay(uint8_t delayLoop) { this->delayLoop = delayLoop; }
/**
* @brief Get the Direction
*
@@ -149,6 +125,7 @@ class Speedometer {
private:
void run() override;
void init(uint8_t pin, double diameter, uint16_t steps);
void initAvgBuf();
void clearAvgBuf();
@@ -166,13 +143,11 @@ class Speedometer {
uint8_t printCounter = 0;
uint8_t bufSize = BUFSIZE;
uint8_t delayLoop = DELAY_SPEEDOMETER;
uint8_t bufPos = 0;
uint16_t steps;
int16_t *buf = nullptr;
uint32_t lastMillisLoop = 0;
uint32_t lastMillisCalc = 0;
};
+4 -1
View File
@@ -15,9 +15,10 @@ CalibrateCompass::CalibrateCompass(QMC5883LCompass* compass) {
this->compass = compass;
this->state = State::Ready;
this->clearData();
this->activateOnlyChilds();
}
void CalibrateCompass::loop() {
void CalibrateCompass::runAsChild() {
if (this->state != State::Calibrating)
return;
@@ -67,6 +68,8 @@ void CalibrateCompass::loop() {
}
}
void CalibrateCompass::run() {}
void CalibrateCompass::start() {
if (this->state != State::Ready)
return;
+5 -2
View File
@@ -15,7 +15,9 @@
#include <Preferences.h>
#include <iostream>
class CalibrateCompass {
#include "component.h"
class CalibrateCompass : public Component {
public:
enum State {
Ready,
@@ -29,7 +31,6 @@ class CalibrateCompass {
CalibrateCompass(QMC5883LCompass* compass);
void loop();
void start();
void useData();
void removeCalibration();
@@ -43,6 +44,8 @@ class CalibrateCompass {
friend std::ostream& operator<<(std::ostream& os, const CalibrateCompass& caliComp);
private:
void runAsChild() override;
void run() override;
void checkDataValidity();
QMC5883LCompass* compass;