2
0

SimulateStepsActionServerHandler.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "SimulateStepsActionServerHandler.h"
  9. namespace ROS2SimulationInterfaces
  10. {
  11. void SimulateStepsActionServerHandler::Initialize(rclcpp::Node::SharedPtr& node)
  12. {
  13. ROS2ActionBase::Initialize(node);
  14. SimulationInterfaces::SimulationManagerNotificationsBus::Handler::BusConnect();
  15. }
  16. SimulateStepsActionServerHandler::~SimulateStepsActionServerHandler()
  17. {
  18. AZ::TickBus::Handler::BusDisconnect();
  19. SimulationInterfaces::SimulationManagerNotificationsBus::Handler::BusDisconnect();
  20. }
  21. AZStd::unordered_set<SimulationFeatureType> SimulateStepsActionServerHandler::GetProvidedFeatures()
  22. {
  23. return AZStd::unordered_set<SimulationFeatureType>{ SimulationFeatures::STEP_SIMULATION_ACTION,
  24. SimulationFeatures::STEP_SIMULATION_SINGLE,
  25. SimulationFeatures::STEP_SIMULATION_MULTIPLE };
  26. }
  27. AZStd::string_view SimulateStepsActionServerHandler::GetTypeName() const
  28. {
  29. return "SimulateSteps";
  30. }
  31. AZStd::string_view SimulateStepsActionServerHandler::GetDefaultName() const
  32. {
  33. return "simulate_steps";
  34. }
  35. rclcpp_action::GoalResponse SimulateStepsActionServerHandler::GoalReceivedCallback(
  36. [[maybe_unused]] const rclcpp_action::GoalUUID& uuid, std::shared_ptr<const Goal> goal)
  37. {
  38. if (!IsReadyForExecution())
  39. {
  40. return rclcpp_action::GoalResponse::REJECT;
  41. }
  42. m_goalSteps = goal->steps;
  43. return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
  44. }
  45. rclcpp_action::CancelResponse SimulateStepsActionServerHandler::GoalCancelledCallback(
  46. [[maybe_unused]] const std::shared_ptr<GoalHandle> goal_handle)
  47. {
  48. SimulationInterfaces::SimulationManagerRequestBus::Broadcast(
  49. &SimulationInterfaces::SimulationManagerRequests::CancelStepSimulation);
  50. m_isCancelled = true;
  51. return rclcpp_action::CancelResponse::ACCEPT;
  52. }
  53. void SimulateStepsActionServerHandler::GoalAcceptedCallback(const std::shared_ptr<GoalHandle> goal_handle)
  54. {
  55. m_goalHandle = goal_handle;
  56. bool isPaused = false;
  57. SimulationInterfaces::SimulationManagerRequestBus::BroadcastResult(
  58. isPaused, &SimulationInterfaces::SimulationManagerRequests::IsSimulationPaused);
  59. if (!isPaused)
  60. {
  61. //! If simulation is not paused then action should not be processed and action server should return Result with result code set
  62. //! to the RESULT_OPERATION_FAILED value, according to the documentation:
  63. //! https://github.com/ros-simulation/simulation_interfaces/blob/main/action/SimulateSteps.action
  64. auto result = std::make_shared<Result>();
  65. result->result.result = simulation_interfaces::msg::Result::RESULT_OPERATION_FAILED;
  66. result->result.error_message = "Request cannot be processed - simulation has to be paused. Action will be aborted.";
  67. m_goalHandle->abort(result);
  68. m_goalHandle.reset();
  69. return;
  70. }
  71. m_isCancelled = false;
  72. AZ::TickBus::Handler::BusConnect();
  73. SimulationInterfaces::SimulationManagerRequestBus::Broadcast(
  74. &SimulationInterfaces::SimulationManagerRequests::StepSimulation, m_goalSteps);
  75. }
  76. void SimulateStepsActionServerHandler::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  77. {
  78. if (m_isCancelled)
  79. {
  80. auto result = std::make_shared<Result>();
  81. result->result.error_message = "Action has been cancelled.";
  82. result->result.result = simulation_interfaces::msg::Result::RESULT_OK;
  83. CancelGoal(result);
  84. AZ::TickBus::Handler::BusDisconnect();
  85. return;
  86. }
  87. // If SimulationSteps is active then it means that SimulationManagerRequestBus::Handler is busy now with a previous request
  88. bool isActive = true;
  89. SimulationInterfaces::SimulationManagerRequestBus::BroadcastResult(
  90. isActive, &SimulationInterfaces::SimulationManagerRequests::IsSimulationStepsActive);
  91. if (!isActive)
  92. {
  93. auto result = std::make_shared<Result>();
  94. result->result.error_message = "Action finished.";
  95. result->result.result = simulation_interfaces::msg::Result::RESULT_OK;
  96. GoalSuccess(result);
  97. AZ::TickBus::Handler::BusDisconnect();
  98. }
  99. }
  100. void SimulateStepsActionServerHandler::OnSimulationStepFinish(const AZ::u64 remainingSteps)
  101. {
  102. auto feedback = std::make_shared<Feedback>();
  103. feedback->remaining_steps = remainingSteps;
  104. feedback->completed_steps = m_goalSteps - remainingSteps;
  105. PublishFeedback(feedback);
  106. }
  107. } // namespace ROS2SimulationInterfaces