blob: 7f0ec9fa3d49d4bc8b2d35777c59aed4c889c613 [file] [log] [blame]
Sabina Davis8d20ca82018-02-19 13:17:45 -08001#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_
2#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_
3
John Park33858a32018-09-28 23:05:48 -07004#include "aos/commonmath.h"
5#include "aos/controls/control_loop.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -08006#include "frc971/zeroing/zeroing.h"
7#include "y2018/constants.h"
8#include "y2018/control_loops/superstructure/intake/intake_delayed_plant.h"
9#include "y2018/control_loops/superstructure/intake/intake_plant.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "y2018/control_loops/superstructure/superstructure_output_generated.h"
11#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
12#include "y2018/control_loops/superstructure/superstructure_status_generated.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -080013
14namespace y2018 {
15namespace control_loops {
16namespace superstructure {
17namespace intake {
18
19class IntakeController {
20 public:
21 IntakeController();
22
23 // Sets the current encoder position in radians
24 void set_position(double spring_angle, double output_position);
25
26 // Populates the status structure.
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 void SetStatus(superstructure::IntakeSideStatus::Builder *status,
Sabina Davis8d20ca82018-02-19 13:17:45 -080028 const double *unsafe_goal);
29
30 // Returns the control loop calculated voltage.
31 double voltage() const;
32
Austin Schuh96341532018-03-09 21:17:24 -080033 double output_position() const { return loop_->X_hat(0); }
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 double motor_position() const { return loop_->X_hat(2); }
Austin Schuh96341532018-03-09 21:17:24 -080035
Sabina Davis8d20ca82018-02-19 13:17:45 -080036 // Executes the control loop for a cycle.
37 void Update(bool disabled, const double *unsafe_goal);
38
39 // Resets the kalman filter and any other internal state.
40 void Reset();
41
42 // Sets the goal angle from unsafe_goal.
43 double goal_angle(const double *unsafe_goal);
44
Sabina Davis8d20ca82018-02-19 13:17:45 -080045 constexpr static double kDt =
James Kuszmaul651fc3f2019-05-15 21:14:25 -070046 ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency);
Sabina Davis8d20ca82018-02-19 13:17:45 -080047
48 // Sets the offset of the controller to be the zeroing estimator offset when
49 // possible otherwise zero.
50 void UpdateOffset(double offset);
51
Alex Perrycb7da4b2019-08-28 19:35:56 -070052 const ::frc971::constants::Range intake_range() const {
53 return intake_range_;
54 }
55
56 private:
57 // The control loop.
58 ::std::unique_ptr<
59 StateFeedbackLoop<5, 1, 2, double, StateFeedbackPlant<5, 1, 2>,
60 StateFeedbackObserver<5, 1, 2>>>
61 loop_;
62
Sabina Davis8d20ca82018-02-19 13:17:45 -080063 const ::frc971::constants::Range intake_range_;
64
65 // Stores the current zeroing estimator offset.
66 double offset_ = 0.0;
67
Sabina Davis8d20ca82018-02-19 13:17:45 -080068 bool reset_ = true;
69
70 // The current sensor measurement.
71 Eigen::Matrix<double, 2, 1> Y_;
72
73 DISALLOW_COPY_AND_ASSIGN(IntakeController);
74};
75
76class IntakeSide {
77 public:
78 IntakeSide(const ::frc971::constants::PotAndAbsoluteEncoderZeroingConstants
79 &zeroing_constants);
80
81 // The operating voltage.
82 static constexpr double kOperatingVoltage() { return 12.0; }
83
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 flatbuffers::Offset<superstructure::IntakeSideStatus> Iterate(
85 const double *unsafe_goal,
86 const superstructure::IntakeElasticSensors *position,
87 superstructure::IntakeVoltageT *output,
88 flatbuffers::FlatBufferBuilder *fbb);
Sabina Davis8d20ca82018-02-19 13:17:45 -080089
90 void Reset();
91
92 enum class State : int32_t {
93 UNINITIALIZED,
94 ZEROING,
95 RUNNING,
96 ESTOP,
97 };
98
99 State state() const { return state_; }
100
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 bool estopped() const { return state_ == State::ESTOP; }
102 bool zeroed() const { return zeroing_estimator_.zeroed(); }
103
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700104 bool clear_of_box() const { return controller_.output_position() < -0.1; }
Austin Schuh96341532018-03-09 21:17:24 -0800105
Neil Balchba9cbba2018-04-06 22:26:38 -0700106 double output_position() const { return controller_.output_position(); }
107
Sabina Davis8d20ca82018-02-19 13:17:45 -0800108 private:
109 IntakeController controller_;
110
111 State state_ = State::UNINITIALIZED;
112
Austin Schuh72db9a12019-01-21 18:02:51 -0800113 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator zeroing_estimator_;
Austin Schuh17dd0892018-03-02 20:06:31 -0800114
115 double intake_last_position_ = 0.0;
Sabina Davis8d20ca82018-02-19 13:17:45 -0800116};
117
118} // namespace intake
119} // namespace superstructure
120} // namespace control_loops
121} // namespace y2018
122
123#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_