I want to design a counter when each time my input transition from 0 to 1 and after 5 such transitions within 5 seconds my output goes to 1

11 Ansichten (letzte 30 Tage)
I want to design a counter when each time my input transition from 0 to 1 and after 5 such transitions within 5 seconds my output goes to 1 but we should not count the time when the state is at 1 or 0
Can you help me with this logic either in simulink or using stateflow or both mixed

Antworten (1)

Ayush
Ayush am 19 Jul. 2024
Hey Vignesh,
I understand that you are trying to design a counter that increments each time your input transitions from 0 to 1. If there are 5 such transitions within a 5-second window, the output should be set to 1. The timer should only count the time when the input is neither 0 nor 1 continuously.
Here's a workaround using Simulink and stateflow:
Simulink Setup
  1. Input Signal: Use a Pulse Generator or Constant block to simulate the input signal.
  2. Stateflow Chart: Create a Stateflow chart to handle transition detection, counting, and timing logic.
Stateflow Chart Design
States:
  • Idle: Waiting for the first transition.
  • Counting: Counting transitions within 5 seconds.
  • Output: Setting the output to 1 if the condition is met.
Transitions:
  • Idle to Counting: On detecting the first 0 to 1 transition.
  • Counting to Idle: If 5 seconds pass without 5 transitions.
  • Counting to Output: If 5 transitions are detected within 5 seconds.
Stateflow chart
state Idle {
on entry: count = 0; timer = 0;
if (input == 1) {
transition to Counting;
}
}
state Counting {
on entry: timer = 0;
during: timer = timer + dt;
if (input == 1 && prev_input == 0) {
count = count + 1;
}
if (count >= 5) {
transition to Output;
}
if (timer >= 5) {
transition to Idle;
}
}
state Output {
on entry: output = 1;
on exit: output = 0;
transition to Idle;
}
Simulink Integration
  1. Input: Connect the input signal to the Stateflow chart.
  2. Output: Connect the output of the Stateflow chart to an Outport block.
This is how the complete flow will look like:
[Input Signal] --> [Stateflow Chart] --> [Output Signal]
Please refer to the following MathWorks documentation link(s):
  1. https://www.mathworks.com/help/stateflow/ug/detecting-edges-in-signals.html
  2. https://www.mathworks.com/help/stateflow/ug/modeling-a-simple-system-with-stateflow.html
  3. https://www.mathworks.com/help/stateflow/ug/using-the-after-operator.html (for using timers in Stateflow)
Hope this helps
Regards

Kategorien

Mehr zu Complex Logic finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by