blob: 349131bc119352e095934a9d3eeffc09749e5373 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#ifndef Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_
2#define Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_
3
4#include "aos/events/event_loop.h"
Maxwell Hendersonb392b742023-03-05 07:53:51 -08005#include "aos/json_to_flatbuffer.h"
James Kuszmaulcf451fb2023-03-10 20:42:36 -08006#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -08007#include "frc971/control_loops/control_loop.h"
Maxwell Hendersoncef6f042023-05-26 14:38:09 -07008#include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -08009#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaulec635d22023-08-12 18:39:24 -070010#include "frc971/zeroing/absolute_encoder.h"
11#include "frc971/zeroing/pot_and_absolute_encoder.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080012#include "y2023/constants.h"
James Kuszmaulcf451fb2023-03-10 20:42:36 -080013#include "y2023/constants/constants_generated.h"
milind-u01bbcf22023-02-20 18:00:28 -080014#include "y2023/control_loops/superstructure/arm/arm.h"
Maxwell Hendersonb392b742023-03-05 07:53:51 -080015#include "y2023/control_loops/superstructure/arm/arm_trajectories_generated.h"
Maxwell Henderson589cf272023-02-22 15:56:40 -080016#include "y2023/control_loops/superstructure/end_effector.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080017#include "y2023/control_loops/superstructure/superstructure_goal_generated.h"
18#include "y2023/control_loops/superstructure/superstructure_output_generated.h"
19#include "y2023/control_loops/superstructure/superstructure_position_generated.h"
20#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
21
Maxwell Hendersonb392b742023-03-05 07:53:51 -080022using y2023::control_loops::superstructure::arm::ArmTrajectories;
23using y2023::control_loops::superstructure::arm::TrajectoryAndParams;
24
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080025namespace y2023::control_loops::superstructure {
Maxwell Hendersonad312342023-01-10 12:07:47 -080026
27class Superstructure
28 : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> {
29 public:
30 using RelativeEncoderSubsystem =
31 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystem<
32 ::frc971::zeroing::RelativeEncoderZeroingEstimator,
33 ::frc971::control_loops::RelativeEncoderProfiledJointStatus>;
34
35 using PotAndAbsoluteEncoderSubsystem =
36 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystem<
37 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator,
38 ::frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus>;
39
Maxwell Henderson8ca44562023-02-23 13:11:51 -080040 using AbsoluteEncoderSubsystem =
41 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystem<
42 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator,
43 ::frc971::control_loops::AbsoluteEncoderProfiledJointStatus>;
44
Maxwell Hendersonb392b742023-03-05 07:53:51 -080045 explicit Superstructure(
46 ::aos::EventLoop *event_loop,
47 std::shared_ptr<const constants::Values> values,
48 const aos::FlatbufferVector<ArmTrajectories> &arm_trajectories,
49 const ::std::string &name = "/superstructure");
Maxwell Hendersonad312342023-01-10 12:07:47 -080050
51 double robot_velocity() const;
52
Maxwell Henderson8ca44562023-02-23 13:11:51 -080053 inline const arm::Arm &arm() const { return arm_; }
54 inline const EndEffector &end_effector() const { return end_effector_; }
55 inline const AbsoluteEncoderSubsystem &wrist() const { return wrist_; }
56
Maxwell Hendersonb392b742023-03-05 07:53:51 -080057 static const aos::FlatbufferVector<ArmTrajectories> GetArmTrajectories(
58 const std::string &filename) {
59 return aos::FileToFlatbuffer<arm::ArmTrajectories>(filename);
60 }
61
Maxwell Hendersonad312342023-01-10 12:07:47 -080062 protected:
63 virtual void RunIteration(const Goal *unsafe_goal, const Position *position,
64 aos::Sender<Output>::Builder *output,
65 aos::Sender<Status>::Builder *status) override;
66
67 private:
James Kuszmaulcf451fb2023-03-10 20:42:36 -080068 // Returns the Y coordinate of a game piece given the time-of-flight reading.
69 std::optional<double> LateralOffsetForTimeOfFlight(double reading);
70
Maxwell Hendersonad312342023-01-10 12:07:47 -080071 std::shared_ptr<const constants::Values> values_;
James Kuszmaulcf451fb2023-03-10 20:42:36 -080072 frc971::constants::ConstantsFetcher<Constants> constants_fetcher_;
Maxwell Hendersonad312342023-01-10 12:07:47 -080073
74 aos::Fetcher<frc971::control_loops::drivetrain::Status>
75 drivetrain_status_fetcher_;
76 aos::Fetcher<aos::JoystickState> joystick_state_fetcher_;
77
milind-u01bbcf22023-02-20 18:00:28 -080078 arm::Arm arm_;
Maxwell Henderson589cf272023-02-22 15:56:40 -080079 EndEffector end_effector_;
Maxwell Henderson8ca44562023-02-23 13:11:51 -080080 AbsoluteEncoderSubsystem wrist_;
milind-u01bbcf22023-02-20 18:00:28 -080081
Maxwell Hendersonad312342023-01-10 12:07:47 -080082 aos::Alliance alliance_ = aos::Alliance::kInvalid;
83
84 DISALLOW_COPY_AND_ASSIGN(Superstructure);
85};
86
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080087} // namespace y2023::control_loops::superstructure
Maxwell Hendersonad312342023-01-10 12:07:47 -080088
89#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_