Hold True Value for finite length of time

96 views (last 30 days)
I am fairly new to matlab and have been getting to grips with it over the past month so excuse me if I missed something very obvious.
I am using a relational operator that will output a true value (1) when the input single goes beyond a certain range. What I require is that when the input single goes beyond this certain range and the output single becomes true, to hold this true value (e.g. the constant one) for a period of time e.g. 10 seconds.
I have been trying to use enabled subsystems, switches and zero hold blocks but with little success.
Any help would be much appreciated.
Modeling using Simulink
  1 Comment
Kaustubha Govind
Kaustubha Govind on 11 Jul 2011
You may want to specify that you are modeling in Simulink. :)

Sign in to comment.

Accepted Answer

Doug Eastman
Doug Eastman on 11 Jul 2011
The easiest solution is using Stateflow: create two states off and on, transition from off to on when the signal is tripped, then return to off using 'after(10,sec)'.
In Simulink it's a little more complicated, but basically you can have an enabled subsystem with a constant one going into an integrator going into a compare to constant block set to less than or equal to -1. That will stay on for 10 seconds once the enable port is on. Then the trick is to latch the enable signal so it will stay on as long as necessary. You can use a relay block for that, but you'll need to feed the output of the enabled subsystem back and add it to the initial signal in order to reset the relay after 10 seconds have passed.
Or use a MATLAB Function block set to a discrete sample time of 0.1 with the following code:
function y = fcn(u)
%#codegen
persistent tick started
if isempty(tick)
tick = 0;
started = 0;
end
y=0;
if u == 1
started = 1;
end
if started
if tick<100
tick=tick+1;
y = 1;
else
tick = 0;
started = 0;
end
end
  3 Comments
Dan Willans
Dan Willans on 11 Feb 2015
Hi there,
Are you able to put up an image of how you got this to work? I'm trying to get my simulink model to hold a constant 1 for 20 seconds once a timer has reached the value 10.
I can't seem to work it out!
Thank you!
Dan
Peter Ma
Peter Ma on 27 Sep 2018
Edited: Peter Ma on 27 Sep 2018
I totally agree with what Doug Eastman said.
By using his second method, I have made a block that can convert 'the length of time (s)' to 'pulse' with corresponding time.
inside subsystem:
The output looks like:
Be careful that the initial value of 'Sample and Hold' block should be a very small value, rather than zero. Otherwise, there will be an error for the 'Divide' block. (1 divided by 0 is infinity)

Sign in to comment.

More Answers (3)

Sean de Wolski
Sean de Wolski on 11 Jul 2011
doc pause
or
doc timer
  1 Comment
Owen
Owen on 11 Jul 2011
I have been reading through the timer and pause documentation but have only managed to confuse myself more than before.
I am guessing that I should be looking for to make a timer that starts every time a function is called in simulink.
Once the timer is started it will be compared to a setpoint time (e.g. 10 seconds)using a relational operator and once it passes 10 seconds the relational operator will output a false signal?

Sign in to comment.


Kaustubha Govind
Kaustubha Govind on 11 Jul 2011
An important thing to keep in mind when modeling in Simulink is that blocks run according to their sample time (see this blog post for information how you can look at your block's sample time). So if you are using a zero-order hold block, it will only hold the input for one sample period - you need to sample the sample time of the zero-order hold block to 10 seconds to achieve what you need.
  3 Comments
Kaustubha Govind
Kaustubha Govind on 11 Jul 2011
Owen: It's not clear if you're saying whether this answer resolves your question or not. If it does, please accept it. If not, please elaborate on how it is not sufficient.
Owen
Owen on 11 Jul 2011
Sorry about the lack of clarity.
As I said before I have a rational operator that will output a true signal when an input signal goes beyond a certain range and is false otherwise. The nature of this input signal is that it usually only outside this range for about 5 seconds.
This true value is then used to enable a 'Enabled Subsystem'. This Enabled Subsystem compromises of a PI controller and requires the subsystem to be enabled for at least 10 seconds before it reaches 'normal operating conditions' and can pass control back to the main part of the system.
The problem is that the true signal from the rational operator that is used to operate the 'Enabled Subsystem' only lasts for 5 seconds and I need it to last for 10 seconds. After these 10 seconds, the true signal (1) into the 'Enabled Subsystem' should return to a false signal (zero), so that the 'ES' is ready for the next time the system goes out of range.
I hope the problem is a bit more clear this time and would like to thank you for the response thus far. I have already spent quite some time on this problem with no progress.

Sign in to comment.


Jie
Jie on 18 Jan 2017
Hi, I think I solved the problem in simulink. Please see the picture below. The 'Discrete-Time Integrator' Gain value should set to 1.
  1 Comment
Vijay
Vijay on 1 Aug 2018
I want to hold the newest peak (the last peak – input changing from 0 to 1) for an hour. I used the zero order hold Block for that purpose (sample time 3600s). But the problem with zero order hold block is, it sometimes omits the peak and I am not getting why it does that.
As you see in the attached picture, the blue line is the held curve and the red one is the input. One can see that, in the places where I marked wrong, the peak is not held as wanted it to.
Anybody has experience with this matter?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!