ROS2RobotControlComponent.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "RobotControl/ROS2RobotControlComponent.h"
  9. #include "RobotControl/TwistControl/TwistControl.h"
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Debug/Trace.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/EditContextConstants.inl>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. namespace ROS2
  16. {
  17. void ROS2RobotControlComponent::Activate()
  18. {
  19. switch (m_controlConfiguration.m_steering)
  20. {
  21. case ControlConfiguration::Twist:
  22. m_robotControl = AZStd::make_unique<TwistControl>(m_controlConfiguration);
  23. break;
  24. case ControlConfiguration::Ackermann:
  25. // TODO add ackermann
  26. AZ_Error("ROS2RobotControlComponent", false, "Ackermann steering not yet implemented");
  27. break;
  28. default:
  29. break;
  30. }
  31. if (m_robotControl)
  32. {
  33. m_robotControl->Activate(GetEntity());
  34. }
  35. }
  36. void ROS2RobotControlComponent::Deactivate()
  37. {
  38. if (m_robotControl)
  39. {
  40. m_robotControl->Deactivate();
  41. m_robotControl.reset();
  42. }
  43. }
  44. void ROS2RobotControlComponent::Reflect(AZ::ReflectContext* context)
  45. {
  46. ControlConfiguration::Reflect(context);
  47. RobotConfiguration::Reflect(context);
  48. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  49. {
  50. serialize->Class<ROS2RobotControlComponent, AZ::Component>()->Version(1)->Field(
  51. "ControlConfiguration", &ROS2RobotControlComponent::m_controlConfiguration);
  52. if (AZ::EditContext* ec = serialize->GetEditContext())
  53. {
  54. ec->Class<ROS2RobotControlComponent>("ROS2 Robot control", "[Customizable robot control component]")
  55. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  56. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  57. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game")) // TODO - "Simulation"?
  58. ->DataElement(
  59. AZ::Edit::UIHandlers::Default,
  60. &ROS2RobotControlComponent::m_controlConfiguration,
  61. "Control settings",
  62. "Control bus Configuration");
  63. }
  64. }
  65. }
  66. void ROS2RobotControlComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  67. {
  68. // TODO - also, dependent on current/selected RobotControl implementation for what components are required
  69. required.push_back(AZ_CRC("ROS2Frame"));
  70. }
  71. } // namespace ROS2