SetEntityStateServiceHandler.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "SetEntityStateServiceHandler.h"
  9. #include <ROS2/TF/TransformInterface.h>
  10. #include <ROS2/Utilities/ROS2Conversions.h>
  11. #include <SimulationInterfaces/RegistryUtils.h>
  12. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  13. namespace ROS2SimulationInterfaces
  14. {
  15. AZStd::unordered_set<SimulationFeatureType> SetEntityStateServiceHandler::GetProvidedFeatures()
  16. {
  17. return AZStd::unordered_set<SimulationFeatureType>{ SimulationFeatures::ENTITY_STATE_SETTING };
  18. }
  19. AZStd::optional<SetEntityStateServiceHandler::Response> SetEntityStateServiceHandler::HandleServiceRequest(
  20. const std::shared_ptr<rmw_request_id_t> header, const Request& request)
  21. {
  22. const auto simulatorFrameId = RegistryUtilities::GetSimulatorROS2Frame();
  23. const AZStd::string_view messageFrameId{ request.state.header.frame_id.c_str(), request.state.header.frame_id.length() };
  24. AZ::Transform transformOffset = AZ::Transform::CreateIdentity();
  25. if (!messageFrameId.empty() && simulatorFrameId != messageFrameId)
  26. {
  27. const builtin_interfaces::msg::Time time = request.state.header.stamp;
  28. auto transformInterface = ROS2::TFInterface::Get();
  29. AZ_Assert(transformInterface, "TFInterface is not available, cannot set entity state without transform offset.");
  30. const auto transformOutcome = transformInterface->GetTransform(simulatorFrameId, messageFrameId, time);
  31. if (transformOutcome.IsSuccess())
  32. {
  33. transformOffset = transformOutcome.GetValue();
  34. }
  35. else
  36. {
  37. Response response;
  38. response.result.result = simulation_interfaces::msg::Result::RESULT_OPERATION_FAILED;
  39. response.result.error_message = transformOutcome.GetError().c_str();
  40. return response;
  41. }
  42. }
  43. AZ::Outcome<void, SimulationInterfaces::FailedResult> outcome;
  44. AZStd::string entityName = request.entity.c_str();
  45. SimulationInterfaces::EntityState entityState;
  46. entityState.m_pose = transformOffset * ROS2::ROS2Conversions::FromROS2Pose(request.state.pose);
  47. entityState.m_twistAngular = transformOffset.TransformVector(ROS2::ROS2Conversions::FromROS2Vector3(request.state.twist.angular));
  48. entityState.m_twistLinear = transformOffset.TransformVector(ROS2::ROS2Conversions::FromROS2Vector3(request.state.twist.linear));
  49. SimulationInterfaces::SimulationEntityManagerRequestBus::BroadcastResult(
  50. outcome, &SimulationInterfaces::SimulationEntityManagerRequests::SetEntityState, entityName, entityState);
  51. Response response;
  52. response.result.result = simulation_interfaces::msg::Result::RESULT_OK;
  53. if (!outcome.IsSuccess())
  54. {
  55. const auto& failedResult = outcome.GetError();
  56. response.result.result = failedResult.m_errorCode;
  57. response.result.error_message = failedResult.m_errorString.c_str();
  58. }
  59. return response;
  60. }
  61. } // namespace ROS2SimulationInterfaces