finished new encoder

This commit is contained in:
2023-03-23 19:31:02 +01:00
parent 2f0027b68b
commit e4cdc5f98d
8 changed files with 104 additions and 13 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
#define M_DIR_21 23
#define M_DIR_22 14
#define M_PWM_2 22
#define M_ENCODE_RIGHT 12
#define M_ENCODE_RIGHT 33
// PID config
#define PID_OUT_MIN -100
+56
View File
@@ -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);
}
+30
View File
@@ -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;
};
+8 -7
View File
@@ -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();
+3 -2
View File
@@ -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;
+1 -3
View File
@@ -25,17 +25,15 @@ lib_deps =
marian-craciunescu/ESP32Ping@^1.7
bblanchon/ArduinoJson@^6.20.0
mprograms/QMC5883LCompass@^1.1.1
mike-gofton/ESP32PulseCounter@^0.2.0
https://git.kleiax.de/PlatformIO-Libs/Menu.git#v1.0
upload_port = COM3
[env:release]
build_type = release
lib_deps = mike-gofton/ESP32PulseCounter@^0.2.0
[env:debug]
monitor_filters = esp32_exception_decoder
build_type = debug
monitor_filters = esp32_exception_decoder
check_tool = clangtidy
[platformio]
@@ -13,6 +13,7 @@
SpeedometerTest::SpeedometerTest(Speedometer* speedometer) {
this->speedometer = speedometer;
this->speedometer->setDirection(Speedometer::Forward);
}
+4
View File
@@ -60,6 +60,10 @@ void MoveControl::loop() {
|| left_speed_time > 50
|| right_speed_time > 50) {
// Dont count overTimeCounter if one speedometer is in TestMode.
if (left_speed_time == UINT16_MAX || right_speed_time == UINT16_MAX)
this->overTimeCounter--;
this->overTimeCounter++;
if (this->overTimeCounter >= this->overTimeMax) {
char buf[128];