blob: a9b3590ad2211220b18c86c24ba06c4a927ef7b1 [file] [log] [blame]
Austin Schuhcb091712018-02-21 20:01:55 -08001#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_
2#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_
3
4#include "frc971/zeroing/zeroing.h"
5#include "y2018/constants.h"
6#include "y2018/control_loops/superstructure/arm/dynamics.h"
7#include "y2018/control_loops/superstructure/arm/ekf.h"
Austin Schuh41c71e42018-04-04 20:11:20 -07008#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Austin Schuhcb091712018-02-21 20:01:55 -08009#include "y2018/control_loops/superstructure/arm/graph.h"
10#include "y2018/control_loops/superstructure/arm/trajectory.h"
11#include "y2018/control_loops/superstructure/superstructure.q.h"
12
13namespace y2018 {
14namespace control_loops {
15namespace superstructure {
16namespace arm {
17
Austin Schuhcb091712018-02-21 20:01:55 -080018class Arm {
19 public:
20 Arm();
21
Austin Schuh7a090402018-03-04 01:16:45 -080022 // If true, tune down all the constants for testing.
Austin Schuhb874fd32018-03-05 00:27:10 -080023 static constexpr bool kGrannyMode() { return false; }
Austin Schuh7a090402018-03-04 01:16:45 -080024
Austin Schuhcb091712018-02-21 20:01:55 -080025 // The operating voltage.
Austin Schuh7a090402018-03-04 01:16:45 -080026 static constexpr double kOperatingVoltage() {
Austin Schuh41c71e42018-04-04 20:11:20 -070027 return kGrannyMode() ? 5.0 : 12.0;
Austin Schuh7a090402018-03-04 01:16:45 -080028 }
Austin Schuhcb091712018-02-21 20:01:55 -080029 static constexpr double kDt() { return 0.00505; }
Austin Schuh41c71e42018-04-04 20:11:20 -070030 static constexpr double kAlpha0Max() { return kGrannyMode() ? 5.0 : 15.0; }
31 static constexpr double kAlpha1Max() { return kGrannyMode() ? 5.0 : 15.0; }
Austin Schuhb874fd32018-03-05 00:27:10 -080032
33 static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; }
34 static constexpr double kPathlessVMax() { return 5.0; }
Austin Schuh41c71e42018-04-04 20:11:20 -070035 static constexpr double kGotoPathVMax() { return 6.0; }
Austin Schuhcb091712018-02-21 20:01:55 -080036
Austin Schuh96341532018-03-09 21:17:24 -080037 void Iterate(const uint32_t *unsafe_goal, bool grab_box, bool open_claw,
Neil Balchba9cbba2018-04-06 22:26:38 -070038 bool close_claw, const control_loops::ArmPosition *position,
Austin Schuh96341532018-03-09 21:17:24 -080039 const bool claw_beambreak_triggered,
40 const bool box_back_beambreak_triggered,
Austin Schuhd76546a2018-07-08 16:05:14 -070041 const bool intake_clear_of_box,
42 bool suicide, bool trajectory_override,
43 double *proximal_output,
Austin Schuh96341532018-03-09 21:17:24 -080044 double *distal_output, bool *release_arm_brake,
Austin Schuhd76546a2018-07-08 16:05:14 -070045 bool *claw_closed, control_loops::ArmStatus *status);
Austin Schuhcb091712018-02-21 20:01:55 -080046
47 void Reset();
48
49 enum class State : int32_t {
50 UNINITIALIZED,
51 ZEROING,
Austin Schuh96341532018-03-09 21:17:24 -080052 DISABLED,
53 GOTO_PATH,
Austin Schuhcb091712018-02-21 20:01:55 -080054 RUNNING,
Austin Schuh17e484e2018-03-11 01:11:36 -080055 PREP_CLIMB,
Austin Schuhcb091712018-02-21 20:01:55 -080056 ESTOP,
57 };
58
Austin Schuh96341532018-03-09 21:17:24 -080059 enum class GrabState : int32_t {
Austin Schuhede47322018-07-08 16:04:36 -070060 NORMAL = 0,
61 WAIT_FOR_BOX = 1,
62 TALL_BOX = 2,
63 SHORT_BOX = 3,
64 CLAW_CLOSE = 4,
65 OPEN_INTAKE = 5,
Austin Schuh96341532018-03-09 21:17:24 -080066 };
67
Austin Schuhcb091712018-02-21 20:01:55 -080068 State state() const { return state_; }
Austin Schuh96341532018-03-09 21:17:24 -080069 GrabState grab_state() const { return grab_state_; }
70
71 // Returns the maximum position for the intake. This is used to override the
72 // intake position to release the box when the state machine is lifting.
73 double max_intake_override() const { return max_intake_override_; }
Austin Schuhcb091712018-02-21 20:01:55 -080074
Austin Schuhd76546a2018-07-08 16:05:14 -070075 uint32_t current_node() const { return current_node_; }
76
77 double path_distance_to_go() { return follower_.path_distance_to_go(); }
78
Austin Schuhcb091712018-02-21 20:01:55 -080079 private:
Austin Schuh96341532018-03-09 21:17:24 -080080 bool AtState(uint32_t state) const { return current_node_ == state; }
Austin Schuh83cdd8a2018-03-21 20:49:02 -070081 bool NearEnd(double threshold = 0.03) const {
Austin Schuh96341532018-03-09 21:17:24 -080082 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
83 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
84 follower_.path_distance_to_go() < 1e-3;
85 }
86
Austin Schuhcb091712018-02-21 20:01:55 -080087 State state_ = State::UNINITIALIZED;
88
Austin Schuh96341532018-03-09 21:17:24 -080089 GrabState grab_state_ = GrabState::NORMAL;
90
91 ::aos::monotonic_clock::time_point claw_close_start_time_ =
Austin Schuhcb091712018-02-21 20:01:55 -080092 ::aos::monotonic_clock::min_time;
93
94 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator proximal_zeroing_estimator_;
95 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator distal_zeroing_estimator_;
96
97 double proximal_offset_ = 0.0;
98 double distal_offset_ = 0.0;
99
Austin Schuh96341532018-03-09 21:17:24 -0800100 bool claw_closed_ = true;
101 double max_intake_override_ = 1000.0;
102
Austin Schuhcb091712018-02-21 20:01:55 -0800103 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
104
Austin Schuh41c71e42018-04-04 20:11:20 -0700105 double vmax_ = kVMax();
106
107 ::std::vector<TrajectoryAndParams> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -0800108 SearchGraph search_graph_;
109
110 bool close_enough_for_full_power_ = false;
111
Austin Schuh17e484e2018-03-11 01:11:36 -0800112 int32_t climb_count_ = 0;
Austin Schuh7afcc232018-09-16 16:33:47 -0700113 int32_t brownout_count_ = 0;
Austin Schuh17e484e2018-03-11 01:11:36 -0800114
Austin Schuhcb091712018-02-21 20:01:55 -0800115 EKF arm_ekf_;
116 TrajectoryFollower follower_;
117
Austin Schuhb874fd32018-03-05 00:27:10 -0800118 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
119
Austin Schuhcb091712018-02-21 20:01:55 -0800120 // Start at the 0th index.
121 uint32_t current_node_ = 0;
122
Neil Balchba9cbba2018-04-06 22:26:38 -0700123 uint32_t claw_closed_count_ = 0;
124
Austin Schuhcb091712018-02-21 20:01:55 -0800125 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
126};
127
128} // namespace arm
129} // namespace superstructure
130} // namespace control_loops
131} // namespace y2018
132
133#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_