JointsPIDControllerComponent.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "JointsPIDControllerComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  11. namespace ROS2
  12. {
  13. void JointsPIDControllerComponent::Activate()
  14. {
  15. JointsPositionControllerRequestBus::Handler::BusConnect(GetEntityId());
  16. InitializePIDs();
  17. }
  18. void JointsPIDControllerComponent::Deactivate()
  19. {
  20. JointsPositionControllerRequestBus::Handler::BusDisconnect();
  21. }
  22. void JointsPIDControllerComponent::InitializePIDs()
  23. {
  24. for (auto& [jointName, pid] : m_pidConfiguration)
  25. {
  26. pid.InitializePid();
  27. }
  28. }
  29. AZ::Outcome<void, AZStd::string> JointsPIDControllerComponent::PositionControl(
  30. const AZStd::string& jointName,
  31. JointInfo joint,
  32. JointPosition currentPosition,
  33. JointPosition targetPosition,
  34. float deltaTime)
  35. {
  36. if (joint.m_isArticulation)
  37. {
  38. return AZ::Failure(AZStd::string::format("Joint %s is articulation link, JointsPIDControllerComponent only handles classic Hinge joints. Use "
  39. "JointsArticulationControllerComponent instead", jointName.c_str()));
  40. }
  41. bool jointPIDdefined = m_pidConfiguration.find(jointName) != m_pidConfiguration.end();
  42. AZ_Warning(
  43. "JointsPIDControllerComponent",
  44. jointPIDdefined,
  45. "PID not defined for joint %s, using a default, the behavior is likely to be wrong for this joint",
  46. jointName.c_str());
  47. Controllers::PidConfiguration defaultConfiguration;
  48. defaultConfiguration.InitializePid();
  49. auto applicablePidConfiguration = jointPIDdefined ? m_pidConfiguration.at(jointName) : defaultConfiguration;
  50. uint64_t deltaTimeNs = deltaTime * 1'000'000'000;
  51. auto positionError = targetPosition - currentPosition;
  52. float desiredVelocity = applicablePidConfiguration.ComputeCommand(positionError, deltaTimeNs);
  53. PhysX::JointRequestBus::Event(joint.m_entityComponentIdPair, &PhysX::JointRequests::SetVelocity, desiredVelocity);
  54. return AZ::Success();
  55. }
  56. void JointsPIDControllerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  57. {
  58. provided.push_back(AZ_CRC_CE("JointsControllerService"));
  59. }
  60. void JointsPIDControllerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  61. {
  62. incompatible.push_back(AZ_CRC_CE("JointsControllerService"));
  63. }
  64. void JointsPIDControllerComponent::Reflect(AZ::ReflectContext* context)
  65. {
  66. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  67. {
  68. serialize->Class<JointsPIDControllerComponent, AZ::Component>()->Version(0)->Field(
  69. "JointsPIDs", &JointsPIDControllerComponent::m_pidConfiguration);
  70. if (AZ::EditContext* ec = serialize->GetEditContext())
  71. {
  72. ec->Class<JointsPIDControllerComponent>("JointsPIDControllerComponent", "Component to move joints")
  73. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  74. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  75. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  76. ->DataElement(
  77. AZ::Edit::UIHandlers::Default,
  78. &JointsPIDControllerComponent::m_pidConfiguration,
  79. "Joint PIDs",
  80. "PID configuration for each free joint in this entity hierarchy");
  81. }
  82. }
  83. }
  84. } // namespace ROS2