AckermannControlComponent.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "AckermannControlComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/EditContextConstants.inl>
  11. #include <AzFramework/Physics/RigidBodyBus.h>
  12. #include <ROS2/VehicleDynamics/VehicleInputControlBus.h>
  13. namespace ROS2
  14. {
  15. void AckermannControlComponent::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serialize->Class<AckermannControlComponent, AZ::Component>()->Version(1);
  20. if (AZ::EditContext* ec = serialize->GetEditContext())
  21. {
  22. ec->Class<AckermannControlComponent>("Ackermann Control", "Relays Ackermann commands to vehicle inputs")
  23. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  24. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  25. ->Attribute(AZ::Edit::Attributes::Category, "ROS2");
  26. }
  27. }
  28. }
  29. void AckermannControlComponent::Activate()
  30. {
  31. AckermannNotificationBus::Handler::BusConnect(GetEntityId());
  32. }
  33. void AckermannControlComponent::Deactivate()
  34. {
  35. AckermannNotificationBus::Handler::BusDisconnect();
  36. }
  37. void AckermannControlComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  38. {
  39. required.push_back(AZ_CRC_CE("ROS2RobotControl"));
  40. required.push_back(AZ_CRC_CE("AckermannModelService"));
  41. }
  42. void AckermannControlComponent::AckermannReceived(const AckermannCommandStruct& acs)
  43. {
  44. // Notify input system for vehicle dynamics. Only speed and steering is currently supported.
  45. VehicleDynamics::VehicleInputControlRequestBus::Event(
  46. GetEntityId(), &VehicleDynamics::VehicleInputControlRequests::SetTargetLinearSpeed, acs.m_speed);
  47. VehicleDynamics::VehicleInputControlRequestBus::Event(
  48. GetEntityId(), &VehicleDynamics::VehicleInputControlRequests::SetTargetSteering, acs.m_steeringAngle);
  49. }
  50. } // namespace ROS2