Best way to apply torque to a constraint

Post Reply
mogumbo
Posts: 6
Joined: Tue May 24, 2016 8:15 pm

Best way to apply torque to a constraint

Post by mogumbo »

I'm trying to simulate thin flexible rods by joining together a series of bodies. (Right now I'm joining them with btGeneric6DofSpring2Cconstraints.) I have stiffness for these rods defined as torques in newton * mm / radian. My plan is to use these stiffness values to control the angular components of my constraints by specifying torques based on the angles of the constraints, but I don't see an obvious way to plug torques into the Bullet API. 3 possible ways I see are:

1. Convert torque to spring "stiffness."
2. Convert torque to motor velocity and max force.
3. Convert torque and timestep to ERP and CFM values.

It's not apparent to me how to accomplish any of these conversions. Is there a recommended way to input torques to a constraint? Also, is there a better constraint to use for flexible rods, such as btConeTwistConstraint?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Best way to apply torque to a constraint

Post by benelot »

Torques can only be applied to bodies and not to constraints. So either you should simulate the rods as explicit bodies (might be softbodies if you like) or you apply the torque to the connected bodies of the joint. This means that you calculate the axis of rotation and then apply the torque to the bodies using applyTorque(...) around the appropriate axis. The conetwist might be helpful if you can compare your flexible rods to a joint of a ragdoll, otherwise it might be a good constraint you are using. If you do not experience any issues, stay with it.
mogumbo
Posts: 6
Joined: Tue May 24, 2016 8:15 pm

Re: Best way to apply torque to a constraint

Post by mogumbo »

Thanks benelot. I'm trying this for each pair of bodies now:

Code: Select all

            btMatrix3x3 matA = bb[i]->getWorldTransform().getBasis();
            btMatrix3x3 matB = bb[i+1]->getWorldTransform().getBasis();
            btQuaternion qA, qB;
            matA.getRotation(qA);
            matB.getRotation(qB);
            btQuaternion qDiff = qA.inverse() * qB;
            btVector3 torque = qDiff.getAxis() * qDiff.getAngle();
            torque *= 500.0;
            const double length = torque.length();
            bb[i]->applyTorque(matA * torque);
            bb[i+1]->applyTorque(matA * -torque);
Unfortunately, this is quite unstable, even if I increase the masses and use high damping. But it's clearly trying to take the correct shape. I also need to use torques in the thousands, not the hundreds, which causes much more instability. Starting to wonder if I need to apply some sort of torque damping technique or apply PIDs to the torque calculations. Suggestions are welcome.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Best way to apply torque to a constraint

Post by benelot »

Check out the code of the gazebo simulator using bullet. They do the same thing as you do (you basically try to move the joint using force instead of using the motor, right?), but from quickly checking, I could not see for sure if your implementation does the same or not.

In this example, it is done for a hinge joint(~line 270):
https://bitbucket.org/osrf/gazebo/src/b ... ew-default

Btw, what is the configuration of your joints? Are all 3 rotational DoF free? That might need some quaternion PID to control.
mogumbo
Posts: 6
Joined: Tue May 24, 2016 8:15 pm

Re: Best way to apply torque to a constraint

Post by mogumbo »

Thanks, I saw that example a couple days ago and I'm doing something similar. Right now I have all linear axes locked and one rotational axis locked. I'm only working on flexion with two axes. Later I want to add torsional stiffness and maybe a stretch/compression linear axis.

I'm trying to use this basic formula to simulation stiffness:

torque = bend_axis * (bend_angle * stiffness / body_length + angular_velocity * damping_const)

It shows promise, but I'm still working out instability issues and maybe unit problems. Right now I don't know if all my problems are coming from how I implement these torques or other parts of the code. If I get a reasonable final solution I'll post it here.

I thought about using a PID but decided against it. If my rod is deflected by an outside force for too long, the integral part would build up and ruin the simulation. I guess the formula I'm using is more like a PD.
veio
Posts: 9
Joined: Sun Aug 21, 2016 6:04 pm

Re: Best way to apply torque to a constraint

Post by veio »

Hi,

im using a hinge joint like in the gazebo source code benelot posted and trying to apply torque to it.
I copied the function benelot mentioned SetForceImpl but nothing happens. The Robot arms are just hanging down.

I called hinge->enableFeedback(true) after creation of the hinge, though, I dont know if it needed here.

I call the applyTorque() function from inside an ActionInterface callback.

Do I need to setup more to enable torque access?

Cheers
veio
veio
Posts: 9
Joined: Sun Aug 21, 2016 6:04 pm

Re: Best way to apply torque to a constraint

Post by veio »

It somehow works now. Maybe my torques were just too low.
Though, it is kinda impossible to hold a position in torque mode without shaking. Or did someone manage to do that?
Can I a read the torques applied by other bodies and gravity on the constraint somehow?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Best way to apply torque to a constraint

Post by benelot »

I am happy that it works now, because it actually should. You are right, it needs quite an amount of applied torque to work properly. However, it is strange that you do not get it to hold a position without shaking. Maybe I should invest time into creating such an example at one point, I had it working in the past. You can read the final forces and torques applied to keep the joint stable from the constraint info. Not sure if this helps you though, it mashes too many things into one.
Post Reply