SimulationEntityManagerRequestBus.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #pragma once
  9. #include "SimulationInterfacesTypeIds.h"
  10. #include "Result.h"
  11. #include "TagFilter.h"
  12. #include <AzCore/EBus/EBus.h>
  13. #include <AzCore/Interface/Interface.h>
  14. #include <AzCore/std/smart_ptr/shared_ptr.h>
  15. #include <AzFramework/Physics/ShapeConfiguration.h>
  16. namespace SimulationInterfaces
  17. {
  18. //! A set of filters to apply to entity queries. See GetEntities, GetEntitiesStates.
  19. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/msg/EntityFilters.msg">EntityFilters.msg</a>
  20. struct EntityFilters
  21. {
  22. //! A posix regular expression to match against entity names,
  23. //! The regular expression syntax is POSIX Extended,
  24. //! @see <a href="https://pubs.opengroup.org/onlinepubs/9799919799">POSIX_Extended</a> definitions
  25. AZStd::string m_nameFilter;
  26. TagFilter m_tagsFilter; //! A filter to match against entity tags
  27. AZStd::shared_ptr<Physics::ShapeConfiguration>
  28. m_boundsShape; //! A shape to use for filtering entities, null means no bounds filtering
  29. AZ::Transform m_boundsPose{ AZ::Transform::CreateIdentity() };
  30. };
  31. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/msg/EntityState.msg">EntityState.msg</a>
  32. struct EntityState
  33. {
  34. AZ::Transform m_pose; //! The pose of the entity
  35. AZ::Vector3 m_twistLinear; //! The linear velocity of the entity (in the entity frame)
  36. AZ::Vector3 m_twistAngular; //! The angular velocity of the entity (in the entity frame)
  37. };
  38. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/msg/Spawnable.msg">Spawnable.msg</a>
  39. struct Spawnable
  40. {
  41. AZStd::string m_uri;
  42. AZStd::string m_description;
  43. AZStd::string m_boundsSphere;
  44. };
  45. using EntityNameList = AZStd::vector<AZStd::string>;
  46. using MultipleEntitiesStates = AZStd::unordered_map<AZStd::string, EntityState>;
  47. using SpawnableList = AZStd::vector<Spawnable>;
  48. using DeletionCompletedCb = AZStd::function<void(const AZ::Outcome<void, FailedResult>&)>;
  49. using SpawnCompletedCb = AZStd::function<void(const AZ::Outcome<AZStd::string, FailedResult>&)>;
  50. class SimulationEntityManagerRequests
  51. {
  52. public:
  53. AZ_RTTI(SimulationEntityManagerRequests, SimulationInterfacesRequestsTypeId);
  54. virtual ~SimulationEntityManagerRequests() = default;
  55. //! # Get a list of entities that match the filter.
  56. //! Supported filters:
  57. //! - name : a posix regular expression to match against entity names
  58. //! - bounds : a shape to use for filtering entities, null means no bounds filtering
  59. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/srv/GetEntities.srv">GetEntities.srv</a>
  60. virtual AZ::Outcome<EntityNameList, FailedResult> GetEntities(const EntityFilters& filter) = 0;
  61. //! Get the state of an entity.
  62. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/srv/GetEntityState.srv">GetEntityState.srv</a>
  63. virtual AZ::Outcome<EntityState, FailedResult> GetEntityState(const AZStd::string& name) = 0;
  64. //! Get the state of all entities that match the filter.
  65. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/srv/GetEntitiesStates.srv">GetEntitiesStates.srv</a>
  66. virtual AZ::Outcome<MultipleEntitiesStates, FailedResult> GetEntitiesStates(const EntityFilters& filter) = 0;
  67. //! Set the state of an entity.
  68. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/srv/SetEntityState.srv">SetEntityState.srv</a>
  69. virtual AZ::Outcome<void, FailedResult> SetEntityState(const AZStd::string& name, const EntityState& state) = 0;
  70. //! Remove previously spawned entity from the simulation.
  71. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/srv/DeleteEntity.srv">DeleteEntity.srv</a>
  72. virtual void DeleteEntity(const AZStd::string& name, DeletionCompletedCb completedCb) = 0;
  73. //! Remove all previously spawned entity from the simulation.
  74. virtual void DeleteAllEntities(DeletionCompletedCb completedCb) = 0;
  75. //! Get a list of spawnable entities.
  76. //! @see <a href="https://github.com/ros-simulation/simulation_interfaces/blob/main/srv/GetSpawnables.srv">GetSpawnables.srv</a>
  77. virtual AZ::Outcome<SpawnableList, FailedResult> GetSpawnables() = 0;
  78. //! Callback for when an entity has been spawned and registered. The string is the name of the entity in the simulation interface.
  79. //! Note: The names are empty, if the entity could not be registered (e.g. prefab has no simulated entities)
  80. virtual void SpawnEntity(
  81. const AZStd::string& name,
  82. const AZStd::string& uri,
  83. const AZStd::string& entityNamespace,
  84. const AZ::Transform& initialPose,
  85. const bool allowRename,
  86. SpawnCompletedCb completedCb) = 0;
  87. //! Reset the simulation to begin.
  88. //! This will revert the entire simulation to the initial state.
  89. virtual void ResetAllEntitiesToInitialState() = 0;
  90. };
  91. class SimulationInterfacesBusTraits : public AZ::EBusTraits
  92. {
  93. public:
  94. //////////////////////////////////////////////////////////////////////////
  95. // EBusTraits overrides
  96. static constexpr AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  97. static constexpr AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  98. //////////////////////////////////////////////////////////////////////////
  99. };
  100. using SimulationEntityManagerRequestBus = AZ::EBus<SimulationEntityManagerRequests, SimulationInterfacesBusTraits>;
  101. using SimulationEntityManagerInterface = AZ::Interface<SimulationEntityManagerRequests>;
  102. } // namespace SimulationInterfaces