Encoder feature in STM32F070CB
Hello,
I need a help in Motor frequency calculation with more precision.
I am using STM32F070CB for my application. My requirements are
1) Monitor the Motor frequency based on quadrature pulses.
2) If Frequency varies by +/-15%, need to process some safety activity within 100 milli seconds.
3) find the direction of rotation.
Data: Motor nominal frequency is 100Hz ( when motor running at full speed)
I have configured TIM3 in Encoder mode in such a way that counter counts in UP/DOWN based on the direction of the motor rotation.
I have configured another general purpose timer (TIM6) for 50ms periodic Interrupt.
My Idea is to read the Encoder counter value for every 50ms (in TIM6 interrupt) and clear the Encoder counter value. That is, (counter value / 50ms ) is the frequency.
The problem here I am facing is, the calculated frequency is fluctuating ( 80Hz to 100Hz ) rarely which allows me in misjudgment for the requirement 2.
(Note: My earlier implementation for the same requirement was without using encoder. I have used Capture/compare feature in same timer where the counter was clocked by internal 24MHz clock). Every complete pulse will give an interrupt and the counter value at the time of interrupt will be one pulse width. 1/pulse width is my frequency. Same method is applied for second signal and finding the direction based on phase shift. This method is serving my purpose but leaving me with more interrupts and some extra processing time. So I tried to use Encoder)
Observation:
Encoder counter value is not always constant. Counter values for every 50ms timer are 5, 5, 4, 5, 4, 5....
When the count value changing from 5 to 4, calculated frequency is changing from 100Hz to 80Hz and then back to 100Hz, where the actual system is running constantly at 100Hz.
I have tried another method by averaging the sequence of counts now with the same above data, calculated frequency is changing from 100 Hz to 92Hz.
My Expectations:
Deviation shall not exceed +/- 2Hz.
If the counter values are more, then the deviation will be minimum. I can not increase the periodicity as per the requirement 2.
Need clarifications:
- Is to possible to configure the encoder counter clocked by internal clock and generate the interrupt on number of signal pulses? (this pulses or cycles number shall be configurable)? (OR)
- Is it possible to configure the Encoder counter to count in incrementing order only (even the motor rotates in DOWN direction also), so that I can configure ARR value to required number of pulses or cycles to generate an interrupt. Configuring another timer to treat as pulse width). I may get more precised frequency with this method.
Sorry for the long description, but I just want to provide every detail.
Thanks in advance.