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 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 9 | using frc971::control_loops::AbsoluteAndAbsoluteEncoderProfiledJointStatus; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 10 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 11 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 12 | |
| 13 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 14 | const ::std::string &name) |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 15 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 16 | name), |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 17 | hood_(constants::GetValues().hood), |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 18 | intake_joint_(constants::GetValues().intake), |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 19 | turret_(constants::GetValues().turret.subsystem_params), |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 20 | drivetrain_status_fetcher_( |
| 21 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 22 | "/drivetrain")), |
| 23 | joystick_state_fetcher_( |
| 24 | event_loop->MakeFetcher<aos::JoystickState>("/aos")) { |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 25 | event_loop->SetRuntimeRealtimePriority(30); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 26 | } |
| 27 | |
milind upadhyay | aec1aee | 2020-10-13 13:44:33 -0700 | [diff] [blame] | 28 | double Superstructure::robot_speed() const { |
| 29 | return (drivetrain_status_fetcher_.get() != nullptr |
| 30 | ? drivetrain_status_fetcher_->robot_speed() |
| 31 | : 0.0); |
| 32 | } |
| 33 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 34 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 35 | const Position *position, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 36 | aos::Sender<Output>::Builder *output, |
| 37 | aos::Sender<Status>::Builder *status) { |
| 38 | if (WasReset()) { |
| 39 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 40 | hood_.Reset(); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 41 | intake_joint_.Reset(); |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 42 | turret_.Reset(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 45 | const aos::monotonic_clock::time_point position_timestamp = |
| 46 | event_loop()->context().monotonic_event_time; |
| 47 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 48 | if (drivetrain_status_fetcher_.Fetch()) { |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 49 | aos::Alliance alliance = aos::Alliance::kInvalid; |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame] | 50 | joystick_state_fetcher_.Fetch(); |
| 51 | if (joystick_state_fetcher_.get() != nullptr) { |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 52 | alliance = joystick_state_fetcher_->alliance(); |
| 53 | } |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 54 | const turret::Aimer::WrapMode mode = |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 55 | (unsafe_goal != nullptr && unsafe_goal->shooting()) |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 56 | ? turret::Aimer::WrapMode::kAvoidWrapping |
| 57 | : turret::Aimer::WrapMode::kAvoidEdges; |
| 58 | aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode, |
| 59 | turret::Aimer::ShotMode::kShootOnTheFly); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 60 | } |
| 61 | |
milind upadhyay | aec1aee | 2020-10-13 13:44:33 -0700 | [diff] [blame] | 62 | const float velocity = robot_speed(); |
| 63 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 64 | const flatbuffers::Offset<AimerStatus> aimer_status_offset = |
| 65 | aimer_.PopulateStatus(status->fbb()); |
| 66 | |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 67 | const double distance_to_goal = aimer_.DistanceToGoal(); |
| 68 | |
| 69 | aos::FlatbufferFixedAllocatorArray< |
| 70 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64> |
| 71 | hood_goal; |
| 72 | aos::FlatbufferFixedAllocatorArray<ShooterGoal, 64> shooter_goal; |
| 73 | |
| 74 | constants::Values::ShotParams shot_params; |
milind-u | 0a178a8 | 2021-09-28 18:42:09 -0700 | [diff] [blame] | 75 | constants::Values::FlywheelShotParams flywheel_shot_params; |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 76 | if (constants::GetValues().shot_interpolation_table.GetInRange( |
milind-u | 0a178a8 | 2021-09-28 18:42:09 -0700 | [diff] [blame] | 77 | distance_to_goal, &shot_params) && |
| 78 | constants::GetValues().flywheel_shot_interpolation_table.GetInRange( |
| 79 | shot_params.velocity_ball, &flywheel_shot_params)) { |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 80 | hood_goal.Finish(frc971::control_loops:: |
| 81 | CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 82 | *hood_goal.fbb(), shot_params.hood_angle)); |
| 83 | |
milind-u | 0a178a8 | 2021-09-28 18:42:09 -0700 | [diff] [blame] | 84 | shooter_goal.Finish(CreateShooterGoal( |
| 85 | *shooter_goal.fbb(), flywheel_shot_params.velocity_accelerator, |
| 86 | flywheel_shot_params.velocity_finisher)); |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 87 | } else { |
| 88 | hood_goal.Finish( |
| 89 | frc971::control_loops:: |
| 90 | CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 91 | *hood_goal.fbb(), constants::GetValues().hood.range.upper)); |
| 92 | |
| 93 | shooter_goal.Finish(CreateShooterGoal(*shooter_goal.fbb(), 0.0, 0.0)); |
| 94 | } |
| 95 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 96 | OutputT output_struct; |
| 97 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 98 | flatbuffers::Offset<AbsoluteAndAbsoluteEncoderProfiledJointStatus> |
| 99 | hood_status_offset = hood_.Iterate( |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 100 | unsafe_goal != nullptr |
| 101 | ? (unsafe_goal->hood_tracking() ? &hood_goal.message() |
| 102 | : unsafe_goal->hood()) |
| 103 | : nullptr, |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 104 | position->hood(), |
| 105 | output != nullptr ? &(output_struct.hood_voltage) : nullptr, |
| 106 | status->fbb()); |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 107 | |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 108 | if (unsafe_goal != nullptr) { |
| 109 | if (unsafe_goal->shooting() && |
| 110 | shooting_start_time_ == aos::monotonic_clock::min_time) { |
| 111 | shooting_start_time_ = position_timestamp; |
| 112 | } |
| 113 | |
| 114 | if (unsafe_goal->shooting()) { |
| 115 | constexpr std::chrono::milliseconds kPeriod = |
| 116 | std::chrono::milliseconds(250); |
| 117 | if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) < |
| 118 | kPeriod) { |
| 119 | intake_joint_.set_min_position(-0.25); |
| 120 | } else { |
| 121 | intake_joint_.set_min_position(-0.75); |
| 122 | } |
| 123 | } else { |
| 124 | intake_joint_.clear_min_position(); |
| 125 | } |
| 126 | |
| 127 | if (!unsafe_goal->shooting()) { |
| 128 | shooting_start_time_ = aos::monotonic_clock::min_time; |
| 129 | } |
| 130 | } |
| 131 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 132 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset = |
| 133 | intake_joint_.Iterate( |
| 134 | unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr, |
| 135 | position->intake_joint(), |
| 136 | output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr, |
| 137 | status->fbb()); |
| 138 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 139 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 140 | *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking() |
| 141 | ? aimer_.TurretGoal() |
| 142 | : unsafe_goal->turret()) |
| 143 | : nullptr; |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 144 | |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 145 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 146 | turret_status_offset = turret_.Iterate( |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 147 | turret_goal, position->turret(), |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 148 | output != nullptr ? &(output_struct.turret_voltage) : nullptr, |
| 149 | status->fbb()); |
| 150 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 151 | flatbuffers::Offset<ShooterStatus> shooter_status_offset = |
| 152 | shooter_.RunIteration( |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 153 | unsafe_goal != nullptr |
| 154 | ? (unsafe_goal->shooter_tracking() ? &shooter_goal.message() |
| 155 | : unsafe_goal->shooter()) |
| 156 | : nullptr, |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 157 | position->shooter(), status->fbb(), |
| 158 | output != nullptr ? &(output_struct) : nullptr, position_timestamp); |
| 159 | |
John Park | 0a245a0 | 2020-02-02 14:10:15 -0800 | [diff] [blame] | 160 | climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr); |
| 161 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 162 | const AbsoluteAndAbsoluteEncoderProfiledJointStatus *const hood_status = |
Austin Schuh | 78f0bfd | 2020-02-29 23:04:21 -0800 | [diff] [blame] | 163 | GetMutableTemporaryPointer(*status->fbb(), hood_status_offset); |
| 164 | |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 165 | const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status = |
| 166 | GetMutableTemporaryPointer(*status->fbb(), turret_status_offset); |
| 167 | |
| 168 | if (output != nullptr) { |
| 169 | // Friction is a pain and putting a really high burden on the integrator. |
James Kuszmaul | 9cbdb02 | 2021-09-19 17:42:29 -0700 | [diff] [blame] | 170 | // TODO(james): I'm not sure how helpful this gain is. |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame] | 171 | const double turret_velocity_sign = |
| 172 | turret_status->velocity() * kTurretFrictionGain; |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 173 | output_struct.turret_voltage += |
Austin Schuh | 78f0bfd | 2020-02-29 23:04:21 -0800 | [diff] [blame] | 174 | std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit, |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 175 | kTurretFrictionVoltageLimit); |
James Kuszmaul | 9cbdb02 | 2021-09-19 17:42:29 -0700 | [diff] [blame] | 176 | const double time_sec = |
| 177 | aos::time::DurationInSeconds(position_timestamp.time_since_epoch()); |
| 178 | output_struct.turret_voltage += |
| 179 | kTurretDitherGain * std::sin(2.0 * M_PI * time_sec * 30.0); |
James Kuszmaul | b7fe49e | 2020-03-05 08:24:44 -0800 | [diff] [blame] | 180 | output_struct.turret_voltage = |
| 181 | std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(), |
| 182 | turret_.operating_voltage()); |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 185 | bool zeroed; |
| 186 | bool estopped; |
| 187 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 188 | { |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 189 | const AbsoluteEncoderProfiledJointStatus *const intake_status = |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 190 | GetMutableTemporaryPointer(*status->fbb(), intake_status_offset); |
| 191 | |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 192 | zeroed = hood_status->zeroed() && intake_status->zeroed() && |
| 193 | turret_status->zeroed(); |
| 194 | estopped = hood_status->estopped() || intake_status->estopped() || |
| 195 | turret_status->estopped(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 199 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 200 | status_builder.add_zeroed(zeroed); |
| 201 | status_builder.add_estopped(estopped); |
| 202 | |
| 203 | status_builder.add_hood(hood_status_offset); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 204 | status_builder.add_intake(intake_status_offset); |
Kai Tinkess | fb46037 | 2020-02-08 14:05:48 -0800 | [diff] [blame] | 205 | status_builder.add_turret(turret_status_offset); |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 206 | status_builder.add_shooter(shooter_status_offset); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 207 | status_builder.add_aimer(aimer_status_offset); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 208 | |
| 209 | status->Send(status_builder.Finish()); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 210 | |
| 211 | if (output != nullptr) { |
| 212 | if (unsafe_goal) { |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 213 | output_struct.washing_machine_spinner_voltage = 0.0; |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 214 | if (unsafe_goal->shooting()) { |
Austin Schuh | 263dead | 2021-04-04 21:19:19 -0700 | [diff] [blame] | 215 | if (shooter_.ready() && shooter_.finisher_goal() > 10.0 && |
| 216 | shooter_.accelerator_goal() > 10.0) { |
Austin Schuh | 93109a5 | 2020-03-04 21:37:33 -0800 | [diff] [blame] | 217 | output_struct.feeder_voltage = 12.0; |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 218 | } else { |
| 219 | output_struct.feeder_voltage = 0.0; |
| 220 | } |
| 221 | output_struct.washing_machine_spinner_voltage = 5.0; |
| 222 | output_struct.intake_roller_voltage = 3.0; |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 223 | } else { |
| 224 | output_struct.feeder_voltage = 0.0; |
milind upadhyay | aec1aee | 2020-10-13 13:44:33 -0700 | [diff] [blame] | 225 | output_struct.intake_roller_voltage = |
| 226 | unsafe_goal->roller_voltage() + |
| 227 | std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0f); |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 228 | } |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame] | 229 | } else { |
| 230 | output_struct.intake_roller_voltage = 0.0; |
| 231 | } |
| 232 | output->Send(Output::Pack(*output->fbb(), &output_struct)); |
| 233 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 236 | } // namespace superstructure |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 237 | } // namespace control_loops |
| 238 | } // namespace y2020 |