blob: ece0790873867a906b9b6d9d4612b86d66939b6c [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
13namespace y2023 {
14namespace control_loops {
15namespace superstructure {
16
milind-u01bbcf22023-02-20 18:00:28 -080017using ::aos::monotonic_clock;
18
Maxwell Hendersonad312342023-01-10 12:07:47 -080019using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
20using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
21using frc971::control_loops::RelativeEncoderProfiledJointStatus;
22
Maxwell Hendersonb392b742023-03-05 07:53:51 -080023Superstructure::Superstructure(
24 ::aos::EventLoop *event_loop,
25 std::shared_ptr<const constants::Values> values,
26 const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories,
27 const ::std::string &name)
Maxwell Hendersonad312342023-01-10 12:07:47 -080028 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
29 name),
30 values_(values),
James Kuszmaulcf451fb2023-03-10 20:42:36 -080031 constants_fetcher_(event_loop),
Maxwell Hendersonad312342023-01-10 12:07:47 -080032 drivetrain_status_fetcher_(
33 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
34 "/drivetrain")),
35 joystick_state_fetcher_(
milind-u01bbcf22023-02-20 18:00:28 -080036 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Maxwell Hendersonb392b742023-03-05 07:53:51 -080037 arm_(values_, arm_trajectories.message()),
Maxwell Henderson8ca44562023-02-23 13:11:51 -080038 end_effector_(),
Austin Schuh595ffc72023-02-24 16:24:37 -080039 wrist_(values->wrist.subsystem_params) {
40 event_loop->SetRuntimeRealtimePriority(30);
41}
Maxwell Hendersonad312342023-01-10 12:07:47 -080042
43void Superstructure::RunIteration(const Goal *unsafe_goal,
44 const Position *position,
45 aos::Sender<Output>::Builder *output,
46 aos::Sender<Status>::Builder *status) {
milind-u01bbcf22023-02-20 18:00:28 -080047 const monotonic_clock::time_point timestamp =
48 event_loop()->context().monotonic_event_time;
Maxwell Hendersonad312342023-01-10 12:07:47 -080049
50 if (WasReset()) {
51 AOS_LOG(ERROR, "WPILib reset, restarting\n");
milind-u01bbcf22023-02-20 18:00:28 -080052 arm_.Reset();
Maxwell Henderson589cf272023-02-22 15:56:40 -080053 end_effector_.Reset();
Austin Schuhbdabc702023-02-24 16:21:07 -080054 wrist_.Reset();
Maxwell Hendersonad312342023-01-10 12:07:47 -080055 }
56
57 OutputT output_struct;
58 if (joystick_state_fetcher_.Fetch() &&
59 joystick_state_fetcher_->has_alliance()) {
60 alliance_ = joystick_state_fetcher_->alliance();
61 }
62 drivetrain_status_fetcher_.Fetch();
milind-u01bbcf22023-02-20 18:00:28 -080063
64 const uint32_t arm_goal_position =
65 unsafe_goal != nullptr ? unsafe_goal->arm_goal_position() : 0u;
66
67 flatbuffers::Offset<superstructure::ArmStatus> arm_status_offset =
68 arm_.Iterate(
69 timestamp, unsafe_goal != nullptr ? &(arm_goal_position) : nullptr,
70 position->arm(),
71 unsafe_goal != nullptr ? unsafe_goal->trajectory_override() : false,
72 output != nullptr ? &output_struct.proximal_voltage : nullptr,
73 output != nullptr ? &output_struct.distal_voltage : nullptr,
milind-u18a901d2023-02-17 21:51:55 -080074 output != nullptr ? &output_struct.roll_joint_voltage : nullptr,
milind-u01bbcf22023-02-20 18:00:28 -080075 status->fbb());
76
Maxwell Henderson8ca44562023-02-23 13:11:51 -080077 flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> wrist_offset =
Austin Schuhbdabc702023-02-24 16:21:07 -080078 wrist_.Iterate(
79 unsafe_goal != nullptr ? unsafe_goal->wrist() : nullptr,
80 position->wrist(),
81 output != nullptr ? &(output_struct.wrist_voltage) : nullptr,
82 status->fbb());
Maxwell Henderson8ca44562023-02-23 13:11:51 -080083
milind-u71da5392023-02-26 12:45:00 -080084 end_effector_.RunIteration(
Maxwell Henderson5938a832023-02-23 09:33:15 -080085 timestamp,
86 unsafe_goal != nullptr ? unsafe_goal->roller_goal() : RollerGoal::IDLE,
milind-u6ff6d9e2023-02-24 20:15:06 -080087 position->has_roller_falcon()
88 ? position->roller_falcon()->torque_current()
89 : 0.0,
90 position->cone_position(), position->end_effector_cube_beam_break(),
Maxwell Henderson64f37452023-03-11 13:39:21 -080091 &output_struct.roller_voltage,
92 unsafe_goal != nullptr ? unsafe_goal->preloaded_with_cone() : false);
Maxwell Henderson589cf272023-02-22 15:56:40 -080093
milind-u01bbcf22023-02-20 18:00:28 -080094 if (output) {
95 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
96 }
97
Maxwell Hendersonad312342023-01-10 12:07:47 -080098 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhbdabc702023-02-24 16:21:07 -080099 status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed());
100 status_builder.add_estopped(wrist_.estopped() || arm_.estopped());
milind-u01bbcf22023-02-20 18:00:28 -0800101 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -0800102 status_builder.add_wrist(wrist_offset);
milind-u71da5392023-02-26 12:45:00 -0800103 status_builder.add_end_effector_state(end_effector_.state());
104 // TODO(milind): integrate this with ML game piece detection somehow
105 status_builder.add_game_piece(end_effector_.game_piece());
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800106 const std::optional<double> game_piece_position =
107 LateralOffsetForTimeOfFlight(position->cone_position());
108 if (game_piece_position.has_value()) {
109 status_builder.add_game_piece_position(game_piece_position.value());
110 }
milind-u01bbcf22023-02-20 18:00:28 -0800111
Maxwell Hendersonad312342023-01-10 12:07:47 -0800112 (void)status->Send(status_builder.Finish());
113}
114
115double Superstructure::robot_velocity() const {
116 return (drivetrain_status_fetcher_.get() != nullptr
117 ? drivetrain_status_fetcher_->robot_speed()
118 : 0.0);
119}
120
James Kuszmaulcf451fb2023-03-10 20:42:36 -0800121std::optional<double> Superstructure::LateralOffsetForTimeOfFlight(
122 double reading) {
123 switch (end_effector_.game_piece()) {
124 case vision::Class::NONE:
125 return std::nullopt;
126 case vision::Class::CUBE:
127 // Cubes are definitionally centered.
128 return 0.0;
129 case vision::Class::CONE_UP:
130 case vision::Class::CONE_DOWN:
131 // execute logic below.
132 break;
133 }
134 constexpr double kInvalidReading = 0.93;
135 if (reading > kInvalidReading) {
136 return std::nullopt;
137 }
138 const TimeOfFlight *calibration = CHECK_NOTNULL(
139 CHECK_NOTNULL(constants_fetcher_.constants().robot())->tof());
140 // TODO(james): Use a generic interpolation table class.
141 auto table = CHECK_NOTNULL(calibration->interpolation_table());
142 CHECK_EQ(2u, table->size());
143 double x1 = table->Get(0)->tof_reading();
144 double x2 = table->Get(1)->tof_reading();
145 double y1 = table->Get(0)->lateral_position();
146 double y2 = table->Get(1)->lateral_position();
147 return frc971::shooter_interpolation::Blend((reading - x1) / (x2 - x1), y1,
148 y2);
149}
150
Maxwell Hendersonad312342023-01-10 12:07:47 -0800151} // namespace superstructure
152} // namespace control_loops
153} // namespace y2023