JointsPIDControllerComponent.cpp 4.1 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, JointInfo joint, JointPosition currentPosition, JointPosition targetPosition, float deltaTime)
  31. {
  32. if (joint.m_isArticulation)
  33. {
  34. return AZ::Failure(AZStd::string::format(
  35. "Joint %s is articulation link, JointsPIDControllerComponent only handles classic Hinge joints. Use "
  36. "JointsArticulationControllerComponent instead",
  37. jointName.c_str()));
  38. }
  39. bool jointPIDdefined = m_pidConfiguration.find(jointName) != m_pidConfiguration.end();
  40. AZ_Warning(
  41. "JointsPIDControllerComponent",
  42. jointPIDdefined,
  43. "PID not defined for joint %s, using a default, the behavior is likely to be wrong for this joint",
  44. jointName.c_str());
  45. Controllers::PidConfiguration defaultConfiguration;
  46. defaultConfiguration.InitializePid();
  47. auto applicablePidConfiguration = jointPIDdefined ? m_pidConfiguration.at(jointName) : defaultConfiguration;
  48. uint64_t deltaTimeNs = deltaTime * 1'000'000'000;
  49. auto positionError = targetPosition - currentPosition;
  50. float desiredVelocity = applicablePidConfiguration.ComputeCommand(positionError, deltaTimeNs);
  51. PhysX::JointRequestBus::Event(joint.m_entityComponentIdPair, &PhysX::JointRequests::SetVelocity, desiredVelocity);
  52. return AZ::Success();
  53. }
  54. void JointsPIDControllerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  55. {
  56. provided.push_back(AZ_CRC_CE("JointsControllerService"));
  57. }
  58. void JointsPIDControllerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  59. {
  60. incompatible.push_back(AZ_CRC_CE("JointsControllerService"));
  61. }
  62. void JointsPIDControllerComponent::Reflect(AZ::ReflectContext* context)
  63. {
  64. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  65. {
  66. serialize->Class<JointsPIDControllerComponent, AZ::Component>()->Version(0)->Field(
  67. "JointsPIDs", &JointsPIDControllerComponent::m_pidConfiguration);
  68. if (AZ::EditContext* ec = serialize->GetEditContext())
  69. {
  70. ec->Class<JointsPIDControllerComponent>("JointsPIDControllerComponent", "Component to move joints")
  71. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  72. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  73. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  74. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/JointsPIDControllerComponent.svg")
  75. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/JointsPIDControllerComponent.svg")
  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