remove most of the defines
This commit is contained in:
@@ -13,8 +13,8 @@ Counter::Counter(uint8_t pin) {
|
||||
pcnt_config_t config;
|
||||
config.unit = this->unit;
|
||||
config.channel = PCNT_CHANNEL_0;
|
||||
config.counter_h_lim = COUNTER_HIGH_LIMIT;
|
||||
config.counter_l_lim = COUNTER_LOW_LIMIT;
|
||||
config.counter_h_lim = Counter::highLimit;
|
||||
config.counter_l_lim = Counter::lowLimit;
|
||||
config.ctrl_gpio_num = PCNT_PIN_NOT_USED;
|
||||
config.hctrl_mode = PCNT_CHANNEL_LEVEL_ACTION_KEEP;
|
||||
config.lctrl_mode = PCNT_CHANNEL_LEVEL_ACTION_KEEP;
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
#include <Arduino.h>
|
||||
#include <driver/pcnt.h>
|
||||
|
||||
#define COUNTER_HIGH_LIMIT INT16_MAX
|
||||
#define COUNTER_LOW_LIMIT 0
|
||||
|
||||
class Counter {
|
||||
public:
|
||||
Counter(uint8_t pin);
|
||||
@@ -21,6 +18,9 @@ class Counter {
|
||||
void filterDisable();
|
||||
|
||||
private:
|
||||
static constexpr int16_t highLimit = INT16_MAX;
|
||||
static constexpr uint8_t lowLimit = 0;
|
||||
|
||||
static uint8_t amountOfCounter;
|
||||
|
||||
bool initalised = false;
|
||||
|
||||
@@ -22,20 +22,20 @@ void LcdWrapper::run() {
|
||||
return;
|
||||
|
||||
this->lcd->clear();
|
||||
for (uint8_t i = 0; i < DISPLAY_WRAPPER_LINES; i++) {
|
||||
for (uint8_t i = 0; i < LcdWrapper::totalLines; i++) {
|
||||
this->lcd->setCursor(0, i);
|
||||
this->lcd->print(this->data[i]);
|
||||
}
|
||||
|
||||
if (this->callback)
|
||||
this->callback(this->data, DISPLAY_WRAPPER_LINES, DISPLAY_WRAPPER_ROWS);
|
||||
this->callback(this->data, LcdWrapper::totalLines, LcdWrapper::totalRows);
|
||||
|
||||
this->changed = false;
|
||||
}
|
||||
|
||||
void LcdWrapper::clear() {
|
||||
for (uint8_t i = 0; i < DISPLAY_WRAPPER_LINES; i++) {
|
||||
for (uint8_t j = 0; j < DISPLAY_WRAPPER_ROWS; j++) {
|
||||
for (uint8_t i = 0; i < LcdWrapper::totalLines; i++) {
|
||||
for (uint8_t j = 0; j < LcdWrapper::totalRows; j++) {
|
||||
data[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
@@ -43,11 +43,11 @@ void LcdWrapper::clear() {
|
||||
}
|
||||
|
||||
void LcdWrapper::setCursor(uint8_t row, uint8_t line) {
|
||||
if (row > DISPLAY_WRAPPER_ROWS - 1)
|
||||
row = DISPLAY_WRAPPER_ROWS - 1;
|
||||
if (row > LcdWrapper::totalRows - 1)
|
||||
row = LcdWrapper::totalRows - 1;
|
||||
|
||||
if (line > DISPLAY_WRAPPER_LINES - 1)
|
||||
line = DISPLAY_WRAPPER_LINES - 1;
|
||||
if (line > LcdWrapper::totalLines - 1)
|
||||
line = LcdWrapper::totalLines - 1;
|
||||
|
||||
this->cursorRow = row;
|
||||
this->cursorLine = line;
|
||||
@@ -55,7 +55,7 @@ void LcdWrapper::setCursor(uint8_t row, uint8_t line) {
|
||||
|
||||
void LcdWrapper::print(const char *str) {
|
||||
uint8_t inputStringPosition = 0;
|
||||
for (uint8_t i = this->cursorRow; i < DISPLAY_WRAPPER_ROWS; i++) {
|
||||
for (uint8_t i = this->cursorRow; i < LcdWrapper::totalRows; i++) {
|
||||
if (str[inputStringPosition] == '\0')
|
||||
break;
|
||||
else
|
||||
|
||||
@@ -17,11 +17,7 @@
|
||||
#include <displayWrapper.h>
|
||||
#include <component.h>
|
||||
|
||||
#define DISPLAY_WRAPPER_ROWS 16
|
||||
#define DISPLAY_WRAPPER_LINES 2
|
||||
|
||||
typedef void (*LcdWrapperCallback) (const char data[][DISPLAY_WRAPPER_ROWS], uint8_t lines, uint8_t rows);
|
||||
|
||||
typedef void (*LcdWrapperCallback) (const char data[][16], uint8_t lines, uint8_t rows);
|
||||
/**
|
||||
* @brief A class for the Menu class to print information
|
||||
*
|
||||
@@ -60,13 +56,15 @@ class LcdWrapper : public DisplayWrapper, public Component {
|
||||
*/
|
||||
void print(const char *str) override;
|
||||
|
||||
|
||||
static constexpr uint8_t totalRows = 16;
|
||||
static constexpr uint8_t totalLines = 2;
|
||||
private:
|
||||
void run() override;
|
||||
|
||||
|
||||
LiquidCrystal_I2C* lcd;
|
||||
LcdWrapperCallback callback = nullptr;
|
||||
char data[DISPLAY_WRAPPER_LINES][DISPLAY_WRAPPER_ROWS];
|
||||
char data[LcdWrapper::totalLines][LcdWrapper::totalRows];
|
||||
|
||||
uint8_t cursorRow = 0;
|
||||
uint8_t cursorLine = 0;
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
#include "motorControl.h"
|
||||
|
||||
MotorControl::MotorControl() {
|
||||
this->setMinPwm(PWMMIN);
|
||||
this->setMaxPwm(PWMMAX);
|
||||
this->loopDelay = DELAY;
|
||||
this->setMinPwm(MotorControl::pwmMin);
|
||||
this->setMaxPwm(MotorControl::pwmMax);
|
||||
Component::loopDelay = MotorControl::loopDelay;
|
||||
}
|
||||
|
||||
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2) {
|
||||
@@ -30,7 +30,7 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
|
||||
digitalWrite(this->dir_1, LOW);
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
|
||||
ledcSetup(this->pwmChannel, PWMFREQ, this->pwmRes);
|
||||
ledcSetup(this->pwmChannel, MotorControl::pwmFreq, MotorControl::pwmRes);
|
||||
ledcAttachPin(this->pwmPin, this->pwmChannel);
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
}
|
||||
@@ -42,14 +42,14 @@ void MotorControl::run() {
|
||||
// Difference between targetPower and power
|
||||
int16_t difference = this->targetPower - this->power;
|
||||
|
||||
// Check that the target speed is close to 0 and that the abs_difference is lower than powersteps
|
||||
if (abs(this->targetPower) < powersteps && abs_difference < powersteps) {
|
||||
// Check that the target speed is close to 0 and that the abs_difference is lower than MotorControl::powerSteps
|
||||
if (abs(this->targetPower) < MotorControl::powerSteps && abs_difference < MotorControl::powerSteps) {
|
||||
this->setRealPower(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Correct speed
|
||||
if (abs_difference < powersteps) {
|
||||
if (abs_difference < MotorControl::powerSteps) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -58,23 +58,23 @@ void MotorControl::run() {
|
||||
// Positive or negative speed
|
||||
if (this->power >= 0) {
|
||||
if (difference > 0) {
|
||||
this->increasePower(powersteps);
|
||||
this->increasePower(MotorControl::powerSteps);
|
||||
} else {
|
||||
this->increasePower(-powersteps);
|
||||
this->increasePower(-MotorControl::powerSteps);
|
||||
}
|
||||
} else {
|
||||
this->increasePower(powersteps);
|
||||
this->increasePower(MotorControl::powerSteps);
|
||||
}
|
||||
|
||||
} else {
|
||||
// Positive or negative speed
|
||||
if (this->power >= 0) {
|
||||
this->increasePower(-powersteps);
|
||||
this->increasePower(-MotorControl::powerSteps);
|
||||
} else {
|
||||
if (difference > 0) {
|
||||
this->increasePower(powersteps);
|
||||
this->increasePower(MotorControl::powerSteps);
|
||||
} else {
|
||||
this->increasePower(-powersteps);
|
||||
this->increasePower(-MotorControl::powerSteps);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,11 +94,6 @@ void MotorControl::setMaxPwm(uint8_t max) {
|
||||
this->dutycycleMax = max;
|
||||
}
|
||||
|
||||
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
|
||||
this->powersteps = increment;
|
||||
return (uint16_t) (this->loopDelay * ( 100 / powersteps ));
|
||||
}
|
||||
|
||||
void MotorControl::setTargetPower(int8_t power) {
|
||||
if (power <= 100 && power >= -100)
|
||||
this->targetPower = power;
|
||||
@@ -168,7 +163,7 @@ void MotorControl::setRealPower(int8_t power) {
|
||||
void MotorControl::increasePower(int8_t power) {
|
||||
//TODO: Exceptionhandling
|
||||
//TODO: make a stop befor a direction change
|
||||
if (abs(power) > 2 * powersteps) {
|
||||
if (abs(power) > 2 * MotorControl::powerSteps) {
|
||||
Serial.println("Invalid Argument in MotorControl::increasePower");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -16,14 +16,7 @@
|
||||
#include <iostream>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <component.h>
|
||||
|
||||
#define DELAY 10
|
||||
#define PWMFREQ 16000
|
||||
#define PWMRES 8
|
||||
#define POWERSTEPS 2 // A total of 20 levels ( 100 / SPEED_STEPS ) * RUN_MOTOR_CONTROL_DELAY = 500ms
|
||||
#define PWMMIN 55
|
||||
#define PWMMAX 94 // Max 98% of 2^PWM_RES
|
||||
#include "component.h"
|
||||
|
||||
/**
|
||||
* @brief A class which use PWM to control the power of DC Motor
|
||||
@@ -58,24 +51,6 @@ class MotorControl : public Component {
|
||||
*/
|
||||
void setMaxPwm(uint8_t max);
|
||||
|
||||
/**
|
||||
* @brief Set the Power Steps
|
||||
*
|
||||
* Set the increment of the steps with which the dutycycle is
|
||||
* increased or decreased. Note the dependency between the increment
|
||||
* and delayLoop().
|
||||
*
|
||||
* The formula for the time between 0% and 100% power is:
|
||||
* time[ms] = delayLoop * ( 100 / increment )
|
||||
* 500 ms are recommended
|
||||
*
|
||||
* @see setDelay()
|
||||
*
|
||||
* @param increment
|
||||
* @return time from 0% power to 100% power in Milliseconds
|
||||
*/
|
||||
uint16_t setPowerSteps(uint8_t increment);
|
||||
|
||||
/**
|
||||
* @brief Set the Target Power
|
||||
*
|
||||
@@ -122,20 +97,24 @@ class MotorControl : public Component {
|
||||
void setRealPower(int8_t power);
|
||||
void increasePower(int8_t power);
|
||||
|
||||
static constexpr uint8_t loopDelay = 10;
|
||||
static constexpr uint16_t pwmFreq = 16000;
|
||||
static constexpr uint8_t pwmRes = 8;
|
||||
static constexpr uint8_t powerSteps = 2; // A total of 20 levels ( 100 / SPEED_STEPS ) * RUN_MOTOR_CONTROL_DELAY = 500ms
|
||||
static constexpr uint8_t pwmMin = 55;
|
||||
static constexpr uint8_t pwmMax = 98; // Max 98% of 2^PWM_RES
|
||||
|
||||
int8_t targetPower = 0;
|
||||
int8_t power = 0;
|
||||
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
|
||||
|
||||
uint8_t pwmPin;
|
||||
uint8_t pwmChannel;
|
||||
uint8_t pwmRes = PWMRES;
|
||||
uint16_t dutycycle = 0;
|
||||
uint8_t dutycycleMin;
|
||||
uint8_t dutycycleMax;
|
||||
uint8_t dir_1;
|
||||
uint8_t dir_2;
|
||||
uint8_t powersteps = POWERSTEPS;
|
||||
|
||||
uint8_t dir_2;
|
||||
};
|
||||
|
||||
#endif // MOTOR_CONTROL_H
|
||||
|
||||
@@ -63,7 +63,7 @@ void Navigation::init(Route* route) {
|
||||
caliCompass.loadData();
|
||||
caliCompass.useData();
|
||||
|
||||
this->loopDelay =AZIMUTH_UPDATE_DELAY;
|
||||
Component::loopDelay = Navigation::loopDelay;
|
||||
}
|
||||
|
||||
Navigation::~Navigation() {
|
||||
@@ -168,8 +168,8 @@ Navigation::Status Navigation::addCurrentPosToRoute() {
|
||||
|
||||
// Every Point after the first
|
||||
double distance = this->currentPosition.distanceTo(this->lastPointRouteInsert);
|
||||
if (MIN_DISTANCE_BETWEEN_POINTS <= distance
|
||||
&& MAX_DISTANCE_BETWEEN_POINTS >= distance){
|
||||
if (Navigation::minDisBetweenPoints <= distance
|
||||
&& Navigation::maxDisBetweenPoints >= distance){
|
||||
this->route->addPointToRoute(this->currentPosition);
|
||||
this->lastPointRouteInsert = this->currentPosition;
|
||||
return Status::Updated;
|
||||
|
||||
@@ -23,18 +23,6 @@
|
||||
#include "calibrateCompass.h"
|
||||
#include "component.h"
|
||||
|
||||
/**
|
||||
* @brief The minimal distance between Points
|
||||
*
|
||||
* This is a very critical option. It have to be
|
||||
* around the half accuracy of the positioning system
|
||||
*
|
||||
*/
|
||||
#define MIN_DISTANCE_BETWEEN_POINTS 0.3
|
||||
#define MAX_DISTANCE_BETWEEN_POINTS 10
|
||||
#define MIN_DISTANCE_TO_REACH_POINT 0.5
|
||||
#define AZIMUTH_UPDATE_DELAY 20
|
||||
|
||||
/**
|
||||
* @brief This struct inherits the result of the navigation
|
||||
*
|
||||
@@ -221,6 +209,10 @@ class Navigation : public Component {
|
||||
bool setTargetPoint(Point target);
|
||||
int16_t calculateCourseCorrection(Point& point);
|
||||
|
||||
static constexpr uint8_t loopDelay = 20;
|
||||
static constexpr uint8_t maxDisBetweenPoints = 10;
|
||||
static constexpr float minDisBetweenPoints = 0.3;
|
||||
|
||||
SFE_UBLOX_GNSS* gps;
|
||||
UBX_NAV_PVT_data_t* ubxData = nullptr;
|
||||
Route* route = nullptr;
|
||||
@@ -235,8 +227,6 @@ class Navigation : public Component {
|
||||
Point::Accuracy minAccuracy = Point::Accuracy::twoDigOfCM;
|
||||
CalcAzimuthState calcAzimuthState = CalcAzimuthState::Invalid;
|
||||
|
||||
|
||||
|
||||
bool navigationStarted = false;
|
||||
bool navigationFinished = false;
|
||||
bool isNtripInit = false;
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ int16_t Point::courseTo(const Coordinates& point) const {
|
||||
Coordinates begin = this->coordinates;
|
||||
Coordinates end = point;
|
||||
|
||||
double phi = log( tan(end.lat * ROUTE_DEGREE_TO_RADIANT / 2 + ROUTE_PI / 4) / tan(begin.lat * ROUTE_DEGREE_TO_RADIANT / 2 + ROUTE_PI / 4) );
|
||||
double phi = log( tan(end.lat * ROUTE_DEGREE_TO_RADIANT / 2 + M_PI / 4) / tan(begin.lat * ROUTE_DEGREE_TO_RADIANT / 2 + M_PI / 4) );
|
||||
double lon = (begin.lon * ROUTE_DEGREE_TO_RADIANT - end.lon * ROUTE_DEGREE_TO_RADIANT);
|
||||
|
||||
int16_t res = static_cast<int16_t>(atan2(lon, phi) / ROUTE_DEGREE_TO_RADIANT) * -1;
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
#define ROUTE_DEGREE_TO_RADIANT 0.01745
|
||||
#define ROUTE_DISTANCE_BETWEEN_LATITUDE 111300
|
||||
#define ROUTE_PI 3.14159265358979323846
|
||||
|
||||
/**
|
||||
* @brief A to handle points on the earth
|
||||
|
||||
@@ -11,18 +11,22 @@
|
||||
*/
|
||||
#include "speedometer.h"
|
||||
|
||||
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) {
|
||||
this->init(pin, diameter, steps);
|
||||
this->diameter = diameter;
|
||||
this->steps = steps;
|
||||
|
||||
this->pulseCounter = new Counter(pin);
|
||||
this->pulseCounter->setFilterValue(1000); // ignore pulses less than 1000 x 2.5ns
|
||||
|
||||
this->pulseCounter->clear();
|
||||
this->pulseCounter->resume();
|
||||
|
||||
Component::loopDelay = Speedometer::loopDelay;
|
||||
|
||||
clearAvgBuf();
|
||||
}
|
||||
|
||||
Speedometer::~Speedometer() {
|
||||
delete[] this->buf;
|
||||
delete this->pulseCounter;
|
||||
}
|
||||
|
||||
@@ -58,7 +62,7 @@ void Speedometer::run() {
|
||||
break;
|
||||
}
|
||||
|
||||
this->addValToBuf(static_cast<int16_t>(this->speed * CONVERSION_FACTOR));
|
||||
this->addValToBuf(static_cast<int16_t>(this->speed * Speedometer::conversionFactor));
|
||||
}
|
||||
|
||||
void Speedometer::setDirection(Direction dir) {
|
||||
@@ -68,11 +72,6 @@ void Speedometer::setDirection(Direction dir) {
|
||||
this->clearAvgBuf();
|
||||
}
|
||||
|
||||
void Speedometer::setNumOfValForAvg(uint8_t val) {
|
||||
this->bufSize = val;
|
||||
updateAvgBufSize();
|
||||
}
|
||||
|
||||
void Speedometer::setEncFilter(uint16_t val) {
|
||||
if (val > 1023)
|
||||
val = 1023;
|
||||
@@ -81,7 +80,7 @@ void Speedometer::setEncFilter(uint16_t val) {
|
||||
|
||||
double Speedometer::getAvgSpeed() const {
|
||||
int16_t avg = this->calcAverage();
|
||||
return (float)avg / CONVERSION_FACTOR;
|
||||
return (float)avg / Speedometer::conversionFactor;
|
||||
}
|
||||
|
||||
void Speedometer::calibrationMeasurementStart() {
|
||||
@@ -101,25 +100,6 @@ uint16_t Speedometer::calibrationMeasurementStop() {
|
||||
return res;
|
||||
}
|
||||
|
||||
void Speedometer::init(uint8_t pin, double diameter, uint16_t steps) {
|
||||
this->diameter = diameter;
|
||||
this->steps = steps;
|
||||
|
||||
this->pulseCounter = new Counter(pin);
|
||||
this->pulseCounter->setFilterValue(1000); // ignore pulses less than 1000 x 2.5ns
|
||||
|
||||
this->pulseCounter->clear();
|
||||
this->pulseCounter->resume();
|
||||
|
||||
initAvgBuf();
|
||||
}
|
||||
|
||||
void Speedometer::initAvgBuf() {
|
||||
this->buf = new int16_t[bufSize];
|
||||
for (uint8_t i = 0; i < bufSize; i++)
|
||||
this->buf[i] = 0;
|
||||
}
|
||||
|
||||
void Speedometer::clearAvgBuf() {
|
||||
for (uint8_t i = 0; i < bufSize; i++)
|
||||
this->buf[i] = 0;
|
||||
@@ -133,11 +113,6 @@ void Speedometer::addValToBuf(int16_t val) {
|
||||
bufPos = 0;
|
||||
}
|
||||
|
||||
void Speedometer::updateAvgBufSize() {
|
||||
delete[] this->buf;
|
||||
initAvgBuf();
|
||||
}
|
||||
|
||||
int16_t Speedometer::calcAverage() const {
|
||||
int16_t sum = 0;
|
||||
for (int i = 0; i < this->bufSize; i++)
|
||||
|
||||
@@ -19,19 +19,6 @@
|
||||
#include "counter.h"
|
||||
#include "component.h"
|
||||
|
||||
/**
|
||||
* @brief The default size of numbers to be taken in account for the average.
|
||||
*
|
||||
*/
|
||||
#define BUFSIZE 5
|
||||
#define CONVERSION_FACTOR 100
|
||||
|
||||
/**
|
||||
* @brief Default value for min Millisseconds between each loop
|
||||
* @see setDelay(uint8_t val)
|
||||
*/
|
||||
#define DELAY_SPEEDOMETER 30
|
||||
|
||||
/**
|
||||
* @brief A class which use a encoder to calc the speed
|
||||
*
|
||||
@@ -60,9 +47,7 @@ class Speedometer : public Component {
|
||||
* @param pin Pin on the Esp from the encoder.
|
||||
* @param diameter Diameter of the wheel in meters.
|
||||
* @param steps Encodersteps for a complete wheel rotation.
|
||||
* @param numOfValForAvg Number of last values to be taken into account for the average.
|
||||
*/
|
||||
Speedometer(uint8_t pin, double diameter, uint16_t steps, uint8_t numOfValForAvg);
|
||||
Speedometer(uint8_t pin, double diameter, uint16_t steps);
|
||||
|
||||
~Speedometer();
|
||||
@@ -127,12 +112,14 @@ class Speedometer : public Component {
|
||||
private:
|
||||
void run() override;
|
||||
void init(uint8_t pin, double diameter, uint16_t steps);
|
||||
void initAvgBuf();
|
||||
void clearAvgBuf();
|
||||
void addValToBuf(int16_t val);
|
||||
void updateAvgBufSize();
|
||||
int16_t calcAverage() const;
|
||||
|
||||
static constexpr uint8_t loopDelay = 30;
|
||||
static constexpr uint8_t bufSize = 5;
|
||||
static constexpr uint8_t conversionFactor = 100;
|
||||
|
||||
Counter* pulseCounter;
|
||||
Direction currentDirection = Direction::None;
|
||||
|
||||
@@ -142,11 +129,10 @@ class Speedometer : public Component {
|
||||
double diameter;
|
||||
|
||||
uint8_t printCounter = 0;
|
||||
uint8_t bufSize = BUFSIZE;
|
||||
uint8_t bufPos = 0;
|
||||
uint16_t steps;
|
||||
|
||||
int16_t *buf = nullptr;
|
||||
int16_t buf[Speedometer::bufSize];
|
||||
|
||||
uint32_t lastMillisCalc = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user