PidMotorControllerComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <AzCore/Component/ComponentBus.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  12. #include <ROS2Controllers/Manipulation/MotorizedJoints/PidMotorControllerComponent.h>
  13. #include <imgui/imgui.h>
  14. namespace ROS2Controllers
  15. {
  16. void PidMotorControllerComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. serializeContext->Class<PidMotorControllerComponent, JointMotorControllerComponent>()
  21. ->Field("ZeroOffset", &PidMotorControllerComponent::m_zeroOffset)
  22. ->Field("PidPosition", &PidMotorControllerComponent::m_pidPos)
  23. ->Version(2);
  24. if (AZ::EditContext* ec = serializeContext->GetEditContext())
  25. {
  26. ec->Class<PidMotorControllerComponent>("PID Motor Controller", "")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  29. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  30. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/PidMotorController.svg")
  31. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/PidMotorController.svg")
  32. ->DataElement(
  33. AZ::Edit::UIHandlers::Default,
  34. &PidMotorControllerComponent::m_zeroOffset,
  35. "Zero Offset",
  36. "Allows to change offset of zero to set point")
  37. ->DataElement(AZ::Edit::UIHandlers::Default, &PidMotorControllerComponent::m_pidPos, "Pid Position", "Pid Position");
  38. }
  39. }
  40. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  41. {
  42. behaviorContext->EBus<PidMotorControllerRequestBus>("PidMotorControllerComponent", "PidMotorControllerRequestBus")
  43. ->Attribute(AZ::Edit::Attributes::Category, "ROS2/PidMotorController")
  44. ->Event("SetSetpoint", &PidMotorControllerRequestBus::Events::SetSetpoint)
  45. ->Event("GetSetpoint", &PidMotorControllerRequestBus::Events::GetSetpoint)
  46. ->Event("GetCurrentMeasurement", &PidMotorControllerRequestBus::Events::GetCurrentMeasurement)
  47. ->Event("GetError", &PidMotorControllerRequestBus::Events::GetError);
  48. }
  49. }
  50. void PidMotorControllerComponent::Activate()
  51. {
  52. m_pidPos.InitializePid();
  53. PidMotorControllerRequestBus::Handler::BusConnect(GetEntityId());
  54. JointMotorControllerComponent::Activate();
  55. }
  56. void PidMotorControllerComponent::Deactivate()
  57. {
  58. JointMotorControllerComponent::Deactivate();
  59. PidMotorControllerRequestBus::Handler::BusDisconnect();
  60. }
  61. void PidMotorControllerComponent::SetSetpoint(float setpoint)
  62. {
  63. m_setPoint = setpoint;
  64. }
  65. float PidMotorControllerComponent::GetSetpoint()
  66. {
  67. return m_setPoint;
  68. }
  69. float PidMotorControllerComponent::GetCurrentMeasurement()
  70. {
  71. return m_currentPosition - m_zeroOffset;
  72. }
  73. float PidMotorControllerComponent::GetError()
  74. {
  75. return m_error;
  76. }
  77. float PidMotorControllerComponent::CalculateMotorSpeed([[maybe_unused]] float deltaTime)
  78. {
  79. const float controlPositionError = (m_setPoint + m_zeroOffset) - m_currentPosition;
  80. m_error = controlPositionError;
  81. const auto deltaTimeNs = aznumeric_cast<uint64_t>(deltaTime * 1.0e9f);
  82. return aznumeric_cast<float>(m_pidPos.ComputeCommand(controlPositionError, deltaTimeNs));
  83. }
  84. void PidMotorControllerComponent::DisplayControllerParameters()
  85. {
  86. AZStd::pair<float, float> limits{ 0.0f, 0.0f };
  87. PhysX::JointRequestBus::EventResult(limits, m_jointComponentIdPair, &PhysX::JointRequests::GetLimits);
  88. ImGui::PushItemWidth(200.0f);
  89. ImGui::SliderFloat("SetPoint", &m_setPoint, limits.first + m_zeroOffset, limits.second + m_zeroOffset);
  90. ImGui::PopItemWidth();
  91. }
  92. } // namespace ROS2Controllers