Search code examples
vectormodelicadymolaopenmodelica

How to cap values in equation section in OpenModelica?


I'm developing a drone simulation in OpenModelica. In an equation block I am calculating velocity and position vectors, but I want to cap the velocity to a certain value. This is a simplified example of my drone block.

block drone

 parameter Real mass =  0.985;
 constant Real g = 9.8;
 constant Real maxSpeed = 15.0;

 Input Real Fx,Fy,Fz;

 Real x,y,z;
 Real vX,vY,vZ;

equation
 der(vX) = Fx / mass;
 der(vY) = Fy / mass;
 der(vZ) = Fz / (mass*g);

 der(x) = vX;
 der(y) = vY;
 der(z) = vZ;

end drone;

EDIT: The velocity vector in the example have to be capped only if the speed of the drone exceed the maxSpeed value


Solution

  • As you have physically correct relations between force F, acceleration der(v), speed v and positions x, I wouldn't change anything there.

    You could think about something like:

    der(vZ) = if vZ >-1 then Fz / (mass*g) else 0;
    

    which should result in something like: Velocity-limit

    But I think it would be better to add some kind of friction model, which could be something like:

     der(vZ) = (Fz-vZ*3) / (mass*g);
    

    with 3 being a coefficient for linear friction (chosen to get a nice plot). Note that the above is very rudimentary and should be refined quite a bit - the intention is just to give an idea.
    The result: Friction