blob: 908ecc42cd6faf497e46516a2d037a14279475ca [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,
33 };
34
35 // Returns the current to output to the spring motors.
36 float output() const { return output_; }
37
38 // Returns true if the motor is near the goal.
39 bool Near() { return ::std::abs(angle_ - goal_) < 0.2f; }
40
41 State state() const { return state_; }
42
43 float angle() const { return angle_; }
44 float goal() const { return goal_; }
45
46 int timeout() const { return timeout_; }
47
48 private:
49 void Load() {
50 timeout_ = 5 * 200;
51 state_ = State::LOAD;
52 }
53
54 void Prime() {
55 timeout_ = 1 * 200;
56 state_ = State::PRIME;
57 }
58
59 void Unload() {
60 timeout_ = 10 * 200;
61 state_ = State::UNLOAD;
62 }
63
64 void StuckUnload() {
65 timeout_ = 10 * 200;
66 state_ = State::STUCK_UNLOAD;
67 }
68
69 void Fire() {
70 timeout_ = 100;
71 state_ = State::FIRE;
72 }
73
74 float NextGoal(float goal) {
75 return ::motors::seems_reasonable::NextGoal(goal_, goal);
76 }
77
78 float PreviousGoal(float goal) {
79 return ::motors::seems_reasonable::PreviousGoal(goal_, goal);
80 }
81
82 State state_ = State::UNINITIALIZED;
83
84 // Note, these need to be (-M_PI, M_PI]
85 constexpr static float kLoadGoal = -0.18f;
86 constexpr static float kPrimeGoal = -0.10f;
87 constexpr static float kFireGoal = -0.0f;
88 constexpr static float kUnloadGoal = kFireGoal;
89
90 float angle_ = 0.0f;
91 float goal_ = 0.0f;
92
93 int timeout_ = 0;
94
95 float output_ = 0.0f;
96 float last_error_ = 0.0f;
97};
98
99} // namespace seems_reasonable
100} // namespace motors
101
102#endif // MOTORS_SEEMS_REASONABLE_SPRING_H_