ROS2ContactSensorComponent.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifndef WITH_GAZEBO_MSGS
  10. static_assert(false, "This file should not be included in the build, without WITH_GAZEBO_MSGS defined.");
  11. #endif
  12. #include <AzCore/Component/EntityId.h>
  13. #include <AzCore/RTTI/ReflectContext.h>
  14. #include <AzCore/std/containers/unordered_map.h>
  15. #include <AzCore/std/parallel/mutex.h>
  16. #include <AzCore/std/string/string.h>
  17. #include <AzFramework/Physics/Common/PhysicsSimulatedBodyEvents.h>
  18. #include <ROS2/Sensor/Events/TickBasedSource.h>
  19. #include <ROS2/Sensor/ROS2SensorComponentBase.h>
  20. #include <ROS2Sensors/ROS2SensorsTypeIds.h>
  21. #include <gazebo_msgs/msg/contact_state.hpp>
  22. #include <gazebo_msgs/msg/contacts_state.hpp>
  23. #include <rclcpp/publisher.hpp>
  24. namespace ROS2Sensors
  25. {
  26. //! Contact sensor detects collisions between two objects.
  27. //! It reports the location of the contact associated forces.
  28. //! This component publishes a contact_sensor topic.
  29. //! It doesn't measure torque.
  30. class ROS2ContactSensorComponent : public ROS2::ROS2SensorComponentBase<ROS2::TickBasedSource>
  31. {
  32. public:
  33. using SensorBaseType = ROS2::ROS2SensorComponentBase<ROS2::TickBasedSource>;
  34. AZ_COMPONENT(ROS2ContactSensorComponent, ROS2ContactSensorComponentTypeId, SensorBaseType);
  35. ROS2ContactSensorComponent();
  36. ~ROS2ContactSensorComponent() = default;
  37. static void Reflect(AZ::ReflectContext* context);
  38. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  39. //////////////////////////////////////////////////////////////////////////
  40. // Component overrides
  41. void Activate() override;
  42. void Deactivate() override;
  43. //////////////////////////////////////////////////////////////////////////
  44. private:
  45. //////////////////////////////////////////////////////////////////////////
  46. void FrequencyTick();
  47. void AddNewContact(const AzPhysics::CollisionEvent& event);
  48. AZ::EntityId m_entityId;
  49. AZStd::string m_entityName = "";
  50. AzPhysics::SimulatedBodyEvents::OnCollisionBegin::Handler m_onCollisionBeginHandler;
  51. AzPhysics::SimulatedBodyEvents::OnCollisionPersist::Handler m_onCollisionPersistHandler;
  52. AzPhysics::SimulatedBodyEvents::OnCollisionEnd::Handler m_onCollisionEndHandler;
  53. std::shared_ptr<rclcpp::Publisher<gazebo_msgs::msg::ContactsState>> m_contactsPublisher;
  54. AZStd::unordered_map<AZ::EntityId, gazebo_msgs::msg::ContactState> m_activeContacts;
  55. AZStd::mutex m_activeContactsMutex;
  56. };
  57. } // namespace ROS2Sensors