blob: 4fb5c1d4fb642039762ab3553e9a31205cd8e0ef [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/control_loops/superstructure/superstructure.h"
2
milind-u0beb7dc2021-10-16 19:31:33 -07003#include "aos/containers/sized_array.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08004#include "aos/events/event_loop.h"
Austin Schuh01d81c32021-11-06 22:59:56 -07005#include "aos/network/team_number.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08006
7namespace y2020 {
8namespace control_loops {
9namespace superstructure {
10
Ravago Jones937587c2020-12-26 17:21:09 -080011using frc971::control_loops::AbsoluteAndAbsoluteEncoderProfiledJointStatus;
Stephan Massaltd021f972020-01-05 20:41:23 -080012using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
13using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
14
15Superstructure::Superstructure(::aos::EventLoop *event_loop,
16 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070017 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
18 name),
Sabina Davis0f2d38c2020-02-08 17:01:21 -080019 hood_(constants::GetValues().hood),
Kai Tinkessfb460372020-02-08 14:05:48 -080020 intake_joint_(constants::GetValues().intake),
Sabina Davis0f31d3f2020-02-20 20:41:00 -080021 turret_(constants::GetValues().turret.subsystem_params),
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080022 drivetrain_status_fetcher_(
23 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
James Kuszmaula53c3ac2020-02-22 19:36:01 -080024 "/drivetrain")),
25 joystick_state_fetcher_(
Austin Schuh01d81c32021-11-06 22:59:56 -070026 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
27 has_turret_(::aos::network::GetTeamNumber() != 9971) {
Sabina Daviscf08b152020-01-31 22:12:09 -080028 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080029}
30
milind upadhyayaec1aee2020-10-13 13:44:33 -070031double Superstructure::robot_speed() const {
32 return (drivetrain_status_fetcher_.get() != nullptr
33 ? drivetrain_status_fetcher_->robot_speed()
34 : 0.0);
35}
36
Sabina Daviscf08b152020-01-31 22:12:09 -080037void Superstructure::RunIteration(const Goal *unsafe_goal,
38 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080039 aos::Sender<Output>::Builder *output,
40 aos::Sender<Status>::Builder *status) {
41 if (WasReset()) {
42 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080043 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080044 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080045 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080046 }
47
Sabina Davis0f31d3f2020-02-20 20:41:00 -080048 const aos::monotonic_clock::time_point position_timestamp =
49 event_loop()->context().monotonic_event_time;
50
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080051 if (drivetrain_status_fetcher_.Fetch()) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080052 aos::Alliance alliance = aos::Alliance::kInvalid;
James Kuszmaul519585d2020-03-08 22:32:48 -070053 joystick_state_fetcher_.Fetch();
54 if (joystick_state_fetcher_.get() != nullptr) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080055 alliance = joystick_state_fetcher_->alliance();
56 }
James Kuszmaul3b393d72020-02-26 19:43:51 -080057 const turret::Aimer::WrapMode mode =
James Kuszmaulb83d6e12020-02-22 20:44:48 -080058 (unsafe_goal != nullptr && unsafe_goal->shooting())
James Kuszmaul3b393d72020-02-26 19:43:51 -080059 ? turret::Aimer::WrapMode::kAvoidWrapping
60 : turret::Aimer::WrapMode::kAvoidEdges;
61 aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode,
62 turret::Aimer::ShotMode::kShootOnTheFly);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080063 }
64
milind upadhyayaec1aee2020-10-13 13:44:33 -070065 const float velocity = robot_speed();
66
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080067 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
68 aimer_.PopulateStatus(status->fbb());
69
James Kuszmaul98154a22021-04-03 16:09:29 -070070 const double distance_to_goal = aimer_.DistanceToGoal();
71
72 aos::FlatbufferFixedAllocatorArray<
73 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64>
74 hood_goal;
75 aos::FlatbufferFixedAllocatorArray<ShooterGoal, 64> shooter_goal;
76
77 constants::Values::ShotParams shot_params;
78 if (constants::GetValues().shot_interpolation_table.GetInRange(
milind-uf7fadbf2021-11-07 14:10:54 -080079 distance_to_goal, &shot_params)) {
James Kuszmaul98154a22021-04-03 16:09:29 -070080 hood_goal.Finish(frc971::control_loops::
81 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
82 *hood_goal.fbb(), shot_params.hood_angle));
83
milind-u0a178a82021-09-28 18:42:09 -070084 shooter_goal.Finish(CreateShooterGoal(
milind-uf7fadbf2021-11-07 14:10:54 -080085 *shooter_goal.fbb(), shot_params.velocity_accelerator,
86 shot_params.velocity_finisher));
James Kuszmaul98154a22021-04-03 16:09:29 -070087 } 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 Daviscf08b152020-01-31 22:12:09 -080096 OutputT output_struct;
97
Ravago Jones937587c2020-12-26 17:21:09 -080098 flatbuffers::Offset<AbsoluteAndAbsoluteEncoderProfiledJointStatus>
99 hood_status_offset = hood_.Iterate(
James Kuszmaul98154a22021-04-03 16:09:29 -0700100 unsafe_goal != nullptr
101 ? (unsafe_goal->hood_tracking() ? &hood_goal.message()
102 : unsafe_goal->hood())
103 : nullptr,
Ravago Jones937587c2020-12-26 17:21:09 -0800104 position->hood(),
105 output != nullptr ? &(output_struct.hood_voltage) : nullptr,
106 status->fbb());
Sabina Daviscf08b152020-01-31 22:12:09 -0800107
Austin Schuh13e55522020-02-29 23:11:17 -0800108 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()) {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700115 intake_joint_.set_max_acceleration(30.0);
Austin Schuh13e55522020-02-29 23:11:17 -0800116 constexpr std::chrono::milliseconds kPeriod =
117 std::chrono::milliseconds(250);
118 if ((position_timestamp - shooting_start_time_) % (kPeriod * 2) <
119 kPeriod) {
120 intake_joint_.set_min_position(-0.25);
121 } else {
122 intake_joint_.set_min_position(-0.75);
123 }
124 } else {
Austin Schuh93ddcb42021-10-25 21:54:11 -0700125 intake_joint_.clear_max_acceleration();
Austin Schuh13e55522020-02-29 23:11:17 -0800126 intake_joint_.clear_min_position();
127 }
128
129 if (!unsafe_goal->shooting()) {
130 shooting_start_time_ = aos::monotonic_clock::min_time;
131 }
132 }
133
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800134 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset =
135 intake_joint_.Iterate(
136 unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr,
137 position->intake_joint(),
138 output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr,
139 status->fbb());
140
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800141 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
142 *turret_goal = unsafe_goal != nullptr ? (unsafe_goal->turret_tracking()
143 ? aimer_.TurretGoal()
144 : unsafe_goal->turret())
145 : nullptr;
James Kuszmaul98154a22021-04-03 16:09:29 -0700146
Kai Tinkessfb460372020-02-08 14:05:48 -0800147 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
148 turret_status_offset = turret_.Iterate(
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800149 turret_goal, position->turret(),
Kai Tinkessfb460372020-02-08 14:05:48 -0800150 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
151 status->fbb());
152
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800153 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
154 shooter_.RunIteration(
James Kuszmaul98154a22021-04-03 16:09:29 -0700155 unsafe_goal != nullptr
156 ? (unsafe_goal->shooter_tracking() ? &shooter_goal.message()
157 : unsafe_goal->shooter())
158 : nullptr,
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800159 position->shooter(), status->fbb(),
160 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
161
Ravago Jones937587c2020-12-26 17:21:09 -0800162 const AbsoluteAndAbsoluteEncoderProfiledJointStatus *const hood_status =
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800163 GetMutableTemporaryPointer(*status->fbb(), hood_status_offset);
164
Austin Schuh2fb23642020-02-29 15:10:51 -0800165 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 Kuszmaul9cbdb022021-09-19 17:42:29 -0700170 // TODO(james): I'm not sure how helpful this gain is.
James Kuszmaul519585d2020-03-08 22:32:48 -0700171 const double turret_velocity_sign =
172 turret_status->velocity() * kTurretFrictionGain;
Austin Schuh2fb23642020-02-29 15:10:51 -0800173 output_struct.turret_voltage +=
Austin Schuh78f0bfd2020-02-29 23:04:21 -0800174 std::clamp(turret_velocity_sign, -kTurretFrictionVoltageLimit,
Austin Schuh2fb23642020-02-29 15:10:51 -0800175 kTurretFrictionVoltageLimit);
James Kuszmaul9cbdb022021-09-19 17:42:29 -0700176 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 Kuszmaulb7fe49e2020-03-05 08:24:44 -0800180 output_struct.turret_voltage =
181 std::clamp(output_struct.turret_voltage, -turret_.operating_voltage(),
182 turret_.operating_voltage());
Austin Schuh2fb23642020-02-29 15:10:51 -0800183 }
184
Sabina Daviscf08b152020-01-31 22:12:09 -0800185 bool zeroed;
186 bool estopped;
187
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800188 {
Kai Tinkessfb460372020-02-08 14:05:48 -0800189 const AbsoluteEncoderProfiledJointStatus *const intake_status =
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800190 GetMutableTemporaryPointer(*status->fbb(), intake_status_offset);
191
Kai Tinkessfb460372020-02-08 14:05:48 -0800192 zeroed = hood_status->zeroed() && intake_status->zeroed() &&
193 turret_status->zeroed();
194 estopped = hood_status->estopped() || intake_status->estopped() ||
195 turret_status->estopped();
Stephan Massaltd021f972020-01-05 20:41:23 -0800196 }
197
milind-u0beb7dc2021-10-16 19:31:33 -0700198 flatbuffers::Offset<flatbuffers::Vector<Subsystem>>
199 subsystems_not_ready_offset;
200 const bool turret_ready =
Austin Schuh01d81c32021-11-06 22:59:56 -0700201 (std::abs(turret_.goal(0) - turret_.position()) < 0.025) || !has_turret_;
milind-u0beb7dc2021-10-16 19:31:33 -0700202 if (unsafe_goal && unsafe_goal->shooting() &&
203 (!shooter_.ready() || !turret_ready)) {
204 aos::SizedArray<Subsystem, 3> subsystems_not_ready;
205 if (!shooter_.finisher_ready()) {
206 subsystems_not_ready.push_back(Subsystem::FINISHER);
207 }
208 if (!shooter_.accelerator_ready()) {
209 subsystems_not_ready.push_back(Subsystem::ACCELERATOR);
210 }
211 if (!turret_ready) {
212 subsystems_not_ready.push_back(Subsystem::TURRET);
213 }
214
215 subsystems_not_ready_offset =
216 status->fbb()->CreateVector(subsystems_not_ready.backing_array().data(),
217 subsystems_not_ready.size());
218 }
219
Stephan Massaltd021f972020-01-05 20:41:23 -0800220 Status::Builder status_builder = status->MakeBuilder<Status>();
221
Sabina Daviscf08b152020-01-31 22:12:09 -0800222 status_builder.add_zeroed(zeroed);
223 status_builder.add_estopped(estopped);
224
225 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800226 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800227 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800228 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800229 status_builder.add_aimer(aimer_status_offset);
milind-u0beb7dc2021-10-16 19:31:33 -0700230 status_builder.add_subsystems_not_ready(subsystems_not_ready_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800231
232 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800233
234 if (output != nullptr) {
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700235 output_struct.washing_machine_spinner_voltage = 0.0;
236 output_struct.feeder_voltage = 0.0;
237 output_struct.intake_roller_voltage = 0.0;
Austin Schuh46712f52021-10-24 22:24:45 -0700238 output_struct.climber_voltage = 0.0;
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800239 if (unsafe_goal) {
Austin Schuh46712f52021-10-24 22:24:45 -0700240 if (unsafe_goal->has_turret()) {
241 output_struct.climber_voltage =
242 std::clamp(unsafe_goal->climber_voltage(), -12.0f, 12.0f);
Ravago Jonese92ff112021-10-23 17:27:44 -0700243
Austin Schuh46712f52021-10-24 22:24:45 -0700244 // Make sure the turret is relatively close to the goal before turning
245 // the climber on.
246 CHECK(unsafe_goal->has_turret());
247 if (std::abs(unsafe_goal->turret()->unsafe_goal() -
248 turret_.position()) > 0.1) {
249 output_struct.climber_voltage = 0;
250 }
milind-ud6534142021-10-24 17:42:58 -0700251 }
252
Ravago Jonesac850da2021-10-13 20:38:29 -0700253 if (unsafe_goal->shooting() || unsafe_goal->intake_preloading()) {
254 preloading_timeout_ = position_timestamp + kPreloadingTimeout;
255 }
256
257 if (position_timestamp <= preloading_timeout_ &&
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700258 !position->intake_beambreak_triggered()) {
259 output_struct.washing_machine_spinner_voltage = 5.0;
260 output_struct.feeder_voltage = 12.0;
Ravago Jonesac850da2021-10-13 20:38:29 -0700261
262 preloading_backpower_timeout_ =
263 position_timestamp + kPreloadingBackpowerDuration;
264 }
265
266 if (position->intake_beambreak_triggered() &&
267 position_timestamp <= preloading_backpower_timeout_) {
268 output_struct.feeder_voltage = -12.0;
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700269 }
270
Austin Schuh43a220f2020-02-26 22:02:34 -0800271 if (unsafe_goal->shooting()) {
Austin Schuh01d81c32021-11-06 22:59:56 -0700272 if ((shooter_.ready() ||
273 (!has_turret_ && shooter_.accelerator_ready())) &&
274 turret_ready) {
Austin Schuh93109a52020-03-04 21:37:33 -0800275 output_struct.feeder_voltage = 12.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800276 }
277 output_struct.washing_machine_spinner_voltage = 5.0;
278 output_struct.intake_roller_voltage = 3.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800279 } else {
milind upadhyayaec1aee2020-10-13 13:44:33 -0700280 output_struct.intake_roller_voltage =
281 unsafe_goal->roller_voltage() +
282 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0f);
Austin Schuh43a220f2020-02-26 22:02:34 -0800283 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800284 }
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700285
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800286 output->Send(Output::Pack(*output->fbb(), &output_struct));
287 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800288}
289
Sabina Daviscf08b152020-01-31 22:12:09 -0800290} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800291} // namespace control_loops
292} // namespace y2020