GripperActionServerComponent.cpp 6.2 KB

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