Skip to content

Commit d7cd0c1

Browse files
author
Richard Unger
committed
improvements to STM32HardwareEncoder
1 parent 47fd048 commit d7cd0c1

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

src/encoders/stm32hwencoder/STM32HWEncoder.cpp

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ STM32HWEncoder::STM32HWEncoder(unsigned int _ppr, int8_t pinA, int8_t pinB, int8
2121
prev_timestamp = getCurrentMicros();
2222
pulse_timestamp = getCurrentMicros();
2323

24-
_pinA = pinA;
25-
_pinB = pinB;
26-
_pinI = pinI;
24+
_pinA = digitalPinToPinName(pinA);
25+
_pinB = digitalPinToPinName(pinB);
26+
_pinI = digitalPinToPinName(pinI);
2727
}
2828

2929

@@ -76,9 +76,7 @@ int32_t STM32HWEncoder::getFullRotations() {
7676
float STM32HWEncoder::getVelocity() {
7777
// sampling time calculation
7878
float dt = (pulse_timestamp - prev_timestamp) * 1e-6f;
79-
// quick fix for strange cases (micros overflow)
80-
if (dt <= 0 || dt > 0.5f)
81-
dt = 1e-3f;
79+
if (dt < min_elapsed_time) return velocity; // don't update velocity if deltaT is too small
8280

8381
// time from last impulse
8482
int32_t overflow_diff = overflow_count - prev_overflow_count;
@@ -87,7 +85,8 @@ float STM32HWEncoder::getVelocity() {
8785
float pulse_per_second = dN / dt;
8886

8987
// velocity calculation
90-
return pulse_per_second / (static_cast<float>(cpr)) * _2PI;
88+
velocity = pulse_per_second / (static_cast<float>(cpr)) * _2PI;
89+
return velocity;
9190
}
9291

9392
// getter for index pin
@@ -99,33 +98,25 @@ int STM32HWEncoder::hasIndex() { return 0; }
9998
// encoder initialisation of the hardware pins
10099
// and calculation variables
101100
void STM32HWEncoder::init() {
101+
// counter setup
102+
overflow_count = 0;
103+
count = 0;
104+
prev_count = 0;
105+
prev_overflow_count = 0;
106+
102107
// overflow handling
103108
rotations_per_overflow = 0xFFFF / cpr;
104109
ticks_per_overflow = cpr * rotations_per_overflow;
105110

106-
// set up GPIO
107-
GPIO_InitTypeDef gpio;
108-
109-
PinName pinA = digitalPinToPinName(_pinA);
110-
TIM_TypeDef *InstanceA = (TIM_TypeDef *)pinmap_peripheral(pinA, PinMap_PWM);
111-
gpio.Pin = digitalPinToBitMask(_pinA);
112-
gpio.Mode = GPIO_MODE_AF_PP;
113-
gpio.Pull = GPIO_NOPULL;
114-
gpio.Speed = GPIO_SPEED_FREQ_MEDIUM;
115-
#ifndef STM32F1xx_HAL_GPIO_H
116-
gpio.Alternate = pinmap_function(pinA, PinMap_PWM);
117-
#endif
118-
HAL_GPIO_Init(digitalPinToPort(_pinA), &gpio);
119-
120-
// lets assume pinB is on the same timer as pinA... otherwise it can't work but the API currently doesn't allow us to fail gracefully
121-
gpio.Pin = digitalPinToBitMask(_pinB);
122-
gpio.Mode = GPIO_MODE_AF_PP;
123-
gpio.Pull = GPIO_NOPULL;
124-
gpio.Speed = GPIO_SPEED_FREQ_MEDIUM;
125-
#ifndef STM32F1xx_HAL_GPIO_H
126-
gpio.Alternate = pinmap_function(digitalPinToPinName(_pinB), PinMap_PWM);
127-
#endif
128-
HAL_GPIO_Init(digitalPinToPort(_pinB), &gpio);
111+
// GPIO configuration
112+
TIM_TypeDef *InstanceA = (TIM_TypeDef *)pinmap_peripheral(_pinA, PinMap_PWM);
113+
TIM_TypeDef *InstanceB = (TIM_TypeDef *)pinmap_peripheral(_pinB, PinMap_PWM);
114+
if (InstanceA != InstanceB) {
115+
initialized = false;
116+
return;
117+
}
118+
pinmap_pinout(_pinA, PinMap_TIM);
119+
pinmap_pinout(_pinB, PinMap_TIM);
129120

130121
// set up timer for encoder
131122
encoder_handle.Init.Period = ticks_per_overflow - 1;
@@ -152,19 +143,18 @@ void STM32HWEncoder::init() {
152143
encoder_handle.Instance = InstanceA; // e.g. TIM4;
153144
enableTimerClock(&encoder_handle);
154145
if (HAL_TIM_Encoder_Init(&encoder_handle, &encoder_config) != HAL_OK) {
155-
_Error_Handler(__FILE__, __LINE__);
146+
initialized = false;
147+
return;
156148
}
157149

158-
HAL_TIM_Encoder_Start(&encoder_handle, TIM_CHANNEL_1);
159-
160-
// counter setup
161-
overflow_count = 0;
162-
count = 0;
163-
prev_count = 0;
164-
prev_overflow_count = 0;
150+
if (HAL_TIM_Encoder_Start(&encoder_handle, TIM_CHANNEL_1) != HAL_OK) {
151+
initialized = false;
152+
return;
153+
}
165154

166155
prev_timestamp = getCurrentMicros();
167156
pulse_timestamp = getCurrentMicros();
157+
initialized = true;
168158
}
169159

170160
#endif

src/encoders/stm32hwencoder/STM32HWEncoder.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

2-
#ifndef STM32_HARDWARE_ENCODER_H
3-
#define STM32_HARDWARE_ENCODER_H
2+
#pragma once
43

54
#include <Arduino.h>
65

@@ -44,6 +43,9 @@ class STM32HWEncoder : public Sensor {
4443
*/
4544
int needsSearch() override;
4645

46+
bool initialized = false;
47+
PinName _pinA, _pinB, _pinI;
48+
4749
private:
4850
int hasIndex(); // !< function returning 1 if encoder has index pin and 0 if not.
4951

@@ -62,9 +64,6 @@ class STM32HWEncoder : public Sensor {
6264

6365
// velocity calculation variables
6466
volatile int32_t pulse_timestamp, prev_timestamp;
65-
66-
int8_t _pinA, _pinB, _pinI;
6767
};
6868

69-
#endif
7069
#endif

0 commit comments

Comments
 (0)