ManipulationUtils.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "ManipulationUtils.h"
  9. #include <PhysX/ArticulationJointBus.h>
  10. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  11. namespace ROS2Controllers::Utils
  12. {
  13. JointStateData GetJointState(const JointInfo& jointInfo)
  14. {
  15. JointStateData result;
  16. if (jointInfo.m_isArticulation)
  17. {
  18. PhysX::ArticulationJointRequestBus::Event(
  19. jointInfo.m_entityComponentIdPair.GetEntityId(),
  20. [&](PhysX::ArticulationJointRequests* articulationJointRequests)
  21. {
  22. result.position = articulationJointRequests->GetJointPosition(jointInfo.m_axis);
  23. result.velocity = articulationJointRequests->GetJointVelocity(jointInfo.m_axis);
  24. const bool is_acceleration_driven = articulationJointRequests->IsAccelerationDrive(jointInfo.m_axis);
  25. if (!is_acceleration_driven)
  26. {
  27. const float stiffness = articulationJointRequests->GetDriveStiffness(jointInfo.m_axis);
  28. const float damping = articulationJointRequests->GetDriveDamping(jointInfo.m_axis);
  29. const float targetPosition = articulationJointRequests->GetDriveTarget(jointInfo.m_axis);
  30. const float targetVelocity = articulationJointRequests->GetDriveTargetVelocity(jointInfo.m_axis);
  31. const float maxEffort = articulationJointRequests->GetMaxForce(jointInfo.m_axis);
  32. result.effort = stiffness * -(result.position - targetPosition) + damping * (targetVelocity - result.velocity);
  33. result.effort = AZ::GetClamp(result.effort, -maxEffort, maxEffort);
  34. }
  35. });
  36. }
  37. else
  38. {
  39. PhysX::JointRequestBus::Event(
  40. jointInfo.m_entityComponentIdPair,
  41. [&](PhysX::JointRequests* jointRequests)
  42. {
  43. result.position = jointRequests->GetPosition();
  44. result.velocity = jointRequests->GetVelocity();
  45. });
  46. }
  47. return result;
  48. }
  49. } // namespace ROS2Controllers::Utils