ROS2ContactSensorComponent.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/Sensor/Events/TickBasedSource.h>
  16. #include <ROS2/Sensor/ROS2SensorComponentBase.h>
  17. #include <ROS2Sensors/ROS2SensorsTypeIds.h>
  18. #include <gazebo_msgs/msg/contact_state.hpp>
  19. #include <gazebo_msgs/msg/contacts_state.hpp>
  20. #include <rclcpp/publisher.hpp>
  21. namespace ROS2Sensors
  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 ROS2::ROS2SensorComponentBase<ROS2::TickBasedSource>
  28. {
  29. public:
  30. using SensorBaseType = ROS2::ROS2SensorComponentBase<ROS2::TickBasedSource>;
  31. AZ_COMPONENT(ROS2ContactSensorComponent, ROS2ContactSensorComponentTypeId, SensorBaseType);
  32. ROS2ContactSensorComponent();
  33. ~ROS2ContactSensorComponent() = default;
  34. static void Reflect(AZ::ReflectContext* context);
  35. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  36. //////////////////////////////////////////////////////////////////////////
  37. // Component overrides
  38. void Activate() override;
  39. void Deactivate() override;
  40. //////////////////////////////////////////////////////////////////////////
  41. private:
  42. //////////////////////////////////////////////////////////////////////////
  43. void FrequencyTick();
  44. void AddNewContact(const AzPhysics::CollisionEvent& event);
  45. AZ::EntityId m_entityId;
  46. AZStd::string m_entityName = "";
  47. AzPhysics::SimulatedBodyEvents::OnCollisionBegin::Handler m_onCollisionBeginHandler;
  48. AzPhysics::SimulatedBodyEvents::OnCollisionPersist::Handler m_onCollisionPersistHandler;
  49. AzPhysics::SimulatedBodyEvents::OnCollisionEnd::Handler m_onCollisionEndHandler;
  50. std::shared_ptr<rclcpp::Publisher<gazebo_msgs::msg::ContactsState>> m_contactsPublisher;
  51. AZStd::unordered_map<AZ::EntityId, gazebo_msgs::msg::ContactState> m_activeContacts;
  52. AZStd::mutex m_activeContactsMutex;
  53. };
  54. } // namespace ROS2Sensors