StepSimulationServiceHandler.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "StepSimulationServiceHandler.h"
  9. #include <AzCore/std/string/string.h>
  10. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  11. #include <SimulationInterfaces/SimulationMangerRequestBus.h>
  12. namespace ROS2SimulationInterfaces
  13. {
  14. AZStd::unordered_set<SimulationFeatureType> StepSimulationServiceHandler::GetProvidedFeatures()
  15. {
  16. return AZStd::unordered_set<SimulationFeatureType>{ SimulationFeatures::STEP_SIMULATION_SINGLE,
  17. SimulationFeatures::STEP_SIMULATION_MULTIPLE };
  18. }
  19. AZStd::optional<StepSimulationServiceHandler::Response> StepSimulationServiceHandler::HandleServiceRequest(
  20. const std::shared_ptr<rmw_request_id_t> header, const Request& request)
  21. {
  22. bool isPaused = false;
  23. SimulationInterfaces::SimulationManagerRequestBus::BroadcastResult(
  24. isPaused, &SimulationInterfaces::SimulationManagerRequests::IsSimulationPaused);
  25. if (!isPaused)
  26. {
  27. Response response;
  28. response.result.result = simulation_interfaces::msg::Result::RESULT_OPERATION_FAILED;
  29. response.result.error_message = "Request cannot be processed - simulation has to be paused.";
  30. return response;
  31. }
  32. SimulationInterfaces::SimulationManagerRequestBus::Broadcast(
  33. &SimulationInterfaces::SimulationManagerRequests::StepSimulation, request.steps);
  34. AZ::TickBus::Handler::BusConnect();
  35. return AZStd::nullopt;
  36. }
  37. void StepSimulationServiceHandler::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  38. {
  39. bool isActive = true;
  40. SimulationInterfaces::SimulationManagerRequestBus::BroadcastResult(
  41. isActive, &SimulationInterfaces::SimulationManagerRequests::IsSimulationStepsActive);
  42. if (!isActive)
  43. {
  44. Response response;
  45. response.result.result = simulation_interfaces::msg::Result::RESULT_OK;
  46. SendResponse(response);
  47. AZ::TickBus::Handler::BusDisconnect();
  48. }
  49. }
  50. } // namespace ROS2SimulationInterfaces