Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clock that goes from 0 to 720 and start again (SIMULINK)

1,437 views
Skip to first unread message

Matt

unread,
Apr 25, 2012, 3:12:07 PM4/25/12
to
Hello !

Under simulink I need to build a clock which does the following.

It goes linearly from 0 to 720 and when reaching, it gets the value 0 and start again. How can I build this with the blocks in SIMULINK ? I have really no idea.

If a function "reset clock" exist, that would be great, otherwise I have no idea.

Thanks in advance for your help ! :-)

Matt.

Steven_Lord

unread,
Apr 25, 2012, 3:27:35 PM4/25/12
to


"Matt " <mathieu...@gmail.com> wrote in message
news:jn9ia7$32t$1...@newscl01ah.mathworks.com...
Take a look at the Math Function block's Mod option.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Matt

unread,
Apr 25, 2012, 3:47:07 PM4/25/12
to
Thank you for your answer !

Following your advice, I came across this thread :

http://www.mathworks.com/matlabcentral/newsreader/view_thread/44275

Problem is that I need to have ratio set at 1 and by doing this, there is a divide by 0 problem so I cannot use that function. Triangular wave can be built using Taylor Dev' but I don't know how can I put that in a Simulink file !

Surely someone already had to do such thing :-)

Matt.

Steven_Lord

unread,
Apr 25, 2012, 5:20:27 PM4/25/12
to


"Matt " <mathieu...@gmail.com> wrote in message
news:jn9kbr$cgp$1...@newscl01ah.mathworks.com...
Try these in MATLAB:

x1 = 715:725;
[x1; mod(x1, 721)]

x2 = 719:(1/8):721;
[x2; mod(x2, 720)]

You can mimic this code using the block I mentioned.

Matt

unread,
Apr 25, 2012, 5:35:17 PM4/25/12
to
Yes it is exactly what I have to do, now I'm gonna figure out how to use this with SIMULINK blocks ! :-)

Phil Goddard

unread,
Apr 25, 2012, 7:02:39 PM4/25/12
to
This is what the Limited Counter block does:

http://www.mathworks.com/help/toolbox/simulink/slref/counterlimited.html

Phil.

Matt

unread,
Apr 26, 2012, 8:24:12 AM4/26/12
to
Hmmm I didn't find something useful in there I'm afraid. But I did experiments and using the following SIMULINK blocks :

http://img208.imageshack.us/img208/9899/block1k.jpg

I'm getting the following result :

http://img191.imageshack.us/img191/1050/block2d.jpg

My questions now are :

1) How can I make the vertical drop from 720 to 0 a REAL vertical line ?
2) How can I choose when the drop happens ?

Thanks a lot in advance ! :-)

Matt.

Phil Goddard

unread,
Apr 26, 2012, 10:55:12 AM4/26/12
to
The limited counter gives discrete steps.

If you want a ramp then use the following:

1. An Integrator, set to Show State Port, and be resettable at an external rising edge.
2. A Constant of 1 fed into the integrator input.
3. A Compare To Constant set to > 720, with a boolean output, and zero crossing detection on.
4. Feed the output of the integrator stateport (the one coming out of the top of the integrator) into the input of the Compare To Constant.
5. Feed the output of Compare To Constant into the integrator's reset port.

The output of the integrator is then what you want.

Phil.

Matt

unread,
Apr 26, 2012, 11:34:13 AM4/26/12
to
Fantastic Phil this is exactly what I need ! Do you know how can I change the frequency of the signal (aka being able to chose when it goes to 0 rather than having it set to 720) ? :-)

Phil Goddard

unread,
Apr 26, 2012, 1:10:12 PM4/26/12
to
The ramp/integrator will reset anytime the reset signal rises.
You can use any logic you want to create that signal.

Comparing to a constant (as with 720 in this case) is only one of an infinite number of possibilities.

Phil.

Matt

unread,
Apr 26, 2012, 3:04:30 PM4/26/12
to
Thank you Phil I got it now ! Many thanks ! :-D

Matt

unread,
Apr 27, 2012, 4:46:07 AM4/27/12
to
Sorry for bringing this back up but following the problem of the timer which has now been solved, I'm facing another problem related to this issue.

I'm using a lookup dynamic table to simulate a camshaft, but the output values are not correct. Here is the schema :

http://img708.imageshack.us/img708/3840/aacw.jpg

I'm resetting the signal every 360 (which is 360°), and the xdat and ydat values are :

xdat (camshaft angle) : [0 50 100 200 250 360]
ydat (valve lift) : [0 0 4 4 0 0]

But as you can see below, the output is not correct, it seems there is an offset :

http://img441.imageshack.us/img441/5050/aac2.jpg

I don't know what's the problem, any help would be welcome ! :-)

Phil Goddard

unread,
Apr 27, 2012, 9:35:21 AM4/27/12
to

I can't get a model that looks like this,

> http://img708.imageshack.us/img708/3840/aacw.jpg

to give anything other than the expected x-y plot (i.e. rising from 0 at x=50 and falling to 0 at x=250).

What changes have you made to the solver, any sample rates, and the stop time of the model?
What's the value of the conversion constant?

Phil.

Matt

unread,
Apr 27, 2012, 10:49:07 AM4/27/12
to
Indeed Phil you were right, I had set a timestep and it was too big compared to the simulation time ! Now everything is sloved, I thank you again once again for your interest in my case ! :-)

hoangpham8182

unread,
May 18, 2012, 4:27:27 AM5/18/12
to
On Apr 27, 4:49 pm, "Matt " <mathieu.hor...@gmail.com> wrote:
> Indeed Phil you were right, I had set a timestep and it was too big compared to the simulation time ! Now everything is sloved, I thank you again once again for your interest in my case ! :-)

Many thanks for Matt and Phil for discussing this topic. I found
something very useful for my project.
Hoang.

Matt

unread,
Jun 13, 2012, 3:17:10 AM6/13/12
to
My problem isn't totally solved. I had a closer look at the block system I did, following Phil's advice, and I must say it isn't exactly doing what it should be. Here is the the block schema :

http://img339.imageshack.us/img339/5820/7201d.jpg

Normally the scope should show the signal going up to 720, down to 0, and up again. Problem is that it doesn't go up to 720 :

http://img684.imageshack.us/img684/3784/7202k.jpg

Could anybody let me know what the problem is ? Also if there is another solution than using integrator to do this job, I would be really happy because I'm afraid the integration method may compromise some operation I may have to do later on.

Thanks a lot ! :-)

Matt.

Matt

unread,
Jun 13, 2012, 4:29:07 PM6/13/12
to
"Matt" wrote in message <jr9epm$b4d$1...@newscl01ah.mathworks.com>...
I know it has been only 1 day, but I am stuck in this project, so any help is warmly welcome ! :-)

Phil Goddard

unread,
Jun 13, 2012, 11:26:07 PM6/13/12
to
Creating the given model from scratch gives without changing any default settings gives a hit at exactly 720.

What solver are you using?
Have you made any changes to any of the block default settings?

What sort of issues are you expecting the integrator to give you in other arts of your model?

Phil.

Matt

unread,
Jun 14, 2012, 3:07:07 PM6/14/12
to
"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <jrblkf$9ki$1...@newscl01ah.mathworks.com>...
I found that the issue was a too big time step (despite time step being already set to 0.0001 !). But reducing the time step size (using "ode3" solver) is increasing the simulation time by quite some margin. Is there another way to achieve this 0-720-0-720... function without the use of integrator ? :-)

Phil Goddard

unread,
Jun 17, 2012, 12:06:07 AM6/17/12
to
This is a solver selection issue -- with a fixed step solver (ode3 for instance) you are never going to get a vertical line.
The peak will be one time period before 720 would nominally be reached and this will slope (very steeply) down to 0 at the time where you are wanting 720 to be reached.

It doesn't matter how you implement it -- using an integrator or any other way -- you cannot get around that that is the way a fixed step solver works.

On the other hand, with a variable step solver you will get a time step epsilon before the time and another epsilon after the time giving the impression of a vertical line.

Phil.
0 new messages