blob: 92e3dbea95d55e7d8bb5199d95e37210bcf78332 [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
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
Brian Silvermanb94069c2014-04-17 14:34:24 -070027 // Useful for preventing windup etc.
28 void MoveGoal(double dx) {
29 output_(0, 0) += dx;
30 }
31
32 void SetGoal(double x) {
33 output_(0, 0) = x;
34 }
35
briansf0165ca2013-03-02 06:17:47 +000036 void set_maximum_acceleration(double maximum_acceleration) {
37 maximum_acceleration_ = maximum_acceleration;
38 }
39 void set_maximum_velocity(double maximum_velocity) {
40 maximum_velocity_ = maximum_velocity;
41 }
42
43 private:
44 // Basic kinematics to update output_, given that we are going to accelerate
45 // by acceleration over delta_time.
46 void UpdateVals(double acceleration, double delta_time);
47 // Calculates how long to go for each segment.
48 void CalculateTimes(double distance_to_target,
49 double goal_velocity);
50 // output_ is where it should go at time_.
51 Eigen::Matrix<double, 2, 1> output_;
52
53 double acceleration_time_;
54 double acceleration_;
55 double constant_time_;
56 double deceleration_time_;
57 double deceleration_;
58 double current_velocity_;
59
60 double maximum_acceleration_;
61 double maximum_velocity_;
62
63 // How long between calls to Update.
64 const time::Time timestep_;
65
66 DISALLOW_COPY_AND_ASSIGN(TrapezoidProfile);
67};
68
69} // namespace util
70} // namespace aos
71
72#endif // AOS_COMMON_UTIL_TRAPEZOID_PROFILE_H_