GripperActionServerComponent.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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
  4. * of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #include "Utils.h"
  10. #include "GripperActionServerComponent.h"
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzFramework/Components/TransformComponent.h>
  14. #include <ROS2/Frame/ROS2FrameComponent.h>
  15. #include <ROS2/ROS2GemUtilities.h>
  16. #include <ROS2/Utilities/ROS2Names.h>
  17. namespace ROS2
  18. {
  19. void GripperActionServerComponent::Activate()
  20. {
  21. auto* ros2Frame = GetEntity()->FindComponent<ROS2FrameComponent>();
  22. AZ_Assert(ros2Frame, "Missing Frame Component!");
  23. AZStd::string namespacedAction = ROS2Names::GetNamespacedName(ros2Frame->GetNamespace(), m_gripperActionServerName);
  24. AZ_Printf("GripperActionServerComponent", "Creating Gripper Action Server: %s\n", namespacedAction.c_str());
  25. m_gripperActionServer = AZStd::make_unique<GripperActionServer>(namespacedAction, GetEntityId());
  26. AZ::TickBus::Handler::BusConnect();
  27. }
  28. void GripperActionServerComponent::Deactivate()
  29. {
  30. AZ::TickBus::Handler::BusDisconnect();
  31. m_gripperActionServer.reset();
  32. }
  33. void GripperActionServerComponent::Reflect(AZ::ReflectContext* context)
  34. {
  35. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  36. {
  37. serialize->Class<GripperActionServerComponent, AZ::Component>()
  38. ->Field("ActionServerName", &GripperActionServerComponent::m_gripperActionServerName)
  39. ->Version(1);
  40. if (AZ::EditContext* ec = serialize->GetEditContext())
  41. {
  42. ec->Class<GripperActionServerComponent>("GripperActionServerComponent", "Component for the gripper action server")
  43. ->ClassElement(AZ::Edit::ClassElements::EditorData, "GripperActionServer")
  44. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  45. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  46. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/GripperActionServerComponent.svg")
  47. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/GripperActionServerComponent.svg")
  48. ->DataElement(
  49. AZ::Edit::UIHandlers::Default,
  50. &GripperActionServerComponent::m_gripperActionServerName,
  51. "Gripper Action Server",
  52. "Action name for the gripper server.");
  53. }
  54. }
  55. }
  56. void GripperActionServerComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  57. {
  58. required.push_back(AZ_CRC_CE("ROS2Frame"));
  59. required.push_back(AZ_CRC_CE("GripperService"));
  60. }
  61. void GripperActionServerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  62. {
  63. provided.push_back(AZ_CRC_CE("GripperServerService"));
  64. }
  65. void GripperActionServerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  66. {
  67. incompatible.push_back(AZ_CRC_CE("GripperServerService"));
  68. }
  69. std::shared_ptr<GripperActionServer::GripperCommand::Feedback> GripperActionServerComponent::ProduceFeedback() const
  70. {
  71. auto feedback = std::make_shared<GripperCommand::Feedback>();
  72. float position = 0.0f;
  73. float effort = 0.0f;
  74. GripperRequestBus::EventResult(position, GetEntityId(), &GripperRequestBus::Events::GetGripperPosition);
  75. GripperRequestBus::EventResult(effort, GetEntityId(), &GripperRequestBus::Events::GetGripperEffort);
  76. feedback->position = position;
  77. feedback->effort = effort;
  78. feedback->reached_goal = false;
  79. feedback->stalled = false;
  80. return feedback;
  81. }
  82. std::shared_ptr<GripperActionServer::GripperCommand::Result> GripperActionServerComponent::ProduceResult() const
  83. {
  84. auto result = std::make_shared<GripperCommand::Result>();
  85. float position = 0.0f;
  86. float effort = 0.0f;
  87. bool stalled = false;
  88. bool reachedGoal = false;
  89. GripperRequestBus::EventResult(position, GetEntityId(), &GripperRequestBus::Events::GetGripperPosition);
  90. GripperRequestBus::EventResult(effort, GetEntityId(), &GripperRequestBus::Events::GetGripperEffort);
  91. GripperRequestBus::EventResult(stalled, GetEntityId(), &GripperRequestBus::Events::IsGripperNotMoving);
  92. GripperRequestBus::EventResult(reachedGoal, GetEntityId(), &GripperRequestBus::Events::HasGripperReachedGoal);
  93. result->position = position;
  94. result->position = effort;
  95. result->reached_goal = reachedGoal;
  96. result->stalled = stalled;
  97. return result;
  98. }
  99. void GripperActionServerComponent::OnTick(float delta, AZ::ScriptTimePoint timePoint)
  100. {
  101. AZ_Assert(m_gripperActionServer, "GripperActionServer::OnTick: GripperActionServer is null!");
  102. if (!m_gripperActionServer->IsGoalActiveState())
  103. {
  104. return;
  105. }
  106. bool isDone = false;
  107. bool isStalled;
  108. bool isCancelled = false;
  109. GripperRequestBus::EventResult(isDone, GetEntityId(), &GripperRequestBus::Events::HasGripperReachedGoal);
  110. GripperRequestBus::EventResult(isStalled, GetEntityId(), &GripperRequestBus::Events::IsGripperNotMoving);
  111. GripperRequestBus::EventResult(isCancelled, GetEntityId(), &GripperRequestBus::Events::HasGripperCommandBeenCancelled);
  112. if (isCancelled)
  113. {
  114. m_gripperActionServer->CancelGoal(ProduceResult());
  115. return;
  116. }
  117. if (isDone || isStalled)
  118. {
  119. AZ_Printf("GripperActionServer::OnTick", "GripperActionServer::OnTick: Gripper reached goal!");
  120. m_gripperActionServer->GoalSuccess(ProduceResult());
  121. return;
  122. }
  123. m_gripperActionServer->PublishFeedback(ProduceFeedback());
  124. return;
  125. }
  126. } // namespace ROS2