blob: 944c4231bf926d17b80e6c85cd836fd68c11c059 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_UTIL_TRAPEZOID_PROFILE_H_
2#define AOS_UTIL_TRAPEZOID_PROFILE_H_
briansf0165ca2013-03-02 06:17:47 +00003
4#include "Eigen/Dense"
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/macros.h"
7#include "aos/time/time.h"
briansf0165ca2013-03-02 06:17:47 +00008
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.
Austin Schuh214e9c12016-11-25 17:26:20 -080020 TrapezoidProfile(::std::chrono::nanoseconds delta_time);
briansf0165ca2013-03-02 06:17:47 +000021
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.
Austin Schuh214e9c12016-11-25 17:26:20 -080061 ::std::chrono::nanoseconds timestep_;
briansf0165ca2013-03-02 06:17:47 +000062
63 DISALLOW_COPY_AND_ASSIGN(TrapezoidProfile);
64};
65
66} // namespace util
67} // namespace aos
68
John Park33858a32018-09-28 23:05:48 -070069#endif // AOS_UTIL_TRAPEZOID_PROFILE_H_