blob: 60881f2d15bccac8e331c98b28fde2ca1eab3217 [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -070010ABSL_FLAG(bool, ignore_distance, false,
11 "If true, ignore distance when shooting and obay joystick_reader");
Maxwell Hendersonad312342023-01-10 12:07:47 -080012
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_(),
James Kuszmaul630ab1d2024-01-09 16:38:57 -080037 wrist_(constants_fetcher_.constants().wrist(),
38 constants_fetcher_.constants().robot()->wrist_zero()) {
Austin Schuh595ffc72023-02-24 16:24:37 -080039 event_loop->SetRuntimeRealtimePriority(30);
40}
Maxwell Hendersonad312342023-01-10 12:07:47 -080041
42void Superstructure::RunIteration(const Goal *unsafe_goal,
43 const Position *position,
44 aos::Sender<Output>::Builder *output,
45 aos::Sender<Status>::Builder *status) {
milind-u01bbcf22023-02-20 18:00:28 -080046 const monotonic_clock::time_point timestamp =
47 event_loop()->context().monotonic_event_time;
Maxwell Hendersonad312342023-01-10 12:07:47 -080048
49 if (WasReset()) {
50 AOS_LOG(ERROR, "WPILib reset, restarting\n");
milind-u01bbcf22023-02-20 18:00:28 -080051 arm_.Reset();
Maxwell Henderson589cf272023-02-22 15:56:40 -080052 end_effector_.Reset();
Austin Schuhbdabc702023-02-24 16:21:07 -080053 wrist_.Reset();
Maxwell Hendersonad312342023-01-10 12:07:47 -080054 }
55
56 OutputT output_struct;
57 if (joystick_state_fetcher_.Fetch() &&
58 joystick_state_fetcher_->has_alliance()) {
59 alliance_ = joystick_state_fetcher_->alliance();
60 }
61 drivetrain_status_fetcher_.Fetch();
milind-u01bbcf22023-02-20 18:00:28 -080062
63 const uint32_t arm_goal_position =
64 unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u;
65
66 flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset =
67 arm_.Iterate(
68 timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr,
69 position->arm(),
70 unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false,
71 output != nullptr ? &output_struct.proximal_voltage : nullptr,
72 output != nullptr ? &output_struct.distal_voltage : nullptr,
milind-u18a901d2023-02-17 21:51:55 -080073 output != nullptr ? &output_struct.roll_joint_voltage : nullptr,
milind-u01bbcf22023-02-20 18:00:28 -080074 status->fbb());
75
Maxwell Henderson8ca44562023-02-23 13:11:51 -080076 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset =
Austin Schuhbdabc702023-02-24 16:21:07 -080077 wrist_.Iterate(
78 unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr,
79 position->wrist(),
80 output != nullptr ? &(output_struct.wrist_voltage) : nullptr,
81 status->fbb());
Maxwell Henderson8ca44562023-02-23 13:11:51 -080082
milind-u71da5392023-02-26 12:45:00 -080083 end_effector_.RunIteration(
Maxwell Henderson5938a832023-02-23 09:33:15 -080084 timestamp,
85 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
milind-u6ff6d9e2023-02-24 20:15:06 -080086 position->has_roller_falcon()
87 ? position->roller_falcon()->torque_current()
88 : 0.0,
89 position->cone_position(), position->end_effector_cube_beam_break(),
Maxwell Henderson64f37452023-03-11 13:39:21 -080090 &output_struct.roller_voltage,
91 unsafe_goal != nullptr ? unsafe_goal->preloaded_with_cone() : false);
Maxwell Henderson589cf272023-02-22 15:56:40 -080092
milind-u01bbcf22023-02-20 18:00:28 -080093 if (output) {
94 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
95 }
96
Maxwell Hendersonad312342023-01-10 12:07:47 -080097 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhbdabc702023-02-24 16:21:07 -080098 status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed());
99 status_builder.add_estopped(wrist_.estopped() || arm_.estopped());
milind-u01bbcf22023-02-20 18:00:28 -0800100 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -0800101 status_builder.add_wrist(wrist_offset);
milind-u71da5392023-02-26 12:45:00 -0800102 status_builder.add_end_effector_state(end_effector_.state());
103 // TODO(milind): integrate this with ML game piece detection somehow
104 status_builder.add_game_piece(end_effector_.game_piece());
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800105 const std::optional<double> game_piece_position =
106 LateralOffsetForTimeOfFlight(position->cone_position());
107 if (game_piece_position.has_value()) {
108 status_builder.add_game_piece_position(game_piece_position.value());
109 }
milind-u01bbcf22023-02-20 18:00:28 -0800110
Maxwell Hendersonad312342023-01-10 12:07:47 -0800111 (void)status->Send(status_builder.Finish());
112}
113
114double Superstructure::robot_velocity() const {
115 return (drivetrain_status_fetcher_.get() != nullptr
116 ? drivetrain_status_fetcher_->robot_speed()
117 : 0.0);
118}
119
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800120std::optional<double> Superstructure::LateralOffsetForTimeOfFlight(
121 double reading) {
122 switch (end_effector_.game_piece()) {
123 case vision::Class::NONE:
124 return std::nullopt;
125 case vision::Class::CUBE:
126 // Cubes are definitionally centered.
127 return 0.0;
128 case vision::Class::CONE_UP:
129 case vision::Class::CONE_DOWN:
130 // execute logic below.
131 break;
132 }
133 constexpr double kInvalidReading = 0.93;
James Kuszmaul4fe5a772023-04-05 20:17:43 -0700134 if (reading > kInvalidReading || !std::isfinite(reading)) {
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800135 return std::nullopt;
136 }
Austin Schuh6bdcc372024-06-27 14:49:11 -0700137 auto robot = constants_fetcher_.constants().robot();
138 CHECK(robot != nullptr);
139 const TimeOfFlight *calibration =
140 constants_fetcher_.constants().robot()->tof();
141 CHECK(calibration != nullptr);
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800142 // TODO(james): Use a generic interpolation table class.
Austin Schuh6bdcc372024-06-27 14:49:11 -0700143 auto table = calibration->interpolation_table();
144 CHECK(table != nullptr);
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800145 CHECK_EQ(2u, table->size());
146 double x1 = table->Get(0)->tof_reading();
147 double x2 = table->Get(1)->tof_reading();
148 double y1 = table->Get(0)->lateral_position();
149 double y2 = table->Get(1)->lateral_position();
150 return frc971::shooter_interpolation::Blend((reading - x1) / (x2 - x1), y1,
151 y2);
152}
153
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800154} // namespace y2023::control_loops::superstructure