How to control and limit btMultiBody Spherical joint

benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

How to control and limit btMultiBody Spherical joint

Post by benelot »

Hello,

I started from the InvertedPendulumPDControl example and tried to make it using spherical joints and some limits.

Basically I am trying to setup a simple example with a btMultiBody that controls and limits a spherical joint properly. I already have a btMultiBody with a spherical joint between two colliders. I tried to control the joint via the joint motors (did not work at all) and via the addJointTorqueMultiDof(jointindex,dof,force) but it is very hard to control it. Does anybody have an example how this is done correctly? I tried to PID control it, but it is very hard.

Also what is the output of getJointPosMultiDof(jointIndex) of the btMultiBody? I thought it was three angles, but after some test I believe it is not.

Secondly I tried to add limits to the joint in some way but failed. Can I do something like limiting one Dof to -PI/4;PI/4 (using angles here), another Dof to (0,PI) and leave the third unlimited?

I would really appreciate help on this, I am a bit lost here.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

So it is not possible yet?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

Any kind of answer about the progress of the btMultiBody and the joints including joint motors and limits would be helpful.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by Erwin Coumans »

btMultiBody joint limits and motors are only implemented for 1-DOF prismatic (slider) and revolute (hinge) joints.

At some stage I hope to find time to implement conical joint limits for prismatic joints in btMultiBody.
Also what is the output of getJointPosMultiDof(jointIndex) of the btMultiBody?
This depends on the type of joint of btMultiBody. For a prismatic joint, it is the joint position, for a revolute joint this is the joint angle, and for a spherical joint this is the joint quaternion (4 position variables, see m_posVarCount)

Until we support spherical joint limits in btMultiBody, it is better to use btRigidBody and btTwistConeConstraint, as btTwistConeConstraint support such limits.
If you want to implement spherical joint limits for btMultiBody, you could get some inspiration from btTwistConeConstraint as well.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

From what I read about Featherstone solvers, they actually do only know 1DoF joints. They simulate a 3DoF joint by replacing it with three 1DoF joints connected with zero length links. I know this has the drawback of gimbal lock, but for me that is no problem. Is it safe to use that or are there any other problems to that idea?

I can not get a stable simulation with btRigidBody and btTwistConeJoints when I randomly generate my creatures/robots (creatures are basically branching structures where at each braching point(node), there are btRigidBodies connected with joints to each other) attaching boxes and capsules together with joints (surface points with bullet rays).

I use a pretty small constant simulation timestep of 0.001. I disable collision between constrained btRigidBodies. I can even disable self collision in my creatures so that there should be no unreachable states, but I still get extremely twitching, flying, jumping creatures. I have no idea what could cause it. Is there something you know could cause such strong twitching?
I will check if double precision helps it.

I even tried to "calm" the creatures by clearing forces and velocities, but it did not help. Interesting is that even though the creatures do not have to resolve their constraints when unlimited and non-selfcolliding, they still get very high velocities quickly after seconds.

I tried to discard twitching creatures, but I also could not detect the behavior properly using something like penetration depth of boxes or applied torque to the joint etc. Any idea on how to detect that joints are not able to get into a valid configuration?
IresomPCH
Posts: 18
Joined: Tue Sep 24, 2013 12:29 pm

Re: How to control and limit btMultiBody Spherical joint

Post by IresomPCH »

Hello benelot,

I have not try the btMultiBody and FeatherStone solver...
However, for the btRigidBody and SI/MLCP solver, It seems there's several aspects you can test:

1. Check the way you setup those joints.
You may build a simple "creature" using less links and joints in a demo project (Ragdoll demo and motor demo are good reference), and see if it works properly.
Incorrect setting of joints (incorrect local transform matrix, parent-child relation, etc.) may cause the structure to be unstable.
DebugDrawer also helps to see if joints are set up validly.

2. Try bigger time steps.
When the time step is small, sometimes a tiny movement causes big impact force. Also, spring constraints become jumpy.

3. Double precision do help the convergency.
According to previous experience, using double precision helps the convergency of some structure configuration, especially loop structure.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

Hi IresomPCH,

Thank you very much for your inputs. I wrote an extended debug drawing for my creatures that show me how the joints are built. It looks pretty correct from my perspective. But it might be one special case that is not correct that sets up an invalid joint. I am still trying to figure out what an incorrect setting could be in the situation where I turn off all collisions among the limbs (not only constrained limbs, but all of them).

It surprises me that you say to use a bigger time step. To me, the small time step gives me smaller integration time steps, making force integration more precise. If you have some other experience, I am happy to try that. I just do not see the reasoning why it increases impact forces.

I will try double precision, I figured out my problem I had why I could not compile against it (Never use float exlicitly but use btScalar, I know it now).

I will write back my results as soon as I have them.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to control and limit btMultiBody Spherical joint

Post by Basroil »

IresomPCH wrote: 2. Try bigger time steps.
When the time step is small, sometimes a tiny movement causes big impact force. Also, spring constraints become jumpy.
You'll need to change ERP/CFM if you have issues with springs. There's a nice little formula on how ERP and CFM can be used to simulate springs in the ODE physics engine manual.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

Ok, I will look into that as well. Any other settings that I should consider to improve the stability of a simulation? Is there a page where all the settings are listed and described?

I am still searching for the reason why there exists a too small time step...
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to control and limit btMultiBody Spherical joint

Post by Basroil »

benelot wrote: I am still searching for the reason why there exists a too small time step...
That part is easy, the definition of ERP and CFM means that spring constants increase with decreasing time-step size (http://ode-wiki.org/wiki/index.php?titl ... RP_and_CFM). Since the sympletic euler is not necessarily stable at high gains, you can get some really strange results if you don't compensate for step size. Luckily Bullet lets you set ERP and CFM for individual constraints, so you can get the benefits of stiff joints with soft contacts if you calculate out the ERP/CFM from desired spring-damper values.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

So a too small time step is not only relevant in the case of explicit joint springs such as in the generic spring joint, but also regarding the overall "softness" of the joint constraint? That could fix MANY of the problems I was facing for weeks. So basically I should calculate the ERP and CFM values for each joint by using the formulas with k_p and k_d in it? Or should I increase the time step? Why is the compensation not done by default if it is such a serious issue that comes with small time steps?

I also believe that bullet has a general ERP and CFM as well somewhere...
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to control and limit btMultiBody Spherical joint

Post by Basroil »

benelot wrote:So a too small time step is not only relevant in the case of explicit joint springs such as in the generic spring joint, but also regarding the overall "softness" of the joint constraint? That could fix MANY of the problems I was facing for weeks.

I had the same reaction when I first discovered it :lol:
benelot wrote:So basically I should calculate the ERP and CFM values for each joint by using the formulas with k_p and k_d in it? Or should I increase the time step?
Whether to reformulate the ERP and CFM or increase timestep is something only you can decide. More or less it comes down to if you need the extra integration and contact (and friction if using MLCP) accuracy that comes with smaller timesteps or if you need performance and easier settings.
benelot wrote:Why is the compensation not done by default if it is such a serious issue that comes with small time steps?
Well, small time steps aren't default either, so it's expected that issues will arise if you don't understand the way everything is being calculated. Bullet works pretty much as expected with everything on default, but changing any settings is discouraged unless you understand the entire code base which that setting affects (Visual Studio's Intellisense is a blessing for that, though you need to do an old fashion search to make sure you catch everything)
benelot wrote: I also believe that bullet has a general ERP and CFM as well somewhere...
You can set ERP and CFM from the solver info using

Code: Select all

btContactSolverInfo& info = m_dynamicsWorld->getSolverInfo();
info.m_erp;
info.m_cfm;
Or you can also set it by individual constraints. I generally do a hybrid, where joints are stiffer than contact/friction constraints.
IresomPCH
Posts: 18
Joined: Tue Sep 24, 2013 12:29 pm

Re: How to control and limit btMultiBody Spherical joint

Post by IresomPCH »

@ Basroil
You'll need to change ERP/CFM if you have issues with springs. There's a nice little formula on how ERP and CFM can be used to simulate springs in the ODE physics engine manual.
Thanks for the instruction :lol:
Aside from springs, another situation is that contacts became much harder with small time steps than larger time steps.
Now I realize it's necessary to tune the ERP/CFM parameters according to time step.
I know codes which change the ERP/CFM of joint constraints, but how to change the contact softness?
Thanks~
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to control and limit btMultiBody Spherical joint

Post by benelot »

@ Basroil:

I have seen that you do some servo motor experiments or similar. Mine is a scientific experiment on evolutionary algorithms (you probably read that in one of my various question introductions). I can change the speed of my simulator, which done via a fixed timestep that is always used regardless of speed settings but the simulator just gives more or less processing time to the physics within the application. Thereby it can do more or less fixed time steps depending on the settings. The time step is also not substepped, because I need every step to process it within the joint position controllers, otherwise I get not enough samples to do accurate control.

What time step would recommend to run a simulation at if some accuracy is required so that the simulation would be called accurate (I know that is a discussion in itself, but if my 0.001 timestep is too low, I am willing to increase it a bit to improve the maximum speed of my simulator)? I will also add ERP/CFM fix you proposed.

My idea is basically just to get some of your experiences doing experiments using Bullet Physics and what time step (or what range) you would propose.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to control and limit btMultiBody Spherical joint

Post by Basroil »

benelot wrote: I have seen that you do some servo motor experiments or similar. Mine is a scientific experiment on evolutionary algorithms (you probably read that in one of my various question introductions). I can change the speed of my simulator, which done via a fixed timestep that is always used regardless of speed settings but the simulator just gives more or less processing time to the physics within the application. Thereby it can do more or less fixed time steps depending on the settings. The time step is also not substepped, because I need every step to process it within the joint position controllers, otherwise I get not enough samples to do accurate control.
Have you considered using joint callbacks for the controller? Basically at the start of the substep you can call back to whatever piece of code you want (hopefully nothing that adds or subtracts bodies or joints) in each substep. Basically you can keep steps the same size but you can choose what the size of the external step is based on your needs (for data logging, display, etc). The old Dynamic Control demo might help with that. Hell, I've also experimented with putting a simulation based controller in the controller so you can simulate while you simulate :wink: (yes, I'm a bit too old for that, but it's funnier because it's actually something I've tried doing)
benelot wrote: What time step would recommend to run a simulation at if some accuracy is required so that the simulation would be called accurate (I know that is a discussion in itself, but if my 0.001 timestep is too low, I am willing to increase it a bit to improve the maximum speed of my simulator)? I will also add ERP/CFM fix you proposed. My idea is basically just to get some of your experiences doing experiments using Bullet Physics and what time step (or what range) you would propose.
Sadly I can't really help there, the minimum step time really depends on your particular simulation, your controllers, and even how you simulate the controls. If you were simulating the solar system, steps of hours would probably work just fine, but if you're simulating 10000 RPM motors you probably are going to need smaller timesteps than 1ms. You'll also have to worry about simulation stability, especially if you have high control gains. For example, my simulations of MX-28 motors tend to be unstable when simulated with steps above 0.5ms using traditional methods (external forces or joints set up to deliver a prescribed torque), but using other methods you can get stable and accurate simulations with steps then times the size. A good place to start is the same order as the control time constant for your fastest controlled joint
Post Reply