Jelajahi Sumber

Changed buses which were supposed to be local to Entity to such (#257)

Signed-off-by: Adam Dąbrowski <[email protected]>
Adam Dąbrowski 3 tahun lalu
induk
melakukan
d1e20f4761

+ 3 - 0
Gems/ROS2/Code/Source/RobotControl/Ackermann/AckermannBus.h

@@ -18,6 +18,9 @@ namespace ROS2
     class AckermannNotifications : public AZ::EBusTraits
     {
     public:
+        static constexpr AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
+        using BusIdType = AZ::EntityId;
+
         //! Handle Ackermann command
         //! @param ackermannCommand A structure with AckermannDrive message fields
         virtual void AckermannReceived(const AckermannCommandStruct& angular) = 0;

+ 2 - 2
Gems/ROS2/Code/Source/RobotControl/Ackermann/AckermannSubscriptionHandler.cpp

@@ -12,7 +12,7 @@
 
 namespace ROS2
 {
-    void AckermannSubscriptionHandler::BroadcastBus(const ackermann_msgs::msg::AckermannDrive& message)
+    void AckermannSubscriptionHandler::SendToBus(const ackermann_msgs::msg::AckermannDrive& message)
     {
         AckermannCommandStruct acs;
         acs.m_acceleration = message.acceleration;
@@ -20,6 +20,6 @@ namespace ROS2
         acs.m_speed = message.speed;
         acs.m_steeringAngle = message.steering_angle;
         acs.m_steeringAngleVelocity = message.steering_angle_velocity;
-        AckermannNotificationBus::Broadcast(&AckermannNotifications::AckermannReceived, acs);
+        AckermannNotificationBus::Event(GetEntityId(), &AckermannNotifications::AckermannReceived, acs);
     }
 } // namespace ROS2

+ 1 - 1
Gems/ROS2/Code/Source/RobotControl/Ackermann/AckermannSubscriptionHandler.h

@@ -15,6 +15,6 @@ namespace ROS2
     class AckermannSubscriptionHandler : public ControlSubscriptionHandler<ackermann_msgs::msg::AckermannDrive>
     {
     private:
-        void BroadcastBus(const ackermann_msgs::msg::AckermannDrive& message) override;
+        void SendToBus(const ackermann_msgs::msg::AckermannDrive& message) override;
     };
 } // namespace ROS2

+ 10 - 2
Gems/ROS2/Code/Source/RobotControl/ControlSubscriptionHandler.h

@@ -37,6 +37,7 @@ namespace ROS2
         void Activate(const AZ::Entity* entity, const ControlConfiguration& controlConfiguration) final
         {
             m_active = true;
+            m_entityId = entity->GetId();
             if (!m_controlSubscription)
             {
                 auto ros2Frame = entity->FindComponent<ROS2FrameComponent>();
@@ -61,17 +62,24 @@ namespace ROS2
 
         virtual ~ControlSubscriptionHandler() = default;
 
+    protected:
+        AZ::EntityId GetEntityId() const
+        {
+            return m_entityId;
+        }
+
     private:
         void OnControlMessage(const T& message)
         {
             if (!m_active)
                 return;
 
-            BroadcastBus(message);
+            SendToBus(message);
         };
 
-        virtual void BroadcastBus(const T& message) = 0;
+        virtual void SendToBus(const T& message) = 0;
 
+        AZ::EntityId m_entityId;
         bool m_active = false;
         typename rclcpp::Subscription<T>::SharedPtr m_controlSubscription;
     };

+ 5 - 5
Gems/ROS2/Code/Source/RobotControl/Controllers/AckermannController/AckermannControlComponent.cpp

@@ -31,7 +31,7 @@ namespace ROS2
 
     void AckermannControlComponent::Activate()
     {
-        AckermannNotificationBus::Handler::BusConnect();
+        AckermannNotificationBus::Handler::BusConnect(GetEntityId());
     }
     void AckermannControlComponent::Deactivate()
     {
@@ -47,9 +47,9 @@ namespace ROS2
     void AckermannControlComponent::AckermannReceived(const AckermannCommandStruct& acs)
     {
         // Notify input system for vehicle dynamics. Only speed and steering is currently supported.
-        VehicleDynamics::VehicleInputControlRequestBus::Broadcast(
-            &VehicleDynamics::VehicleInputControlRequests::SetTargetLinearSpeed, acs.m_speed);
-        VehicleDynamics::VehicleInputControlRequestBus::Broadcast(
-            &VehicleDynamics::VehicleInputControlRequests::SetTargetSteering, acs.m_steeringAngle);
+        VehicleDynamics::VehicleInputControlRequestBus::Event(
+            GetEntityId(), &VehicleDynamics::VehicleInputControlRequests::SetTargetLinearSpeed, acs.m_speed);
+        VehicleDynamics::VehicleInputControlRequestBus::Event(
+            GetEntityId(), &VehicleDynamics::VehicleInputControlRequests::SetTargetSteering, acs.m_steeringAngle);
     }
 } // namespace ROS2

+ 1 - 1
Gems/ROS2/Code/Source/RobotControl/Controllers/RigidBodyController/RigidBodyTwistControlComponent.cpp

@@ -32,7 +32,7 @@ namespace ROS2
 
     void RigidBodyTwistControlComponent::Activate()
     {
-        TwistNotificationBus::Handler::BusConnect();
+        TwistNotificationBus::Handler::BusConnect(GetEntityId());
     }
 
     void RigidBodyTwistControlComponent::Deactivate()

+ 3 - 0
Gems/ROS2/Code/Source/RobotControl/Twist/TwistBus.h

@@ -18,6 +18,9 @@ namespace ROS2
     class TwistNotifications : public AZ::EBusTraits
     {
     public:
+        static constexpr AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
+        using BusIdType = AZ::EntityId;
+
         //! Handle control command
         //! @param linear Linear speed in each axis, in robot reference frame, in m/s.
         //! @param angular Angular speed in each axis, in robot reference frame, in m/s.

+ 2 - 2
Gems/ROS2/Code/Source/RobotControl/Twist/TwistSubscriptionHandler.cpp

@@ -12,10 +12,10 @@
 
 namespace ROS2
 {
-    void TwistSubscriptionHandler::BroadcastBus(const geometry_msgs::msg::Twist& message)
+    void TwistSubscriptionHandler::SendToBus(const geometry_msgs::msg::Twist& message)
     {
         const AZ::Vector3 linearVelocity = ROS2Conversions::FromROS2Vector3(message.linear);
         const AZ::Vector3 angularVelocity = ROS2Conversions::FromROS2Vector3(message.angular);
-        TwistNotificationBus::Broadcast(&TwistNotifications::TwistReceived, linearVelocity, angularVelocity);
+        TwistNotificationBus::Event(GetEntityId(), &TwistNotifications::TwistReceived, linearVelocity, angularVelocity);
     }
 } // namespace ROS2

+ 1 - 1
Gems/ROS2/Code/Source/RobotControl/Twist/TwistSubscriptionHandler.h

@@ -15,6 +15,6 @@ namespace ROS2
     class TwistSubscriptionHandler : public ControlSubscriptionHandler<geometry_msgs::msg::Twist>
     {
     private:
-        void BroadcastBus(const geometry_msgs::msg::Twist& message) override;
+        void SendToBus(const geometry_msgs::msg::Twist& message) override;
     };
 } // namespace ROS2

+ 6 - 8
Gems/ROS2/Code/Source/VehicleDynamics/ManualControlEventHandler.h

@@ -54,25 +54,23 @@ namespace VehicleDynamics
     class ManualControlEventHandler
     {
     public:
-        ManualControlEventHandler()
+        void Activate(AZ::EntityId ownerEntity)
         {
             m_eventHandlers.push_back(ManualControlSingleEventHandler(
                 "steering",
-                [](float inputValue)
+                [ownerEntity](float inputValue)
                 {
-                    VehicleInputControlRequestBus::Broadcast(&VehicleInputControlRequests::SetTargetSteeringFraction, inputValue);
+                    VehicleInputControlRequestBus::Event(ownerEntity, &VehicleInputControlRequests::SetTargetSteeringFraction, inputValue);
                 }));
 
             m_eventHandlers.push_back(ManualControlSingleEventHandler(
                 "accelerate",
-                [](float inputValue)
+                [ownerEntity](float inputValue)
                 {
-                    VehicleInputControlRequestBus::Broadcast(&VehicleInputControlRequests::SetTargetLinearSpeedFraction, inputValue);
+                    VehicleInputControlRequestBus::Event(
+                        ownerEntity, &VehicleInputControlRequests::SetTargetLinearSpeedFraction, inputValue);
                 }));
-        }
 
-        void Activate()
-        {
             for (auto& handler : m_eventHandlers)
             {
                 handler.Activate();

+ 6 - 10
Gems/ROS2/Code/Source/VehicleDynamics/VehicleInputControlBus.h

@@ -7,6 +7,7 @@
  */
 #pragma once
 
+#include <AzCore/Component/EntityId.h>
 #include <AzCore/EBus/EBus.h>
 #include <AzCore/Interface/Interface.h>
 
@@ -15,10 +16,12 @@ namespace VehicleDynamics
     //! Inputs (speed, steering, acceleration etc.) for vehicle dynamics system
     //! Inputs are valid for a short time (configurable) and need to be repeated if continuous movement is needed.
     //! (Use cruise system to set cruise speed).
-    class VehicleInputControlRequests
+    class VehicleInputControlRequests : public AZ::EBusTraits
     {
     public:
-        AZ_RTTI(VehicleInputControlRequests, "{AB0F1D2A-3A73-41B6-B882-62316DE32010}");
+        static constexpr AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
+        using BusIdType = AZ::EntityId;
+
         virtual ~VehicleInputControlRequests() = default;
 
         //! Set target for the vehicle linear speed. It should be realized over time according to drive model.
@@ -45,12 +48,5 @@ namespace VehicleDynamics
         virtual void SetTargetLinearSpeedFraction(float speedFraction) = 0;
     };
 
-    class VehicleInputControlBusTraits : public AZ::EBusTraits
-    {
-    public:
-        static constexpr AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
-        static constexpr AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
-    };
-    using VehicleInputControlRequestBus = AZ::EBus<VehicleInputControlRequests, VehicleInputControlBusTraits>;
-    using VehicleInputControlInterface = AZ::Interface<VehicleInputControlRequests>;
+    using VehicleInputControlRequestBus = AZ::EBus<VehicleInputControlRequests>;
 } // namespace VehicleDynamics

+ 7 - 2
Gems/ROS2/Code/Source/VehicleDynamics/VehicleModelComponent.cpp

@@ -20,8 +20,8 @@ namespace VehicleDynamics
 {
     void VehicleModelComponent::Activate()
     {
-        VehicleInputControlRequestBus::Handler::BusConnect();
-        m_manualControlEventHandler.Activate();
+        VehicleInputControlRequestBus::Handler::BusConnect(GetEntityId());
+        m_manualControlEventHandler.Activate(GetEntityId());
         AZ::TickBus::Handler::BusConnect();
         m_driveModel.Activate(m_vehicleConfiguration);
     }
@@ -77,6 +77,11 @@ namespace VehicleDynamics
         provided.push_back(AZ_CRC_CE("VehicleModelService"));
     }
 
+    void VehicleModelComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
+    { // Only one per entity
+        incompatible.push_back(AZ_CRC_CE("VehicleModelService"));
+    }
+
     void VehicleModelComponent::SetTargetLinearSpeed(float speedMps)
     {
         auto limitedSpeed = VehicleModelLimits::LimitValue(speedMps, m_vehicleLimits.m_speedLimit);

+ 1 - 0
Gems/ROS2/Code/Source/VehicleDynamics/VehicleModelComponent.h

@@ -32,6 +32,7 @@ namespace VehicleDynamics
         void Activate() override;
         void Deactivate() override;
         static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
+        static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
         static void Reflect(AZ::ReflectContext* context);
 
     private: