blob: 70ea319b83273c4e6cd8bacfa117b551d272b04f [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
Ravago Jones937587c2020-12-26 17:21:09 -08009using frc971::control_loops::AbsoluteAndAbsoluteEncoderProfiledJointStatus;
Stephan Massaltd021f972020-01-05 20:41:23 -080010using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
11using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
12
13Superstructure::Superstructure(::aos::EventLoop *event_loop,
14 const ::std::string &name)
James Kuszmaul61750662021-06-21 21:32:33 -070015 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
16 name),
Sabina Davis0f2d38c2020-02-08 17:01:21 -080017 hood_(constants::GetValues().hood),
Kai Tinkessfb460372020-02-08 14:05:48 -080018 intake_joint_(constants::GetValues().intake),
Sabina Davis0f31d3f2020-02-20 20:41:00 -080019 turret_(constants::GetValues().turret.subsystem_params),
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080020 drivetrain_status_fetcher_(
21 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
James Kuszmaula53c3ac2020-02-22 19:36:01 -080022 "/drivetrain")),
23 joystick_state_fetcher_(
24 event_loop->MakeFetcher<aos::JoystickState>("/aos")) {
Sabina Daviscf08b152020-01-31 22:12:09 -080025 event_loop->SetRuntimeRealtimePriority(30);
Stephan Massaltd021f972020-01-05 20:41:23 -080026}
27
milind upadhyayaec1aee2020-10-13 13:44:33 -070028double Superstructure::robot_speed() const {
29 return (drivetrain_status_fetcher_.get() != nullptr
30 ? drivetrain_status_fetcher_->robot_speed()
31 : 0.0);
32}
33
Sabina Daviscf08b152020-01-31 22:12:09 -080034void Superstructure::RunIteration(const Goal *unsafe_goal,
35 const Position *position,
Stephan Massaltd021f972020-01-05 20:41:23 -080036 aos::Sender<Output>::Builder *output,
37 aos::Sender<Status>::Builder *status) {
38 if (WasReset()) {
39 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Sabina Daviscf08b152020-01-31 22:12:09 -080040 hood_.Reset();
Sabina Davis0f2d38c2020-02-08 17:01:21 -080041 intake_joint_.Reset();
Kai Tinkessfb460372020-02-08 14:05:48 -080042 turret_.Reset();
Stephan Massaltd021f972020-01-05 20:41:23 -080043 }
44
Sabina Davis0f31d3f2020-02-20 20:41:00 -080045 const aos::monotonic_clock::time_point position_timestamp =
46 event_loop()->context().monotonic_event_time;
47
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080048 if (drivetrain_status_fetcher_.Fetch()) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080049 aos::Alliance alliance = aos::Alliance::kInvalid;
James Kuszmaul519585d2020-03-08 22:32:48 -070050 joystick_state_fetcher_.Fetch();
51 if (joystick_state_fetcher_.get() != nullptr) {
James Kuszmaula53c3ac2020-02-22 19:36:01 -080052 alliance = joystick_state_fetcher_->alliance();
53 }
James Kuszmaul3b393d72020-02-26 19:43:51 -080054 const turret::Aimer::WrapMode mode =
James Kuszmaulb83d6e12020-02-22 20:44:48 -080055 (unsafe_goal != nullptr && unsafe_goal->shooting())
James Kuszmaul3b393d72020-02-26 19:43:51 -080056 ? turret::Aimer::WrapMode::kAvoidWrapping
57 : turret::Aimer::WrapMode::kAvoidEdges;
58 aimer_.Update(drivetrain_status_fetcher_.get(), alliance, mode,
59 turret::Aimer::ShotMode::kShootOnTheFly);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080060 }
61
milind upadhyayaec1aee2020-10-13 13:44:33 -070062 const float velocity = robot_speed();
63
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -080064 const flatbuffers::Offset<AimerStatus> aimer_status_offset =
65 aimer_.PopulateStatus(status->fbb());
66
James Kuszmaul98154a22021-04-03 16:09:29 -070067 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-u0a178a82021-09-28 18:42:09 -070075 constants::Values::FlywheelShotParams flywheel_shot_params;
James Kuszmaul98154a22021-04-03 16:09:29 -070076 if (constants::GetValues().shot_interpolation_table.GetInRange(
milind-u0a178a82021-09-28 18:42:09 -070077 distance_to_goal, &shot_params) &&
78 constants::GetValues().flywheel_shot_interpolation_table.GetInRange(
79 shot_params.velocity_ball, &flywheel_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(
85 *shooter_goal.fbb(), flywheel_shot_params.velocity_accelerator,
86 flywheel_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()) {
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 Davis0f2d38c2020-02-08 17:01:21 -0800132 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 Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800139 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 Kuszmaul98154a22021-04-03 16:09:29 -0700144
Kai Tinkessfb460372020-02-08 14:05:48 -0800145 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
146 turret_status_offset = turret_.Iterate(
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800147 turret_goal, position->turret(),
Kai Tinkessfb460372020-02-08 14:05:48 -0800148 output != nullptr ? &(output_struct.turret_voltage) : nullptr,
149 status->fbb());
150
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800151 flatbuffers::Offset<ShooterStatus> shooter_status_offset =
152 shooter_.RunIteration(
James Kuszmaul98154a22021-04-03 16:09:29 -0700153 unsafe_goal != nullptr
154 ? (unsafe_goal->shooter_tracking() ? &shooter_goal.message()
155 : unsafe_goal->shooter())
156 : nullptr,
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800157 position->shooter(), status->fbb(),
158 output != nullptr ? &(output_struct) : nullptr, position_timestamp);
159
John Park0a245a02020-02-02 14:10:15 -0800160 climber_.Iterate(unsafe_goal, output != nullptr ? &(output_struct) : nullptr);
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
198 Status::Builder status_builder = status->MakeBuilder<Status>();
199
Sabina Daviscf08b152020-01-31 22:12:09 -0800200 status_builder.add_zeroed(zeroed);
201 status_builder.add_estopped(estopped);
202
203 status_builder.add_hood(hood_status_offset);
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800204 status_builder.add_intake(intake_status_offset);
Kai Tinkessfb460372020-02-08 14:05:48 -0800205 status_builder.add_turret(turret_status_offset);
Sabina Davis0f31d3f2020-02-20 20:41:00 -0800206 status_builder.add_shooter(shooter_status_offset);
James Kuszmaulb1b2d8e2020-02-21 21:11:46 -0800207 status_builder.add_aimer(aimer_status_offset);
Stephan Massaltd021f972020-01-05 20:41:23 -0800208
209 status->Send(status_builder.Finish());
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800210
211 if (output != nullptr) {
212 if (unsafe_goal) {
Austin Schuh13e55522020-02-29 23:11:17 -0800213 output_struct.washing_machine_spinner_voltage = 0.0;
Austin Schuh43a220f2020-02-26 22:02:34 -0800214 if (unsafe_goal->shooting()) {
Austin Schuh263dead2021-04-04 21:19:19 -0700215 if (shooter_.ready() && shooter_.finisher_goal() > 10.0 &&
216 shooter_.accelerator_goal() > 10.0) {
Austin Schuh93109a52020-03-04 21:37:33 -0800217 output_struct.feeder_voltage = 12.0;
Austin Schuh13e55522020-02-29 23:11:17 -0800218 } 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 Schuh43a220f2020-02-26 22:02:34 -0800223 } else {
224 output_struct.feeder_voltage = 0.0;
milind upadhyayaec1aee2020-10-13 13:44:33 -0700225 output_struct.intake_roller_voltage =
226 unsafe_goal->roller_voltage() +
227 std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0f);
Austin Schuh43a220f2020-02-26 22:02:34 -0800228 }
Sabina Davis0f2d38c2020-02-08 17:01:21 -0800229 } else {
230 output_struct.intake_roller_voltage = 0.0;
231 }
232 output->Send(Output::Pack(*output->fbb(), &output_struct));
233 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800234}
235
Sabina Daviscf08b152020-01-31 22:12:09 -0800236} // namespace superstructure
Stephan Massaltd021f972020-01-05 20:41:23 -0800237} // namespace control_loops
238} // namespace y2020