AckermannDriveModel.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "AckermannDriveModel.h"
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzFramework/Physics/RigidBodyBus.h>
  14. #include <HingeJointComponent.h>
  15. #include <PhysX/ArticulationJointBus.h>
  16. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  17. #include <VehicleDynamics/Utilities.h>
  18. namespace ROS2Controllers::VehicleDynamics
  19. {
  20. void AckermannDriveModel::Reflect(AZ::ReflectContext* context)
  21. {
  22. AckermannModelLimits::Reflect(context);
  23. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  24. {
  25. serialize->Class<AckermannDriveModel, DriveModel>()
  26. ->Version(1)
  27. ->Field("SteeringPID", &AckermannDriveModel::m_steeringPid)
  28. ->Field("Limits", &AckermannDriveModel::m_limits);
  29. if (AZ::EditContext* ec = serialize->GetEditContext())
  30. {
  31. ec->Class<AckermannDriveModel>("Simplified Drive Model", "Configuration of a simplified vehicle dynamics drive model")
  32. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  33. ->DataElement(
  34. AZ::Edit::UIHandlers::Default,
  35. &AckermannDriveModel::m_steeringPid,
  36. "Steering PID",
  37. "Configuration of steering PID controller")
  38. ->DataElement(AZ::Edit::UIHandlers::Default, &AckermannDriveModel::m_limits, "Vehicle Limits", "Limits");
  39. }
  40. }
  41. }
  42. AckermannDriveModel::AckermannDriveModel(const AckermannModelLimits& limits, const PidConfiguration& steeringPid)
  43. : m_limits(limits)
  44. , m_steeringPid(steeringPid)
  45. {
  46. }
  47. void AckermannDriveModel::Activate(const VehicleConfiguration& vehicleConfig)
  48. {
  49. m_driveWheelsData.clear();
  50. m_steeringData.clear();
  51. m_vehicleConfiguration = vehicleConfig;
  52. m_steeringPid.InitializePid();
  53. }
  54. void AckermannDriveModel::ApplyState(const VehicleInputs& inputs, AZ::u64 deltaTimeNs)
  55. {
  56. if (m_driveWheelsData.empty())
  57. {
  58. m_driveWheelsData = VehicleDynamics::Utilities::GetAllDriveWheelsData(m_vehicleConfiguration);
  59. }
  60. if (m_steeringData.empty())
  61. {
  62. m_steeringData = VehicleDynamics::Utilities::GetAllSteeringEntitiesData(m_vehicleConfiguration);
  63. }
  64. const auto jointPositions = inputs.m_jointRequestedPosition;
  65. const float steering = jointPositions.empty() ? 0 : jointPositions.front();
  66. ApplySteering(steering, deltaTimeNs);
  67. ApplySpeed(inputs.m_speed.GetX(), deltaTimeNs);
  68. }
  69. void AckermannDriveModel::ApplyWheelSteering(SteeringDynamicsData& wheelData, float steering, double deltaTimeNs)
  70. {
  71. const auto& steeringEntity = wheelData.m_steeringEntity;
  72. const auto& jointComponent = wheelData.m_steeringJoint;
  73. auto id = AZ::EntityComponentIdPair(steeringEntity, jointComponent);
  74. auto scaledSteering = steering * wheelData.m_steeringScale;
  75. if (wheelData.m_isArticulation)
  76. {
  77. PhysX::ArticulationJointRequestBus::Event(
  78. steeringEntity,
  79. [&](PhysX::ArticulationJointRequests* joint)
  80. {
  81. double currentSteeringAngle = joint->GetJointPosition(wheelData.m_axis);
  82. const double pidCommand = m_steeringPid.ComputeCommand(scaledSteering - currentSteeringAngle, deltaTimeNs);
  83. joint->SetDriveTargetVelocity(wheelData.m_axis, pidCommand);
  84. });
  85. }
  86. else
  87. {
  88. PhysX::JointRequestBus::Event(
  89. id,
  90. [&](PhysX::JointRequests* joint)
  91. {
  92. double currentSteeringAngle = joint->GetPosition();
  93. const double pidCommand = m_steeringPid.ComputeCommand(scaledSteering - currentSteeringAngle, deltaTimeNs);
  94. joint->SetVelocity(pidCommand);
  95. });
  96. }
  97. }
  98. void AckermannDriveModel::ApplySteering(float steering, AZ::u64 deltaTimeNs)
  99. {
  100. if (m_disabled)
  101. {
  102. return;
  103. }
  104. if (m_steeringData.empty())
  105. {
  106. AZ_Warning("ApplySteering", false, "Cannot apply steering since no steering elements are defined in the model");
  107. return;
  108. }
  109. auto innerSteering = AZ::Atan2(
  110. (m_vehicleConfiguration.m_wheelbase * tan(steering)),
  111. (m_vehicleConfiguration.m_wheelbase - 0.5 * m_vehicleConfiguration.m_track * tan(steering)));
  112. auto outerSteering = AZ::Atan2(
  113. (m_vehicleConfiguration.m_wheelbase * tan(steering)),
  114. (m_vehicleConfiguration.m_wheelbase + 0.5 * m_vehicleConfiguration.m_track * tan(steering)));
  115. ApplyWheelSteering(m_steeringData.front(), innerSteering, deltaTimeNs);
  116. ApplyWheelSteering(m_steeringData.back(), outerSteering, deltaTimeNs);
  117. }
  118. void AckermannDriveModel::ApplySpeed(float speed, AZ::u64 deltaTimeNs)
  119. {
  120. if (m_disabled)
  121. {
  122. return;
  123. }
  124. const float acceleration = m_limits.GetLinearAcceleration();
  125. const float maxSpeed = m_limits.GetLinearSpeedLimit();
  126. m_speedCommand = Utilities::ComputeRampVelocity(speed, m_speedCommand, deltaTimeNs, acceleration, maxSpeed);
  127. if (m_driveWheelsData.empty())
  128. {
  129. AZ_Warning("ApplySpeed", false, "Cannot apply speed since no driving wheels are defined in the model");
  130. return;
  131. }
  132. for (const auto& wheelData : m_driveWheelsData)
  133. {
  134. float wheelRadius = wheelData.m_wheelRadius;
  135. AZ_Assert(wheelRadius != 0, "wheelRadius must be non-zero");
  136. auto desiredAngularSpeedX = (m_speedCommand / wheelRadius);
  137. VehicleDynamics::Utilities::SetWheelRotationSpeed(wheelData, desiredAngularSpeedX);
  138. }
  139. }
  140. const VehicleModelLimits* AckermannDriveModel::GetVehicleLimitPtr() const
  141. {
  142. return &m_limits;
  143. }
  144. AZStd::pair<AZ::Vector3, AZ::Vector3> AckermannDriveModel::GetVelocityFromModel()
  145. {
  146. AZ_Warning("ApplySpeed", false, "GetVelocityFromModel is not implemented for AckermannDriveModel");
  147. return AZStd::pair<AZ::Vector3, AZ::Vector3>{ AZ::Vector3::CreateZero(), AZ::Vector3::CreateZero() };
  148. }
  149. } // namespace ROS2Controllers::VehicleDynamics