GripperActionServer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "GripperActionServer.h"
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzFramework/Components/TransformComponent.h>
  14. #include <ROS2/ROS2Bus.h>
  15. #include <ROS2/ROS2GemUtilities.h>
  16. namespace ROS2
  17. {
  18. GripperActionServer::GripperActionServer(const AZStd::string& actionName, const AZ::EntityId& entityId)
  19. : m_entityId(entityId)
  20. {
  21. auto ros2Node = ROS2Interface::Get()->GetNode();
  22. actionServer = rclcpp_action::create_server<GripperCommand>(
  23. ros2Node,
  24. actionName.data(),
  25. AZStd::bind(&GripperActionServer::GoalReceivedCallback, this, AZStd::placeholders::_1, AZStd::placeholders::_2),
  26. AZStd::bind(&GripperActionServer::GoalCancelledCallback, this, AZStd::placeholders::_1),
  27. AZStd::bind(&GripperActionServer::GoalAcceptedCallback, this, AZStd::placeholders::_1));
  28. }
  29. bool GripperActionServer::IsGoalActiveState() const
  30. {
  31. if (!m_goalHandle)
  32. {
  33. return false;
  34. }
  35. return m_goalHandle->is_active() || m_goalHandle->is_executing() || m_goalHandle->is_canceling();
  36. }
  37. bool GripperActionServer::IsReadyForExecution() const
  38. {
  39. // Has no goal handle yet - can be accepted.
  40. if (!m_goalHandle)
  41. {
  42. return true;
  43. }
  44. // Accept if the previous one is in a terminal state.
  45. return IsGoalActiveState() == false;
  46. }
  47. rclcpp_action::GoalResponse GripperActionServer::GoalReceivedCallback(
  48. [[maybe_unused]] const rclcpp_action::GoalUUID& uuid, std::shared_ptr<const GripperCommand::Goal> goal)
  49. {
  50. if (!IsReadyForExecution())
  51. {
  52. AZ_Printf("GripperActionServer", "GripperActionServer::handleGoal: Rejected goal, already executing\n");
  53. if (m_goalHandle)
  54. {
  55. AZ_Trace(
  56. "GripperActionServer",
  57. " is_active: %d, is_executing %d, is_canceling : %d\n",
  58. m_goalHandle->is_active(),
  59. m_goalHandle->is_executing(),
  60. m_goalHandle->is_canceling());
  61. }
  62. return rclcpp_action::GoalResponse::REJECT;
  63. }
  64. AZ::Outcome<void, AZStd::string> commandOutcome = AZ::Failure(AZStd::string("No gripper component found!"));
  65. GripperRequestBus::EventResult(
  66. commandOutcome, m_entityId, &GripperRequestBus::Events::GripperCommand, goal->command.position, goal->command.max_effort);
  67. if (!commandOutcome.IsSuccess())
  68. {
  69. AZ_Trace("GripperActionServer", "GripperCommand could not be accepted: %s\n", commandOutcome.GetError().c_str());
  70. return rclcpp_action::GoalResponse::REJECT;
  71. }
  72. AZ_Trace("GripperActionServer", "GripperActionServer::handleGoal: GripperCommand accepted!\n");
  73. return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
  74. }
  75. rclcpp_action::CancelResponse GripperActionServer::GoalCancelledCallback(const std::shared_ptr<GoalHandleGripperCommand> goal_handle)
  76. {
  77. AZ::Outcome<void, AZStd::string> cancelOutcome = AZ::Failure(AZStd::string("No gripper component found!"));
  78. GripperRequestBus::EventResult(cancelOutcome, m_entityId, &GripperRequestBus::Events::CancelGripperCommand);
  79. if (!cancelOutcome)
  80. { // This will not happen in a simulation unless intentionally done for behavior validation
  81. AZ_Trace("GripperActionServer", "Cancelling could not be accepted: %s\n", cancelOutcome.GetError().c_str());
  82. return rclcpp_action::CancelResponse::REJECT;
  83. }
  84. return rclcpp_action::CancelResponse::ACCEPT;
  85. }
  86. void GripperActionServer::GoalAcceptedCallback(const std::shared_ptr<GoalHandleGripperCommand> goal_handle)
  87. {
  88. AZ_Trace("GripperActionServer", "Goal accepted!\n");
  89. m_goalHandle = goal_handle;
  90. }
  91. void GripperActionServer::CancelGoal(std::shared_ptr<GripperCommand::Result> result)
  92. {
  93. AZ_Assert(m_goalHandle, "Invalid goal handle!");
  94. if (m_goalHandle && m_goalHandle->is_canceling())
  95. {
  96. AZ_Trace("GripperActionServer", "Cancelling goal\n");
  97. m_goalHandle->canceled(result);
  98. }
  99. }
  100. void GripperActionServer::GoalSuccess(std::shared_ptr<GripperCommand::Result> result)
  101. {
  102. AZ_Assert(m_goalHandle, "Invalid goal handle!");
  103. if (m_goalHandle && (m_goalHandle->is_executing() || m_goalHandle->is_canceling()))
  104. {
  105. AZ_Trace("GripperActionServer", "Goal succeeded\n");
  106. m_goalHandle->succeed(result);
  107. }
  108. }
  109. void GripperActionServer::PublishFeedback(std::shared_ptr<GripperCommand::Feedback> feedback)
  110. {
  111. AZ_Assert(m_goalHandle, "Invalid goal handle!");
  112. if (m_goalHandle && m_goalHandle->is_executing())
  113. {
  114. m_goalHandle->publish_feedback(feedback);
  115. }
  116. }
  117. } // namespace ROS2