blob: 460982752771fec0087f1975aac23baad2dccf5f [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"
8#include "y2018/control_loops/superstructure/arm/graph.h"
9#include "y2018/control_loops/superstructure/arm/trajectory.h"
10#include "y2018/control_loops/superstructure/superstructure.q.h"
11
12namespace y2018 {
13namespace control_loops {
14namespace superstructure {
15namespace arm {
16
Austin Schuhcb091712018-02-21 20:01:55 -080017class Arm {
18 public:
19 Arm();
20
Austin Schuh7a090402018-03-04 01:16:45 -080021 // If true, tune down all the constants for testing.
Austin Schuhb874fd32018-03-05 00:27:10 -080022 static constexpr bool kGrannyMode() { return false; }
Austin Schuh7a090402018-03-04 01:16:45 -080023
Austin Schuhcb091712018-02-21 20:01:55 -080024 // The operating voltage.
Austin Schuh7a090402018-03-04 01:16:45 -080025 static constexpr double kOperatingVoltage() {
Austin Schuh96341532018-03-09 21:17:24 -080026 return kGrannyMode() ? 4.0 : 12.0;
Austin Schuh7a090402018-03-04 01:16:45 -080027 }
Austin Schuhcb091712018-02-21 20:01:55 -080028 static constexpr double kDt() { return 0.00505; }
Austin Schuhef978fc2018-03-21 20:38:06 -070029 static constexpr double kAlpha0Max() { return kGrannyMode() ? 10.0 : 10.0; }
30 static constexpr double kAlpha1Max() { return kGrannyMode() ? 10.0 : 10.0; }
Austin Schuhb874fd32018-03-05 00:27:10 -080031
32 static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; }
33 static constexpr double kPathlessVMax() { return 5.0; }
Austin Schuhcb091712018-02-21 20:01:55 -080034
Austin Schuh96341532018-03-09 21:17:24 -080035 void Iterate(const uint32_t *unsafe_goal, bool grab_box, bool open_claw,
Austin Schuhcb091712018-02-21 20:01:55 -080036 const control_loops::ArmPosition *position,
Austin Schuh96341532018-03-09 21:17:24 -080037 const bool claw_beambreak_triggered,
38 const bool box_back_beambreak_triggered,
39 const bool intake_clear_of_box, double *proximal_output,
40 double *distal_output, bool *release_arm_brake,
Austin Schuh17e484e2018-03-11 01:11:36 -080041 bool *claw_closed, control_loops::ArmStatus *status,
42 bool suicide);
Austin Schuhcb091712018-02-21 20:01:55 -080043
44 void Reset();
45
46 enum class State : int32_t {
47 UNINITIALIZED,
48 ZEROING,
Austin Schuh96341532018-03-09 21:17:24 -080049 DISABLED,
50 GOTO_PATH,
Austin Schuhcb091712018-02-21 20:01:55 -080051 RUNNING,
Austin Schuh17e484e2018-03-11 01:11:36 -080052 PREP_CLIMB,
Austin Schuhcb091712018-02-21 20:01:55 -080053 ESTOP,
54 };
55
Austin Schuh96341532018-03-09 21:17:24 -080056 enum class GrabState : int32_t {
57 NORMAL,
58 WAIT_FOR_BOX,
59 TALL_BOX,
60 SHORT_BOX,
61 CLAW_CLOSE,
62 OPEN_INTAKE,
63 };
64
Austin Schuhcb091712018-02-21 20:01:55 -080065 State state() const { return state_; }
Austin Schuh96341532018-03-09 21:17:24 -080066 GrabState grab_state() const { return grab_state_; }
67
68 // Returns the maximum position for the intake. This is used to override the
69 // intake position to release the box when the state machine is lifting.
70 double max_intake_override() const { return max_intake_override_; }
Austin Schuhcb091712018-02-21 20:01:55 -080071
72 private:
Austin Schuh96341532018-03-09 21:17:24 -080073 bool AtState(uint32_t state) const { return current_node_ == state; }
Austin Schuhb874fd32018-03-05 00:27:10 -080074 bool NearEnd(double threshold = 0.02) const {
Austin Schuh96341532018-03-09 21:17:24 -080075 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
76 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
77 follower_.path_distance_to_go() < 1e-3;
78 }
79
Austin Schuhcb091712018-02-21 20:01:55 -080080 State state_ = State::UNINITIALIZED;
81
Austin Schuh96341532018-03-09 21:17:24 -080082 GrabState grab_state_ = GrabState::NORMAL;
83
84 ::aos::monotonic_clock::time_point claw_close_start_time_ =
Austin Schuhcb091712018-02-21 20:01:55 -080085 ::aos::monotonic_clock::min_time;
86
87 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator proximal_zeroing_estimator_;
88 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator distal_zeroing_estimator_;
89
90 double proximal_offset_ = 0.0;
91 double distal_offset_ = 0.0;
92
Austin Schuh96341532018-03-09 21:17:24 -080093 bool claw_closed_ = true;
94 double max_intake_override_ = 1000.0;
95
Austin Schuhcb091712018-02-21 20:01:55 -080096 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
97
Austin Schuh7dfccf62018-03-03 21:28:14 -080098 ::std::vector<Trajectory> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -080099 SearchGraph search_graph_;
100
101 bool close_enough_for_full_power_ = false;
102
Austin Schuh17e484e2018-03-11 01:11:36 -0800103 int32_t climb_count_ = 0;
104
Austin Schuhcb091712018-02-21 20:01:55 -0800105 EKF arm_ekf_;
106 TrajectoryFollower follower_;
107
Austin Schuhb874fd32018-03-05 00:27:10 -0800108 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
109
Austin Schuhcb091712018-02-21 20:01:55 -0800110 // Start at the 0th index.
111 uint32_t current_node_ = 0;
112
113 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
114};
115
116} // namespace arm
117} // namespace superstructure
118} // namespace control_loops
119} // namespace y2018
120
121#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_