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). |
| 12 | // Supports having the end speed and position changed in the middle. |
| 13 | // |
| 14 | // The only units assumption that this class makes is that the unit of time is |
| 15 | // seconds. |
| 16 | class TrapezoidProfile { |
| 17 | public: |
| 18 | // delta_time is how long between each call to Update. |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 19 | TrapezoidProfile(::std::chrono::nanoseconds delta_time); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 20 | |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 21 | // Updates the state. |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 22 | const Eigen::Matrix<double, 2, 1> &Update(double goal_position, |
| 23 | double goal_velocity); |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 24 | // Useful for preventing windup etc. |
| 25 | void MoveCurrentState(const Eigen::Matrix<double, 2, 1> ¤t) { |
| 26 | output_ = current; |
| 27 | } |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 28 | |
Brian Silverman | b94069c | 2014-04-17 14:34:24 -0700 | [diff] [blame] | 29 | // Useful for preventing windup etc. |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 30 | void MoveGoal(double dx) { output_(0, 0) += dx; } |
Brian Silverman | b94069c | 2014-04-17 14:34:24 -0700 | [diff] [blame] | 31 | |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 32 | void SetGoal(double x) { output_(0, 0) = x; } |
Brian Silverman | b94069c | 2014-04-17 14:34:24 -0700 | [diff] [blame] | 33 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 34 | void set_maximum_acceleration(double maximum_acceleration) { |
| 35 | maximum_acceleration_ = maximum_acceleration; |
| 36 | } |
| 37 | void set_maximum_velocity(double maximum_velocity) { |
| 38 | maximum_velocity_ = maximum_velocity; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | // Basic kinematics to update output_, given that we are going to accelerate |
| 43 | // by acceleration over delta_time. |
| 44 | void UpdateVals(double acceleration, double delta_time); |
| 45 | // Calculates how long to go for each segment. |
Austin Schuh | 69365d3 | 2015-02-22 21:38:24 -0800 | [diff] [blame] | 46 | void CalculateTimes(double distance_to_target, double goal_velocity); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 47 | // output_ is where it should go at time_. |
| 48 | Eigen::Matrix<double, 2, 1> output_; |
| 49 | |
| 50 | double acceleration_time_; |
| 51 | double acceleration_; |
| 52 | double constant_time_; |
| 53 | double deceleration_time_; |
| 54 | double deceleration_; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 55 | |
| 56 | double maximum_acceleration_; |
| 57 | double maximum_velocity_; |
| 58 | |
| 59 | // How long between calls to Update. |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 60 | ::std::chrono::nanoseconds timestep_; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(TrapezoidProfile); |
| 63 | }; |
| 64 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 65 | } // namespace aos::util |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 66 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 67 | #endif // AOS_UTIL_TRAPEZOID_PROFILE_H_ |