MATLAB: How count pulse when go up in simulink

discrete timeembedded systemsimulink

How count pulse when go up in simulink, to obtaint the speed of the motor throught a encoder that emits 50 pulses for revolution. This is in discrete time 0.001. I delay the pulse and compare the pulse with the delay, but neither not count good and reset the counter a zero. Besides i do this through embedded function that this, where u is the pulse, u_r is the pulse delay, t is a clock, an y_r is the feedback of y. I need help, please.
function [y] = fcn(u,u_r,t,y_r) %#eml
persistent count; persistent tiempo;
if isempty(count) count=0; end
if isempty(tiempo) tiempo=0; end
y=y_r; % Realimentacion de velocidad
if count==0 % Si contactor esta cero, ponemos en marcha el tiempo tiempo=t; end if u==1 && u_r==0 % Si detecta flanco positivo, aumenta el contador count=count+1; end if count==50; % suponemos da 50 pulsos por vuelta, % Si llega a 50 pulsos, calculamos tiempo en dar una vuelta y=t-tiempo; count=0; % reseteamos contador end

Best Answer

One potential method for counting pulses is to use the Detect Increase or Detect Rising Edge block to detect the rising edge of each pulse. If you set the output of either of these blocks to be an integer rather than a boolean, you can numerically sum the pulses via a Sum block. To keep a running total, you can use the Memory block.
Here are some links to some useful documentation: