2
0

GripperActionServerComponent.cpp 6.1 KB

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