blob: 27588f1ad66f3a0284b01e58b727683b74fbc72e [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; }
milind-u01bbcf22023-02-20 18:00:28 -080029
milind-u37385182023-02-20 15:07:28 -080030 // the operating voltage.
31 static constexpr double kOperatingVoltage() {
32 return kGrannyMode() ? 5.0 : 12.0;
33 }
milind-u37385182023-02-20 15:07:28 -080034 static constexpr double kDt() { return 0.00505; }
milind-u37385182023-02-20 15:07:28 -080035 static constexpr double kAlpha0Max() { return kGrannyMode() ? 5.0 : 15.0; }
36 static constexpr double kAlpha1Max() { return kGrannyMode() ? 5.0 : 15.0; }
37
38 static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; }
milind-u37385182023-02-20 15:07:28 -080039 static constexpr double kPathlessVMax() { return 5.0; }
40 static constexpr double kGotoPathVMax() { return 6.0; }
41
42 flatbuffers::Offset<superstructure::ArmStatus> Iterate(
43 const ::aos::monotonic_clock::time_point /*monotonic_now*/,
44 const uint32_t *unsafe_goal, const superstructure::ArmPosition *position,
45 bool trajectory_override, double *proximal_output, double *distal_output,
46 bool /*intake*/, bool /*spit*/, flatbuffers::FlatBufferBuilder *fbb);
47
48 void Reset();
49
50 ArmState state() const { return state_; }
milind-u37385182023-02-20 15:07:28 -080051
milind-u01bbcf22023-02-20 18:00:28 -080052 bool estopped() const { return state_ == ArmState::ESTOP; }
milind-u37385182023-02-20 15:07:28 -080053 bool zeroed() const {
54 return (proximal_zeroing_estimator_.zeroed() &&
55 distal_zeroing_estimator_.zeroed());
56 }
milind-u01bbcf22023-02-20 18:00:28 -080057
milind-u37385182023-02-20 15:07:28 -080058 // Returns the maximum position for the intake. This is used to override the
59 // intake position to release the box when the state machine is lifting.
60 double max_intake_override() const { return max_intake_override_; }
milind-u01bbcf22023-02-20 18:00:28 -080061
milind-u37385182023-02-20 15:07:28 -080062 uint32_t current_node() const { return current_node_; }
milind-u01bbcf22023-02-20 18:00:28 -080063
milind-u37385182023-02-20 15:07:28 -080064 double path_distance_to_go() { return follower_.path_distance_to_go(); }
65
66 private:
67 bool AtState(uint32_t state) const { return current_node_ == state; }
68 bool NearEnd(double threshold = 0.03) const {
69 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
70 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
71 follower_.path_distance_to_go() < 1e-3;
72 }
73
74 std::shared_ptr<const constants::Values> values_;
milind-u01bbcf22023-02-20 18:00:28 -080075
milind-u37385182023-02-20 15:07:28 -080076 ArmState state_;
77
78 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
79 proximal_zeroing_estimator_;
80 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
81 distal_zeroing_estimator_;
82
83 double proximal_offset_;
84 double distal_offset_;
milind-u01bbcf22023-02-20 18:00:28 -080085
milind-u37385182023-02-20 15:07:28 -080086 double max_intake_override_;
87
88 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
milind-u01bbcf22023-02-20 18:00:28 -080089
milind-u37385182023-02-20 15:07:28 -080090 double vmax_ = kVMax();
91
92 frc971::control_loops::arm::Dynamics dynamics_;
93
94 ::std::vector<TrajectoryAndParams> trajectories_;
milind-u37385182023-02-20 15:07:28 -080095 SearchGraph search_graph_;
96
97 bool close_enough_for_full_power_;
98
99 size_t brownout_count_;
100
101 EKF arm_ekf_;
milind-u37385182023-02-20 15:07:28 -0800102 TrajectoryFollower follower_;
103
104 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
105
106 // Start at the 0th index.
107 uint32_t current_node_;
108
109 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
110};
milind-u01bbcf22023-02-20 18:00:28 -0800111
milind-u37385182023-02-20 15:07:28 -0800112} // namespace arm
113} // namespace superstructure
114} // namespace control_loops
115} // namespace y2023
116
117#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_