JoyAxisJointTransformWriter.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 RosSharp.Urdf;
  15. using UnityEngine;
  16. using Joint = UnityEngine.Joint;
  17. namespace RosSharp.RosBridgeClient
  18. {
  19. [RequireComponent(typeof(Joint)), RequireComponent(typeof(JointStateWriter)), RequireComponent(typeof(UrdfJoint))]
  20. public class JoyAxisJointTransformWriter : JoyAxisWriter
  21. {
  22. public float MaxVelocity;
  23. private JointStateWriter jointStateWriter;
  24. private UrdfJoint urdfJoint;
  25. private bool isMessageReceived;
  26. private float lastMessageTime;
  27. private float state;
  28. private HingeJointLimitsManager hingeJointLimitsManager;
  29. private PrismaticJointLimitsManager prismaticJointLimitsManager;
  30. private bool useLimits;
  31. private float currentAxisValue;
  32. private void Start()
  33. {
  34. jointStateWriter = GetComponent<JointStateWriter>();
  35. urdfJoint = GetComponent<UrdfJoint>();
  36. hingeJointLimitsManager = GetComponent<HingeJointLimitsManager>();
  37. prismaticJointLimitsManager = GetComponent<PrismaticJointLimitsManager>();
  38. useLimits = (hingeJointLimitsManager != null || prismaticJointLimitsManager != null);
  39. }
  40. private void ApplyLimits()
  41. {
  42. Vector2 limits = Vector2.zero;
  43. if (urdfJoint.IsRevoluteOrContinuous)
  44. limits = new Vector2(-hingeJointLimitsManager.LargeAngleLimitMax,-hingeJointLimitsManager.LargeAngleLimitMin);
  45. else if (urdfJoint.JointType == UrdfJoint.JointTypes.Prismatic)
  46. limits = new Vector2(prismaticJointLimitsManager.PositionLimitMin, prismaticJointLimitsManager.PositionLimitMax);
  47. state = Mathf.Clamp(state, limits.x, limits.y);
  48. }
  49. private void Update()
  50. {
  51. if (isMessageReceived)
  52. ProcessMessage();
  53. }
  54. private void ProcessMessage()
  55. {
  56. float deltaTime = Time.timeSinceLevelLoad - lastMessageTime;
  57. state += currentAxisValue * MaxVelocity * deltaTime;
  58. if (useLimits)
  59. ApplyLimits();
  60. lastMessageTime = Time.timeSinceLevelLoad;
  61. jointStateWriter.Write(urdfJoint.IsRevoluteOrContinuous ? state * Mathf.Deg2Rad : state);
  62. isMessageReceived = false;
  63. }
  64. public override void Write(float value)
  65. {
  66. currentAxisValue = value;
  67. isMessageReceived = true;
  68. }
  69. }
  70. }