ROS2WheelOdometrySensorComponent.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  9. #include <AzCore/Math/Transform.h>
  10. #include <ROS2/Sensor/Events/PhysicsBasedSource.h>
  11. #include <ROS2Sensors/Odometry/ROS2OdometryCovariance.h>
  12. #include <ROS2Sensors/Odometry/WheelOdometryConfigurationRequestBus.h>
  13. #include <ROS2Sensors/Sensor/ROS2SensorComponentBase.h>
  14. #include <nav_msgs/msg/odometry.hpp>
  15. #include <rclcpp/publisher.hpp>
  16. namespace ROS2Sensors
  17. {
  18. class JsonROS2WheelOdometryComponentConfigSerializer : public AZ::BaseJsonSerializer
  19. {
  20. public:
  21. AZ_RTTI(JsonROS2WheelOdometryComponentConfigSerializer, "{3f7f9be6-d964-4a55-b856-c03cc5754df0}", AZ::BaseJsonSerializer);
  22. AZ_CLASS_ALLOCATOR_DECL;
  23. AZ::JsonSerializationResult::Result Load(
  24. void* outputValue,
  25. const AZ::Uuid& outputValueTypeId,
  26. const rapidjson::Value& inputValue,
  27. AZ::JsonDeserializerContext& context) override;
  28. };
  29. //! Wheel odometry sensor component.
  30. //! It constructs and publishes an odometry message, which contains information about the vehicle's velocity and position in space.
  31. //! This is a physical sensor that takes a vehicle's configuration and computes updates from the wheels' rotations.
  32. //! @see <a href="https://index.ros.org/p/nav_msgs/">nav_msgs package</a>.
  33. class ROS2WheelOdometryComponent
  34. : public ROS2SensorComponentBase<ROS2::PhysicsBasedSource>
  35. , protected WheelOdometryConfigurationRequestBus::Handler
  36. {
  37. friend class JsonROS2WheelOdometryComponentConfigSerializer;
  38. public:
  39. AZ_COMPONENT(ROS2WheelOdometryComponent, ROS2Sensors::ROS2WheelOdometryComponentTypeId, SensorBaseType);
  40. ROS2WheelOdometryComponent();
  41. ~ROS2WheelOdometryComponent() = default;
  42. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  43. static void Reflect(AZ::ReflectContext* context);
  44. //////////////////////////////////////////////////////////////////////////
  45. // Component overrides
  46. void Activate() override;
  47. void Deactivate() override;
  48. //////////////////////////////////////////////////////////////////////////
  49. private:
  50. //////////////////////////////////////////////////////////////////////////
  51. // WheelOdometryConfigurationRequestBus::Handler overrides
  52. const WheelOdometrySensorConfiguration GetConfiguration() override;
  53. void SetConfiguration(const WheelOdometrySensorConfiguration& configuration) override;
  54. ROS2OdometryCovariance GetPoseCovariance() override;
  55. void SetPoseCovariance(const ROS2OdometryCovariance& covariance) override;
  56. ROS2OdometryCovariance GetTwistCovariance() override;
  57. void SetTwistCovariance(const ROS2OdometryCovariance& covariance) override;
  58. //////////////////////////////////////////////////////////////////////////
  59. std::shared_ptr<rclcpp::Publisher<nav_msgs::msg::Odometry>> m_odometryPublisher;
  60. nav_msgs::msg::Odometry m_odometryMsg;
  61. AZ::Vector3 m_robotPose = AZ::Vector3::CreateZero();
  62. AZ::Quaternion m_robotRotation = AZ::Quaternion::CreateIdentity();
  63. WheelOdometrySensorConfiguration m_odometryConfiguration;
  64. void OnOdometryEvent();
  65. void OnPhysicsEvent(float physicsDeltaTime);
  66. };
  67. } // namespace ROS2Sensors