TwistPublisher.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.RosBridgeClient
  16. {
  17. public class TwistPublisher : UnityPublisher<MessageTypes.Geometry.Twist>
  18. {
  19. public Transform PublishedTransform;
  20. private MessageTypes.Geometry.Twist message;
  21. private float previousRealTime;
  22. private Vector3 previousPosition = Vector3.zero;
  23. private Quaternion previousRotation = Quaternion.identity;
  24. protected override void Start()
  25. {
  26. base.Start();
  27. InitializeMessage();
  28. }
  29. private void FixedUpdate()
  30. {
  31. UpdateMessage();
  32. }
  33. private void InitializeMessage()
  34. {
  35. message = new MessageTypes.Geometry.Twist();
  36. message.linear = new MessageTypes.Geometry.Vector3();
  37. message.angular = new MessageTypes.Geometry.Vector3();
  38. }
  39. private void UpdateMessage()
  40. {
  41. Vector3 linearVelocity = (PublishedTransform.localPosition - previousPosition) / Time.fixedDeltaTime;
  42. Vector3 angularVelocity = (PublishedTransform.localRotation.eulerAngles - previousRotation.eulerAngles) / Time.fixedDeltaTime;
  43. message.linear = GetGeometryVector3(linearVelocity.Unity2Ros());
  44. message.angular = GetGeometryVector3(-angularVelocity.Unity2Ros());
  45. previousPosition = PublishedTransform.localPosition;
  46. previousRotation = PublishedTransform.localRotation;
  47. Publish(message);
  48. }
  49. private static MessageTypes.Geometry.Vector3 GetGeometryVector3(Vector3 vector3)
  50. {
  51. MessageTypes.Geometry.Vector3 geometryVector3 = new MessageTypes.Geometry.Vector3();
  52. geometryVector3.x = vector3.x;
  53. geometryVector3.y = vector3.y;
  54. geometryVector3.z = vector3.z;
  55. return geometryVector3;
  56. }
  57. }
  58. }