# Counter for Sawtooth signal in Simulink

7 views (last 30 days)
s0ter48 on 3 May 2021
Answered: s0ter48 on 8 May 2021
Hello everybody,
I'm trying to set up a round counter for my model. The round counter should count how many revolutions (360 ° or 6.2832rad) my motor has turned.
The signal is a sawtooth and works discretely (50us).
Example of a rising sawtoothes:
With a rising sawtooth ++ (increment) and with a falling - - (decrement).
But I didn't get any further with normal counting.
I think the discrete signal makes it a little more difficult.
I Added an example, but It is missing a sawtooth. But I don´t see where the mistake is.

Andy Bartlett on 4 May 2021
Look at the edge detection blocks and the discrete integrator in accumulator mode.
You can also look under the mask of the edge detection blocks to see the underlying design pattern.
Instead of detecting a fall below zero, from the plot it looks like you'll need a different threshold value.
s0ter48 on 7 May 2021
But what exactly does the discrete integrator (in accumulation mode) hast to do with the counting?
I forgot to mention that, when the sawtooth gos into the other direction it should decrement the counter.

Andy Bartlett on 7 May 2021
The icon shows the Z-domain description of the Integrator block in accumulator mode (noting that K = 1)
Y(z) = ( z / (z-1) ) * U(z)
which becomes
z * Y(z) - Y(z) = z * U(z)
with some algebra becomes
Y(z) = z^-1 * Y(z) + U(z)
transforming to time domain becomes and noting that z^-1 corresponed to a 1 time step delay.
Y(i) = Y(i-1) + U(i)
So the output at time step i is computed by adding the input at time step i to the output at the previsous time step.
Initializing the first Y to zero, the block is adding up all the input values over time (also called accumlation).
If the inputs are boolean they have values 0 and 1, so the accumulator is doing a simple count of the 1's that have appeared since the simulation started.
To count both up and down feed the appropriate sequence of -1, 0, or 1 into the accumulator.
To create that sequence study what detect fall and detect rise blocks are doing, then generalize the for your situation to create boolean signals representing the wrap forward or wrap back events.

s0ter48 on 7 May 2021
I created two counters now.
counter #1 is doing what I want but some how there is a shift and he is loosing a sawtooth i think. You can see the result in the scope.
counter #2 everything is ok until the turnaround comes into play. He still increments the counter and continous.
Maybe you see something I don´t.
I attached the model.

s0ter48 on 8 May 2021
I had a small (condition) mistake... .
But it should be fixed now.
I don´t see any further mistakes or counting errors.
If you do tell me =).
You find the model in the attachments.
Best regards.

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!