ROS2RobotControlComponent.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #pragma once
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. #include "RobotControl/ROS2RobotControlComponent.h"
  14. #include "RobotControl/TwistControl.h"
  15. #include "Utilities/ROS2Names.h"
  16. namespace ROS2
  17. {
  18. void ROS2RobotControlComponent::Activate()
  19. {
  20. auto ros2Frame = GetEntity()->FindComponent<ROS2FrameComponent>();
  21. auto namespacedTopic = ROS2Names::GetNamespacedName(ros2Frame->GetNamespace(), m_topic);
  22. // TODO - instead, create/reset robot control in Activate based on selected implementation (in the component)
  23. m_robotControl = std::make_unique<TwistControl>();
  24. m_robotControl->Activate(GetEntity(), namespacedTopic, m_qos);
  25. }
  26. void ROS2RobotControlComponent::Deactivate()
  27. {
  28. m_robotControl->Deactivate();
  29. m_robotControl.reset();
  30. }
  31. void ROS2RobotControlComponent::Reflect(AZ::ReflectContext* context)
  32. {
  33. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  34. {
  35. serialize->Class<ROS2RobotControlComponent, AZ::Component>()
  36. ->Version(1)
  37. ->Field("topic", &ROS2RobotControlComponent::m_topic)
  38. ->Field("qos", &ROS2RobotControlComponent::m_qos)
  39. ;
  40. if (AZ::EditContext* ec = serialize->GetEditContext())
  41. {
  42. ec->Class<ROS2RobotControlComponent>("ROS2 Robot control", "[Customizable robot control component]")
  43. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  44. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game")) // TODO - "Simulation"?
  45. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2RobotControlComponent::m_topic, "Topic", "ROS2 topic to subscribe to")
  46. ->Attribute(AZ::Edit::Attributes::ChangeValidate, &ROS2Names::ValidateTopicField)
  47. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2RobotControlComponent::m_qos, "QoS", "Quality of Service settings for subscriber")
  48. ;
  49. }
  50. }
  51. }
  52. void ROS2RobotControlComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  53. {
  54. // TODO - also, dependent on current/selected RobotControl implementation for what components are required
  55. required.push_back(AZ_CRC("ROS2Frame"));
  56. }
  57. } // namespace ROS2