@@ -21,9 +21,9 @@ STM32HWEncoder::STM32HWEncoder(unsigned int _ppr, int8_t pinA, int8_t pinB, int8
21
21
prev_timestamp = getCurrentMicros ();
22
22
pulse_timestamp = getCurrentMicros ();
23
23
24
- _pinA = pinA;
25
- _pinB = pinB;
26
- _pinI = pinI;
24
+ _pinA = digitalPinToPinName ( pinA) ;
25
+ _pinB = digitalPinToPinName ( pinB) ;
26
+ _pinI = digitalPinToPinName ( pinI) ;
27
27
}
28
28
29
29
@@ -76,9 +76,7 @@ int32_t STM32HWEncoder::getFullRotations() {
76
76
float STM32HWEncoder::getVelocity () {
77
77
// sampling time calculation
78
78
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
82
80
83
81
// time from last impulse
84
82
int32_t overflow_diff = overflow_count - prev_overflow_count;
@@ -87,7 +85,8 @@ float STM32HWEncoder::getVelocity() {
87
85
float pulse_per_second = dN / dt;
88
86
89
87
// 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;
91
90
}
92
91
93
92
// getter for index pin
@@ -99,33 +98,25 @@ int STM32HWEncoder::hasIndex() { return 0; }
99
98
// encoder initialisation of the hardware pins
100
99
// and calculation variables
101
100
void STM32HWEncoder::init () {
101
+ // counter setup
102
+ overflow_count = 0 ;
103
+ count = 0 ;
104
+ prev_count = 0 ;
105
+ prev_overflow_count = 0 ;
106
+
102
107
// overflow handling
103
108
rotations_per_overflow = 0xFFFF / cpr;
104
109
ticks_per_overflow = cpr * rotations_per_overflow;
105
110
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);
129
120
130
121
// set up timer for encoder
131
122
encoder_handle.Init .Period = ticks_per_overflow - 1 ;
@@ -152,19 +143,18 @@ void STM32HWEncoder::init() {
152
143
encoder_handle.Instance = InstanceA; // e.g. TIM4;
153
144
enableTimerClock (&encoder_handle);
154
145
if (HAL_TIM_Encoder_Init (&encoder_handle, &encoder_config) != HAL_OK) {
155
- _Error_Handler (__FILE__, __LINE__);
146
+ initialized = false ;
147
+ return ;
156
148
}
157
149
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
+ }
165
154
166
155
prev_timestamp = getCurrentMicros ();
167
156
pulse_timestamp = getCurrentMicros ();
157
+ initialized = true ;
168
158
}
169
159
170
160
#endif
0 commit comments