done with all changes for new encoder

This commit is contained in:
2023-02-21 11:18:49 +01:00
parent 8bb38d560e
commit ba91da7ed3
7 changed files with 106 additions and 85 deletions
View File
-27
View File
@@ -1,27 +0,0 @@
/**
* @file pulseCounter.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2023-02-18
*
* @copyright Copyright (c) 2023
*
*/
#pragma once
#include <stdint.h.h>
#include <Arduino.h>
#include <esp32_pcnt.h>
class PulseCounter {
public:
PulseCounter(uint8_t pin, uint8_t unit);
private:
uint8_t pin;
uint8_t unit;
int16_t counter = 0;
};
+52 -33
View File
@@ -11,25 +11,18 @@
*/
#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);
Speedometer::Speedometer(uint8_t pin, double diameter, uint16_t steps, uint8_t numOfValForAvg) {
this->init(pin, diameter, steps);
this->bufSize = numOfValForAvg;
}
void Speedometer::init(uint8_t pinA, uint8_t pinB, double diameter, uint16_t steps) {
ESP32Encoder::useInternalWeakPullResistors=DOWN;
this->encoder.attachFullQuad(pinA, pinB);
this->diameter = diameter;
this->steps = steps;
Speedometer::Speedometer(uint8_t pin, double diameter, uint16_t steps) {
this->init(pin, diameter, steps);
}
initAvgBuf();
this->isInit = true;
Speedometer::~Speedometer() {
delete[] this->buf;
delete this->pulseCounter;
}
uint16_t Speedometer::loop() {
@@ -54,9 +47,27 @@ void Speedometer::runSpeedometer() {
uint16_t elapsed_time = time - last_millis_calc;
last_millis_calc = time;
int16_t count = encoder.getCount();
this->addValToBuf(count);
this->encoder.clearCount();
int16_t count = this->pulseCounter->get_value();
switch (this->currentDirection) {
case Direction::Forward :
this->addValToBuf(count);
break;
case Direction::Backward :
this->addValToBuf(-count);
break;
case Direction::None :
this->addValToBuf(0);
break;
default:
std::cout << "Wrong value in Speedometer::runSpeedometer" << std::endl;
break;
}
this->pulseCounter->clear();
this->pulseCounter->resume();
uint16_t count_abs = abs(this->calcAverage()); // Absolute time in milliseconds
double n = (double)count_abs / steps; // Wheel revolutions in absolute time
@@ -71,41 +82,49 @@ void Speedometer::runSpeedometer() {
this->speed = 0;
}
std::cout << "Speedometer::runSpeedometer speed: " << (int) this->speed << std::endl;
// std::cout << "Speedometer::runSpeedometer speed: " << (int) this->speed << std::endl;
}
void Speedometer::setNumOfValForAvg(uint8_t val) {
this->bufSize = val;
if (this->isInit)
updateAvgBufSize();
updateAvgBufSize();
}
void Speedometer::setEncFilter(uint16_t val) {
if (val > 1023)
val = 1023;
this->encoder.setFilter(val);
}
void Speedometer::setDelay(uint8_t delayLoop) {
this->delayLoop = delayLoop;
}
double Speedometer::getSpeed() {
return this->speed;
this->pulseCounter->set_filter_value(val);
}
void Speedometer::calibrationMeasurementStart() {
this->calibrationRunning = true;
this->encoder.clearCount();
this->pulseCounter->clear();
this->pulseCounter->resume();
}
uint16_t Speedometer::calibrationMeasurementStop() {
this->calibrationRunning = false;
uint16_t res = abs(this->encoder.getCount());
this->encoder.clearCount();
uint16_t res = abs(this->pulseCounter->get_value());
this->pulseCounter->clear();
this->pulseCounter->resume();
return res;
}
void Speedometer::init(uint8_t pin, double diameter, uint16_t steps) {
this->diameter = diameter;
this->steps = steps;
this->pulseCounter = new PulseCounter();
this->pulseCounter->initialise(pin, PCNT_PIN_NOT_USED);
this->pulseCounter->set_mode(PCNT_COUNT_INC, PCNT_COUNT_DIS, PCNT_MODE_KEEP, PCNT_MODE_KEEP);
this->pulseCounter->set_filter_value(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++)
+41 -13
View File
@@ -15,7 +15,7 @@
#include <Arduino.h>
#include <cstdint>
#include <iostream>
#include <ESP32Encoder.h>
#include <esp32_pcnt.h>
/**
* @brief The default size of numbers to be taken in account for the average.
@@ -39,20 +39,31 @@
*/
class Speedometer {
public:
Speedometer();
~Speedometer();
/**
* @brief Enum to control the direction.
*
* If the Direction is Forward, the internal counter counts up and a positiv speed will be returned.
* If the Direction is Backward, the internal counter counts down and a negativ speed will be returned.
* If the Dorection is None, no measurement will be taken.
*/
enum Direction {
None,
Forward,
Backward
};
/**
* @brief Initialize the speedometer
* @brief Construct a new Speedometer object
*
* @param pinA Pin on the Esp from the encoder.
* @param pinB Pin on the Esp from the encoder.
* @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.
*/
void init(uint8_t pinA, uint8_t pinB, double diameter, uint16_t steps, uint8_t numOfValForAvg);
void init(uint8_t pinA, uint8_t pinB, double diameter, uint16_t steps);
Speedometer(uint8_t pin, double diameter, uint16_t steps, uint8_t numOfValForAvg);
Speedometer(uint8_t pin, double diameter, uint16_t steps);
~Speedometer();
/**
* @brief Calls runSpeedometer() to update all Values.
@@ -72,6 +83,13 @@ class Speedometer {
*/
void runSpeedometer();
/**
* @brief Set the direction
*
* @param dir Direction
*/
void setDirection(Direction dir) { this->currentDirection = dir; }
/**
* @brief Set the number of last values to be taken into account for the average.
*
@@ -82,7 +100,9 @@ class Speedometer {
/**
* @brief Set the Enc Filter to prevent bouncing
*
* @param val default = 250, max = 1023
* ignore pulses less than val x 2.5ns
*
* @param val default = 1000, max = 1023
*/
void setEncFilter(uint16_t val);
@@ -91,14 +111,21 @@ class Speedometer {
*
* @param delayLoop time in Milliseconds
*/
void setDelay(uint8_t delayLoop);
void setDelay(uint8_t delayLoop) { this->delayLoop = delayLoop; }
/**
* @brief Get the Direction
*
* @return Direction
*/
Direction getDirection() { return this->currentDirection; }
/**
* @brief Get the calculated speed of the Wheel
*
* @return double speed in m/s
*/
double getSpeed();
double getSpeed() { return this->speed; }
/**
* @brief Start calibration
@@ -120,14 +147,15 @@ class Speedometer {
private:
void init(uint8_t pin, double diameter, uint16_t steps);
void initAvgBuf();
void addValToBuf(int16_t val);
void updateAvgBufSize();
int16_t calcAverage();
ESP32Encoder encoder;
PulseCounter* pulseCounter;
Direction currentDirection = Direction::None;
bool isInit = false;
bool calibrationRunning = false;
double speed = 0;