From e9b633e46ca57a2b21f9688f4634f8b997d2329c Mon Sep 17 00:00:00 2001 From: Erki Date: Sun, 18 Apr 2021 17:32:27 +0300 Subject: [PATCH] Fix encoder to read and act properly --- Peripherals/Inc/peripherals_encoder.hpp | 29 +++++-------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/Peripherals/Inc/peripherals_encoder.hpp b/Peripherals/Inc/peripherals_encoder.hpp index b0b7efc..e12f189 100644 --- a/Peripherals/Inc/peripherals_encoder.hpp +++ b/Peripherals/Inc/peripherals_encoder.hpp @@ -53,14 +53,13 @@ public: void setRevolutionTickCount(const std::uint16_t& count) { - __HAL_TIM_SET_COMPARE(_htim, _channels, count); __HAL_TIM_SET_AUTORELOAD(_htim, count); } std::uint16_t getCurrentClicks() const { const std::uint16_t val = __HAL_TIM_GET_COUNTER(_htim); - return val >> 1; + return val; } std::int32_t getFullRevolutions() const @@ -70,41 +69,25 @@ public: Dirs getDirection() const { - return _direction; - } - - // Should be called somewhat regularly. - void update() - { - const std::uint16_t current = __HAL_TIM_GET_COUNTER(_htim); - if (current > _last_read) - _direction = Dirs::FORWARD; + if (__HAL_TIM_IS_TIM_COUNTING_DOWN(_htim)) + return Dirs::BACKWARD; else - _direction = Dirs::BACKWARD; - - _last_read = current; + return Dirs::FORWARD; } void timerUpdateEvent() { - if (_direction == Dirs::FORWARD) - { + if (!__HAL_TIM_IS_TIM_COUNTING_DOWN(_htim)) _full_revolutions++; - } else - { + else _full_revolutions--; - } - - _last_read = 0; } private: TIM_HandleTypeDef* _htim; std::uint32_t _channels; - Dirs _direction = Dirs::FORWARD; std::int32_t _full_revolutions = 0; - std::uint16_t _last_read = 0; }; }// namespace Peripherals