Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/events/event_loop.h" |
| 4 | |
| 5 | namespace y2020 { |
| 6 | namespace control_loops { |
| 7 | namespace superstructure { |
| 8 | |
| 9 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 10 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 11 | |
| 12 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 13 | const ::std::string &name) |
| 14 | : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 15 | name), |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 16 | hood_(constants::GetValues().hood), |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 17 | intake_joint_(constants::GetValues().intake), |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 18 | turret_(constants::GetValues().turret.subsystem_params), |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 19 | drivetrain_status_fetcher_( |
| 20 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 21 | "/drivetrain")), |
| 22 | joystick_state_fetcher_( |
| 23 | event_loop->MakeFetcher<aos::JoystickState>("/aos")) { |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 24 | event_loop->SetRuntimeRealtimePriority(30); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 27 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 28 | const Position *position, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 29 | aos::Sender<Output>::Builder *output, |
| 30 | aos::Sender<Status>::Builder *status) { |
| 31 | if (WasReset()) { |
| 32 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 33 | hood_.Reset(); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 34 | intake_joint_.Reset(); |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 35 | turret_.Reset(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 38 | const aos::monotonic_clock::time_point position_timestamp = |
| 39 | event_loop()->context().monotonic_event_time; |
| 40 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 41 | if (drivetrain_status_fetcher_.Fetch()) { |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 42 | aos::Alliance alliance = aos::Alliance::kInvalid; |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame^] | 43 | joystick_state_fetcher_.Fetch(); |
| 44 | if (joystick_state_fetcher_.get() != nullptr) { |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 45 | alliance = joystick_state_fetcher_->alliance(); |
| 46 | } |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 47 | const turret::Aimer::WrapMode mode = |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 48 | (unsafe_goal != nullptr && unsafe_goal->shooting()) |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 49 | ? turret::Aimer::WrapMode::kAvoidWrapping |
| 50 | : turret::Aimer::WrapMode::kAvoidEdges; |
| 51 | aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode, |
| 52 | turret::Aimer::ShotMode::kShootOnTheFly); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | const flatbuffers::Offset<AimerStatus> aimer_status_offset = |
| 56 | aimer_.PopulateStatus(status->fbb()); |
| 57 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 58 | OutputT output_struct; |
| 59 | |
| 60 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset = |
| 61 | hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr, |
| 62 | position->hood(), |
| 63 | output != nullptr ? &(output_struct.hood_voltage) : nullptr, |
| 64 | status->fbb()); |
| 65 | |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 66 | if (unsafe_goal != nullptr) { |
| 67 | if (unsafe_goal->shooting() && |
| 68 | shooting_start_time_ == aos::monotonic_clock::min_time) { |
| 69 | shooting_start_time_ = position_timestamp; |
| 70 | } |
| 71 | |
| 72 | if (unsafe_goal->shooting()) { |
| 73 | constexpr std::chrono::milliseconds kPeriod = |
| 74 | std::chrono::milliseconds(250); |
| 75 | if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) < |
| 76 | kPeriod) { |
| 77 | intake_joint_.set_min_position(-0.25); |
| 78 | } else { |
| 79 | intake_joint_.set_min_position(-0.75); |
| 80 | } |
| 81 | } else { |
| 82 | intake_joint_.clear_min_position(); |
| 83 | } |
| 84 | |
| 85 | if (!unsafe_goal->shooting()) { |
| 86 | shooting_start_time_ = aos::monotonic_clock::min_time; |
| 87 | } |
| 88 | } |
| 89 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 90 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset = |
| 91 | intake_joint_.Iterate( |
| 92 | unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr, |
| 93 | position->intake_joint(), |
| 94 | output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr, |
| 95 | status->fbb()); |
| 96 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 97 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 98 | *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking() |
| 99 | ? aimer_.TurretGoal() |
| 100 | : unsafe_goal->turret()) |
| 101 | : nullptr; |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 102 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 103 | turret_status_offset = turret_.Iterate( |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 104 | turret_goal, position->turret(), |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 105 | output != nullptr ? &(output_struct.turret_voltage) : nullptr, |
| 106 | status->fbb()); |
| 107 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 108 | flatbuffers::Offset<ShooterStatus> shooter_status_offset = |
| 109 | shooter_.RunIteration( |
| 110 | unsafe_goal != nullptr ? unsafe_goal->shooter() : nullptr, |
| 111 | position->shooter(), status->fbb(), |
| 112 | output != nullptr ? &(output_struct) : nullptr, position_timestamp); |
| 113 | |
John Park | 0a245a0 | 2020-02-02 14:10:15 -0800 | [diff] [blame] | 114 | climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr); |
| 115 | |
Austin Schuh | 78f0bfd | 2020-02-29 23:04:21 -0800 | [diff] [blame] | 116 | const AbsoluteEncoderProfiledJointStatus *const hood_status = |
| 117 | GetMutableTemporaryPointer(*status->fbb(), hood_status_offset); |
| 118 | |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 119 | const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status = |
| 120 | GetMutableTemporaryPointer(*status->fbb(), turret_status_offset); |
| 121 | |
| 122 | if (output != nullptr) { |
| 123 | // Friction is a pain and putting a really high burden on the integrator. |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame^] | 124 | const double turret_velocity_sign = |
| 125 | turret_status->velocity() * kTurretFrictionGain; |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 126 | output_struct.turret_voltage += |
Austin Schuh | 78f0bfd | 2020-02-29 23:04:21 -0800 | [diff] [blame] | 127 | std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit, |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 128 | kTurretFrictionVoltageLimit); |
James Kuszmaul | b7fe49e | 2020-03-05 08:24:44 -0800 | [diff] [blame] | 129 | output_struct.turret_voltage = |
| 130 | std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(), |
| 131 | turret_.operating_voltage()); |
Austin Schuh | 78f0bfd | 2020-02-29 23:04:21 -0800 | [diff] [blame] | 132 | |
| 133 | // Friction is a pain and putting a really high burden on the integrator. |
| 134 | const double hood_velocity_sign = hood_status->velocity() * kHoodFrictionGain; |
| 135 | output_struct.hood_voltage += |
| 136 | std::clamp(hood_velocity_sign, -kHoodFrictionVoltageLimit, |
| 137 | kHoodFrictionVoltageLimit); |
| 138 | |
| 139 | // And dither the output. |
| 140 | time_ += 0.00505; |
| 141 | output_struct.hood_voltage += 1.3 * std::sin(time_ * 2.0 * M_PI * 30.0); |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 144 | bool zeroed; |
| 145 | bool estopped; |
| 146 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 147 | { |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 148 | const AbsoluteEncoderProfiledJointStatus *const intake_status = |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 149 | GetMutableTemporaryPointer(*status->fbb(), intake_status_offset); |
| 150 | |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 151 | zeroed = hood_status->zeroed() && intake_status->zeroed() && |
| 152 | turret_status->zeroed(); |
| 153 | estopped = hood_status->estopped() || intake_status->estopped() || |
| 154 | turret_status->estopped(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 158 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 159 | status_builder.add_zeroed(zeroed); |
| 160 | status_builder.add_estopped(estopped); |
| 161 | |
| 162 | status_builder.add_hood(hood_status_offset); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 163 | status_builder.add_intake(intake_status_offset); |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 164 | status_builder.add_turret(turret_status_offset); |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 165 | status_builder.add_shooter(shooter_status_offset); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 166 | status_builder.add_aimer(aimer_status_offset); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 167 | |
| 168 | status->Send(status_builder.Finish()); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 169 | |
| 170 | if (output != nullptr) { |
| 171 | if (unsafe_goal) { |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 172 | output_struct.washing_machine_spinner_voltage = 0.0; |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 173 | if (unsafe_goal->shooting()) { |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 174 | if (shooter_.ready() && |
| 175 | unsafe_goal->shooter()->velocity_accelerator() > 10.0 && |
| 176 | unsafe_goal->shooter()->velocity_finisher() > 10.0) { |
Austin Schuh | 93109a5 | 2020-03-04 21:37:33 -0800 | [diff] [blame] | 177 | output_struct.feeder_voltage = 12.0; |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 178 | } else { |
| 179 | output_struct.feeder_voltage = 0.0; |
| 180 | } |
| 181 | output_struct.washing_machine_spinner_voltage = 5.0; |
| 182 | output_struct.intake_roller_voltage = 3.0; |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 183 | } else { |
| 184 | output_struct.feeder_voltage = 0.0; |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 185 | output_struct.intake_roller_voltage = unsafe_goal->roller_voltage(); |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 186 | } |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 187 | } else { |
| 188 | output_struct.intake_roller_voltage = 0.0; |
| 189 | } |
| 190 | output->Send(Output::Pack(*output->fbb(), &output_struct)); |
| 191 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 194 | } // namespace superstructure |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 195 | } // namespace control_loops |
| 196 | } // namespace y2020 |