blob: 224a24fbc6f26463fd474d1ce2c5d00256c84753 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/control_loops/superstructure/superstructure.h"
2
3#include "aos/events/event_loop.h"
4
5namespace y2020 {
6namespace control_loops {
7namespace superstructure {
8
9using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
10using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
11
12Superstructure::Superstructure(::aos::EventLoop *event_loop,
13 const ::std::string &name)
14 : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
Sabina Daviscf08b152020-01-31 22:12:09 -080015 name),
Sabina Davis0f2d38c2020-02-08 17:01:21 -080016 hood_(constants::GetValues().hood),
Kai Tinkessfb460372020-02-08 14:05:48 -080017 intake_joint_(constants::GetValues().intake),
Sabina Davis0f31d3f2020-02-20 20:41:00 -080018 turret_(constants::GetValues().turret.subsystem_params),
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080019 drivetrain_status_fetcher_(
20 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
James Kuszmaula53c3ac2020-02-22 19:36:01 -080021 "/drivetrain")),
22 joystick_state_fetcher_(
23 event_loop->MakeFetcher<aos::JoystickState>("/aos")) {
Sabina Daviscf08b152020-01-31 22:12:09 -080024 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080025}
26
Sabina Daviscf08b152020-01-31 22:12:09 -080027void Superstructure::RunIteration(const Goal *unsafe_goal,
28 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080029 aos::Sender<Output>::Builder *output,
30 aos::Sender<Status>::Builder *status) {
31 if (WasReset()) {
32 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080033 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080034 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080035 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080036 }
37
Sabina Davis0f31d3f2020-02-20 20:41:00 -080038 const aos::monotonic_clock::time_point position_timestamp =
39 event_loop()->context().monotonic_event_time;
40
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080041 if (drivetrain_status_fetcher_.Fetch()) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080042 aos::Alliance alliance = aos::Alliance::kInvalid;
43 if (joystick_state_fetcher_.Fetch()) {
44 alliance = joystick_state_fetcher_->alliance();
45 }
James Kuszmaul3b393d72020-02-26 19:43:51 -080046 const turret::Aimer::WrapMode mode =
James Kuszmaulb83d6e12020-02-22 20:44:48 -080047 (unsafe_goal != nullptr && unsafe_goal->shooting())
James Kuszmaul3b393d72020-02-26 19:43:51 -080048 ? turret::Aimer::WrapMode::kAvoidWrapping
49 : turret::Aimer::WrapMode::kAvoidEdges;
50 aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode,
51 turret::Aimer::ShotMode::kShootOnTheFly);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080052 }
53
54 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
55 aimer_.PopulateStatus(status->fbb());
56
Sabina Daviscf08b152020-01-31 22:12:09 -080057 OutputT output_struct;
58
59 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset =
60 hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr,
61 position->hood(),
62 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
63 status->fbb());
64
Austin Schuh13e55522020-02-29 23:11:17 -080065 if (unsafe_goal != nullptr) {
66 if (unsafe_goal->shooting() &&
67 shooting_start_time_ == aos::monotonic_clock::min_time) {
68 shooting_start_time_ = position_timestamp;
69 }
70
71 if (unsafe_goal->shooting()) {
72 constexpr std::chrono::milliseconds kPeriod =
73 std::chrono::milliseconds(250);
74 if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) <
75 kPeriod) {
76 intake_joint_.set_min_position(-0.25);
77 } else {
78 intake_joint_.set_min_position(-0.75);
79 }
80 } else {
81 intake_joint_.clear_min_position();
82 }
83
84 if (!unsafe_goal->shooting()) {
85 shooting_start_time_ = aos::monotonic_clock::min_time;
86 }
87 }
88
Sabina Davis0f2d38c2020-02-08 17:01:21 -080089 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
90 intake_joint_.Iterate(
91 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
92 position->intake_joint(),
93 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
94 status->fbb());
95
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080096 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
97 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
98 ? aimer_.TurretGoal()
99 : unsafe_goal->turret())
100 : nullptr;
Kai Tinkessfb460372020-02-08 14:05:48 -0800101 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
102 turret_status_offset = turret_.Iterate(
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800103 turret_goal, position->turret(),
Kai Tinkessfb460372020-02-08 14:05:48 -0800104 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
105 status->fbb());
106
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800107 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
108 shooter_.RunIteration(
109 unsafe_goal != nullptr ? unsafe_goal->shooter() : nullptr,
110 position->shooter(), status->fbb(),
111 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
112
John Park0a245a02020-02-02 14:10:15 -0800113 climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr);
114
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800115 const AbsoluteEncoderProfiledJointStatus *const hood_status =
116 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
117
Austin Schuh2fb23642020-02-29 15:10:51 -0800118 const PotAndAbsoluteEncoderProfiledJointStatus *const turret_status =
119 GetMutableTemporaryPointer(*status->fbb(), turret_status_offset);
120
121 if (output != nullptr) {
122 // Friction is a pain and putting a really high burden on the integrator.
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800123 const double turret_velocity_sign = turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -0800124 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800125 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -0800126 kTurretFrictionVoltageLimit);
James Kuszmaulb7fe49e2020-03-05 08:24:44 -0800127 output_struct.turret_voltage =
128 std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(),
129 turret_.operating_voltage());
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800130
131 // Friction is a pain and putting a really high burden on the integrator.
132 const double hood_velocity_sign = hood_status->velocity() * kHoodFrictionGain;
133 output_struct.hood_voltage +=
134 std::clamp(hood_velocity_sign, -kHoodFrictionVoltageLimit,
135 kHoodFrictionVoltageLimit);
136
137 // And dither the output.
138 time_ += 0.00505;
139 output_struct.hood_voltage += 1.3 * std::sin(time_ * 2.0 * M_PI * 30.0);
Austin Schuh2fb23642020-02-29 15:10:51 -0800140 }
141
Sabina Daviscf08b152020-01-31 22:12:09 -0800142 bool zeroed;
143 bool estopped;
144
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800145 {
Kai Tinkessfb460372020-02-08 14:05:48 -0800146 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800147 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
148
Kai Tinkessfb460372020-02-08 14:05:48 -0800149 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
150 turret_status->zeroed();
151 estopped = hood_status->estopped() || intake_status->estopped() ||
152 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800153 }
154
155 Status::Builder status_builder = status->MakeBuilder<Status>();
156
Sabina Daviscf08b152020-01-31 22:12:09 -0800157 status_builder.add_zeroed(zeroed);
158 status_builder.add_estopped(estopped);
159
160 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800161 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800162 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800163 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800164 status_builder.add_aimer(aimer_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800165
166 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800167
168 if (output != nullptr) {
169 if (unsafe_goal) {
Austin Schuh13e55522020-02-29 23:11:17 -0800170 output_struct.washing_machine_spinner_voltage = 0.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800171 if (unsafe_goal->shooting()) {
Austin Schuh13e55522020-02-29 23:11:17 -0800172 if (shooter_.ready() &&
173 unsafe_goal->shooter()->velocity_accelerator() > 10.0 &&
174 unsafe_goal->shooter()->velocity_finisher() > 10.0) {
175 output_struct.feeder_voltage = 9.0;
176 } else {
177 output_struct.feeder_voltage = 0.0;
178 }
179 output_struct.washing_machine_spinner_voltage = 5.0;
180 output_struct.intake_roller_voltage = 3.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800181 } else {
182 output_struct.feeder_voltage = 0.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800183 output_struct.intake_roller_voltage = unsafe_goal->roller_voltage();
Austin Schuh43a220f2020-02-26 22:02:34 -0800184 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800185 } else {
186 output_struct.intake_roller_voltage = 0.0;
187 }
188 output->Send(Output::Pack(*output->fbb(), &output_struct));
189 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800190}
191
Sabina Daviscf08b152020-01-31 22:12:09 -0800192} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800193} // namespace control_loops
194} // namespace y2020