Fix encoder to read and act properly
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Erki 2021-04-18 17:32:27 +03:00
parent 5a24602d86
commit e9b633e46c

View File

@ -53,14 +53,13 @@ public:
void setRevolutionTickCount(const std::uint16_t& count) void setRevolutionTickCount(const std::uint16_t& count)
{ {
__HAL_TIM_SET_COMPARE(_htim, _channels, count);
__HAL_TIM_SET_AUTORELOAD(_htim, count); __HAL_TIM_SET_AUTORELOAD(_htim, count);
} }
std::uint16_t getCurrentClicks() const std::uint16_t getCurrentClicks() const
{ {
const std::uint16_t val = __HAL_TIM_GET_COUNTER(_htim); const std::uint16_t val = __HAL_TIM_GET_COUNTER(_htim);
return val >> 1; return val;
} }
std::int32_t getFullRevolutions() const std::int32_t getFullRevolutions() const
@ -70,41 +69,25 @@ public:
Dirs getDirection() const Dirs getDirection() const
{ {
return _direction; if (__HAL_TIM_IS_TIM_COUNTING_DOWN(_htim))
} return Dirs::BACKWARD;
// Should be called somewhat regularly.
void update()
{
const std::uint16_t current = __HAL_TIM_GET_COUNTER(_htim);
if (current > _last_read)
_direction = Dirs::FORWARD;
else else
_direction = Dirs::BACKWARD; return Dirs::FORWARD;
_last_read = current;
} }
void timerUpdateEvent() void timerUpdateEvent()
{ {
if (_direction == Dirs::FORWARD) if (!__HAL_TIM_IS_TIM_COUNTING_DOWN(_htim))
{
_full_revolutions++; _full_revolutions++;
} else else
{
_full_revolutions--; _full_revolutions--;
}
_last_read = 0;
} }
private: private:
TIM_HandleTypeDef* _htim; TIM_HandleTypeDef* _htim;
std::uint32_t _channels; std::uint32_t _channels;
Dirs _direction = Dirs::FORWARD;
std::int32_t _full_revolutions = 0; std::int32_t _full_revolutions = 0;
std::uint16_t _last_read = 0;
}; };
}// namespace Peripherals }// namespace Peripherals