brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 1 | #ifndef AOS_COMMON_UTIL_TRAPEZOID_PROFILE_H_ |
| 2 | #define AOS_COMMON_UTIL_TRAPEZOID_PROFILE_H_ |
| 3 | |
| 4 | #include "Eigen/Dense" |
| 5 | |
| 6 | #include "aos/common/macros.h" |
| 7 | #include "aos/common/time.h" |
| 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. |
| 20 | TrapezoidProfile(const time::Time &delta_time); |
| 21 | |
| 22 | // Updates the state. The names of the arguments are pretty easy to figure |
| 23 | // out. |
| 24 | const Eigen::Matrix<double, 2, 1> &Update(double goal_position, |
| 25 | double goal_velocity); |
| 26 | |
| 27 | void set_maximum_acceleration(double maximum_acceleration) { |
| 28 | maximum_acceleration_ = maximum_acceleration; |
| 29 | } |
| 30 | void set_maximum_velocity(double maximum_velocity) { |
| 31 | maximum_velocity_ = maximum_velocity; |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | // Basic kinematics to update output_, given that we are going to accelerate |
| 36 | // by acceleration over delta_time. |
| 37 | void UpdateVals(double acceleration, double delta_time); |
| 38 | // Calculates how long to go for each segment. |
| 39 | void CalculateTimes(double distance_to_target, |
| 40 | double goal_velocity); |
| 41 | // output_ is where it should go at time_. |
| 42 | Eigen::Matrix<double, 2, 1> output_; |
| 43 | |
| 44 | double acceleration_time_; |
| 45 | double acceleration_; |
| 46 | double constant_time_; |
| 47 | double deceleration_time_; |
| 48 | double deceleration_; |
| 49 | double current_velocity_; |
| 50 | |
| 51 | double maximum_acceleration_; |
| 52 | double maximum_velocity_; |
| 53 | |
| 54 | // How long between calls to Update. |
| 55 | const time::Time timestep_; |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(TrapezoidProfile); |
| 58 | }; |
| 59 | |
| 60 | } // namespace util |
| 61 | } // namespace aos |
| 62 | |
| 63 | #endif // AOS_COMMON_UTIL_TRAPEZOID_PROFILE_H_ |