2
0

Utilities.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "Utilities.h"
  9. #include "Source/ArticulationLinkComponent.h"
  10. #include "WheelControllerComponent.h"
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Component/Entity.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <HingeJointComponent.h>
  15. #include <PhysX/Joint/PhysXJointRequestsBus.h>
  16. #include <Utilities/ArticulationsUtilities.h>
  17. namespace ROS2Controllers::VehicleDynamics::Utilities
  18. {
  19. AxleConfiguration Create2WheelAxle(
  20. AZ::EntityId leftWheel, AZ::EntityId rightWheel, AZStd::string tag, float wheelRadius, bool steering, bool drive)
  21. {
  22. VehicleDynamics::AxleConfiguration axleConfiguration;
  23. axleConfiguration.m_axleWheels.push_back(leftWheel);
  24. axleConfiguration.m_axleWheels.push_back(rightWheel);
  25. axleConfiguration.m_axleTag = AZStd::move(tag);
  26. axleConfiguration.m_isSteering = steering;
  27. axleConfiguration.m_isDrive = drive;
  28. axleConfiguration.m_wheelRadius = wheelRadius;
  29. return axleConfiguration;
  30. }
  31. AxleConfiguration CreateFrontSteerAndDriveAxle(AZ::EntityId leftWheel, AZ::EntityId rightWheel, float wheelRadius)
  32. {
  33. return Create2WheelAxle(leftWheel, rightWheel, "Front", wheelRadius, true, true);
  34. }
  35. AxleConfiguration CreateRearDriveAxle(AZ::EntityId leftWheel, AZ::EntityId rightWheel, float wheelRadius)
  36. {
  37. return Create2WheelAxle(leftWheel, rightWheel, "Rear", wheelRadius, false, true);
  38. }
  39. AZStd::vector<VehicleDynamics::SteeringDynamicsData> GetAllSteeringEntitiesData(const VehicleConfiguration& vehicleConfig)
  40. {
  41. AZStd::vector<VehicleDynamics::SteeringDynamicsData> steeringEntitiesAndAxis;
  42. for (const auto& axle : vehicleConfig.m_axles)
  43. {
  44. if (!axle.m_isSteering)
  45. { // Only steering axles will have steering entities
  46. continue;
  47. }
  48. for (const auto& wheel : axle.m_axleWheels)
  49. {
  50. if (!wheel.IsValid())
  51. {
  52. AZ_Warning("GetAllSteeringEntitiesData", false, "Wheel entity in axle %s is invalid, ignoring", axle.m_axleTag.c_str());
  53. continue;
  54. }
  55. AZ::Entity* wheelEntity = nullptr;
  56. AZ::ComponentApplicationBus::BroadcastResult(wheelEntity, &AZ::ComponentApplicationRequests::FindEntity, wheel);
  57. if (!wheelEntity)
  58. {
  59. AZ_Warning("GetAllSteeringEntitiesData", false, "Wheel entity in axle %s is null, ignoring", axle.m_axleTag.c_str());
  60. continue;
  61. }
  62. auto* controllerComponent = wheelEntity->FindComponent<WheelControllerComponent>();
  63. if (!controllerComponent)
  64. {
  65. AZ_Warning(
  66. "GetAllSteeringEntitiesData",
  67. false,
  68. "Missing a WheelController in wheel entity %s, ignoring",
  69. wheel.ToString().c_str());
  70. continue;
  71. }
  72. AZ::EntityId steeringEntity = controllerComponent->m_steeringEntity;
  73. if (!steeringEntity.IsValid())
  74. {
  75. AZ_Warning(
  76. "GetAllSteeringEntitiesData",
  77. false,
  78. "Steering entity specified for WheelController in entity %s is invalid, ignoring",
  79. wheel.ToString().c_str());
  80. continue;
  81. }
  82. const float steeringScale = controllerComponent->m_steeringScale;
  83. AZ::Entity* steeringEntityptr{ nullptr };
  84. AZ::ComponentApplicationBus::BroadcastResult(
  85. steeringEntityptr, &AZ::ComponentApplicationRequests::FindEntity, steeringEntity);
  86. AZ_Assert(steeringEntityptr, "Cannot find a steering entity for %s", steeringEntity.ToString().c_str());
  87. PhysX::HingeJointComponent* hingeComponent{ nullptr };
  88. hingeComponent = steeringEntityptr->FindComponent<PhysX::HingeJointComponent>();
  89. PhysX::ArticulationLinkComponent* articulation{ nullptr };
  90. articulation = steeringEntityptr->FindComponent<PhysX::ArticulationLinkComponent>();
  91. if (!hingeComponent && !articulation)
  92. {
  93. AZ_Warning(
  94. "GetAllSteeringEntitiesData",
  95. false,
  96. "Steering entity specified for WheelController in entity %s does not have either a HingeJointComponent or an "
  97. "ArticulationLinkComponent, ignoring",
  98. wheel.ToString().c_str());
  99. continue;
  100. }
  101. if (articulation)
  102. {
  103. if (articulation->m_config.m_articulationJointType != PhysX::ArticulationJointType::Hinge)
  104. {
  105. AZ_Warning(
  106. "GetAllSteeringEntitiesData",
  107. false,
  108. "Steering entity specified for WheelController in entity %s has an Articulation Link, but it's not a hinge "
  109. "joint, ignoring",
  110. wheel.ToString().c_str());
  111. continue;
  112. }
  113. }
  114. VehicleDynamics::SteeringDynamicsData steeringData;
  115. steeringData.m_steeringScale = steeringScale;
  116. steeringData.m_steeringEntity = steeringEntity;
  117. steeringData.m_isArticulation = articulation;
  118. if (articulation)
  119. {
  120. steeringData.m_steeringJoint = articulation->GetId();
  121. [[maybe_unused]] const bool hasFreeAxis =
  122. Utils::TryGetFreeArticulationAxis(steeringData.m_steeringEntity, steeringData.m_axis);
  123. AZ_Error("VehicleDynamics::Utilities", hasFreeAxis, "Articulation steering has no free axis somehow");
  124. }
  125. else
  126. {
  127. steeringData.m_steeringJoint = hingeComponent->GetId();
  128. }
  129. steeringEntitiesAndAxis.push_back(steeringData);
  130. }
  131. }
  132. return steeringEntitiesAndAxis;
  133. }
  134. AZStd::vector<VehicleDynamics::WheelDynamicsData> GetAllDriveWheelsData(const VehicleConfiguration& vehicleConfig)
  135. {
  136. AZStd::vector<VehicleDynamics::WheelDynamicsData> driveWheelEntities;
  137. for (const AxleConfiguration& axle : vehicleConfig.m_axles)
  138. {
  139. if (!axle.m_isDrive)
  140. { // Get only drive wheels, which are attached to a drive axle
  141. continue;
  142. }
  143. for (const AZ::EntityId& wheel : axle.m_axleWheels)
  144. {
  145. if (!wheel.IsValid())
  146. {
  147. AZ_Warning("GetAllSteeringEntitiesData", false, "Wheel entity in axle %s is invalid, ignoring", axle.m_axleTag.c_str());
  148. continue;
  149. }
  150. AZ::Entity* wheelEntity = nullptr;
  151. AZ::ComponentApplicationBus::BroadcastResult(wheelEntity, &AZ::ComponentApplicationRequests::FindEntity, wheel);
  152. if (!wheelEntity)
  153. {
  154. AZ_Warning("GetAllSteeringEntitiesData", false, "Wheel entity in axle %s is null, ignoring", axle.m_axleTag.c_str());
  155. continue;
  156. }
  157. auto* controllerComponent = wheelEntity->FindComponent<WheelControllerComponent>();
  158. if (!controllerComponent)
  159. {
  160. AZ_Warning(
  161. "GetAllDriveWheelsData",
  162. false,
  163. "Wheel entity for axle %s is missing a WheelController component, ignoring",
  164. axle.m_axleTag.c_str());
  165. continue;
  166. }
  167. VehicleDynamics::WheelDynamicsData wheelData = GetWheelData(wheel, axle.m_wheelRadius);
  168. driveWheelEntities.push_back(wheelData);
  169. }
  170. }
  171. return driveWheelEntities;
  172. }
  173. VehicleDynamics::WheelDynamicsData GetWheelData(const AZ::EntityId wheelEntityId, float wheelRadius)
  174. {
  175. VehicleDynamics::WheelDynamicsData wheelData;
  176. wheelData.m_wheelEntity = wheelEntityId;
  177. wheelData.m_wheelRadius = wheelRadius;
  178. AZ::Entity* wheelEntity = nullptr;
  179. AZ::ComponentApplicationBus::BroadcastResult(wheelEntity, &AZ::ComponentApplicationRequests::FindEntity, wheelEntityId);
  180. if (!wheelEntity)
  181. {
  182. AZ_Warning("GetWheelDynamicData", false, "Entity %s was not found", wheelEntityId.ToString().c_str());
  183. return wheelData;
  184. }
  185. PhysX::HingeJointComponent* hingeComponent{ nullptr };
  186. hingeComponent = wheelEntity->FindComponent<PhysX::HingeJointComponent>();
  187. PhysX::ArticulationLinkComponent* articulationComponent{ nullptr };
  188. articulationComponent = wheelEntity->FindComponent<PhysX::ArticulationLinkComponent>();
  189. if (hingeComponent)
  190. {
  191. wheelData.m_isArticulation = false;
  192. wheelData.m_wheelJoint = hingeComponent->GetId();
  193. return wheelData;
  194. }
  195. if (articulationComponent)
  196. {
  197. wheelData.m_isArticulation = true;
  198. Utils::TryGetFreeArticulationAxis(wheelEntityId, wheelData.m_axis);
  199. wheelData.m_wheelJoint = articulationComponent->GetId();
  200. return wheelData;
  201. }
  202. AZ_Warning("GetWheelDynamicData", false, "Entity %s has no PhysX::HingeJointComponent", wheelEntityId.ToString().c_str());
  203. return wheelData;
  204. }
  205. float ComputeRampVelocity(float targetVelocity, float lastVelocity, AZ::u64 deltaTimeNs, float acceleration, float maxVelocity)
  206. {
  207. const float deltaTimeSec = 1e-9f * static_cast<float>(deltaTimeNs);
  208. const float deltaAcceleration = deltaTimeSec * acceleration;
  209. float commandVelocity = 0;
  210. if (AZStd::abs(lastVelocity - targetVelocity) < deltaAcceleration)
  211. {
  212. commandVelocity = targetVelocity;
  213. }
  214. else if (targetVelocity > lastVelocity)
  215. {
  216. commandVelocity = lastVelocity + deltaAcceleration;
  217. }
  218. else if (targetVelocity < lastVelocity)
  219. {
  220. commandVelocity = lastVelocity - deltaAcceleration;
  221. }
  222. return AZStd::clamp(commandVelocity, -maxVelocity, maxVelocity);
  223. }
  224. void SetWheelRotationSpeed(const VehicleDynamics::WheelDynamicsData& data, float wheelRotationSpeed)
  225. {
  226. if (data.m_isArticulation)
  227. {
  228. PhysX::ArticulationJointRequestBus::Event(
  229. data.m_wheelEntity, &PhysX::ArticulationJointRequests::SetDriveTargetVelocity, data.m_axis, wheelRotationSpeed);
  230. }
  231. else
  232. {
  233. PhysX::JointRequestBus::Event(
  234. AZ::EntityComponentIdPair(data.m_wheelEntity, data.m_wheelJoint), &PhysX::JointRequests::SetVelocity, wheelRotationSpeed);
  235. }
  236. }
  237. AZ::Transform GetJointTransform(const VehicleDynamics::WheelDynamicsData& data)
  238. {
  239. AZ::Transform hingeTransform{ AZ::Transform::Identity() };
  240. if (!data.m_isArticulation)
  241. {
  242. PhysX::JointRequestBus::EventResult(
  243. hingeTransform, AZ::EntityComponentIdPair(data.m_wheelEntity, data.m_wheelJoint), &PhysX::JointRequests::GetTransform);
  244. }
  245. return hingeTransform;
  246. }
  247. } // namespace ROS2Controllers::VehicleDynamics::Utilities