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