VacuumGripperComponent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  4. * of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #pragma once
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzFramework/Physics/Common/PhysicsEvents.h>
  13. #include <AzFramework/Physics/Common/PhysicsSimulatedBodyEvents.h>
  14. #include <AzFramework/Physics/PhysicsSystem.h>
  15. #include <AzFramework/Physics/RigidBodyBus.h>
  16. #include <ImGuiBus.h>
  17. #include <LmbrCentral/Scripting/TagComponentBus.h>
  18. #include <ROS2Controllers/Gripper/GripperRequestBus.h>
  19. #include <ROS2Controllers/ROS2ControllersTypeIds.h>
  20. namespace ROS2Controllers
  21. {
  22. //! This component implements vacuum gripper functionality.
  23. //! It allows to attach to gripper objects that are in inside designated trigger collider to objects that has "Grippable" tag.
  24. //! To use component:
  25. //! - Attach component to root of robot articulation.
  26. //! - Assign to m_gripperEffectorCollider EntityId of the collider that will be used as the gripper effector.
  27. //! - Add tag "Grippable" to objects that can be gripped.
  28. class VacuumGripperComponent
  29. : public AZ::Component
  30. , public GripperRequestBus::Handler
  31. , public ImGui::ImGuiUpdateListenerBus::Handler
  32. , public AZ::TickBus::Handler
  33. {
  34. public:
  35. static constexpr AZ::Crc32 GrippableTag = AZ_CRC_CE("Grippable");
  36. AZ_COMPONENT(VacuumGripperComponent, VacuumGripperComponentTypeId, AZ::Component);
  37. VacuumGripperComponent() = default;
  38. ~VacuumGripperComponent() = default;
  39. // AZ::Component overrides...
  40. void Activate() override;
  41. void Deactivate() override;
  42. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  43. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  44. static void Reflect(AZ::ReflectContext* context);
  45. private:
  46. // GripperRequestBus::Handler overrides...
  47. AZ::Outcome<void, AZStd::string> GripperCommand(float position, float maxEffort) override;
  48. AZ::Outcome<void, AZStd::string> CancelGripperCommand() override;
  49. float GetGripperPosition() const override;
  50. float GetGripperEffort() const override;
  51. bool IsGripperNotMoving() const override;
  52. bool HasGripperReachedGoal() const override;
  53. bool HasGripperCommandBeenCancelled() const override;
  54. // AZ::TickBus::Handler overrides...
  55. void OnTick(float delta, AZ::ScriptTimePoint timePoint) override;
  56. // ImGui::ImGuiUpdateListenerBus::Handler overrides...
  57. void OnImGuiUpdate() override;
  58. //! Entity that contains the collider that will be used as the gripper
  59. //! effector/ The collider must be a trigger collider.
  60. AZ::EntityId m_gripperEffectorCollider;
  61. //! Entity that contains the articulation link that will be used as the gripper
  62. AZ::EntityId m_gripperEffectorArticulationLink;
  63. //! The physics body handle to m_gripperEffectorArticulationLink.
  64. AzPhysics::SimulatedBodyHandle m_gripperEffectorBodyHandle;
  65. //! EntityId of the object that is currently gripped by the gripper effector.
  66. AZ::EntityId m_grippedObjectInEffector;
  67. //! Handle to joint created by vacuum gripper.
  68. AzPhysics::JointHandle m_vacuumJoint;
  69. bool m_tryingToGrip{ false };
  70. bool m_cancelGripperCommand{ false };
  71. AzPhysics::SimulatedBodyEvents::OnTriggerEnter::Handler m_onTriggerEnterHandler;
  72. AzPhysics::SimulatedBodyEvents::OnTriggerExit::Handler m_onTriggerExitHandler;
  73. //! Checks if object is grippable (has Tag).
  74. bool isObjectGrippable(const AZ::EntityId entityId);
  75. //! Checks if an object is in the gripper effector collider and creates a joint between gripper effector and object.
  76. bool TryToGripObject();
  77. //! Releases object from gripper effector.
  78. void ReleaseGrippedObject();
  79. void AttachToGripper(
  80. AzPhysics::SimulatedBody* gripperBody, AzPhysics::RigidBody* grippedRigidBody, AzPhysics::SceneInterface* sceneInterface);
  81. };
  82. } // namespace ROS2Controllers