blob: 6669777e2ee896db7b0de7fb57c0095599b88c16 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -07005
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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08009namespace aos::util {
briansf0165ca2013-03-02 06:17:47 +000010
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.
16class TrapezoidProfile {
17 public:
18 // delta_time is how long between each call to Update.
Austin Schuh214e9c12016-11-25 17:26:20 -080019 TrapezoidProfile(::std::chrono::nanoseconds delta_time);
briansf0165ca2013-03-02 06:17:47 +000020
Austin Schuh69365d32015-02-22 21:38:24 -080021 // Updates the state.
briansf0165ca2013-03-02 06:17:47 +000022 const Eigen::Matrix<double, 2, 1> &Update(double goal_position,
23 double goal_velocity);
Austin Schuh69365d32015-02-22 21:38:24 -080024 // Useful for preventing windup etc.
25 void MoveCurrentState(const Eigen::Matrix<double, 2, 1> &current) {
26 output_ = current;
27 }
briansf0165ca2013-03-02 06:17:47 +000028
Brian Silvermanb94069c2014-04-17 14:34:24 -070029 // Useful for preventing windup etc.
Austin Schuh69365d32015-02-22 21:38:24 -080030 void MoveGoal(double dx) { output_(0, 0) += dx; }
Brian Silvermanb94069c2014-04-17 14:34:24 -070031
Austin Schuh69365d32015-02-22 21:38:24 -080032 void SetGoal(double x) { output_(0, 0) = x; }
Brian Silvermanb94069c2014-04-17 14:34:24 -070033
briansf0165ca2013-03-02 06:17:47 +000034 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 Schuh69365d32015-02-22 21:38:24 -080046 void CalculateTimes(double distance_to_target, double goal_velocity);
briansf0165ca2013-03-02 06:17:47 +000047 // 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_;
briansf0165ca2013-03-02 06:17:47 +000055
56 double maximum_acceleration_;
57 double maximum_velocity_;
58
59 // How long between calls to Update.
Austin Schuh214e9c12016-11-25 17:26:20 -080060 ::std::chrono::nanoseconds timestep_;
briansf0165ca2013-03-02 06:17:47 +000061
62 DISALLOW_COPY_AND_ASSIGN(TrapezoidProfile);
63};
64
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080065} // namespace aos::util
briansf0165ca2013-03-02 06:17:47 +000066
John Park33858a32018-09-28 23:05:48 -070067#endif // AOS_UTIL_TRAPEZOID_PROFILE_H_