blob: 6b2864a76e18b1887fe3aa6c1acc4d574eee6c7b [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,
41 const bool intake_clear_of_box, double *proximal_output,
42 double *distal_output, bool *release_arm_brake,
Austin Schuh17e484e2018-03-11 01:11:36 -080043 bool *claw_closed, control_loops::ArmStatus *status,
44 bool suicide);
Austin Schuhcb091712018-02-21 20:01:55 -080045
46 void Reset();
47
48 enum class State : int32_t {
49 UNINITIALIZED,
50 ZEROING,
Austin Schuh96341532018-03-09 21:17:24 -080051 DISABLED,
52 GOTO_PATH,
Austin Schuhcb091712018-02-21 20:01:55 -080053 RUNNING,
Austin Schuh17e484e2018-03-11 01:11:36 -080054 PREP_CLIMB,
Austin Schuhcb091712018-02-21 20:01:55 -080055 ESTOP,
56 };
57
Austin Schuh96341532018-03-09 21:17:24 -080058 enum class GrabState : int32_t {
59 NORMAL,
60 WAIT_FOR_BOX,
61 TALL_BOX,
62 SHORT_BOX,
63 CLAW_CLOSE,
64 OPEN_INTAKE,
65 };
66
Austin Schuhcb091712018-02-21 20:01:55 -080067 State state() const { return state_; }
Austin Schuh96341532018-03-09 21:17:24 -080068 GrabState grab_state() const { return grab_state_; }
69
70 // Returns the maximum position for the intake. This is used to override the
71 // intake position to release the box when the state machine is lifting.
72 double max_intake_override() const { return max_intake_override_; }
Austin Schuhcb091712018-02-21 20:01:55 -080073
74 private:
Austin Schuh96341532018-03-09 21:17:24 -080075 bool AtState(uint32_t state) const { return current_node_ == state; }
Austin Schuh83cdd8a2018-03-21 20:49:02 -070076 bool NearEnd(double threshold = 0.03) const {
Austin Schuh96341532018-03-09 21:17:24 -080077 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
78 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
79 follower_.path_distance_to_go() < 1e-3;
80 }
81
Austin Schuhcb091712018-02-21 20:01:55 -080082 State state_ = State::UNINITIALIZED;
83
Austin Schuh96341532018-03-09 21:17:24 -080084 GrabState grab_state_ = GrabState::NORMAL;
85
86 ::aos::monotonic_clock::time_point claw_close_start_time_ =
Austin Schuhcb091712018-02-21 20:01:55 -080087 ::aos::monotonic_clock::min_time;
88
89 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator proximal_zeroing_estimator_;
90 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator distal_zeroing_estimator_;
91
92 double proximal_offset_ = 0.0;
93 double distal_offset_ = 0.0;
94
Austin Schuh96341532018-03-09 21:17:24 -080095 bool claw_closed_ = true;
96 double max_intake_override_ = 1000.0;
97
Austin Schuhcb091712018-02-21 20:01:55 -080098 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
99
Austin Schuh41c71e42018-04-04 20:11:20 -0700100 double vmax_ = kVMax();
101
102 ::std::vector<TrajectoryAndParams> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -0800103 SearchGraph search_graph_;
104
105 bool close_enough_for_full_power_ = false;
106
Austin Schuh17e484e2018-03-11 01:11:36 -0800107 int32_t climb_count_ = 0;
108
Austin Schuhcb091712018-02-21 20:01:55 -0800109 EKF arm_ekf_;
110 TrajectoryFollower follower_;
111
Austin Schuhb874fd32018-03-05 00:27:10 -0800112 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
113
Austin Schuhcb091712018-02-21 20:01:55 -0800114 // Start at the 0th index.
115 uint32_t current_node_ = 0;
116
Neil Balchba9cbba2018-04-06 22:26:38 -0700117 uint32_t claw_closed_count_ = 0;
118
Austin Schuhcb091712018-02-21 20:01:55 -0800119 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
120};
121
122} // namespace arm
123} // namespace superstructure
124} // namespace control_loops
125} // namespace y2018
126
127#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_