VehicleInputs.h 2.2 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/Math/Vector3.h>
  10. #include <AzCore/Time/ITime.h>
  11. #include <AzCore/std/containers/vector.h>
  12. namespace ROS2Controllers::VehicleDynamics
  13. {
  14. //! Inputs with an expiration date - effectively is zero after a certain time since update
  15. template<typename T>
  16. class InputZeroedOnTimeout
  17. {
  18. public:
  19. InputZeroedOnTimeout(int64_t timeoutUs = 200000)
  20. : m_timeoutUs(timeoutUs)
  21. {
  22. }
  23. void UpdateValue(T updatedInput)
  24. {
  25. m_input = updatedInput;
  26. m_lastUpdateUs = GetTimeSinceStartupUs();
  27. }
  28. T& GetValue()
  29. {
  30. if (auto timeSinceStartUs = GetTimeSinceStartupUs(); timeSinceStartUs - m_lastUpdateUs > m_timeoutUs)
  31. {
  32. m_input = Zero(m_input);
  33. }
  34. return m_input;
  35. }
  36. private:
  37. T& Zero(T& input);
  38. int64_t GetTimeSinceStartupUs() const
  39. {
  40. return static_cast<int64_t>(AZ::Interface<AZ::ITime>::Get()->GetElapsedTimeUs());
  41. }
  42. int64_t m_timeoutUs;
  43. int64_t m_lastUpdateUs = 0;
  44. T m_input{ 0 };
  45. };
  46. //! Structure defining the most recent vehicle inputs state
  47. struct VehicleInputs
  48. {
  49. AZ::Vector3 m_speed; //!< Linear speed control measured in m/s
  50. AZ::Vector3 m_angularRates; //!< Angular speed control of vehicle
  51. AZStd::vector<float> m_jointRequestedPosition; //!< Steering angle in radians. Negative is right, positive is left,
  52. };
  53. struct VehicleInputDeadline
  54. {
  55. InputZeroedOnTimeout<AZ::Vector3> m_speed; //!< Linear speed control measured in m/s
  56. InputZeroedOnTimeout<AZ::Vector3> m_angularRates; //!< Linear speed control measured in m/s
  57. InputZeroedOnTimeout<AZStd::vector<float>>
  58. m_jointRequestedPosition; //!< Steering angle in radians. Negative is right, positive is left,
  59. VehicleInputs GetValueCheckingDeadline();
  60. };
  61. } // namespace ROS2Controllers::VehicleDynamics