blob: fe63352b5171138d0e90cf34e5b3de83b3e11d2f [file] [log] [blame]
briansf0165ca2013-03-02 06:17:47 +00001#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
9namespace aos {
10namespace 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.
17class TrapezoidProfile {
18 public:
19 // delta_time is how long between each call to Update.
20 TrapezoidProfile(const time::Time &delta_time);
21
Austin Schuh69365d32015-02-22 21:38:24 -080022 // Updates the state.
briansf0165ca2013-03-02 06:17:47 +000023 const Eigen::Matrix<double, 2, 1> &Update(double goal_position,
24 double goal_velocity);
Austin Schuh69365d32015-02-22 21:38:24 -080025 // Useful for preventing windup etc.
26 void MoveCurrentState(const Eigen::Matrix<double, 2, 1> &current) {
27 output_ = current;
28 }
briansf0165ca2013-03-02 06:17:47 +000029
Brian Silvermanb94069c2014-04-17 14:34:24 -070030 // Useful for preventing windup etc.
Austin Schuh69365d32015-02-22 21:38:24 -080031 void MoveGoal(double dx) { output_(0, 0) += dx; }
Brian Silvermanb94069c2014-04-17 14:34:24 -070032
Austin Schuh69365d32015-02-22 21:38:24 -080033 void SetGoal(double x) { output_(0, 0) = x; }
Brian Silvermanb94069c2014-04-17 14:34:24 -070034
briansf0165ca2013-03-02 06:17:47 +000035 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 Schuh69365d32015-02-22 21:38:24 -080047 void CalculateTimes(double distance_to_target, double goal_velocity);
briansf0165ca2013-03-02 06:17:47 +000048 // 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_;
briansf0165ca2013-03-02 06:17:47 +000056
57 double maximum_acceleration_;
58 double maximum_velocity_;
59
60 // How long between calls to Update.
61 const time::Time timestep_;
62
63 DISALLOW_COPY_AND_ASSIGN(TrapezoidProfile);
64};
65
66} // namespace util
67} // namespace aos
68
69#endif // AOS_COMMON_UTIL_TRAPEZOID_PROFILE_H_