add menuTest
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file speedometer.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief Implemention of the class speedometer.h
|
||||
* @brief Implementation of the class speedometer.h
|
||||
* @see speedometer.h
|
||||
* @version 0.1
|
||||
* @date 2021-12-13
|
||||
@@ -12,6 +12,9 @@
|
||||
#include "speedometer.h"
|
||||
|
||||
Speedometer::Speedometer() {}
|
||||
Speedometer::~Speedometer() {
|
||||
delete[] this->buf;
|
||||
}
|
||||
|
||||
void Speedometer::init(uint8_t pinA, uint8_t pinB, double diameter, uint16_t steps, uint8_t numOfValForAvg) {
|
||||
this->init(pinA, pinB, diameter, steps);
|
||||
@@ -30,13 +33,16 @@ void Speedometer::init(uint8_t pinA, uint8_t pinB, double diameter, uint16_t ste
|
||||
}
|
||||
|
||||
uint16_t Speedometer::loop() {
|
||||
if (this->calibrationRunning)
|
||||
return -1;
|
||||
|
||||
uint32_t time = millis();
|
||||
uint16_t elapsed_time = time - this->last_millis_loop;
|
||||
|
||||
//Cancel if delay is not reached
|
||||
if (elapsed_time < delay) {
|
||||
//Cancel if delayLoop is not reached
|
||||
if (elapsed_time < this->delayLoop)
|
||||
return elapsed_time;
|
||||
}
|
||||
|
||||
runSpeedometer();
|
||||
this->last_millis_loop = time;
|
||||
return elapsed_time;
|
||||
@@ -50,7 +56,7 @@ void Speedometer::runSpeedometer() {
|
||||
|
||||
int16_t count = encoder.getCount();
|
||||
this->addValToBuf(count);
|
||||
encoder.clearCount();
|
||||
this->encoder.clearCount();
|
||||
|
||||
uint16_t count_abs = abs(this->calcAverage()); // Absolute time in milliseconds
|
||||
double n = (double)count_abs / steps; // Wheel revolutions in absolute time
|
||||
@@ -64,6 +70,8 @@ void Speedometer::runSpeedometer() {
|
||||
} else {
|
||||
this->speed = 0;
|
||||
}
|
||||
|
||||
std::cout << "Speedometer::runSpeedometer speed: " << (int) this->speed << std::endl;
|
||||
}
|
||||
|
||||
void Speedometer::setNumOfValForAvg(uint8_t val) {
|
||||
@@ -73,18 +81,31 @@ void Speedometer::setNumOfValForAvg(uint8_t val) {
|
||||
}
|
||||
|
||||
void Speedometer::setEncFilter(uint16_t val) {
|
||||
if (val > 1023) val = 1023;
|
||||
if (val > 1023)
|
||||
val = 1023;
|
||||
this->encoder.setFilter(val);
|
||||
}
|
||||
|
||||
void Speedometer::setDelay(uint8_t delay) {
|
||||
this->delay = delay;
|
||||
void Speedometer::setDelay(uint8_t delayLoop) {
|
||||
this->delayLoop = delayLoop;
|
||||
}
|
||||
|
||||
double Speedometer::getSpeed() {
|
||||
return this->speed;
|
||||
}
|
||||
|
||||
void Speedometer::calibrationMeasurementStart() {
|
||||
this->calibrationRunning = true;
|
||||
this->encoder.clearCount();
|
||||
}
|
||||
|
||||
uint16_t Speedometer::calibrationMeasurementStop() {
|
||||
this->calibrationRunning = false;
|
||||
uint16_t res = abs(this->encoder.getCount());
|
||||
this->encoder.clearCount();
|
||||
return res;
|
||||
}
|
||||
|
||||
void Speedometer::initAvgBuf() {
|
||||
this->buf = new int16_t[bufSize];
|
||||
for (uint8_t i = 0; i < bufSize; i++)
|
||||
@@ -92,13 +113,11 @@ void Speedometer::initAvgBuf() {
|
||||
}
|
||||
|
||||
void Speedometer::addValToBuf(int16_t val) {
|
||||
static uint8_t bufPos = 0;
|
||||
this->buf[bufPos] = val;
|
||||
bufPos++;
|
||||
this->buf[this->bufPos] = val;
|
||||
this->bufPos++;
|
||||
|
||||
if (bufPos == bufSize) {
|
||||
if (bufPos == bufSize)
|
||||
bufPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Speedometer::updateAvgBufSize() {
|
||||
@@ -108,7 +127,7 @@ void Speedometer::updateAvgBufSize() {
|
||||
|
||||
int16_t Speedometer::calcAverage() {
|
||||
int16_t sum = 0;
|
||||
for (int i = 0; i < bufSize; i++)
|
||||
for (int i = 0; i < this->bufSize; i++)
|
||||
sum += this->buf[i];
|
||||
return sum / bufSize;
|
||||
}
|
||||
return sum / this->bufSize;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <ESP32Encoder.h>
|
||||
|
||||
/**
|
||||
@@ -39,6 +40,7 @@
|
||||
class Speedometer {
|
||||
public:
|
||||
Speedometer();
|
||||
~Speedometer();
|
||||
|
||||
/**
|
||||
* @brief Initialize the speedometer
|
||||
@@ -55,7 +57,7 @@ class Speedometer {
|
||||
/**
|
||||
* @brief Calls runSpeedometer() to update all Values.
|
||||
*
|
||||
* This function should be called every mainloop. If the delay is not reached, than the
|
||||
* This function should be called every mainloop. If the delayLoop is not reached, than the
|
||||
* functions returns immediately.
|
||||
* @see runSpeedometer()
|
||||
* @see DELAY_SPEEDOMETER
|
||||
@@ -85,11 +87,11 @@ class Speedometer {
|
||||
void setEncFilter(uint16_t val);
|
||||
|
||||
/**
|
||||
* @brief Set the min delay between each loop
|
||||
* @brief Set the min delayLoop between each loop
|
||||
*
|
||||
* @param delay time in Milliseconds
|
||||
* @param delayLoop time in Milliseconds
|
||||
*/
|
||||
void setDelay(uint8_t delay);
|
||||
void setDelay(uint8_t delayLoop);
|
||||
|
||||
/**
|
||||
* @brief Get the calculated speed of the Wheel
|
||||
@@ -98,6 +100,9 @@ class Speedometer {
|
||||
*/
|
||||
double getSpeed();
|
||||
|
||||
void calibrationMeasurementStart();
|
||||
uint16_t calibrationMeasurementStop();
|
||||
|
||||
|
||||
private:
|
||||
void initAvgBuf();
|
||||
@@ -108,15 +113,17 @@ class Speedometer {
|
||||
ESP32Encoder encoder;
|
||||
|
||||
bool isInit = false;
|
||||
bool calibrationRunning = false;
|
||||
|
||||
double speed = 0;
|
||||
double diameter;
|
||||
|
||||
uint8_t bufSize = BUFSIZE;
|
||||
uint8_t delayLoop = DELAY_SPEEDOMETER;
|
||||
uint8_t bufPos = 0;
|
||||
uint16_t steps;
|
||||
uint8_t delay = DELAY_SPEEDOMETER;
|
||||
|
||||
int16_t *buf;
|
||||
int16_t *buf = nullptr;
|
||||
|
||||
uint32_t last_millis_loop = 0;
|
||||
uint32_t last_millis_calc = 0;
|
||||
|
||||
Reference in New Issue
Block a user