ROS2WheelOdometrySensorComponent.h 3.6 KB

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