ROS2RobotControlComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ROS2Controllers
  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. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/ROS2RobotControl.svg")
  62. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/ROS2RobotControl.svg")
  63. ->DataElement(
  64. AZ::Edit::UIHandlers::Default,
  65. &ROS2RobotControlComponent::m_controlConfiguration,
  66. "Control settings",
  67. "Control subscription setting and type of control")
  68. ->DataElement(
  69. AZ::Edit::UIHandlers::Default,
  70. &ROS2RobotControlComponent::m_subscriberConfiguration,
  71. "Topic subscriber configuration",
  72. "Configuration of ROS2 topic Robot Control subscribes to");
  73. }
  74. }
  75. }
  76. void ROS2RobotControlComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  77. {
  78. required.push_back(AZ_CRC_CE("ROS2Frame"));
  79. }
  80. const ControlConfiguration& ROS2RobotControlComponent::GetControlConfiguration() const
  81. {
  82. return m_controlConfiguration;
  83. }
  84. const ROS2::TopicConfiguration& ROS2RobotControlComponent::GetSubscriberConfiguration() const
  85. {
  86. return m_subscriberConfiguration;
  87. }
  88. void ROS2RobotControlComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  89. {
  90. provided.push_back(AZ_CRC_CE("ROS2RobotControl"));
  91. }
  92. void ROS2RobotControlComponent::SetControlConfiguration(const ControlConfiguration& controlConfiguration)
  93. {
  94. m_controlConfiguration = controlConfiguration;
  95. }
  96. void ROS2RobotControlComponent::SetSubscriberConfiguration(const ROS2::TopicConfiguration& subscriberConfiguration)
  97. {
  98. m_subscriberConfiguration = subscriberConfiguration;
  99. }
  100. } // namespace ROS2Controllers