finished new encoder
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#include "counter.h"
|
||||
#include <inttypes.h>
|
||||
|
||||
uint8_t Counter::amountOfCounter = 0;
|
||||
|
||||
Counter::Counter(uint8_t pin) {
|
||||
this->pulsePin = pin;
|
||||
|
||||
this->unit = static_cast<pcnt_unit_t>(Counter::amountOfCounter);
|
||||
if (Counter::amountOfCounter < 7)
|
||||
Counter::amountOfCounter++;
|
||||
|
||||
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.ctrl_gpio_num = PCNT_PIN_NOT_USED;
|
||||
config.hctrl_mode = PCNT_CHANNEL_LEVEL_ACTION_KEEP;
|
||||
config.lctrl_mode = PCNT_CHANNEL_LEVEL_ACTION_KEEP;
|
||||
config.pulse_gpio_num = this->pulsePin;
|
||||
config.pos_mode = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
|
||||
config.neg_mode = PCNT_CHANNEL_EDGE_ACTION_HOLD;
|
||||
pcnt_unit_config(&config);
|
||||
}
|
||||
|
||||
void Counter::pause() {
|
||||
pcnt_counter_pause(this->unit);
|
||||
}
|
||||
|
||||
void Counter::resume() {
|
||||
pcnt_counter_resume(this->unit);
|
||||
}
|
||||
|
||||
void Counter::clear() {
|
||||
pcnt_counter_clear(this->unit);
|
||||
}
|
||||
|
||||
int16_t Counter::getValue() {
|
||||
int16_t res;
|
||||
pcnt_get_counter_value(this->unit, &res);
|
||||
return res;
|
||||
}
|
||||
|
||||
void Counter::setFilterValue(uint16_t value) {
|
||||
pcnt_set_filter_value(this->unit, value);
|
||||
this->filterEnable();
|
||||
}
|
||||
|
||||
void Counter::filterEnable() {
|
||||
pcnt_filter_enable(this->unit);
|
||||
}
|
||||
|
||||
void Counter::filterDisable() {
|
||||
pcnt_filter_disable(this->unit);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#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);
|
||||
|
||||
void pause();
|
||||
void resume();
|
||||
void clear();
|
||||
|
||||
int16_t getValue();
|
||||
|
||||
void setFilterValue(uint16_t value);
|
||||
void filterEnable();
|
||||
void filterDisable();
|
||||
|
||||
private:
|
||||
static uint8_t amountOfCounter;
|
||||
|
||||
bool initalised = false;
|
||||
|
||||
uint8_t pulsePin;
|
||||
pcnt_unit_t unit;
|
||||
};
|
||||
@@ -47,7 +47,7 @@ void Speedometer::runSpeedometer() {
|
||||
uint16_t elapsed_time = time - last_millis_calc;
|
||||
last_millis_calc = time;
|
||||
|
||||
int16_t count = this->pulseCounter->get_value();
|
||||
int16_t count = this->pulseCounter->getValue();
|
||||
switch (this->currentDirection) {
|
||||
case Direction::Forward :
|
||||
this->addValToBuf(count);
|
||||
@@ -93,20 +93,23 @@ void Speedometer::setNumOfValForAvg(uint8_t val) {
|
||||
void Speedometer::setEncFilter(uint16_t val) {
|
||||
if (val > 1023)
|
||||
val = 1023;
|
||||
this->pulseCounter->set_filter_value(val);
|
||||
this->pulseCounter->setFilterValue(val);
|
||||
}
|
||||
|
||||
void Speedometer::calibrationMeasurementStart() {
|
||||
std::cout << "Start" << std::endl;
|
||||
this->calibrationRunning = true;
|
||||
this->pulseCounter->clear();
|
||||
this->pulseCounter->resume();
|
||||
}
|
||||
|
||||
uint16_t Speedometer::calibrationMeasurementStop() {
|
||||
std::cout << "Ende" << std::endl;
|
||||
this->calibrationRunning = false;
|
||||
uint16_t res = abs(this->pulseCounter->get_value());
|
||||
uint16_t res = abs(this->pulseCounter->getValue());
|
||||
this->pulseCounter->clear();
|
||||
this->pulseCounter->resume();
|
||||
std::cout << "Result: " << res << std::endl;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -114,10 +117,8 @@ 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 = new Counter(pin);
|
||||
this->pulseCounter->setFilterValue(1000); // ignore pulses less than 1000 x 2.5ns
|
||||
|
||||
this->pulseCounter->clear();
|
||||
this->pulseCounter->resume();
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
#include <Arduino.h>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <esp32_pcnt.h>
|
||||
|
||||
#include "counter.h"
|
||||
|
||||
/**
|
||||
* @brief The default size of numbers to be taken in account for the average.
|
||||
@@ -153,7 +154,7 @@ class Speedometer {
|
||||
void updateAvgBufSize();
|
||||
int16_t calcAverage();
|
||||
|
||||
PulseCounter* pulseCounter;
|
||||
Counter* pulseCounter;
|
||||
Direction currentDirection = Direction::None;
|
||||
|
||||
bool calibrationRunning = false;
|
||||
|
||||
Reference in New Issue
Block a user