cancel
Showing results for 
Search instead for 
Did you mean: 

Measure time between encoder counts wtih STM2f439ZI

Samy Guerra
Associate

Hello, I'm super new to all this STM32 stuff so bear with me please.

I'm using a Nucle board with an STM32f439ZI and I want to measure the speed of my motor.

So far I managed to count the increments of my incremental encoder. I did that by configurating TIM2 in encoder Mode, and I'm getting the counts with __HAL_TIM_GET_COUNTER(&htim2).

To calculate the velocity I want to use the method described in a Ben Katz blog :

"Getting good damping performance is dependent on good velocity measurement, which is not trivial. The gist of the problem is that differentiating your position signal *****. By the time you can filter the differentiated signal enough to have a nice looking velocity, the phase lag caused by the filter ruins your signal. Fortunately, the MA700 sensor I'm using for position sensing has an encoder output as well as an absolute position output. I'm able to use the encoder output to my advantage for velocity measurement. By configuring one timer on the STM32F4 in encoder mode to keep track of encoder count, and then configuring another free-running timer as a slave which resets whenever the count changes, I'm able to automatically capture the time between encoder edges. This gives a value inversely proportional to the motor speed, which gives good results for velocity measurement when combined with some logic to handle the zero-speed case where the count isn't changing."

I don't really know how to set this other timer and to trigger it when my counter changes. Could someone help me to set this up please ?

Ty in advance.

2 REPLIES 2

Set one of the two channels of TIM2 as TRGO in TIM2_CR2, and choose a timer which can be slave to it, see TIMx_SMCR, set properly the TS field, and one one channel of the slave timer, set input capture in TIMx_CCMRx so that the input signal comes from TRGI. The quote then suggests to set also TIMx_SMCR.SMS to Reset, so that the captured value directly represents time between two successive riding edges.

JW

Ty for your response.

Is there a way of setting those in STM32CubeIDE ?

This is my TIM2 config so far:

0693W000008wfZUQAY.jpgSo I guess I need to set Trigger output -> master/slave mode to enable?

What about the other settings ?