blob: edefca498aa728696183b827e909d8f2d958c91d [file] [log] [blame]
Sabina Davis8d20ca82018-02-19 13:17:45 -08001#include "y2018/control_loops/superstructure/intake/intake.h"
2
3#include <chrono>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/commonmath.h"
John Park33858a32018-09-28 23:05:48 -07006#include "aos/logging/logging.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -08007#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"
10
11namespace y2018 {
12namespace control_loops {
13namespace superstructure {
14namespace intake {
15
16namespace chrono = ::std::chrono;
17using ::aos::monotonic_clock;
18
19constexpr double IntakeController::kDt;
20
21IntakeController::IntakeController()
22 : loop_(new StateFeedbackLoop<5, 1, 2, double, StateFeedbackPlant<5, 1, 2>,
23 StateFeedbackObserver<5, 1, 2>>(
24 superstructure::intake::MakeDelayedIntakeLoop())),
25 intake_range_(::y2018::constants::Values::kIntakeRange()) {
26 Y_.setZero();
27}
28
29void IntakeController::set_position(double spring_angle,
30 double output_position) {
31 // Update position in the model.
32 Y_ << spring_angle, (output_position + offset_);
33}
34
35double IntakeController::voltage() const { return loop_->U(0, 0); }
36
37void IntakeController::Reset() { reset_ = true; }
38
39void IntakeController::UpdateOffset(double offset) {
40 const double doffset = offset - offset_;
41 offset_ = offset;
42
43 loop_->mutable_X_hat(0) += doffset;
44 loop_->mutable_X_hat(2) += doffset;
45}
46
47double IntakeController::goal_angle(const double *unsafe_goal) {
48 if (unsafe_goal == nullptr) {
49 return 0;
50 } else {
51 return ::aos::Clip(*unsafe_goal, intake_range_.lower, intake_range_.upper);
52 }
53}
54
55void IntakeController::Update(bool disabled, const double *unsafe_goal) {
56 if (reset_) {
57 loop_->mutable_X_hat().setZero();
58 loop_->mutable_X_hat(0) = Y_(0) + Y_(1);
59 loop_->mutable_X_hat(2) = Y_(1);
60 reset_ = false;
61 }
62
63 double goal_velocity;
64 loop_->Correct(Y_);
65
66 if (unsafe_goal == nullptr) {
67 disabled = true;
68 goal_velocity = 0.0;
69 } else {
70 goal_velocity = ::aos::Clip(
Austin Schuh17dd0892018-03-02 20:06:31 -080071 ((goal_angle(unsafe_goal) - loop_->X_hat(0, 0)) * 12.0), -16.0, 16.0);
Sabina Davis8d20ca82018-02-19 13:17:45 -080072 }
73 // Computes the goal.
74 loop_->mutable_R() << 0.0, goal_velocity, 0.0, goal_velocity,
75 (goal_velocity / (kGearRatio * kMotorVelocityConstant));
76
77 loop_->Update(disabled);
78}
79
Alex Perrycb7da4b2019-08-28 19:35:56 -070080void IntakeController::SetStatus(IntakeSideStatus::Builder *status,
Sabina Davis8d20ca82018-02-19 13:17:45 -080081 const double *unsafe_goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 status->add_goal_position(goal_angle(unsafe_goal));
83 status->add_goal_velocity(loop_->R(1, 0));
84 status->add_spring_position(loop_->X_hat(0) - loop_->X_hat(2));
85 status->add_spring_velocity(loop_->X_hat(1) - loop_->X_hat(3));
86 status->add_motor_position(loop_->X_hat(2));
87 status->add_motor_velocity(loop_->X_hat(3));
88 status->add_delayed_voltage(loop_->X_hat(4));
Sabina Davis8d20ca82018-02-19 13:17:45 -080089}
90
91IntakeSide::IntakeSide(
92 const ::frc971::constants::PotAndAbsoluteEncoderZeroingConstants
Stephan Massalta769ca22019-01-09 05:29:13 +000093 &zeroing_constants,
94 const double spring_offset)
95 : zeroing_estimator_(zeroing_constants), spring_offset_(spring_offset) {}
Sabina Davis8d20ca82018-02-19 13:17:45 -080096
97void IntakeSide::Reset() { state_ = State::UNINITIALIZED; }
98
Alex Perrycb7da4b2019-08-28 19:35:56 -070099flatbuffers::Offset<superstructure::IntakeSideStatus> IntakeSide::Iterate(
100 const double *unsafe_goal,
101 const superstructure::IntakeElasticSensors *position,
102 superstructure::IntakeVoltageT *output,
103 flatbuffers::FlatBufferBuilder *fbb) {
104 zeroing_estimator_.UpdateEstimate(*position->motor_position());
Sabina Davis8d20ca82018-02-19 13:17:45 -0800105
106 switch (state_) {
107 case State::UNINITIALIZED:
108 // Wait in the uninitialized state until the intake is initialized.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700109 AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -0800110 zeroing_estimator_.Reset();
111 controller_.Reset();
Stephan Massalta769ca22019-01-09 05:29:13 +0000112 spring_unwrap_.Reset();
Sabina Davis8d20ca82018-02-19 13:17:45 -0800113 state_ = State::ZEROING;
114 break;
115
116 case State::ZEROING:
117 // Zero by not moving.
118 if (zeroing_estimator_.zeroed()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(INFO, "Now zeroed\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -0800120 controller_.UpdateOffset(zeroing_estimator_.offset());
121 state_ = State::RUNNING;
122 }
123 break;
124
125 case State::RUNNING:
126 if (!(zeroing_estimator_.zeroed())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700127 AOS_LOG(ERROR, "Zeroing estimator is no longer zeroed\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -0800128 state_ = State::UNINITIALIZED;
129 }
130 if (zeroing_estimator_.error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700131 AOS_LOG(ERROR, "Zeroing estimator error\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -0800132 state_ = State::UNINITIALIZED;
133 }
134 // ESTOP if we hit the hard limits.
Stephan Massalta769ca22019-01-09 05:29:13 +0000135 if ((controller_.motor_position()) >
136 controller_.intake_range().upper_hard ||
137 (controller_.motor_position()) <
138 controller_.intake_range().lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700139 AOS_LOG(ERROR, "Hit hard limits\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -0800140 state_ = State::ESTOP;
141 }
142 break;
143
144 case State::ESTOP:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700145 AOS_LOG(ERROR, "Estop\n");
Sabina Davis8d20ca82018-02-19 13:17:45 -0800146 break;
147 }
148
149 const bool disable = (output == nullptr) || state_ != State::RUNNING;
Stephan Massalta769ca22019-01-09 05:29:13 +0000150 controller_.set_position(spring_unwrap_.Unwrap(position->spring_angle()),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 position->motor_position()->encoder());
Sabina Davis8d20ca82018-02-19 13:17:45 -0800152
153 controller_.Update(disable, unsafe_goal);
154
155 if (output) {
156 output->voltage_elastic = controller_.voltage();
157 }
158
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
160 estimator_state = zeroing_estimator_.GetEstimatorState(fbb);
161
162 superstructure::IntakeSideStatus::Builder status_builder(*fbb);
Sabina Davis8d20ca82018-02-19 13:17:45 -0800163 // Save debug/internal state.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 status_builder.add_estimator_state(estimator_state);
Stephan Massalta769ca22019-01-09 05:29:13 +0000165 // Save the spring wrapped status.
166 status_builder.add_spring_wrapped(spring_unwrap_.sensor_wrapped());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167
168 controller_.SetStatus(&status_builder, unsafe_goal);
169 status_builder.add_calculated_velocity(
170 (zeroing_estimator_.offset() + position->motor_position()->encoder() -
171 intake_last_position_) /
172 controller_.kDt);
173 status_builder.add_zeroed(zeroing_estimator_.zeroed());
174 status_builder.add_estopped(estopped());
Stephan Massalta769ca22019-01-09 05:29:13 +0000175 status_builder.add_state(static_cast<int32_t>(state_));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 intake_last_position_ =
177 zeroing_estimator_.offset() + position->motor_position()->encoder();
178
179 return status_builder.Finish();
Sabina Davis8d20ca82018-02-19 13:17:45 -0800180}
181
182} // namespace intake
183} // namespace superstructure
184} // namespace control_loops
185} // namespace y2018