Message Boards Message Boards


Switch between two flanges while using the MultiBody library in SM?

Posted 1 year ago
12 Replies
5 Total Likes

I am currently working with multibody mechanical systems using the MultiBody library in SystemModeler.

Trying to implement a switch between flanges but can't seem to make it work:

model FlangeSwitch "Switch between flanges"
  input Modelica.Mechanics.Translational.Interfaces.Flange_a flanges_a[2];
  input Modelica.Mechanics.Translational.Interfaces.Flange_b flanges_b[2];
  output Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a_exit;
  output Modelica.Mechanics.Translational.Interfaces.Flange_b flange_b_exit;
  input Modelica.Blocks.Interfaces.BooleanInput u;
  Integer k;
initial equation
  k = 1;
  flange_a_exit = flanges_a[k];
  flange_b_exit = flanges_b[k];
  when u then
    k = 1;
  elsewhen not u then
    k = 2;
  end when;
end FlangeSwitch;

The model cannot pass verification check. The following error comes up:

[:0:0-0:0] Error: Model is using the variable flanges_b which does not have a declaration.
[:0:0-0:0] Error: Model is using the variable flanges_a which does not have a declaration.

Any clue on what's going on?

12 Replies
Posted 1 year ago

What I am trying to achieve is the following:

In a multibody system, there is a prismatic joint that I want to control in two different ways. The first part is to move the joint from the current position to an exact position, using the Modelica.Mechanics.Translational.Sources.Position component. Once this is done, I would like to switch the attached flange to connect a Modelica.Mechanics.Translational.Sources.Force component to apply a force to this joint. In a nutshell, I would like to force a displacement on the joint and then leave the joint without restriction for a free movement under external forces (gravity and other applied forces).

Is this possible to do in Modelica?

Posted 1 year ago

A simpler approach was also tested:

model FlangeSwitch "Switch between flanges"
  Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a_1;
  Modelica.Mechanics.Translational.Interfaces.Flange_b flange_b_1;
  Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a_2;
  Modelica.Mechanics.Translational.Interfaces.Flange_b flange_b_2;
  Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a_exit;
  Modelica.Mechanics.Translational.Interfaces.Flange_b flange_b_exit;
  Modelica.Blocks.Interfaces.BooleanInput u;
  if u then
    flange_a_exit = flange_a_2;
    flange_b_exit = flange_b_2;
    flange_a_exit = flange_a_1;
    flange_b_exit = flange_b_1;
  end if;
end FlangeSwitch;

This way the system is not balanced: 10 equations and 12 variables.


This should be possible.

When you say that you want position enforced followed by a force constraint, is your position constraint a limit? For example, you can easily model a chair on the floor. The floor limits position but forces and gravity can cause it to leave the floor. Is this the type of constraint you want? If so, I can show you an example. If not, Please give a bit more detail for me.


Posted 1 year ago

Hi Neil,

In a simplified way, this is what I am trying to achieve:

I have body connected to the ground with a prismatic joint (with the axis flange enabled) and I would like to control the movement of that joint.

At first, I would like to force the movement of the joint (connecting Modelica.Mechanics.Translational.Sources.Position to the joint's axis flange) to move from X1 to X2. Once this is done, I would like to free the body from this position constraint and move it around using a force (using Modelica.Mechanics.Translational.Sources.Force).


Is the move from X1 to X2 based on time -- for example for the first 5 seconds you use position control moving form X1 to X2 and hold it there and then the object is free to move after 5 seconds in either direction (away from X1 and/or towards X1)?

I can show you how to write a custom constraint to add onto the prismatic joint for this case .

However you can use the built in elastigap if the movement is more like this:

You want more of a cam-like motion -- it moves from X1 to X2 but at any point in time a force can pull it away from the position constraint (moves from X2 in a direction which is away from X1)? This is more like a hard, moving stop but the object is free to move away from the hard stop if the forces dictate that motion. This can be done entirely with built-in components.

The problem with your Flange switch approach is that you must put constraints on the unused flanges otherwise you get a mismatch between equations and variables. Even though you use flange a1 and b1, you must do something with a2 and b2. You can cut them into a separate, meaningless systems I think I could get this to work with some experimentation but I think there are easier and more general approaches to do what you want.

Based on your answer above, I can suggest an approach.


Also note in my second example that if you use the elastogap to do the moving stop, you can move it from X1 to X2, hold it there and then quickly move it out of the way at a given time. This is another way to implement what you want. Would this behavior work? (so there are 3 approaches!)


To out line the various approaches mentioned above, look at this post: dropping a bar onto a surface. Here I used elastogap to simulate the surface. You can move the surface as your position constraint and let the forces act on the body. The key to this is turning on the useAxisFlange in the prismatic constraint. This allows you to connect a constraint to the prismatic joint. In the case referenced I used elastogap but you could use a spring or write a custom constraint that changes based on an external input. If you do the custom constraint you may have a numerical instability problem when you suddenly switch behaviors in the middle of the simulation. This discontinuity (depending on what you are doing) could cause the integration to slow down to a crawl or fail completely. To get around this you will likely have to add some very stiff flexibility to your position constraint. Elastogap code is a good example of this -- you can examine it and see that they needed to add a very stiff spring so that impacts against the hard stop would not destabilize the integration.

I hope this gets you started.



Posted 1 year ago

Is the move from X1 to X2 based on time -- for example for the first 5 seconds you use position control moving form X1 to X2 and hold it there and then the object is free to move after 5 seconds in either direction (away from X1 and/or towards X1)?

This is exactly the motion that is expected from the model.

The first few seconds of the movement correspond to a forced controlled movement, its a kind of "move to initial position" motion. Once this position is reached, the object is "free to move" and actually it is a dynamic control which kicks in in order to maintain equilibrium (the model is similar to an inverted pendulum). I don't really see how the elastigap approach could help me.



*(I am using Jose's account, but I am not him).

William (the Engineer formerly known as Jose),

The elastogap is a useful component because it lets you model a fixed or moving wall with intermittent contact.

I took the standard pendulum example from SystemModeler and added a moving wall. The combiTimeTable lets me input a time varying position of the wall. In this example, it moves to the right pushing the pendulum up for the first 3 seconds and then holds the position until 5 seconds, then it releases the pendulum by suddenly moving out of the way. I animated the prismatic movement of the wall with a big green arrow so you can see it move. This simulation uses all built-in components -- no need to write any custom code.

The model looks like this at 3 seconds: before the pendulum is released.

enter image description here

Here is the animation:

enter image description here

The model looks like this:

enter image description here

To run this model you must disable "dynamic state selection" because we have closed loops in the model (because of our "moving wall")

Go into preferences:

enter image description here

Why does this work?:

The CombiTimeTable outputs your position constraint over time to a translational position block. The position is attached to a prismatic joint so it moves the prismatic joint over time. The prismatic joint is attached to a planar constraint because the pendulum moves up and down so we must allow up/down sliding (y direction). The planar constraint attaches to another prismatic joint connected to an elastogap. This prismatic joint is constrained to allow discontinuous contact. you can set the spring rate and damping to get various behaviors from the elastogap -- i.e. super damped or very bouncy. Lastly, I added a Universal joint. This is more subtle but the prismatic joint ONLY allows X motion, The Planar joint gives you Y motion. However, you also need two rotations because as the pendulum swings, its angle changes so you must allow the angle of connection between the prismatic joint and the pendulum bob to rotate. (Remember -- our goal is to only constrain this bob in the x direction even though it can move up and down and rotate.

The model is attached.

I hope this helps.



*(Actually Neil was abducted by aliens so I am his evil twin...)

Also, One cool thing to try:

If you change the CombiTimeTable to move the "wall" to X=0 instead of X=-0.5 (out of the way), and change the damping in the elastogap from1000 to 10 you can see it interact dynamically with the "bouncy wall"

enter image description here

Posted 1 year ago


The elastogap is a very interesting component, but I found another to way to achieve this motion. In my opinion, this is much more appropriate for this application and avoids the additional closed loop.

enter image description here

The trick is to use an additional dummy body with the same joint as the one that we are trying to move and apply the desired motion. A torque (or force if it is a translational joint) proportional to the relative position between these two bodies will keep them together.

The result is the same as using the elastogap feature and once the driven motion is not needed, a conditional expression in the definition of the magnitude of the torque is enough to set both bodies independent.

enter image description here

This dummy body could also be used in further simulation to drive the body for a second time once the free motion sequence is over.

Best regards,




Your simulation looks good. It is a nice approach. What you essentially did was construct a (stiff) spring between a position reference and your system. The torque on the system is proportional to the displacement. You set your spring constant to 0 when your timer value is reached. I suggest deleting the dummy body -- it is unnecessary -- you create an absolute position reference and tie it to the actual system with a spring (just as eleastogap puts a spring between the absolute reference and the actual system). Elastogap does not need the mass component and neither do you. (unless you were using it for something else?)

In my post the spring in elastogap is moved out of the way at time = 5.0. In your approach you set the spring constant to 0 at time = 5.0. Both approaches will work.

If you use your approach in more instances, I suggest you make a "dynamic spring" block out of your sensor, Torque, realExpression and Product blocks. You can even add the revolute joint and position blocks to make one block that takes a position input, a "trigger time" and has two flanges -- it becomes your new "EnforceMotion" or "EnforcePosition" block and you can reuse it in other situations.

For example the Elastogap solution I posted came out of a block I was using to model an object sitting on a surface subject to accelerations so it could leave the surface at any time. By adding a position reference to what was the "fixed" surface it became a "moving wall" for this application.

For your application, I agree that your approach makes more sense since you have no intention of allowing the object to leave the position constraint early under dynamic influences.

Nice work.



Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract