COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

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

• Email
• Follow

```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.

Matt.
```
 0
Reply mathieu.horsky (76) 4/25/2012 7:12:07 PM

See related articles to this posting

```
"Matt " <mathieu.horsky@gmail.com> wrote in message
news:jn9ia7\$32t\$1@newscl01ah.mathworks.com...
> 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.
>

Take a look at the Math Function block's Mod option.

--
Steve Lord
slord@mathworks.com
http://www.mathworks.com

```
 0
Reply slord (13686) 4/25/2012 7:27:35 PM

```Thank you for your answer !

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 !

Matt.
```
 0
Reply mathieu.horsky (76) 4/25/2012 7:47:07 PM

```
"Matt " <mathieu.horsky@gmail.com> wrote in message
news:jn9kbr\$cgp\$1@newscl01ah.mathworks.com...
>
>
>
> 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
>

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.

--
Steve Lord
slord@mathworks.com
http://www.mathworks.com

```
 0
Reply slord (13686) 4/25/2012 9:20:27 PM

```Yes it is exactly what I have to do, now I'm gonna figure out how to use this with SIMULINK blocks ! :-)
```
 0
Reply mathieu.horsky (76) 4/25/2012 9:35:17 PM

```This is what the Limited Counter block does:

Phil.
```
 0
Reply phil986 (331) 4/25/2012 11:02:39 PM

```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.
```
 0
Reply mathieu.horsky (76) 4/26/2012 12:24:12 PM

```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.
```
 0
Reply phil986 (331) 4/26/2012 2:55:12 PM

```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) ? :-)
```
 0
Reply mathieu.horsky (76) 4/26/2012 3:34:13 PM

```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.
```
 0
Reply phil986 (331) 4/26/2012 5:10:12 PM

```Thank you Phil I got it now ! Many thanks ! :-D
```
 0
Reply mathieu.horsky (76) 4/26/2012 7:04:30 PM

```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 ! :-)
```
 0
Reply mathieu.horsky (76) 4/27/2012 8:46:07 AM

```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.
```
 0
Reply phil986 (331) 4/27/2012 1:35:21 PM

```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 ! :-)
```
 0
Reply mathieu.horsky (76) 4/27/2012 2:49:07 PM

```On Apr 27, 4:49=A0pm, "Matt " <mathieu.hor...@gmail.com> wrote:
> Indeed Phil you were right, I had set a timestep and it was too big compa=
red to the simulation time ! Now everything is sloved, I thank you again on=
ce 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.
```
 0

```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.
```
 0
Reply mathieu.horsky (76) 6/13/2012 7:17:10 AM

```"Matt" wrote in message <jr9epm\$b4d\$1@newscl01ah.mathworks.com>...
> 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.

I know it has been only 1 day, but I am stuck in this project, so any help is warmly welcome ! :-)
```
 0
Reply mathieu.horsky (76) 6/13/2012 8:29:07 PM

```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.
```
 0
Reply phil986 (331) 6/14/2012 3:26:07 AM

```"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <jrblkf\$9ki\$1@newscl01ah.mathworks.com>...
> 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.

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 ? :-)
```
 0
Reply mathieu.horsky (76) 6/14/2012 7:07:07 PM

```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
Reply phil986 (331) 6/17/2012 4:06:07 AM

19 Replies
48 Views

Similar Articles

12/6/2013 1:52:38 AM
[PageSpeed]