Help - Search - Members - Calendar
Full Version: how to program an event at an specific time?
Modelica Forum > Modelica > Modelica
Leonardo Laguna
Hi! I'm trying to make some models for pulse width modulators but I'm having problems to describe it in modelica.

My background is in VHDL-AMS and many times I use the instruction "wait for", for example when making testbenches:

loop
wait for 1ms;
clock<=not clock;
end loop;

this produces a clock signal of 2ms of period.

How can I make this in Modelica????

other problem that I have is in code like this:

loop
clock<=not clock;
delay<=calculate_delay();
wait for delay;
end loop;

with this I get a clock signal with variable period.



At the end I only need to program events at an specified time, but I don't want to use something like:

if time>delay then...

because I thing that this slows down the simulation while finding the exact point when time=delay. am I right???

Thanks



liuliu
hi, i have tried to write two models in Dymola to anwer your first two questions. They are included in the attached package file. if you do not use dymola, you can also read the source code.

Further , i have no idea about your last question, since i use exact the same way (time>delay) to define a time event. and i think it is the only way to define a time event.


regards

liu

Leonardo Laguna
QUOTE(liuliu @ May 10 2007, 01:55 PM) *

hi, i have tried to write two models in Dymola to anwer your first two questions. They are included in the attached package file. if you do not use dymola, you can also read the source code.

Further , i have no idea about your last question, since i use exact the same way (time>delay) to define a time event. and i think it is the only way to define a time event.
regards

liu


Hi! thanks for the help.

I'm using the trial version of MathModelica and I couldn't try the code you sent me, but I got the main idea.

Instead I use this code for a simple PWM based on the Pulse block of modelica.

block PWM
parameter Real frequency=10000;
parameter Real phase=0;
Modelica.Blocks.Interfaces.RealInput d;
Modelica.Blocks.Interfaces.BooleanOutput o1;
Modelica.Blocks.Interfaces.BooleanOutput o1_n;
protected
Modelica.SIunits.Time T0;
Modelica.SIunits.Time T_width;

equation
T_width=1/frequency*d;

when sample(phase/frequency/360, 1/frequency) then
T0=time;
end when;

o1=if time >= T0 + T_width then true else false;
o1_n=not o1;

end PWM;



but I didn't get the results that I was especting, because the comparison time >= T0 + T_width seems to occur at any time and the output signal is a mess.


does Dymola has a precise control, because I need a really high precition?


thank you
liuliu
Again i can only give you some modified code for Dymola, you can get the principle idea,

the error you have made is that you calculate T_width in every simulation step but T0 only at sampled event. so you got incorrect output. i have tried the following code,



parameter Real frequency=5000;
parameter Real phase=0;
constant Real pi=Modelica.Constants.pi;

Modelica.SIunits.Time T0;
Modelica.SIunits.Time T_width;

Real d;
output Boolean o1;
output Boolean o1_n;
equation
d=Modelica.Math.sin(2*pi*100*time)/2+0.5;

when sample(phase/frequency/360, 1/frequency) then
T0=time;
T_width=1/frequency*d;
end when;

o1=if time >= T0 + T_width then true else false;
o1_n=not o1;



as you see, the T_width will also be calculated at the sample event. The result is given in the attached file.

Regards

liu


Click to view attachment
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.