HingeJointLimitsManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. © Siemens AG, 2017-2018
  3. Author: Dr. Martin Bischoff (martin.bischoff@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 UnityEngine;
  15. namespace RosSharp
  16. {
  17. [RequireComponent(typeof(HingeJoint))]
  18. [ExecuteInEditMode]
  19. public class HingeJointLimitsManager : MonoBehaviour
  20. {
  21. public float LargeAngleLimitMin;
  22. public float LargeAngleLimitMax;
  23. public float Tolerance = 5;
  24. public float AngleActual { get; private set; }
  25. private float anglePrevious;
  26. public int RotationNumberActual { get; private set; }
  27. public int RotationNumberMin { get; private set; }
  28. public int RotationNumberMax { get; private set; }
  29. public float AngleLimitMin { get; private set; }
  30. public float AngleLimitMax { get; private set; }
  31. private HingeJoint _hingeJoint;
  32. private void Awake()
  33. {
  34. _hingeJoint = GetComponent<HingeJoint>();
  35. RecalculateJointLimits();
  36. }
  37. private void Update()
  38. {
  39. RecalculateJointLimits();
  40. }
  41. private void FixedUpdate()
  42. {
  43. RecalculateJointLimits();
  44. UpdateAngles();
  45. }
  46. private void RecalculateJointLimits()
  47. {
  48. if (LargeAngleLimitMax < LargeAngleLimitMin)
  49. LargeAngleLimitMin = LargeAngleLimitMax;
  50. AngleLimitMin = GetAngleLimit(LargeAngleLimitMin);
  51. RotationNumberMin = GetRotationLimit(LargeAngleLimitMin);
  52. AngleLimitMax = GetAngleLimit(LargeAngleLimitMax);
  53. RotationNumberMax = GetRotationLimit(LargeAngleLimitMax);
  54. FixAngleLimits();
  55. ApplyJointLimits();
  56. }
  57. //Keeps limits from getting near +/- 180, where the joint gets stuck
  58. private void FixAngleLimits()
  59. {
  60. if (LargeAngleLimitMin < 0 && 180 - AngleLimitMin < Tolerance)
  61. {
  62. AngleLimitMin = -180;
  63. RotationNumberMin += 1;
  64. }
  65. else if (LargeAngleLimitMin > 0 && 180 + AngleLimitMin < Tolerance)
  66. {
  67. AngleLimitMin = 180;
  68. RotationNumberMin -= 1;
  69. }
  70. if (LargeAngleLimitMax > 0 && 180 + AngleLimitMax < Tolerance)
  71. {
  72. AngleLimitMax = 180;
  73. RotationNumberMax -= 1;
  74. }
  75. else if (LargeAngleLimitMax < 0 && 180 - AngleLimitMax < Tolerance)
  76. {
  77. AngleLimitMax = -180;
  78. RotationNumberMax += 1;
  79. }
  80. }
  81. private float GetAngleLimit(float largeAngleLimit)
  82. {
  83. if (largeAngleLimit > 0)
  84. return ((largeAngleLimit + 180) % 360) - 180;
  85. else
  86. return ((largeAngleLimit - 180) % 360) + 180;
  87. }
  88. private int GetRotationLimit(float largeAngleLimit)
  89. {
  90. if (largeAngleLimit > 0)
  91. return (int) ((largeAngleLimit + 180) / 360);
  92. else
  93. return (int) ((largeAngleLimit - 180) / 360);
  94. }
  95. private void UpdateAngles()
  96. {
  97. anglePrevious = AngleActual;
  98. AngleActual = _hingeJoint.angle;
  99. if (anglePrevious < -90 && AngleActual > 90)
  100. RotationNumberActual -= 1;
  101. else if (anglePrevious > 90 && AngleActual < -90)
  102. RotationNumberActual += 1;
  103. }
  104. private void ApplyJointLimits()
  105. {
  106. if (RotationNumberActual == RotationNumberMin && RotationNumberActual == RotationNumberMax)
  107. {
  108. _hingeJoint.limits = UpdateLimits(_hingeJoint.limits, AngleLimitMin, AngleLimitMax);
  109. _hingeJoint.useLimits = true;
  110. }
  111. else if (RotationNumberActual == RotationNumberMin && AngleActual - AngleLimitMin <= Tolerance)
  112. {
  113. _hingeJoint.limits = UpdateLimits(_hingeJoint.limits, AngleLimitMin, 180);
  114. _hingeJoint.useLimits = true;
  115. }
  116. else if (RotationNumberActual == RotationNumberMax && AngleLimitMax - AngleActual <= Tolerance)
  117. {
  118. _hingeJoint.limits = UpdateLimits(_hingeJoint.limits, -180, AngleLimitMax);
  119. _hingeJoint.useLimits = true;
  120. }
  121. else
  122. _hingeJoint.useLimits = false;
  123. }
  124. private static JointLimits UpdateLimits(JointLimits jointLimits, float min, float max)
  125. {
  126. jointLimits.min = min;
  127. jointLimits.max = max;
  128. return jointLimits;
  129. }
  130. public void InitializeLimits(Urdf.Joint.Limit limit, HingeJoint joint)
  131. {
  132. LargeAngleLimitMin = (float)limit.upper * -1.0f * Mathf.Rad2Deg;
  133. LargeAngleLimitMax = (float)limit.lower * -1.0f * Mathf.Rad2Deg;
  134. _hingeJoint = joint;
  135. RecalculateJointLimits();
  136. }
  137. }
  138. }