ROS2WheelOdometrySensorComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "ROS2WheelOdometrySensorComponent.h"
  9. #include <AzCore/Serialization/Json/JsonSerialization.h>
  10. #include <AzCore/Serialization/Json/JsonSerializationResult.h>
  11. #include <AzCore/Serialization/Json/RegistrationContext.h>
  12. #include <ROS2/Clock/ROS2ClockRequestBus.h>
  13. #include <ROS2/ROS2NamesBus.h>
  14. #include <ROS2/Utilities/ROS2Conversions.h>
  15. #include <ROS2Controllers/Sensors/ROS2OdometryCovariance.h>
  16. #include <ROS2Controllers/VehicleDynamics/VehicleInputControlBus.h>
  17. namespace ROS2Controllers
  18. {
  19. namespace
  20. {
  21. const char* WheelOdometryMsgType = "nav_msgs::msg::Odometry";
  22. }
  23. // Manual conversion between version without a configuration struct and with a configuration struct.
  24. // This is done to maintain backwards compatibility with older versions of the component.
  25. // This function checks if in the prefab there exits a member called "Twist covariance" or "Pose covariance".
  26. // If it does, it will load the values into the new configuration struct.
  27. // If it doesn't, it will treat the loaded json as it would load the new version of the component.
  28. // Checking old members is used instead of checking if there is a member called "Odometry configuration"
  29. // because is default values are used O3DE does not create an empty member and initializes the component with default values.
  30. // Meaning if there does not exist a member called "Odometry configuration" this component could use old values or default ones.
  31. AZ::JsonSerializationResult::Result JsonROS2WheelOdometryComponentConfigSerializer::Load(
  32. void* outputValue, const AZ::Uuid& outputValueTypeId, const rapidjson::Value& inputValue, AZ::JsonDeserializerContext& context)
  33. {
  34. namespace JSR = AZ::JsonSerializationResult;
  35. auto configInstance = reinterpret_cast<ROS2WheelOdometryComponent*>(outputValue);
  36. AZ_Assert(configInstance, "Output value for JsonROS2WheelOdometryComponentConfigSerializer can't be null.");
  37. JSR::ResultCode result(JSR::Tasks::ReadField);
  38. const bool hasOldMembers = inputValue.HasMember("Twist covariance") || inputValue.HasMember("Pose covariance");
  39. if (hasOldMembers)
  40. {
  41. {
  42. JSR::ResultCode componentIdLoadResult = ContinueLoadingFromJsonObjectField(
  43. &configInstance->m_odometryConfiguration.m_poseCovariance,
  44. azrtti_typeid<decltype(configInstance->m_odometryConfiguration.m_poseCovariance)>(),
  45. inputValue,
  46. "Pose covariance",
  47. context);
  48. result.Combine(componentIdLoadResult);
  49. }
  50. {
  51. JSR::ResultCode componentIdLoadResult = ContinueLoadingFromJsonObjectField(
  52. &configInstance->m_odometryConfiguration.m_twistCovariance,
  53. azrtti_typeid<decltype(configInstance->m_odometryConfiguration.m_twistCovariance)>(),
  54. inputValue,
  55. "Twist covariance",
  56. context);
  57. result.Combine(componentIdLoadResult);
  58. }
  59. }
  60. else
  61. {
  62. {
  63. JSR::ResultCode componentIdLoadResult = ContinueLoadingFromJsonObjectField(
  64. &configInstance->m_odometryConfiguration,
  65. azrtti_typeid<decltype(configInstance->m_odometryConfiguration)>(),
  66. inputValue,
  67. "Odometry configuration",
  68. context);
  69. result.Combine(componentIdLoadResult);
  70. }
  71. }
  72. {
  73. JSR::ResultCode componentIdLoadResult = ContinueLoadingFromJsonObjectField(
  74. &configInstance->m_sensorConfiguration,
  75. azrtti_typeid<decltype(configInstance->m_sensorConfiguration)>(),
  76. inputValue,
  77. "Sensor configuration",
  78. context);
  79. result.Combine(componentIdLoadResult);
  80. }
  81. return context.Report(
  82. result,
  83. result.GetProcessing() != JSR::Processing::Halted ? "Successfully loaded ROS2FrameComponent information."
  84. : "Failed to load ROS2FrameComponent information.");
  85. }
  86. AZ_CLASS_ALLOCATOR_IMPL(JsonROS2WheelOdometryComponentConfigSerializer, AZ::SystemAllocator);
  87. void ROS2WheelOdometryComponent::Reflect(AZ::ReflectContext* context)
  88. {
  89. if (auto jsonContext = azrtti_cast<AZ::JsonRegistrationContext*>(context))
  90. {
  91. jsonContext->Serializer<JsonROS2WheelOdometryComponentConfigSerializer>()->HandlesType<ROS2WheelOdometryComponent>();
  92. }
  93. WheelOdometrySensorConfiguration::Reflect(context);
  94. if (auto* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  95. {
  96. serialize->Class<ROS2WheelOdometryComponent, SensorBaseType>()->Version(3)->Field(
  97. "Odometry configuration", &ROS2WheelOdometryComponent::m_odometryConfiguration);
  98. if (auto* editContext = serialize->GetEditContext())
  99. {
  100. editContext->Class<ROS2WheelOdometryComponent>("ROS2 Wheel Odometry Sensor", "Odometry sensor component")
  101. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  102. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  103. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  104. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/ROS2WheelOdometrySensor.svg")
  105. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/ROS2WheelOdometrySensor.svg")
  106. ->DataElement(
  107. AZ::Edit::UIHandlers::Default,
  108. &ROS2WheelOdometryComponent::m_odometryConfiguration,
  109. "Odometry configuration",
  110. "Odometry sensor configuration")
  111. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  112. }
  113. }
  114. }
  115. ROS2WheelOdometryComponent::ROS2WheelOdometryComponent()
  116. {
  117. ROS2::TopicConfiguration tc;
  118. const AZStd::string type = WheelOdometryMsgType;
  119. tc.m_type = type;
  120. tc.m_topic = "odom";
  121. m_sensorConfiguration.m_frequency = 10;
  122. m_sensorConfiguration.m_publishersConfigurations.insert(AZStd::make_pair(type, tc));
  123. }
  124. void ROS2WheelOdometryComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  125. {
  126. required.push_back(AZ_CRC_CE("ROS2Frame"));
  127. required.push_back(AZ_CRC_CE("SkidSteeringModelService"));
  128. }
  129. void ROS2WheelOdometryComponent::OnOdometryEvent()
  130. {
  131. m_odometryMsg.pose.pose.position = ROS2::ROS2Conversions::ToROS2Point(m_robotPose);
  132. m_odometryMsg.pose.pose.orientation = ROS2::ROS2Conversions::ToROS2Quaternion(m_robotRotation);
  133. m_odometryMsg.pose.covariance = m_odometryConfiguration.m_poseCovariance.GetRosCovariance();
  134. m_odometryPublisher->publish(m_odometryMsg);
  135. }
  136. void ROS2WheelOdometryComponent::OnPhysicsEvent(float physicsDeltaTime)
  137. {
  138. AZStd::pair<AZ::Vector3, AZ::Vector3> vt;
  139. VehicleDynamics::VehicleInputControlRequestBus::EventResult(
  140. vt, GetEntityId(), &VehicleDynamics::VehicleInputControlRequests::GetWheelsOdometry);
  141. m_odometryMsg.header.stamp = ROS2::ROS2ClockInterface::Get()->GetROSTimestamp();
  142. m_odometryMsg.twist.twist.linear = ROS2::ROS2Conversions::ToROS2Vector3(vt.first);
  143. m_odometryMsg.twist.twist.angular = ROS2::ROS2Conversions::ToROS2Vector3(vt.second);
  144. m_odometryMsg.twist.covariance = m_odometryConfiguration.m_twistCovariance.GetRosCovariance();
  145. if (m_sensorConfiguration.m_frequency > 0)
  146. {
  147. const auto updatePos = physicsDeltaTime * vt.first; // in meters
  148. const auto updateRot = physicsDeltaTime * vt.second; // in radians
  149. m_robotPose += m_robotRotation.TransformVector(updatePos);
  150. m_robotRotation *= AZ::Quaternion::CreateFromScaledAxisAngle(updateRot);
  151. }
  152. }
  153. void ROS2WheelOdometryComponent::Activate()
  154. {
  155. ROS2SensorComponentBase::Activate();
  156. m_robotPose = AZ::Vector3{ 0 };
  157. m_robotRotation = AZ::Quaternion{ 0, 0, 0, 1 };
  158. // "odom" is globally fixed frame for all robots, no matter the namespace
  159. AZStd::string namespacedFrameId;
  160. ROS2::ROS2NamesRequestBus::BroadcastResult(
  161. namespacedFrameId, &ROS2::ROS2NamesRequestBus::Events::GetNamespacedName, GetNamespace(), "odom");
  162. m_odometryMsg.header.frame_id = namespacedFrameId.c_str();
  163. m_odometryMsg.child_frame_id = GetNamespacedFrameID().c_str();
  164. auto ros2Node = ROS2::ROS2Interface::Get()->GetNode();
  165. AZ_Assert(m_sensorConfiguration.m_publishersConfigurations.size() == 1, "Invalid configuration of publishers for Odometry sensor");
  166. const auto& publisherConfig = m_sensorConfiguration.m_publishersConfigurations[WheelOdometryMsgType];
  167. AZStd::string fullTopic;
  168. ROS2::ROS2NamesRequestBus::BroadcastResult(
  169. fullTopic, &ROS2::ROS2NamesRequestBus::Events::GetNamespacedName, GetNamespace(), publisherConfig.m_topic);
  170. m_odometryPublisher = ros2Node->create_publisher<nav_msgs::msg::Odometry>(fullTopic.data(), publisherConfig.GetQoS());
  171. StartSensor(
  172. m_sensorConfiguration.m_frequency,
  173. [this]([[maybe_unused]] auto&&... args)
  174. {
  175. if (!m_sensorConfiguration.m_publishingEnabled)
  176. {
  177. return;
  178. }
  179. OnOdometryEvent();
  180. },
  181. [this]([[maybe_unused]] AzPhysics::SceneHandle sceneHandle, float physicsDeltaTime)
  182. {
  183. OnPhysicsEvent(physicsDeltaTime);
  184. });
  185. WheelOdometryConfigurationRequestBus::Handler::BusConnect(GetEntityId());
  186. }
  187. void ROS2WheelOdometryComponent::Deactivate()
  188. {
  189. WheelOdometryConfigurationRequestBus::Handler::BusDisconnect(GetEntityId());
  190. StopSensor();
  191. m_odometryPublisher.reset();
  192. ROS2SensorComponentBase::Deactivate();
  193. }
  194. const WheelOdometrySensorConfiguration ROS2WheelOdometryComponent::GetConfiguration()
  195. {
  196. return m_odometryConfiguration;
  197. }
  198. void ROS2WheelOdometryComponent::SetConfiguration(const WheelOdometrySensorConfiguration& configuration)
  199. {
  200. m_odometryConfiguration = configuration;
  201. }
  202. ROS2OdometryCovariance ROS2WheelOdometryComponent::GetPoseCovariance()
  203. {
  204. return m_odometryConfiguration.m_poseCovariance;
  205. }
  206. void ROS2WheelOdometryComponent::SetPoseCovariance(const ROS2OdometryCovariance& covariance)
  207. {
  208. m_odometryConfiguration.m_poseCovariance = covariance;
  209. }
  210. ROS2OdometryCovariance ROS2WheelOdometryComponent::GetTwistCovariance()
  211. {
  212. return m_odometryConfiguration.m_twistCovariance;
  213. }
  214. void ROS2WheelOdometryComponent::SetTwistCovariance(const ROS2OdometryCovariance& covariance)
  215. {
  216. m_odometryConfiguration.m_twistCovariance = covariance;
  217. }
  218. } // namespace ROS2Controllers