John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_UTIL_TRAPEZOID_PROFILE_H_ |
| 2 | #define AOS_UTIL_TRAPEZOID_PROFILE_H_ |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 3 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 4 | #include <chrono> |
| 5 | |
| 6 | #include "Eigen/Core" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/macros.h" |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 10 | namespace aos::util { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 11 | |
| 12 | // Calculates a trapezoidal motion profile (like for a control loop's goals). |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 13 | // Supports having the destination position, acceleration, and velocity caps |
| 14 | // changed in the middle, and for having different accelerations and |
| 15 | // decelerations. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 16 | // |
| 17 | // The only units assumption that this class makes is that the unit of time is |
| 18 | // seconds. |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 19 | class AsymmetricTrapezoidProfile { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 20 | public: |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 21 | // Constructs a profile. delta_time is the timestep to assume when solving. |
| 22 | AsymmetricTrapezoidProfile(::std::chrono::nanoseconds delta_time); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 23 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 24 | // Updates the state to provide the next position and velocity to go to to |
| 25 | // follow the profile. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 26 | const Eigen::Matrix<double, 2, 1> &Update(double goal_position, |
| 27 | double goal_velocity); |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 28 | |
| 29 | // Updates the internal position. Useful for handling windup when a loop |
| 30 | // saturates or gets disabled. |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 31 | void MoveCurrentState(const Eigen::Matrix<double, 2, 1> ¤t) { |
| 32 | output_ = current; |
| 33 | } |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 34 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 35 | // Adjusts the internal position by the provided position delta. |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 36 | void MoveGoal(double dx) { output_(0, 0) += dx; } |
Brian Silverman | b94069c | 2014-04-17 14:34:24 -0700 | [diff] [blame] | 37 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 38 | // Sets the internal position to the provided position. |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 39 | void SetGoal(double x) { output_(0, 0) = x; } |
Brian Silverman | b94069c | 2014-04-17 14:34:24 -0700 | [diff] [blame] | 40 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 41 | void set_maximum_acceleration(double maximum_acceleration) { |
| 42 | maximum_acceleration_ = maximum_acceleration; |
| 43 | } |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 44 | void set_maximum_deceleration(double maximum_deceleration) { |
| 45 | maximum_deceleration_ = maximum_deceleration; |
| 46 | } |
| 47 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 48 | void set_maximum_velocity(double maximum_velocity) { |
| 49 | maximum_velocity_ = maximum_velocity; |
| 50 | } |
| 51 | |
| 52 | private: |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 53 | // Updates output_ to match the basic kinematics, given that we are going to |
| 54 | // accelerate by acceleration over delta_time. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 55 | void UpdateVals(double acceleration, double delta_time); |
| 56 | // Calculates how long to go for each segment. |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 57 | void CalculateTimes(double distance_to_target, double goal_velocity, |
| 58 | Eigen::Matrix<double, 2, 1> current); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 59 | // output_ is where it should go at time_. |
| 60 | Eigen::Matrix<double, 2, 1> output_; |
| 61 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 62 | // Time and acceleration to slow down if we need to reverse directions. |
| 63 | double deceleration_reversal_time_; |
| 64 | double deceleration_reversal_; |
| 65 | |
| 66 | // Time and acceleration to speed up with. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 67 | double acceleration_time_; |
| 68 | double acceleration_; |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 69 | // Time to go at max speed at. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 70 | double constant_time_; |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 71 | // Time and acceleration to slow down with. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 72 | double deceleration_time_; |
| 73 | double deceleration_; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 74 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 75 | double maximum_acceleration_ = 0; |
| 76 | double maximum_deceleration_ = 0; |
| 77 | double maximum_velocity_ = 0; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 78 | |
| 79 | // How long between calls to Update. |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 80 | ::std::chrono::nanoseconds timestep_; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 81 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(AsymmetricTrapezoidProfile); |
| 83 | }; |
| 84 | |
| 85 | // Class to implement a AsymmetricTrapezoidProfile where both acceleration and |
| 86 | // deceleration match. |
| 87 | class TrapezoidProfile { |
| 88 | public: |
| 89 | TrapezoidProfile(::std::chrono::nanoseconds delta_time) |
| 90 | : asymmetric_trapezoid_profile_(delta_time) {} |
| 91 | |
| 92 | // Updates the state to provide the next position and velocity to go to to |
| 93 | // follow the profile. |
| 94 | const Eigen::Matrix<double, 2, 1> &Update(double goal_position, |
| 95 | double goal_velocity) { |
| 96 | return asymmetric_trapezoid_profile_.Update(goal_position, goal_velocity); |
| 97 | } |
| 98 | |
| 99 | // Updates the internal position. Useful for handling windup when a loop |
| 100 | // saturates or gets disabled. |
| 101 | void MoveCurrentState(const Eigen::Matrix<double, 2, 1> ¤t) { |
| 102 | asymmetric_trapezoid_profile_.MoveCurrentState(current); |
| 103 | } |
| 104 | |
| 105 | // Adjusts the internal position by the provided position delta. |
| 106 | void MoveGoal(double dx) { asymmetric_trapezoid_profile_.MoveGoal(dx); } |
| 107 | |
| 108 | // Sets the internal position to the provided position. |
| 109 | void SetGoal(double x) { asymmetric_trapezoid_profile_.SetGoal(x); } |
| 110 | |
| 111 | void set_maximum_acceleration(double maximum_acceleration) { |
| 112 | asymmetric_trapezoid_profile_.set_maximum_acceleration( |
| 113 | maximum_acceleration); |
| 114 | asymmetric_trapezoid_profile_.set_maximum_deceleration( |
| 115 | maximum_acceleration); |
| 116 | } |
| 117 | void set_maximum_velocity(double maximum_velocity) { |
| 118 | asymmetric_trapezoid_profile_.set_maximum_velocity(maximum_velocity); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | AsymmetricTrapezoidProfile asymmetric_trapezoid_profile_; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 125 | } // namespace aos::util |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 126 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 127 | #endif // AOS_UTIL_TRAPEZOID_PROFILE_H_ |