UrdfInertial.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. © Siemens AG, 2018
  3. Author: Suzannah Smith (suzannah.smith@siemens.com)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. <http://www.apache.org/licenses/LICENSE-2.0>.
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. using System;
  15. using UnityEngine;
  16. namespace RosSharp.Urdf
  17. {
  18. [RequireComponent(typeof(Rigidbody))]
  19. public class UrdfInertial : MonoBehaviour
  20. {
  21. [SerializeField] private Rigidbody _rigidbody;
  22. public bool DisplayInertiaGizmo;
  23. public enum RigidbodyDataSource { Urdf, Unity, Manual};
  24. public RigidbodyDataSource rigidbodyDataSource;
  25. public float Mass;
  26. public Vector3 CenterOfMass;
  27. public Vector3 InertiaTensor;
  28. public Quaternion InertiaTensorRotation;
  29. public float UrdfMass;
  30. public Vector3 UrdfCenterOfMass;
  31. public Vector3 UrdfInertiaTensor;
  32. public Quaternion UrdfInertiaTensorRotation;
  33. private const int RoundDigits = 10;
  34. private const float MinInertia = 1e-6f;
  35. private bool isCreated;
  36. public static void Create(GameObject linkObject, Link.Inertial inertial = null)
  37. {
  38. UrdfInertial urdfInertial = linkObject.AddComponent<UrdfInertial>();
  39. urdfInertial.UrdfMass = (float)inertial.mass;
  40. if (inertial.origin != null)
  41. urdfInertial.UrdfCenterOfMass = UrdfOrigin.GetPositionFromUrdf(inertial.origin);
  42. urdfInertial.ImportInertiaData(inertial.inertia);
  43. urdfInertial.Initialize();
  44. urdfInertial.isCreated = true;
  45. }
  46. private void Initialize()
  47. {
  48. rigidbodyDataSource = RigidbodyDataSource.Urdf;
  49. Mass = UrdfMass;
  50. CenterOfMass = UrdfCenterOfMass;
  51. InertiaTensor = UrdfInertiaTensor;
  52. InertiaTensorRotation = UrdfInertiaTensorRotation;
  53. DisplayInertiaGizmo = false;
  54. UpdateRigidBodyData();
  55. }
  56. #region Runtime
  57. private void Reset()
  58. {
  59. if(isCreated)
  60. Initialize();
  61. }
  62. private void OnValidate()
  63. {
  64. if (isCreated)
  65. UpdateRigidBodyData();
  66. }
  67. public void UpdateRigidBodyData()
  68. {
  69. _rigidbody = GetComponent<Rigidbody>();
  70. switch (rigidbodyDataSource)
  71. {
  72. case RigidbodyDataSource.Urdf:
  73. {
  74. _rigidbody.mass = UrdfMass;
  75. _rigidbody.centerOfMass = UrdfCenterOfMass;
  76. _rigidbody.inertiaTensor = UrdfInertiaTensor;
  77. _rigidbody.inertiaTensorRotation = UrdfInertiaTensorRotation;
  78. return;
  79. }
  80. case RigidbodyDataSource.Unity:
  81. {
  82. _rigidbody.mass = Mass;
  83. bool isKinematic = _rigidbody.isKinematic;
  84. _rigidbody.isKinematic = false;
  85. _rigidbody.ResetCenterOfMass();
  86. _rigidbody.ResetInertiaTensor();
  87. _rigidbody.isKinematic = isKinematic;
  88. CenterOfMass = _rigidbody.centerOfMass;
  89. InertiaTensor = _rigidbody.inertiaTensor;
  90. InertiaTensorRotation = _rigidbody.inertiaTensorRotation;
  91. return;
  92. }
  93. case RigidbodyDataSource.Manual:
  94. {
  95. _rigidbody.mass = Mass;
  96. _rigidbody.centerOfMass = CenterOfMass;
  97. _rigidbody.inertiaTensor = InertiaTensor;
  98. _rigidbody.inertiaTensorRotation = InertiaTensorRotation;
  99. return;
  100. }
  101. }
  102. }
  103. private void OnDrawGizmosSelected()
  104. {
  105. if (DisplayInertiaGizmo)
  106. {
  107. Gizmos.color = Color.blue;
  108. Gizmos.DrawRay(transform.position, GetComponent<Rigidbody>().inertiaTensorRotation * Vector3.forward * GetComponent<Rigidbody>().inertiaTensor.z);
  109. Gizmos.color = Color.green;
  110. Gizmos.DrawRay(transform.position, GetComponent<Rigidbody>().inertiaTensorRotation * Vector3.up * GetComponent<Rigidbody>().inertiaTensor.y);
  111. Gizmos.color = Color.red;
  112. Gizmos.DrawRay(transform.position, GetComponent<Rigidbody>().inertiaTensorRotation * Vector3.right * GetComponent<Rigidbody>().inertiaTensor.x);
  113. }
  114. }
  115. #endregion
  116. #region Import
  117. private void ImportInertiaData(Link.Inertial.Inertia inertia)
  118. {
  119. Vector3 eigenvalues;
  120. Vector3[] eigenvectors;
  121. Matrix3x3 rotationMatrix = ToMatrix3x3(inertia);
  122. rotationMatrix.DiagonalizeRealSymmetric(out eigenvalues, out eigenvectors);
  123. UrdfInertiaTensor = ToUnityInertiaTensor(FixMinInertia(eigenvalues));
  124. Debug.Log(UrdfInertiaTensor);
  125. UrdfInertiaTensorRotation = ToQuaternion(eigenvectors[0], eigenvectors[1], eigenvectors[2]).Ros2Unity();
  126. Debug.Log(UrdfInertiaTensorRotation);
  127. }
  128. private static Vector3 ToUnityInertiaTensor(Vector3 vector3)
  129. {
  130. return new Vector3(vector3.y, vector3.z, vector3.x);
  131. }
  132. private static Matrix3x3 ToMatrix3x3(Link.Inertial.Inertia inertia)
  133. {
  134. return new Matrix3x3(
  135. new[] { (float)inertia.ixx, (float)inertia.ixy, (float)inertia.ixz,
  136. (float)inertia.iyy, (float)inertia.iyz,
  137. (float)inertia.izz });
  138. }
  139. private static Vector3 FixMinInertia(Vector3 vector3)
  140. {
  141. for (int i = 0; i < 3; i++)
  142. {
  143. if (vector3[i] < MinInertia)
  144. vector3[i] = MinInertia;
  145. }
  146. return vector3;
  147. }
  148. private static Quaternion ToQuaternion(Vector3 eigenvector0, Vector3 eigenvector1, Vector3 eigenvector2)
  149. {
  150. //From http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
  151. float tr = eigenvector0[0] + eigenvector1[1] + eigenvector2[2];
  152. float qw, qx, qy, qz;
  153. if (tr > 0)
  154. {
  155. float s = Mathf.Sqrt(tr + 1.0f) * 2f; // S=4*qw
  156. qw = 0.25f * s;
  157. qx = (eigenvector1[2] - eigenvector2[1]) / s;
  158. qy = (eigenvector2[0] - eigenvector0[2]) / s;
  159. qz = (eigenvector0[1] - eigenvector1[0]) / s;
  160. }
  161. else if ((eigenvector0[0] > eigenvector1[1]) & (eigenvector0[0] > eigenvector2[2]))
  162. {
  163. float s = Mathf.Sqrt(1.0f + eigenvector0[0] - eigenvector1[1] - eigenvector2[2]) * 2; // S=4*qx
  164. qw = (eigenvector1[2] - eigenvector2[1]) / s;
  165. qx = 0.25f * s;
  166. qy = (eigenvector1[0] + eigenvector0[1]) / s;
  167. qz = (eigenvector2[0] + eigenvector0[2]) / s;
  168. }
  169. else if (eigenvector1[1] > eigenvector2[2])
  170. {
  171. float s = Mathf.Sqrt(1.0f + eigenvector1[1] - eigenvector0[0] - eigenvector2[2]) * 2; // S=4*qy
  172. qw = (eigenvector2[0] - eigenvector0[2]) / s;
  173. qx = (eigenvector1[0] + eigenvector0[1]) / s;
  174. qy = 0.25f * s;
  175. qz = (eigenvector2[1] + eigenvector1[2]) / s;
  176. }
  177. else
  178. {
  179. float s = Mathf.Sqrt(1.0f + eigenvector2[2] - eigenvector0[0] - eigenvector1[1]) * 2; // S=4*qz
  180. qw = (eigenvector0[1] - eigenvector1[0]) / s;
  181. qx = (eigenvector2[0] + eigenvector0[2]) / s;
  182. qy = (eigenvector2[1] + eigenvector1[2]) / s;
  183. qz = 0.25f * s;
  184. }
  185. return new Quaternion(qx, qy, qz, qw);
  186. }
  187. #endregion
  188. #region Export
  189. public Link.Inertial ExportInertialData()
  190. {
  191. // should we read the data from this clas instead of _rigidbody?
  192. Origin inertialOrigin = new Origin(_rigidbody.centerOfMass.Unity2Ros().ToRoundedDoubleArray(), new double[] { 0, 0, 0 });
  193. Link.Inertial.Inertia inertia = ExportInertiaData(_rigidbody);
  194. return new Link.Inertial(Math.Round(_rigidbody.mass, RoundDigits), inertialOrigin, inertia);
  195. }
  196. private static Link.Inertial.Inertia ExportInertiaData(Rigidbody _rigidbody)
  197. {
  198. Matrix3x3 lamdaMatrix = new Matrix3x3(new[] {
  199. _rigidbody.inertiaTensor[0],
  200. _rigidbody.inertiaTensor[1],
  201. _rigidbody.inertiaTensor[2] });
  202. Matrix3x3 qMatrix = Quaternion2Matrix(_rigidbody.inertiaTensorRotation);
  203. Matrix3x3 qMatrixTransposed = qMatrix.Transpose();
  204. Matrix3x3 inertiaMatrix = qMatrix * lamdaMatrix * qMatrixTransposed;
  205. return ToRosCoordinates(ToInertia(inertiaMatrix));
  206. }
  207. private static Matrix3x3 Quaternion2Matrix(Quaternion quaternion)
  208. {
  209. Quaternion rosQuaternion = Quaternion.Normalize(quaternion);
  210. float qx = rosQuaternion.x;
  211. float qy = rosQuaternion.y;
  212. float qz = rosQuaternion.z;
  213. float qw = rosQuaternion.w;
  214. //From http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
  215. return new Matrix3x3(new float[] {
  216. 1 - (2 * qy * qy) - (2 * qz * qz),
  217. (2 * qx * qy) - (2 * qz * qw),
  218. (2 * qx * qz) + (2 * qy * qw),
  219. (2 * qx * qy) + (2 * qz * qw),
  220. 1 - (2 * qx * qx) - (2 * qz * qz),
  221. (2 * qy * qz) - (2 * qx * qw),
  222. (2 * qx * qz) - (2 * qy * qw),
  223. (2 * qy * qz) + (2 * qx * qw),
  224. 1 - (2 * qx * qx) - (2 * qy * qy)});
  225. }
  226. private static Link.Inertial.Inertia ToInertia(Matrix3x3 matrix)
  227. {
  228. return new Link.Inertial.Inertia(matrix[0][0], matrix[0][1], matrix[0][2],
  229. matrix[1][1], matrix[1][2],
  230. matrix[2][2]);
  231. }
  232. private static Link.Inertial.Inertia ToRosCoordinates(Link.Inertial.Inertia unityInertia)
  233. {
  234. return new Link.Inertial.Inertia(0, 0, 0, 0, 0, 0)
  235. {
  236. ixx = unityInertia.izz,
  237. iyy = unityInertia.ixx,
  238. izz = unityInertia.iyy,
  239. ixy = -unityInertia.ixz,
  240. ixz = unityInertia.iyz,
  241. iyz = -unityInertia.ixy
  242. };
  243. }
  244. #endregion
  245. }
  246. }