google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
8.25k stars 823 forks source link

How to actuate a free joint? #2087

Closed omarrayyann closed 1 month ago

omarrayyann commented 1 month ago

I am looking for a way to actuate a free joint using position and quaternion data. While I am aware that mocaps exist and can be applied to bodies, I want to achieve this through actuators so that collisions and other physical interactions still occur. I attempted to approximate this by using six joints (three sliders and three hinges) instead of a free joint, but encountered limitations when it comes to continuously tracking a rotations using an Euler-based controller. Is there an alternative way to actuate a free joint using actuators to be able to use pos and quat?

omarrayyann commented 1 month ago

I'm doing something like this:

<joint name="gripper_joint_x" type="slide" axis="1 0 0" range="-0.6 0.6" damping="0.1"/>
<joint name="gripper_joint_y" type="slide" axis="0 1 0" range="-0.6 0.6" damping="0.1"/>
<joint name="gripper_joint_z" type="slide" axis="0 0 1" range="0.0 0.6" damping="0.1"/>

<!-- Rotational Joints -->
<joint name="gripper_joint_roll" type="hinge" axis="1 0 0" range="-3.14159 3.14159" damping="0.1"/>
<joint name="gripper_joint_pitch" type="hinge" axis="0 1 0" range="-3.14159 3.14159" damping="0.1"/>
<joint name="gripper_joint_yaw" type="hinge" axis="0 0 1" range="-3.14159 3.14159" damping="0.1"/>
yuvaltassa commented 1 month ago

Yes, this is possible. Copy what is happening in this reference model.

omarrayyann commented 1 month ago

Thanks a lot, will try adding a rotation controller. Also you're referring to the gimbal lock issue I'm assuming right?

yuvaltassa commented 1 month ago

It's not really about gimbal lock in this case, is a topological thing. Consider a simple angle x represented as [cos(x), sin(x)] (analogous to 3D rotation and quaternion, for our purposes). If the current angle is 0 (= [1, 0]) and the desired angle is pi (= [-1, 0]), what torque should a "position" controller apply? Should it rotate clockwise? anti-clockwise? no torque at all?

As you can see there is no satisfactory answer. So instead of dealing with this I will let you discover for yourself that quaternion-denominated position controllers are a bad idea, unless you can guarantee that you are always close to the target (maybe you can, for you use-case).

omarrayyann commented 1 month ago

Thanks for the explanation, that makes a lot of sense. I got it to work now.