ArticulationBody.AddForce vs SetJointForces

I’m still confused about the usage of AddForce and SetJointForces. What does the input Vector3 represent in AddForce? Does it represent X Drive, Y Drive, etc… or does it represent the force needed to rotate along each axis? The way I use AddForce for now is to loop through every moveable joints of the robot arm, and call AddForce for each articulationbody. However, I realize that the ArticulationBody is a chain, so does calling AddForce applying a cartesian force on the center of mass of this ArticulationBody? In this case, is SetJointForces a better choice? Thanks!

As per the docs:

So it’s just a force vector, that is, direction and magnitude of the force applied. It’s not an angular drive, or a torque (rotational force).

It isn’t. An ArticulationBody is a “link” in the chain. The chain is the entire physics articulation, composed of multiple ArticulationBodies.

Yes, just like Rigidbody.AddForce the force is applied at the center of mass. If you want to add a force at any other point, use AddForceAtPosition.

Depends on your use case since they do completely different things. AddForce adds an external force to a body. SetJointForces sets internal articulation forces, that is, forces that the joints exert on the bodies attached to them.

1 Like

@arkano22 Thank you! I just implemented a version, but I am not sure why it is not behaving in the way I expected. No matter what values I sent, the arm behaves as if the JointForces are 0 anyways and shows free fall. I am passing a float list from Python to C# side. The way I call SetJointForces is as follows:

First, I parse the float32 from python and store them into a List. I verify this part by setting log to see the values.

        private void SetJointForces(IncomingMessage msg){
         
            int jointCount = msg.ReadInt32();
            //For debug
            if (moveableJoints.Count != jointCount)
            {
                Debug.LogError(string.Format("The number of target joint positions is {0}, but the valid number of joints in robot arm is {1}", jointCount, moveableJoints.Count));
                return;
            }

            List<float> forces = new List<float>();
            for (int i = 0; i < jointCount; i++)
            {
                float f = msg.ReadFloat32();
                Debug.Log($"Forces count before SetJointForces: {f}");
                forces.Add(f);
            }
            // Debug.Log($"Forces count before SetJointForces: {forces.Count}");
            // foreach (var force in forces)
            // {
            //     Debug.Log($"Force: {force}");
            // }
            SetJointForces(forces);
        }

Then, it calls another function that calls the setJointForces in another class. This is a part where I am unsure about, because I’m not sure which articulationBody I should use as the object instance. I set it to the first moveable joints.

        private void SetJointForces(List<float> forces){
            moveableJoints[0].GetUnit().SetJointForces(forces);
        }

After this, the SetJointForces() in the other file sets the forcemode and call ArticulationBody.SetJointForces as follows:

public void SetJointForces(List<float> forces){
forceMode = ForceMode.JointForce;
forces = forces;
if (forces == null)
{
Debug.LogError("Received null forces list from the message.");
return;
}
}
enum ForceMode
    {
        None,
        Force,
        JointForce,
        ForceAtPosition,
        Torque
    }
    private ForceMode forceMode = ForceMode.None;
    private Vector3 force = Vector3.zero;
    private Vector3 position = Vector3.zero;
    private List<float> forces = new List<float>(new float[13]); //CHANGED
    void FixedUpdate()
    {
        // Debug.Log($"Forces count before SetJointForces: {forces.Count}");
        // foreach (var force in forces)
        // {
        //     Debug.Log($"Force: {force}");
        // }
        switch (forceMode)
        {
            case ForceMode.Force:
                articulationBody.AddForce(force);
                break;
            case ForceMode.JointForce:
                articulationBody.SetJointForces(forces); // CHANGED
                break;
            case ForceMode.ForceAtPosition:
                articulationBody.AddForceAtPosition(force, position);
                break;
            case ForceMode.Torque:
                articulationBody.AddTorque(force);
                break;
        }
        forceMode = ForceMode.None;
    }

Another problem is that I think we should set isKinematic() to false to enable our SetJointForces to work. However, if I unticked them in Unity Editor, the meshes disappear as soon as the arm starts to move. I’m not sure what could go wrong.

You won’t prevent free fall by setting internal joint forces, it’s like trying to stay airborne by clenching your buttocks. If you want to make the articulated body levitate or something like that, you need to apply an external force (that is, use AddForce).

Assuming SetJointForces is the correct choice for your use case, you’re not using ArticulationBody.GetDofStartIndices or ArticulationBody.index to find where in the forces array you should write your values, your code assumes exactly one DOF per joint (hinge-like or prismatic joints). Not sure if it is a correct assumption in your case, but the code sure looks broken to me.

Doesn’t matter, since it affects the entire body hierarchy:
https://docs.unity3d.com/ScriptReference/ArticulationBody.SetJointForces.html

Closest thing to kinematic in an articulation is “Immovable”, as far as i know. Anyway immovable or kinematic objects have no forces/accelerations of any kind applied to them.

@arkano22 Thank you so much! You really help me get a better sense of the entire thing. I guess my last question will be if it is better to use AddTorque for torque control, such as gravity compensation, than to use AddForce? When using AddForce on a link, will its movement be affected by the joint attached to the link? If so, AddTorque is also set on the center of mass by default right?

Entirely depends on your use case. AddForce will not impart any rotational motion in a body, since the force is added exactly at their center of mass (it would nee to be applied off-center to cause any rotation). So if you want to have some additional control over the rotation of a body, you either need to use AddForceAtPosition on a position away from the center of mass (which will result in both linear and angular movement) or use AddTorque.

For instance, when controlling a quadcopter drone using a PID controller you typically have 4 off-center forces (one per rotor). These are enough to control both linear and angular movement, no need to tinker with torques directly.

Yep. The body is still part of an articulation hierarchy and constrained by joints, no matter the forces you apply on it.

Right, bodies always rotate around their center of mass so it doesn’t make sense to apply torque anywhere else. Any compound motion that makes it look like the object is rotating around some other point is in fact just a combination of center of mass rotation + center of mass translation.

Thank you! The use case for us is to implement gravity compensation by torque control. Each joint only has 1 DOF, and they appeared to be rotating along y-axis, so for each joint when I sent the force vector, it was <0, a, 0>, where a is the torque. To ensure that the links are rotating with their local y-axis, I used AddRelativeTorque instead of AddTorque. Unfortunately, the behavior is again not as what we expected. Even though each joint only has 1 DOF, setting values on either x or z entry can also rotate the link. Hence, we are unsure what’s happening. Do you mind giving me some insight?

The code workflow is as follows:

On Python end, we send the torque in this way:

def AddJointTorque(kwargs: dict) -> OutgoingMessage:
compulsory_params = ["id", "joint_torques"]
optional_params = [ ]
utility.CheckKwargs(kwargs, compulsory_params)

msg = OutgoingMessage()
joint_torque = kwargs["joint_torques"]
num_joints = len(joint_torque)

msg.write_int32(kwargs["id"])
msg.write_string("AddRelativeTorque")
msg.write_int32(num_joints)
for i in range(num_joints):
msg.write_float32(joint_torque*[0])*
<em>msg.write_float32(joint_torque*[1])*</em>
<em><em>msg.write_float32(joint_torque*[2])*</em></em>
<em>_*return msg*_</em>
<em>_*```*_</em>
<em>_*Then, the information passed from this function will be sent to C# side in this sequence:*_</em>
<em>_*In a case switch statement, it identifies the written string by:*_</em>
<em>_*```*_</em>
<em>_*case "AddRelativeTorque":*_</em>
<em>_*AddRelativeTorque(msg);*_</em>
<em>_*return;*_</em>
<em>_*```*_</em>
<em>_*Then, it calls:*_</em>
<em>_*```*_</em>
<em>_*private void AddRelativeTorque(IncomingMessage msg){*_</em>
<em>_*int jointCount = msg.ReadInt32();*_</em>
<em>_*if (moveableJoints.Count != jointCount)*_</em>
<em>_*{*_</em>
<em>_*Debug.LogError(string.Format("The number of target joint positions is {0}, but the valid number of joints in robot arm is {1}", jointCount, moveableJoints.Count));*_</em>
<em>_*return;*_</em>
<em>_*}*_</em>
<em>_*List<Vector3> jointTorques = new List<Vector3>();*_</em>
<em>_*for (int i = 0; i < jointCount; i++)*_</em>
<em>_*{*_</em>
<em>_*Vector3 force = new Vector3();*_</em>
<em>_*force.x = msg.ReadFloat32();*_</em>
<em>_*force.y = msg.ReadFloat32();*_</em>
<em>_*force.z = msg.ReadFloat32();*_</em>
<em>_*jointTorques.Add(force);*_</em>
<em>_*}*_</em>
<em>_*AddRelativeTorque(jointTorques);*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*Then it calls:*_</em>
<em>_*```*_</em>
<em>_*private void AddRelativeTorque(List<Vector3> jointTorques){*_</em>
<em>_*for (int i = 0; i < moveableJoints.Count; i++)*_</em>
<em>_*{*_</em>
<em><em><em>moveableJoints_.GetUnit().AddRelativeTorque(jointTorques*);*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*```*_</em></em></em>
<em><em><em>_*Which calls in another run-time file:*_</em></em></em>
<em><em><em>_*```*_</em></em></em>
<em><em><em>_*public void AddRelativeTorque(Vector3 forces){*_</em></em></em>
<em><em><em>_*forceMode = ForceMode.RelativeTorque;*_</em></em></em>
<em><em><em>_*force = forces;*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*```*_</em></em></em>
<em><em><em>_*The AddRelativeTorque is called in:*_</em></em></em>
<em><em><em>_*```*_</em></em></em>
<em><em><em>_*void FixedUpdate()*_</em></em></em>
<em><em><em>_*{*_</em></em></em>
<em><em><em>_*// Debug.Log($"Forces count before SetJointForces: {forces.Count}");*_</em></em></em>
<em><em><em>_*// foreach (var force in forces)*_</em></em></em>
<em><em><em>_*// {*_</em></em></em>
<em><em><em>_*// Debug.Log($"Force: {force}");*_</em></em></em>
<em><em><em>_*// }*_</em></em></em>
<em><em><em>_*switch (forceMode)*_</em></em></em>
<em><em><em>_*{*_</em></em></em>
<em><em><em>_*case ForceMode.Force:*_</em></em></em>
<em><em><em>_*articulationBody.AddForce(force);*_</em></em></em>
<em><em><em>_*break;*_</em></em></em>
<em><em><em>_*case ForceMode.RelativeTorque:*_</em></em></em>
<em><em><em>_*articulationBody.AddRelativeTorque(force); // CHANGED*_</em></em></em>
<em><em><em>_*break;*_</em></em></em>
<em><em><em>_*case ForceMode.ForceAtPosition:*_</em></em></em>
<em><em><em>_*articulationBody.AddForceAtPosition(force, position);*_</em></em></em>
<em><em><em>_*break;*_</em></em></em>
<em><em><em>_*case ForceMode.Torque:*_</em></em></em>
<em><em><em>_*articulationBody.AddTorque(force);*_</em></em></em>
<em><em><em>_*break;*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*forceMode = ForceMode.None;*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*```*_</em></em></em>