blob: e11092a7a9a1a27c6b562dbc58909ee2f5e8282a [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include "y2023/control_loops/superstructure/superstructure.h"
2
3#include "aos/events/event_loop.h"
4#include "aos/flatbuffer_merge.h"
5#include "aos/network/team_number.h"
James Kuszmaulcf451fb2023-03-10 20:42:36 -08006#include "frc971/shooter_interpolation/interpolation.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -08007#include "frc971/zeroing/wrap.h"
Maxwell Hendersonb392b742023-03-05 07:53:51 -08008#include "y2023/control_loops/superstructure/arm/arm_trajectories_generated.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -08009
10DEFINE_bool(ignore_distance, false,
11 "If true, ignore distance when shooting and obay joystick_reader");
12
Stephan Pleinesf63bde82024-01-13 15:59:33 -080013namespace y2023::control_loops::superstructure {
Maxwell Hendersonad312342023-01-10 12:07:47 -080014
milind-u01bbcf22023-02-20 18:00:28 -080015using ::aos::monotonic_clock;
16
Maxwell Hendersonad312342023-01-10 12:07:47 -080017using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
18using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
19using frc971::control_loops::RelativeEncoderProfiledJointStatus;
20
Maxwell Hendersonb392b742023-03-05 07:53:51 -080021Superstructure::Superstructure(
22 ::aos::EventLoop *event_loop,
23 std::shared_ptr<const constants::Values> values,
24 const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories,
25 const ::std::string &name)
Maxwell Hendersonad312342023-01-10 12:07:47 -080026 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
27 name),
28 values_(values),
James Kuszmaulcf451fb2023-03-10 20:42:36 -080029 constants_fetcher_(event_loop),
Maxwell Hendersonad312342023-01-10 12:07:47 -080030 drivetrain_status_fetcher_(
31 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
32 "/drivetrain")),
33 joystick_state_fetcher_(
milind-u01bbcf22023-02-20 18:00:28 -080034 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Maxwell Hendersonb392b742023-03-05 07:53:51 -080035 arm_(values_, arm_trajectories.message()),
Maxwell Henderson8ca44562023-02-23 13:11:51 -080036 end_effector_(),
Austin Schuh595ffc72023-02-24 16:24:37 -080037 wrist_(values->wrist.subsystem_params) {
38 event_loop->SetRuntimeRealtimePriority(30);
39}
Maxwell Hendersonad312342023-01-10 12:07:47 -080040
41void Superstructure::RunIteration(const Goal *unsafe_goal,
42 const Position *position,
43 aos::Sender<Output>::Builder *output,
44 aos::Sender<Status>::Builder *status) {
milind-u01bbcf22023-02-20 18:00:28 -080045 const monotonic_clock::time_point timestamp =
46 event_loop()->context().monotonic_event_time;
Maxwell Hendersonad312342023-01-10 12:07:47 -080047
48 if (WasReset()) {
49 AOS_LOG(ERROR, "WPILib reset, restarting\n");
milind-u01bbcf22023-02-20 18:00:28 -080050 arm_.Reset();
Maxwell Henderson589cf272023-02-22 15:56:40 -080051 end_effector_.Reset();
Austin Schuhbdabc702023-02-24 16:21:07 -080052 wrist_.Reset();
Maxwell Hendersonad312342023-01-10 12:07:47 -080053 }
54
55 OutputT output_struct;
56 if (joystick_state_fetcher_.Fetch() &&
57 joystick_state_fetcher_->has_alliance()) {
58 alliance_ = joystick_state_fetcher_->alliance();
59 }
60 drivetrain_status_fetcher_.Fetch();
milind-u01bbcf22023-02-20 18:00:28 -080061
62 const uint32_t arm_goal_position =
63 unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u;
64
65 flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset =
66 arm_.Iterate(
67 timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr,
68 position->arm(),
69 unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false,
70 output != nullptr ? &output_struct.proximal_voltage : nullptr,
71 output != nullptr ? &output_struct.distal_voltage : nullptr,
milind-u18a901d2023-02-17 21:51:55 -080072 output != nullptr ? &output_struct.roll_joint_voltage : nullptr,
milind-u01bbcf22023-02-20 18:00:28 -080073 status->fbb());
74
Maxwell Henderson8ca44562023-02-23 13:11:51 -080075 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset =
Austin Schuhbdabc702023-02-24 16:21:07 -080076 wrist_.Iterate(
77 unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr,
78 position->wrist(),
79 output != nullptr ? &(output_struct.wrist_voltage) : nullptr,
80 status->fbb());
Maxwell Henderson8ca44562023-02-23 13:11:51 -080081
milind-u71da5392023-02-26 12:45:00 -080082 end_effector_.RunIteration(
Maxwell Henderson5938a832023-02-23 09:33:15 -080083 timestamp,
84 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
milind-u6ff6d9e2023-02-24 20:15:06 -080085 position->has_roller_falcon()
86 ? position->roller_falcon()->torque_current()
87 : 0.0,
88 position->cone_position(), position->end_effector_cube_beam_break(),
Maxwell Henderson64f37452023-03-11 13:39:21 -080089 &output_struct.roller_voltage,
90 unsafe_goal != nullptr ? unsafe_goal->preloaded_with_cone() : false);
Maxwell Henderson589cf272023-02-22 15:56:40 -080091
milind-u01bbcf22023-02-20 18:00:28 -080092 if (output) {
93 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
94 }
95
Maxwell Hendersonad312342023-01-10 12:07:47 -080096 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhbdabc702023-02-24 16:21:07 -080097 status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed());
98 status_builder.add_estopped(wrist_.estopped() || arm_.estopped());
milind-u01bbcf22023-02-20 18:00:28 -080099 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -0800100 status_builder.add_wrist(wrist_offset);
milind-u71da5392023-02-26 12:45:00 -0800101 status_builder.add_end_effector_state(end_effector_.state());
102 // TODO(milind): integrate this with ML game piece detection somehow
103 status_builder.add_game_piece(end_effector_.game_piece());
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800104 const std::optional<double> game_piece_position =
105 LateralOffsetForTimeOfFlight(position->cone_position());
106 if (game_piece_position.has_value()) {
107 status_builder.add_game_piece_position(game_piece_position.value());
108 }
milind-u01bbcf22023-02-20 18:00:28 -0800109
Maxwell Hendersonad312342023-01-10 12:07:47 -0800110 (void)status->Send(status_builder.Finish());
111}
112
113double Superstructure::robot_velocity() const {
114 return (drivetrain_status_fetcher_.get() != nullptr
115 ? drivetrain_status_fetcher_->robot_speed()
116 : 0.0);
117}
118
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800119std::optional<double> Superstructure::LateralOffsetForTimeOfFlight(
120 double reading) {
121 switch (end_effector_.game_piece()) {
122 case vision::Class::NONE:
123 return std::nullopt;
124 case vision::Class::CUBE:
125 // Cubes are definitionally centered.
126 return 0.0;
127 case vision::Class::CONE_UP:
128 case vision::Class::CONE_DOWN:
129 // execute logic below.
130 break;
131 }
132 constexpr double kInvalidReading = 0.93;
James Kuszmaul4fe5a772023-04-05 20:17:43 -0700133 if (reading > kInvalidReading || !std::isfinite(reading)) {
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800134 return std::nullopt;
135 }
136 const TimeOfFlight *calibration = CHECK_NOTNULL(
137 CHECK_NOTNULL(constants_fetcher_.constants().robot())->tof());
138 // TODO(james): Use a generic interpolation table class.
139 auto table = CHECK_NOTNULL(calibration->interpolation_table());
140 CHECK_EQ(2u, table->size());
141 double x1 = table->Get(0)->tof_reading();
142 double x2 = table->Get(1)->tof_reading();
143 double y1 = table->Get(0)->lateral_position();
144 double y2 = table->Get(1)->lateral_position();
145 return frc971::shooter_interpolation::Blend((reading - x1) / (x2 - x1), y1,
146 y2);
147}
148
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800149} // namespace y2023::control_loops::superstructure