add menuTest

This commit is contained in:
2023-01-31 08:21:04 +01:00
parent c63428412f
commit d96f382702
7 changed files with 177 additions and 32 deletions
+35 -16
View File
@@ -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;
}