blob: 6e4cc29c5a1bb021b555cae2533159bf49b59711 [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"
milind-u37385182023-02-20 15:07:28 -08008#include "frc971/zeroing/zeroing.h"
9#include "y2023/constants.h"
10#include "y2023/control_loops/superstructure/arm/generated_graph.h"
milind-u18a901d2023-02-17 21:51:55 -080011#include "y2023/control_loops/superstructure/arm/trajectory.h"
milind-u37385182023-02-20 15:07:28 -080012#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;
milind-u37385182023-02-20 15:07:28 -080016
17namespace y2023 {
18namespace control_loops {
19namespace superstructure {
20namespace arm {
21
22class Arm {
23 public:
24 Arm(std::shared_ptr<const constants::Values> values);
25
26 // if true, tune down all the constants for testing.
27 static constexpr bool kGrannyMode() { return false; }
milind-u01bbcf22023-02-20 18:00:28 -080028
milind-u37385182023-02-20 15:07:28 -080029 // the operating voltage.
30 static constexpr double kOperatingVoltage() {
31 return kGrannyMode() ? 5.0 : 12.0;
32 }
milind-u37385182023-02-20 15:07:28 -080033 static constexpr double kDt() { return 0.00505; }
milind-u18a901d2023-02-17 21:51:55 -080034 static constexpr std::chrono::nanoseconds kDtDuration() {
35 return std::chrono::duration_cast<std::chrono::nanoseconds>(
36 std::chrono::duration<double>(kDt()));
37 }
milind-u37385182023-02-20 15:07:28 -080038 static constexpr double kAlpha0Max() { return kGrannyMode() ? 5.0 : 15.0; }
39 static constexpr double kAlpha1Max() { return kGrannyMode() ? 5.0 : 15.0; }
milind-u18a901d2023-02-17 21:51:55 -080040 static constexpr double kAlpha2Max() { return kGrannyMode() ? 5.0 : 15.0; }
milind-u37385182023-02-20 15:07:28 -080041
42 static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; }
milind-u37385182023-02-20 15:07:28 -080043 static constexpr double kPathlessVMax() { return 5.0; }
44 static constexpr double kGotoPathVMax() { return 6.0; }
45
46 flatbuffers::Offset<superstructure::ArmStatus> Iterate(
47 const ::aos::monotonic_clock::time_point /*monotonic_now*/,
48 const uint32_t *unsafe_goal, const superstructure::ArmPosition *position,
49 bool trajectory_override, double *proximal_output, double *distal_output,
Maxwell Henderson5938a832023-02-23 09:33:15 -080050 double *roll_joint_output, flatbuffers::FlatBufferBuilder *fbb);
milind-u37385182023-02-20 15:07:28 -080051
52 void Reset();
53
54 ArmState state() const { return state_; }
milind-u37385182023-02-20 15:07:28 -080055
milind-u01bbcf22023-02-20 18:00:28 -080056 bool estopped() const { return state_ == ArmState::ESTOP; }
milind-u37385182023-02-20 15:07:28 -080057 bool zeroed() const {
58 return (proximal_zeroing_estimator_.zeroed() &&
milind-u18a901d2023-02-17 21:51:55 -080059 distal_zeroing_estimator_.zeroed() &&
60 roll_joint_zeroing_estimator_.zeroed());
milind-u37385182023-02-20 15:07:28 -080061 }
milind-u01bbcf22023-02-20 18:00:28 -080062
milind-u37385182023-02-20 15:07:28 -080063 uint32_t current_node() const { return current_node_; }
milind-u01bbcf22023-02-20 18:00:28 -080064
milind-u37385182023-02-20 15:07:28 -080065 double path_distance_to_go() { return follower_.path_distance_to_go(); }
66
67 private:
68 bool AtState(uint32_t state) const { return current_node_ == state; }
69 bool NearEnd(double threshold = 0.03) const {
70 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
71 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
72 follower_.path_distance_to_go() < 1e-3;
73 }
74
75 std::shared_ptr<const constants::Values> values_;
milind-u01bbcf22023-02-20 18:00:28 -080076
milind-u37385182023-02-20 15:07:28 -080077 ArmState state_;
78
79 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
80 proximal_zeroing_estimator_;
81 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
82 distal_zeroing_estimator_;
milind-u18a901d2023-02-17 21:51:55 -080083 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
84 roll_joint_zeroing_estimator_;
milind-u37385182023-02-20 15:07:28 -080085
86 double proximal_offset_;
87 double distal_offset_;
milind-u18a901d2023-02-17 21:51:55 -080088 double roll_joint_offset_;
milind-u01bbcf22023-02-20 18:00:28 -080089
milind-u18a901d2023-02-17 21:51:55 -080090 const ::Eigen::DiagonalMatrix<double, 3> alpha_unitizer_;
milind-u01bbcf22023-02-20 18:00:28 -080091
milind-u37385182023-02-20 15:07:28 -080092 double vmax_ = kVMax();
93
94 frc971::control_loops::arm::Dynamics dynamics_;
95
96 ::std::vector<TrajectoryAndParams> trajectories_;
milind-u37385182023-02-20 15:07:28 -080097
98 bool close_enough_for_full_power_;
99
100 size_t brownout_count_;
101
milind-u18a901d2023-02-17 21:51:55 -0800102 StateFeedbackLoop<3, 1, 1, double, StateFeedbackPlant<3, 1, 1>,
103 StateFeedbackObserver<3, 1, 1>>
104 roll_joint_loop_;
105 const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>,
106 HybridKalman<3, 1, 1>>
107 hybrid_roll_joint_loop_;
milind-u37385182023-02-20 15:07:28 -0800108 EKF arm_ekf_;
milind-u18a901d2023-02-17 21:51:55 -0800109 SearchGraph search_graph_;
milind-u37385182023-02-20 15:07:28 -0800110 TrajectoryFollower follower_;
111
milind-u18a901d2023-02-17 21:51:55 -0800112 const ::std::vector<::Eigen::Matrix<double, 3, 1>> points_;
milind-u37385182023-02-20 15:07:28 -0800113
114 // Start at the 0th index.
115 uint32_t current_node_;
116
117 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
118};
milind-u01bbcf22023-02-20 18:00:28 -0800119
milind-u37385182023-02-20 15:07:28 -0800120} // namespace arm
121} // namespace superstructure
122} // namespace control_loops
123} // namespace y2023
124
125#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_