JointMotorControllerComponent.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/Serialization/EditContext.h>
  9. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  10. #include <HingeJointComponent.h>
  11. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  12. #include <PrismaticJointComponent.h>
  13. #include <ROS2/ROS2Bus.h>
  14. #include <ROS2/Clock/ROS2ClockRequestBus.h>
  15. #include <ROS2/Utilities/ROS2Conversions.h>
  16. #include <ROS2Controllers/Manipulation/MotorizedJoints/JointMotorControllerComponent.h>
  17. #include <imgui/imgui.h>
  18. namespace ROS2Controllers
  19. {
  20. void JointMotorControllerComponent::Activate()
  21. {
  22. ROS2::ROS2ClockRequestBus::BroadcastResult(m_lastTickTimestamp, &ROS2::ROS2ClockRequestBus::Events::GetROSTimestamp);
  23. AZ::TickBus::Handler::BusConnect();
  24. ImGui::ImGuiUpdateListenerBus::Handler::BusConnect();
  25. AZ::EntityBus::Handler::BusConnect(GetEntityId());
  26. }
  27. void JointMotorControllerComponent::Deactivate()
  28. {
  29. ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect();
  30. AZ::TickBus::Handler::BusDisconnect();
  31. }
  32. void JointMotorControllerComponent::Reflect(AZ::ReflectContext* context)
  33. {
  34. JointMotorControllerConfiguration::Reflect(context);
  35. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  36. {
  37. serializeContext->Class<JointMotorControllerComponent, AZ::Component>()->Version(2)->Field(
  38. "JointMotorControllerConfiguration", &JointMotorControllerComponent::m_jointMotorControllerConfiguration);
  39. if (AZ::EditContext* ec = serializeContext->GetEditContext())
  40. {
  41. ec->Class<JointMotorControllerComponent>(
  42. "Joint Motor Controller Component", "Base component for motor controller components.")
  43. ->DataElement(
  44. AZ::Edit::UIHandlers::Default,
  45. &JointMotorControllerComponent::m_jointMotorControllerConfiguration,
  46. "Motor Controller Configuration",
  47. "Motor Controller Configuration");
  48. }
  49. }
  50. }
  51. void JointMotorControllerComponent::OnImGuiUpdate()
  52. {
  53. if (!m_jointComponentIdPair.GetEntityId().IsValid() ||
  54. !(m_jointMotorControllerConfiguration.m_isDebugController || m_jointMotorControllerConfiguration.m_debugMode))
  55. {
  56. return;
  57. }
  58. AZStd::pair<float, float> limits{ 0.0f, 0.0f };
  59. PhysX::JointRequestBus::EventResult(limits, m_jointComponentIdPair, &PhysX::JointRequests::GetLimits);
  60. PhysX::JointRequestBus::EventResult(m_currentSpeed, m_jointComponentIdPair, &PhysX::JointRequests::GetVelocity);
  61. AZStd::string s =
  62. AZStd::string::format("Joint Motor Controller %s:%s", GetEntity()->GetName().c_str(), GetEntity()->GetId().ToString().c_str());
  63. ImGui::Begin(s.c_str());
  64. ImGui::PushItemWidth(200.0f);
  65. ImGui::SliderFloat("Position", &m_currentPosition, limits.first, limits.second);
  66. ImGui::SliderFloat("Speed", &m_currentSpeed, -5.0f, 5.0f);
  67. ImGui::PopItemWidth();
  68. DisplayControllerParameters();
  69. ImGui::End();
  70. }
  71. void JointMotorControllerComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  72. {
  73. if (!m_jointComponentIdPair.GetEntityId().IsValid())
  74. {
  75. return;
  76. }
  77. PhysX::JointRequestBus::EventResult(m_currentPosition, m_jointComponentIdPair, &PhysX::JointRequests::GetPosition);
  78. builtin_interfaces::msg::Time timestamp;
  79. ROS2::ROS2ClockRequestBus::BroadcastResult(timestamp, &ROS2::ROS2ClockRequestBus::Events::GetROSTimestamp);
  80. const float deltaSimTime = ROS2::ROS2Conversions::GetTimeDifference(m_lastTickTimestamp, timestamp);
  81. const float setSpeed = CalculateMotorSpeed(deltaSimTime);
  82. PhysX::JointRequestBus::Event(m_jointComponentIdPair, &PhysX::JointRequests::SetVelocity, setSpeed);
  83. m_lastTickTimestamp = timestamp;
  84. }
  85. void JointMotorControllerComponent::OnEntityActivated(const AZ::EntityId& entityId)
  86. {
  87. AZ::ComponentId componentId;
  88. if (auto* prismaticJointComponent = GetEntity()->FindComponent<PhysX::PrismaticJointComponent>(); prismaticJointComponent)
  89. {
  90. componentId = prismaticJointComponent->GetId();
  91. }
  92. else if (auto* hingeJointComponent = GetEntity()->FindComponent<PhysX::HingeJointComponent>(); hingeJointComponent)
  93. {
  94. componentId = hingeJointComponent->GetId();
  95. }
  96. else
  97. {
  98. AZ_Warning(
  99. "MotorizedJointComponent",
  100. false,
  101. "Entity with ID %s either has no PhysX::Joint component or the joint is neither a Prismatic nor a Hinge Joint",
  102. GetEntityId().ToString().c_str());
  103. }
  104. m_jointComponentIdPair = { GetEntityId(), componentId };
  105. }
  106. } // namespace ROS2Controllers