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