blob: a595ace8609513394d195534f2212d9c00eeaad3 [file] [log] [blame]
milind-u37385182023-02-20 15:07:28 -08001#ifndef Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_
2#define Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_
3
4#include "aos/time/time.h"
5#include "frc971/control_loops/double_jointed_arm/dynamics.h"
6#include "frc971/control_loops/double_jointed_arm/ekf.h"
7#include "frc971/control_loops/double_jointed_arm/graph.h"
8#include "frc971/control_loops/double_jointed_arm/trajectory.h"
9#include "frc971/zeroing/zeroing.h"
10#include "y2023/constants.h"
11#include "y2023/control_loops/superstructure/arm/generated_graph.h"
12#include "y2023/control_loops/superstructure/superstructure_position_generated.h"
13#include "y2023/control_loops/superstructure/superstructure_status_generated.h"
14
15using frc971::control_loops::arm::EKF;
16using frc971::control_loops::arm::TrajectoryFollower;
17
18namespace y2023 {
19namespace control_loops {
20namespace superstructure {
21namespace arm {
22
23class Arm {
24 public:
25 Arm(std::shared_ptr<const constants::Values> values);
26
27 // if true, tune down all the constants for testing.
28 static constexpr bool kGrannyMode() { return false; }
29 // the operating voltage.
30 static constexpr double kOperatingVoltage() {
31 return kGrannyMode() ? 5.0 : 12.0;
32 }
33
34 static constexpr double kDt() { return 0.00505; }
35
36 static constexpr double kAlpha0Max() { return kGrannyMode() ? 5.0 : 15.0; }
37 static constexpr double kAlpha1Max() { return kGrannyMode() ? 5.0 : 15.0; }
38
39 static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; }
40
41 static constexpr double kPathlessVMax() { return 5.0; }
42 static constexpr double kGotoPathVMax() { return 6.0; }
43
44 flatbuffers::Offset<superstructure::ArmStatus> Iterate(
45 const ::aos::monotonic_clock::time_point /*monotonic_now*/,
46 const uint32_t *unsafe_goal, const superstructure::ArmPosition *position,
47 bool trajectory_override, double *proximal_output, double *distal_output,
48 bool /*intake*/, bool /*spit*/, flatbuffers::FlatBufferBuilder *fbb);
49
50 void Reset();
51
52 ArmState state() const { return state_; }
53 bool estopped() const { return state_ == ArmState::ESTOP; }
54
55 bool zeroed() const {
56 return (proximal_zeroing_estimator_.zeroed() &&
57 distal_zeroing_estimator_.zeroed());
58 }
59 // Returns the maximum position for the intake. This is used to override the
60 // intake position to release the box when the state machine is lifting.
61 double max_intake_override() const { return max_intake_override_; }
62 uint32_t current_node() const { return current_node_; }
63 double path_distance_to_go() { return follower_.path_distance_to_go(); }
64
65 private:
66 bool AtState(uint32_t state) const { return current_node_ == state; }
67 bool NearEnd(double threshold = 0.03) const {
68 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
69 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
70 follower_.path_distance_to_go() < 1e-3;
71 }
72
73 std::shared_ptr<const constants::Values> values_;
74 ArmState state_;
75
76 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
77 proximal_zeroing_estimator_;
78 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
79 distal_zeroing_estimator_;
80
81 double proximal_offset_;
82 double distal_offset_;
83 double max_intake_override_;
84
85 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
86 double vmax_ = kVMax();
87
88 frc971::control_loops::arm::Dynamics dynamics_;
89
90 ::std::vector<TrajectoryAndParams> trajectories_;
91
92 SearchGraph search_graph_;
93
94 bool close_enough_for_full_power_;
95
96 size_t brownout_count_;
97
98 EKF arm_ekf_;
99
100 TrajectoryFollower follower_;
101
102 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
103
104 // Start at the 0th index.
105 uint32_t current_node_;
106
107 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
108};
109} // namespace arm
110} // namespace superstructure
111} // namespace control_loops
112} // namespace y2023
113
114#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_