PoseStampedSubscriber.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 PoseStampedSubscriber : UnitySubscriber<MessageTypes.Geometry.PoseStamped>
  18. {
  19. //public Transform PublishedTransform;
  20. public Vector3 position;
  21. public Quaternion rotation;
  22. public bool isMessageReceived;
  23. protected override void Start()
  24. {
  25. base.Start();
  26. }
  27. private void Update()
  28. {
  29. if (isMessageReceived)
  30. ProcessMessage();
  31. }
  32. protected override void ReceiveMessage(MessageTypes.Geometry.PoseStamped message)
  33. {
  34. position = GetPosition(message).Ros2Unity();
  35. rotation = GetRotation(message).Ros2Unity();
  36. isMessageReceived = true;
  37. }
  38. private void ProcessMessage()
  39. {
  40. // PublishedTransform.position = position;
  41. // PublishedTransform.rotation = rotation;
  42. }
  43. private Vector3 GetPosition(MessageTypes.Geometry.PoseStamped message)
  44. {
  45. return new Vector3(
  46. (float)message.pose.position.x,
  47. (float)message.pose.position.y,
  48. (float)message.pose.position.z);
  49. }
  50. private Quaternion GetRotation(MessageTypes.Geometry.PoseStamped message)
  51. {
  52. return new Quaternion(
  53. (float)message.pose.orientation.x,
  54. (float)message.pose.orientation.y,
  55. (float)message.pose.orientation.z,
  56. (float)message.pose.orientation.w);
  57. }
  58. }
  59. }