RotateTwistPublisher.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace RosSharp.RosBridgeClient
  5. {
  6. public class SpecialTwistPublisher : UnityPublisher<MessageTypes.Geometry.Twist>
  7. {
  8. public Transform PublishedTransform;
  9. private MessageTypes.Geometry.Twist message;
  10. private float previousRealTime;
  11. private Vector3 previousPosition = Vector3.zero;
  12. private Quaternion previousRotation = Quaternion.identity;
  13. //Testing
  14. public Vector3 LINEAR;
  15. public Vector3 ANGULAR;
  16. protected override void Start()
  17. {
  18. base.Start();
  19. InitializeMessage();
  20. }
  21. private void FixedUpdate()
  22. {
  23. UpdateMessage();
  24. }
  25. private void InitializeMessage()
  26. {
  27. message = new MessageTypes.Geometry.Twist();
  28. message.linear = new MessageTypes.Geometry.Vector3();
  29. message.angular = new MessageTypes.Geometry.Vector3();
  30. }
  31. private void UpdateMessage()
  32. {
  33. Vector3 linearVelocity = (PublishedTransform.position - previousPosition) / Time.fixedDeltaTime;
  34. Vector3 angularVelocity = (PublishedTransform.rotation.eulerAngles - previousRotation.eulerAngles) / Time.fixedDeltaTime;
  35. message.linear = GetGeometryVector3(linearVelocity.Unity2Ros());
  36. message.angular = GetGeometryVector3(-angularVelocity.Unity2Ros());
  37. LINEAR=linearVelocity.Unity2Ros();
  38. ANGULAR= (PublishedTransform.position - previousPosition) / Time.fixedDeltaTime;
  39. previousPosition = PublishedTransform.position;
  40. previousRotation = PublishedTransform.rotation;
  41. //message.linear = GetGeometryVector3(new Vector3(0,0,1).Unity2Ros());
  42. //message.angular = GetGeometryVector3(new Vector3(0,0,0).Unity2Ros());
  43. Publish(message);
  44. }
  45. private static MessageTypes.Geometry.Vector3 GetGeometryVector3(Vector3 vector3)
  46. {
  47. MessageTypes.Geometry.Vector3 geometryVector3 = new MessageTypes.Geometry.Vector3();
  48. geometryVector3.x = vector3.x;
  49. geometryVector3.y = vector3.y;
  50. geometryVector3.z = vector3.z;
  51. return geometryVector3;
  52. }
  53. }
  54. }