SkidSteeringDriveModel.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #pragma once
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <VehicleDynamics/DriveModel.h>
  11. #include <ROS2Controllers/VehicleDynamics/VehicleConfiguration.h>
  12. #include <VehicleDynamics/ModelLimits/SkidSteeringModelLimits.h>
  13. #include <VehicleDynamics/VehicleInputs.h>
  14. #include <VehicleDynamics/WheelControllerComponent.h>
  15. #include <VehicleDynamics/WheelDynamicsData.h>
  16. namespace ROS2Controllers::VehicleDynamics
  17. {
  18. //! A simple skid steering system implementation converting speed and steering inputs into wheel rotation
  19. class SkidSteeringDriveModel : public DriveModel
  20. {
  21. public:
  22. AZ_RTTI(SkidSteeringDriveModel, "{04AE1BF2-621A-46C3-B025-E0875856850D}", DriveModel);
  23. SkidSteeringDriveModel() = default;
  24. SkidSteeringDriveModel(const SkidSteeringModelLimits& limits);
  25. // DriveModel overrides
  26. void Activate(const VehicleConfiguration& vehicleConfig) override;
  27. static void Reflect(AZ::ReflectContext* context);
  28. protected:
  29. // DriveModel overrides
  30. void ApplyState(const VehicleInputs& inputs, AZ::u64 deltaTimeNs) override;
  31. const VehicleModelLimits* GetVehicleLimitPtr() const override;
  32. AZStd::pair<AZ::Vector3, AZ::Vector3> GetVelocityFromModel() override;
  33. private:
  34. //! Data to compute the impact of a single wheel on the vehicle's velocity and to access the wheel to get/set target values.
  35. struct SkidSteeringWheelData
  36. {
  37. WheelControllerComponent* wheelControllerComponentPtr{ nullptr }; //!< Pointer to wheel controller to set/get rotation speed.
  38. WheelDynamicsData wheelData; //!< Wheel parameters.
  39. float wheelPosition{ 0.0f }; //!< Normalized distance between the wheel and the axle's center.
  40. float dX{ 0.0f }; //!< Wheel's contribution to vehicle's linear movement.
  41. float dPhi{ 0.0f }; //!< Wheel's contribution to vehicle's rotational movement.
  42. AZ::Vector3 axis{ AZ::Vector3::CreateZero() }; //!< Rotation axis of the wheel.
  43. };
  44. AZStd::vector<SkidSteeringWheelData> m_wheelsData; //!< Buffer with pre-calculated wheels' data.
  45. bool m_initialized = false; //!< Information if m_wheelsData was pre-calculated.
  46. //! Compute all necessary data for wheels' contribution to the vehicle's velocity and store it in the buffer.
  47. void ComputeWheelsData();
  48. //! Compute all necessary data for a single wheel's contribution to the vehicle's velocity. It can be thought of as a column of the
  49. //! Jacobian matrix of the mechanical system. This function returns elements of column that corresponds to the given wheel, stores
  50. //! necessary data to find the wheel's rotation as a scalar, and stores necessary data to set this rotation.
  51. //! @param wheelId - id of the currently processed wheel in the axle vector
  52. //! @param axle - the wheel's axle configuration
  53. //! @param axlesCount - number of axles in the vehicle
  54. //! @returns SkidSteeringWheelData structure for a single wheel
  55. SkidSteeringWheelData ComputeSingleWheelData(const int wheelId, const AxleConfiguration& axle, const int axlesCount) const;
  56. SkidSteeringModelLimits m_limits;
  57. VehicleConfiguration m_config;
  58. float m_currentLinearVelocity{ 0.0f };
  59. float m_currentAngularVelocity{ 0.0f };
  60. };
  61. } // namespace ROS2Controllers::VehicleDynamics