blob: 9b9b11955c2730ecae408ddfe61081877fb9f566 [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"
6#include "frc971/zeroing/wrap.h"
Maxwell Hendersonb392b742023-03-05 07:53:51 -08007#include "y2023/control_loops/superstructure/arm/arm_trajectories_generated.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -08008
9DEFINE_bool(ignore_distance, false,
10 "If true, ignore distance when shooting and obay joystick_reader");
11
12namespace y2023 {
13namespace control_loops {
14namespace superstructure {
15
milind-u01bbcf22023-02-20 18:00:28 -080016using ::aos::monotonic_clock;
17
Maxwell Hendersonad312342023-01-10 12:07:47 -080018using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
19using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
20using frc971::control_loops::RelativeEncoderProfiledJointStatus;
21
Maxwell Hendersonb392b742023-03-05 07:53:51 -080022Superstructure::Superstructure(
23 ::aos::EventLoop *event_loop,
24 std::shared_ptr<const constants::Values> values,
25 const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories,
26 const ::std::string &name)
Maxwell Hendersonad312342023-01-10 12:07:47 -080027 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
28 name),
29 values_(values),
30 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(),
89 &output_struct.roller_voltage);
Maxwell Henderson589cf272023-02-22 15:56:40 -080090
milind-u01bbcf22023-02-20 18:00:28 -080091 if (output) {
92 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
93 }
94
Maxwell Hendersonad312342023-01-10 12:07:47 -080095 Status::Builder status_builder = status->MakeBuilder<Status>();
Austin Schuhbdabc702023-02-24 16:21:07 -080096 status_builder.add_zeroed(wrist_.zeroed() && arm_.zeroed());
97 status_builder.add_estopped(wrist_.estopped() || arm_.estopped());
milind-u01bbcf22023-02-20 18:00:28 -080098 status_builder.add_arm(arm_status_offset);
Maxwell Henderson8ca44562023-02-23 13:11:51 -080099 status_builder.add_wrist(wrist_offset);
milind-u71da5392023-02-26 12:45:00 -0800100 status_builder.add_end_effector_state(end_effector_.state());
101 // TODO(milind): integrate this with ML game piece detection somehow
102 status_builder.add_game_piece(end_effector_.game_piece());
milind-u01bbcf22023-02-20 18:00:28 -0800103
Maxwell Hendersonad312342023-01-10 12:07:47 -0800104 (void)status->Send(status_builder.Finish());
105}
106
107double Superstructure::robot_velocity() const {
108 return (drivetrain_status_fetcher_.get() != nullptr
109 ? drivetrain_status_fetcher_->robot_speed()
110 : 0.0);
111}
112
113} // namespace superstructure
114} // namespace control_loops
115} // namespace y2023