I want to simulate a controller for a mass-spring model which works based on energy:
model model
//constants
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 erorr message:
Translation Error
Error occurred while flattening model
I would appreciate 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.