blob: c7c923afea5b03e1caf31becc1a28d59661fec4a [file] [log] [blame]
Austin Schuh4fae0fc2018-03-27 23:51:42 -07001#ifndef MOTORS_SEEMS_REASONABLE_SPRING_H_
2#define MOTORS_SEEMS_REASONABLE_SPRING_H_
3
4#include <cmath>
5
6namespace motors {
7namespace seems_reasonable {
8
9float NextGoal(float current_goal, float goal);
10float PreviousGoal(float current_goal, float goal);
11
12class Spring {
13 public:
14 Spring() = default;
15 Spring(const Spring &) = delete;
16 Spring &operator=(const Spring &) = delete;
17
18 // Iterates the loop.
19 // If unload is true, unload.
20 // If the encoder isn't valid, unload.
21 // If prime is true, go to primed state.
22 // If prime and fire are true, fire.
23 void Iterate(bool unload, bool prime, bool fire, bool force_reset,
24 bool encoder_valid, float angle);
25
26 enum class State {
27 UNINITIALIZED = 0,
28 STUCK_UNLOAD = 1,
29 UNLOAD = 2,
30 LOAD = 3,
31 PRIME = 4,
32 FIRE = 5,
Austin Schuh72656c42018-04-01 16:37:52 -070033 WAIT_FOR_LOAD = 6,
34 WAIT_FOR_LOAD_RELEASE = 7,
Austin Schuh4fae0fc2018-03-27 23:51:42 -070035 };
36
37 // Returns the current to output to the spring motors.
38 float output() const { return output_; }
39
40 // Returns true if the motor is near the goal.
41 bool Near() { return ::std::abs(angle_ - goal_) < 0.2f; }
42
43 State state() const { return state_; }
44
45 float angle() const { return angle_; }
46 float goal() const { return goal_; }
47
48 int timeout() const { return timeout_; }
49
50 private:
51 void Load() {
52 timeout_ = 5 * 200;
53 state_ = State::LOAD;
54 }
55
56 void Prime() {
57 timeout_ = 1 * 200;
58 state_ = State::PRIME;
59 }
60
61 void Unload() {
62 timeout_ = 10 * 200;
63 state_ = State::UNLOAD;
64 }
65
66 void StuckUnload() {
67 timeout_ = 10 * 200;
68 state_ = State::STUCK_UNLOAD;
69 }
70
71 void Fire() {
72 timeout_ = 100;
73 state_ = State::FIRE;
74 }
75
76 float NextGoal(float goal) {
77 return ::motors::seems_reasonable::NextGoal(goal_, goal);
78 }
79
80 float PreviousGoal(float goal) {
81 return ::motors::seems_reasonable::PreviousGoal(goal_, goal);
82 }
83
84 State state_ = State::UNINITIALIZED;
85
86 // Note, these need to be (-M_PI, M_PI]
Brian Silvermanc8edc952018-03-31 12:30:17 -070087 constexpr static float kLoadGoal = -0.345f;
88 constexpr static float kPrimeGoal = -0.269f;
89 constexpr static float kFireGoal = -0.163f;
Austin Schuh4fae0fc2018-03-27 23:51:42 -070090 constexpr static float kUnloadGoal = kFireGoal;
91
92 float angle_ = 0.0f;
93 float goal_ = 0.0f;
94
95 int timeout_ = 0;
96
97 float output_ = 0.0f;
98 float last_error_ = 0.0f;
99};
100
101} // namespace seems_reasonable
102} // namespace motors
103
104#endif // MOTORS_SEEMS_REASONABLE_SPRING_H_