TwistSubscriber.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. © CentraleSupelec, 2017
  3. Author: Dr. Jeremy Fix (jeremy.fix@centralesupelec.fr)
  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. // Adjustments to new Publication Timing and Execution Framework
  15. // © Siemens AG, 2018, Dr. Martin Bischoff (martin.bischoff@siemens.com)
  16. using UnityEngine;
  17. namespace RosSharp.RosBridgeClient
  18. {
  19. public class TwistSubscriber : UnitySubscriber<MessageTypes.Geometry.Twist>
  20. {
  21. public Transform SubscribedTransform;
  22. private float previousRealTime;
  23. private Vector3 linearVelocity;
  24. private Vector3 angularVelocity;
  25. private bool isMessageReceived;
  26. protected override void Start()
  27. {
  28. base.Start();
  29. }
  30. protected override void ReceiveMessage(MessageTypes.Geometry.Twist message)
  31. {
  32. linearVelocity = ToVector3(message.linear).Ros2Unity();
  33. angularVelocity = -ToVector3(message.angular).Ros2Unity();
  34. isMessageReceived = true;
  35. }
  36. private static Vector3 ToVector3(MessageTypes.Geometry.Vector3 geometryVector3)
  37. {
  38. return new Vector3((float)geometryVector3.x, (float)geometryVector3.y, (float)geometryVector3.z);
  39. }
  40. private void Update()
  41. {
  42. if (isMessageReceived)
  43. ProcessMessage();
  44. previousRealTime = Time.realtimeSinceStartup;
  45. }
  46. private void ProcessMessage()
  47. {
  48. float deltaTime = Time.realtimeSinceStartup - previousRealTime;
  49. SubscribedTransform.Translate(linearVelocity * deltaTime);
  50. SubscribedTransform.Rotate(Vector3.forward, angularVelocity.x * deltaTime);
  51. SubscribedTransform.Rotate(Vector3.up, angularVelocity.y * deltaTime);
  52. SubscribedTransform.Rotate(Vector3.left, angularVelocity.z * deltaTime);
  53. isMessageReceived = false;
  54. }
  55. //Lydia
  56. public bool MessageReceived(){
  57. return isMessageReceived;
  58. }
  59. }
  60. }