0

I want to simulate a controller for a mass-spring model which works based on energy:

model model
//parameters
    parameter Real m = 1;
    parameter Real k = 1;
    parameter Real Fmax = 3;
    parameter Real x0 = 1;
    parameter Real x1 = 2;
    parameter Real t1 = 1;
    
//variables
    Real x, v, a, xy, vm;
    
initial equation
    x = x0;
    v = 2;
    
equation
    v = der(x);
    a = der(v);
    m * a + k * x = F;
    
algorithm
    vm := sign(xy - x)*sqrt(2 * (Fmax * abs(xy - x) + k * (xy^2 - x^2) / 2) / m);

    // step signal
    if time  < t1 then
        xy := x0;
    else 
        xy := x1;
    end if;
    
    if xy == x then
        F := k * x;
    else
        F := sign(vm - v) * Fmax;
    end if;
end model;

But it leads to the error message:

Translation Error

Error occurred while flattening model

I would appreciate it if you could help me know what is the problem and how I can fix it.

P.S.1. SIMULINK is also not able to finish!

P.S.2. New version of the code can be seen here.

P.S.3. According to this discussion on Discord, the algorithm section was not really meant for casual relations. More information about the keyword is here.

marco
  • 5,944
  • 10
  • 22
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • 1
    Seems you forgot to define F, if I add it to the `//variables` the code seems to work in Dymola. As a side note: `xy==y` is recommended to be done with some tolerance. Also it could be dangerous to give the model the name `model`... – Markus A. Feb 20 '19 at 10:08
  • @MarkusA. ah what a silly mistake! Thanks for pointing it out. what do you mean by tolerance? how can I do it? – Foad S. Farimani Feb 20 '19 at 10:10
  • Now I get the warning `Translation Warning In component , in relation xy == x, == on Real numbers is only allowed inside functions.` and it is stuck on 18% ! – Foad S. Farimani Feb 20 '19 at 10:12
  • 1
    Replacing `if xy == x then` with `if abs(xy - x) < 1e-6 then` should do the job you intended. Of course you can adjust the `1e-6` to your needs. One document I found after quick research is https://learn.microsoft.com/en-us/dotnet/api/system.double.equals?view=netframework-4.7.2. This should explain the problem pretty well (I didn't fully read it though)... – Markus A. Feb 20 '19 at 10:16
  • @MarkusA. thanks. funny enough I had asked the same question ages ago [here](https://openmodelica.org/forum/default-topic/2263-openmodelica-translation-warning) :) . your suggestions resolves the warning but the simulations is still stuck on 17% . any idea why is this happening and how I can resolve it? – Foad S. Farimani Feb 20 '19 at 10:19
  • 1
    Simulation is stuck at around 1.8s for me. Seems to happen when `v` and `vm` have similar values. Difficult to tell without fully understanding the model (which is not exactly straightforward just looking at the equations)... – Markus A. Feb 20 '19 at 10:27
  • @MarkusA. `vm` is the maximum velocity for the mass which the kinetic energy can be absorbed by the spring and external force before reaching the `xy` target. basically the controller checks the kinetic energy, if it is less than what is required to get to the point as fast the system can go, it pours energy, otherwise if it is too much it decelerates to reach that maximum velocity. – Foad S. Farimani Feb 20 '19 at 10:33
  • 1
    The model seems to be a bit dubious from a numerical point of view. This basically makes step-size-controlled solvers stop as they cannot fulfill their local error criteria (depending on what tolerance you allow). I assume this results from combining an equation and an algorithm part. As a workaround: For me changing the solver to Forward Euler with a step-size of 1ms made the model run. Still this is not a good solution and should just be a first step for improving the model - which I leave up to you :) One way to go could be using a clocked controller... – Markus A. Feb 20 '19 at 10:44
  • @MarkusA. could you be kind to translate these Dymola parameters to OpenModelica GUI? is it 1. `Simulation Interval > Interval > 0.01` 2. `Integration > euler` – Foad S. Farimani Feb 20 '19 at 10:54
  • 1
    Should mostly be correct, but use an interval of 1e-3 = 0.001. Integration method Euler should be fine (Euler = Forward Euler) – Markus A. Feb 20 '19 at 11:02
  • @MarkusA. now it finishes but [the result](https://i.imgur.com/Z2k8PXk.png) doesn't seem right! – Foad S. Farimani Feb 20 '19 at 11:10
  • 2
    The model doesn't look right. You have m*a+k*x=F and then in the if-statement you have F=k*x countering part of that. The usual approach would be to subtract "k*x" from F - alternatively have two terms in F, and add them together. – Hans Olsson Feb 20 '19 at 12:19
  • 1
    Additionally it seems that x0 and x1 are some form of stops. So you shouldn't use abs(x-xy)<1e-6 but xx1. – Hans Olsson Feb 20 '19 at 12:20
  • @HansOlsson the model is wrong. I'm trying to make a new version [here](https://gist.github.com/Foadsf/5e4b998f5b99266eb031fbaadcc16a9c). basically if the total energy of the system is bigger than what it should be the external force dissipates energy, otherwise it pumps into the system. but so far it does not work. – Foad S. Farimani Feb 20 '19 at 12:25
  • @MarkusA. and Hans thanks a lot. I tested the code on another computer and it just works! you may see the final version [here](https://gist.github.com/Foadsf/5e4b998f5b99266eb031fbaadcc16a9c). The advantage of this controller over PID is that it is faster and has less to no overshoots. disadvantage, it is computationally expensive, requires a model and velocity feedback. your thoughts and comments are highly appreciated. Nest step is to add friction and damper. – Foad S. Farimani Feb 20 '19 at 21:02
  • For some reason when I introduce [friction](https://gist.githubusercontent.com/Foadsf/5e4b998f5b99266eb031fbaadcc16a9c/raw/b366097be86804d8dfd6ba5f96d9f0a1a5ae2b4c/Model2.mo) the system becomes [unstable](https://i.imgur.com/w5LiL1S.png)! – Foad S. Farimani Feb 20 '19 at 23:07
  • I wrote about this idea [here on Reddit](https://www.reddit.com/r/ControlTheory/comments/asvr4e/an_energy_based_controller_with_minimum_overshoot/) – Foad S. Farimani Feb 21 '19 at 08:16
  • I explained the mathematical back ground of this idea in more details [here](https://physics.stackexchange.com/questions/462268/an-energy-model-based-controller-to-minimize-overshoot-and-response-time-of-a-ma) – Foad S. Farimani Feb 22 '19 at 13:46

0 Answers0