ROS2RobotControlComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "ROS2RobotControlComponent.h"
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Debug/Trace.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <RobotControl/Ackermann/AckermannSubscriptionHandler.h>
  15. #include <RobotControl/ROS2RobotControlComponent.h>
  16. #include <RobotControl/Twist/TwistSubscriptionHandler.h>
  17. namespace ROS2
  18. {
  19. void ROS2RobotControlComponent::Activate()
  20. {
  21. switch (m_controlConfiguration.m_steering)
  22. {
  23. case ControlConfiguration::Steering::Twist:
  24. m_subscriptionHandler = AZStd::make_unique<TwistSubscriptionHandler>();
  25. break;
  26. case ControlConfiguration::Steering::Ackermann:
  27. m_subscriptionHandler = AZStd::make_unique<AckermannSubscriptionHandler>();
  28. break;
  29. default:
  30. AZ_Error("ROS2RobotControlComponent", false, "Control type %d not implemented", m_controlConfiguration.m_steering);
  31. break;
  32. }
  33. if (m_subscriptionHandler)
  34. {
  35. m_subscriptionHandler->Activate(GetEntity(), m_subscriberConfiguration);
  36. }
  37. }
  38. void ROS2RobotControlComponent::Deactivate()
  39. {
  40. if (m_subscriptionHandler)
  41. {
  42. m_subscriptionHandler->Deactivate();
  43. m_subscriptionHandler.reset();
  44. }
  45. }
  46. void ROS2RobotControlComponent::Reflect(AZ::ReflectContext* context)
  47. {
  48. ControlConfiguration::Reflect(context);
  49. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  50. {
  51. serialize->Class<ROS2RobotControlComponent, AZ::Component>()
  52. ->Version(2)
  53. ->Field("ControlConfiguration", &ROS2RobotControlComponent::m_controlConfiguration)
  54. ->Field("SubscriberConfiguration", &ROS2RobotControlComponent::m_subscriberConfiguration);
  55. if (AZ::EditContext* ec = serialize->GetEditContext())
  56. {
  57. ec->Class<ROS2RobotControlComponent>("ROS2 Robot control", "Customizable robot control component")
  58. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  59. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  60. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  61. ->DataElement(
  62. AZ::Edit::UIHandlers::Default,
  63. &ROS2RobotControlComponent::m_controlConfiguration,
  64. "Control settings",
  65. "Control subscription setting and type of control")
  66. ->DataElement(
  67. AZ::Edit::UIHandlers::Default,
  68. &ROS2RobotControlComponent::m_subscriberConfiguration,
  69. "Topic subscriber configuration",
  70. "Configuration of ROS2 topic Robot Control subscribes to");
  71. }
  72. }
  73. }
  74. void ROS2RobotControlComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  75. {
  76. required.push_back(AZ_CRC_CE("ROS2Frame"));
  77. }
  78. const ControlConfiguration& ROS2RobotControlComponent::GetControlConfiguration() const
  79. {
  80. return m_controlConfiguration;
  81. }
  82. const TopicConfiguration& ROS2RobotControlComponent::GetSubscriberConfigration() const
  83. {
  84. return m_subscriberConfiguration;
  85. }
  86. void ROS2RobotControlComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  87. {
  88. provided.push_back(AZ_CRC_CE("ROS2RobotControl"));
  89. }
  90. void ROS2RobotControlComponent::SetControlConfiguration(const ControlConfiguration& controlConfiguration)
  91. {
  92. m_controlConfiguration = controlConfiguration;
  93. }
  94. void ROS2RobotControlComponent::SetSubscriberConfiguration(const TopicConfiguration& subscriberConfiguration)
  95. {
  96. m_subscriberConfiguration = subscriberConfiguration;
  97. }
  98. } // namespace ROS2