PoseStampedPublisher.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // Added allocation free alternatives
  15. // UoK , 2019, Odysseas Doumas (od79@kent.ac.uk / odydoum@gmail.com)
  16. using UnityEngine;
  17. namespace RosSharp.RosBridgeClient
  18. {
  19. public class PoseStampedPublisher : UnityPublisher<MessageTypes.Geometry.PoseStamped>
  20. {
  21. private bool wait; //lydia
  22. private TwistSubscriber twist_sub; //Lydia
  23. public Transform PublishedTransform;
  24. public string FrameId = "Unity";
  25. private MessageTypes.Geometry.PoseStamped message;
  26. private Vector3 modifiedPosition;
  27. protected override void Start()
  28. {
  29. base.Start();
  30. InitializeMessage();
  31. //Lydia
  32. wait=false;
  33. twist_sub = GameObject.Find("RosConnector").GetComponent<TwistSubscriber>();
  34. }
  35. private void FixedUpdate()
  36. {
  37. UpdateMessage();
  38. }
  39. private void InitializeMessage()
  40. {
  41. message = new MessageTypes.Geometry.PoseStamped
  42. {
  43. header = new MessageTypes.Std.Header()
  44. {
  45. frame_id = FrameId
  46. }
  47. };
  48. }
  49. private void UpdateMessage()
  50. {
  51. //if(twist_sub.MessageReceived())
  52. //{
  53. // Debug.Log("Received");
  54. // wait =false;
  55. //}
  56. message.header.Update();
  57. GetGeometryPoint(PublishedTransform.position.Unity2Ros(), message.pose.position);
  58. GetGeometryQuaternion(PublishedTransform.rotation.Unity2Ros(), message.pose.orientation);
  59. //(wait==false){
  60. Publish(message); // is the simulated robot already at position?
  61. wait = true;
  62. //Debug.Log("Waiting for 5 seconds");
  63. //System.Threading.Thread.Sleep(5000);
  64. //Debug.Log("finished waiting");
  65. //}else{
  66. // Debug.Log("waiting for robot to move");
  67. //}
  68. }
  69. private static void GetGeometryPoint(Vector3 position, MessageTypes.Geometry.Point geometryPoint)
  70. {
  71. geometryPoint.x = position.x; //y in unity is z for robot
  72. geometryPoint.y = position.y; //x in unity is y for robot
  73. geometryPoint.z = position.z; //z in unity is x for robot
  74. }
  75. private static void GetGeometryQuaternion(Quaternion quaternion, MessageTypes.Geometry.Quaternion geometryQuaternion)
  76. {
  77. geometryQuaternion.x = quaternion.x;
  78. geometryQuaternion.y = quaternion.y;
  79. geometryQuaternion.z = quaternion.z;
  80. geometryQuaternion.w = quaternion.w;
  81. }
  82. public void setWait(bool wait){
  83. this.wait= wait;
  84. }
  85. public bool getWait(){
  86. return wait;
  87. }
  88. }
  89. }