JointStatePublisher.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ROS2/Clock/ROS2ClockRequestBus.h>
  9. #include <ROS2/ROS2Bus.h>
  10. #include <ROS2/ROS2NamesBus.h>
  11. #include <ROS2Controllers/Manipulation/JointsManipulationRequests.h>
  12. #include "JointStatePublisher.h"
  13. #include "ManipulationUtils.h"
  14. namespace ROS2Controllers
  15. {
  16. JointStatePublisher::JointStatePublisher(const ROS2::PublisherConfiguration& configuration, const JointStatePublisherContext& context)
  17. : m_configuration(configuration)
  18. , m_context(context)
  19. {
  20. auto topicConfiguration = m_configuration.m_topicConfiguration;
  21. AZStd::string namespacedTopic;
  22. ROS2::ROS2NamesRequestBus::BroadcastResult(
  23. namespacedTopic,
  24. &ROS2::ROS2NamesRequestBus::Events::GetNamespacedName,
  25. context.m_publisherNamespace,
  26. topicConfiguration.m_topic);
  27. auto ros2Node = ROS2::ROS2Interface::Get()->GetNode();
  28. m_jointStatePublisher =
  29. ros2Node->create_publisher<sensor_msgs::msg::JointState>(namespacedTopic.data(), topicConfiguration.GetQoS());
  30. }
  31. JointStatePublisher::~JointStatePublisher()
  32. {
  33. m_eventSourceAdapter.Stop();
  34. m_adaptedEventHandler.Disconnect();
  35. }
  36. void JointStatePublisher::PublishMessage()
  37. {
  38. std_msgs::msg::Header rosHeader;
  39. AZStd::string namespacedFrameId;
  40. ROS2::ROS2NamesRequestBus::BroadcastResult(
  41. namespacedFrameId, &ROS2::ROS2NamesRequestBus::Events::GetNamespacedName, m_context.m_publisherNamespace, m_context.m_frameId);
  42. rosHeader.frame_id = namespacedFrameId.data();
  43. ROS2::ROS2ClockRequestBus::BroadcastResult(rosHeader.stamp, &ROS2::ROS2ClockRequestBus::Events::GetROSTimestamp);
  44. m_jointStateMsg.header = rosHeader;
  45. AZ_Assert(m_jointNames.size() == m_jointStateMsg.name.size(), "The expected message size doesn't match with the joint list size");
  46. for (size_t i = 0; i < m_jointStateMsg.name.size(); i++)
  47. {
  48. m_jointStateMsg.name[i] = m_jointNames[i].c_str();
  49. JointInfo& jointInfo = m_jointInfos[i];
  50. auto jointStateData = Utils::GetJointState(jointInfo);
  51. m_jointStateMsg.position[i] = jointStateData.position;
  52. m_jointStateMsg.velocity[i] = jointStateData.velocity;
  53. m_jointStateMsg.effort[i] = jointStateData.effort;
  54. }
  55. m_jointStatePublisher->publish(m_jointStateMsg);
  56. }
  57. void JointStatePublisher::InitializePublisher()
  58. {
  59. ManipulationJoints manipulatorJoints;
  60. JointsManipulationRequestBus::EventResult(manipulatorJoints, m_context.m_entityId, &JointsManipulationRequests::GetJoints);
  61. for (const auto& [jointName, jointInfo] : manipulatorJoints)
  62. {
  63. m_jointNames.push_back(jointName);
  64. m_jointInfos.push_back(jointInfo);
  65. }
  66. m_jointStateMsg.name.resize(manipulatorJoints.size());
  67. m_jointStateMsg.position.resize(manipulatorJoints.size());
  68. m_jointStateMsg.velocity.resize(manipulatorJoints.size());
  69. m_jointStateMsg.effort.resize(manipulatorJoints.size());
  70. m_eventSourceAdapter.SetFrequency(m_configuration.m_frequency);
  71. m_adaptedEventHandler = decltype(m_adaptedEventHandler)(
  72. [this](auto&&... args)
  73. {
  74. if (!m_configuration.m_publish)
  75. {
  76. return;
  77. }
  78. PublishMessage();
  79. });
  80. m_eventSourceAdapter.ConnectToAdaptedEvent(m_adaptedEventHandler);
  81. m_eventSourceAdapter.Start();
  82. }
  83. } // namespace ROS2Controllers