ROS2ContactSensorComponent.h 2.5 KB

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