blob: d296703082fd48e9d46965f1f11220bccb6d757b [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.
22 static constexpr bool kGrannyMode() { return true; }
23
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 Schuh7a090402018-03-04 01:16:45 -080029 static constexpr double kAlpha0Max() { return kGrannyMode() ? 10.0 : 25.0; }
30 static constexpr double kAlpha1Max() { return kGrannyMode() ? 10.0 : 25.0; }
31 static constexpr double kVMax() { return kGrannyMode() ? 4.0 : 11.5; }
Austin Schuhcb091712018-02-21 20:01:55 -080032 static constexpr double kPathlessVMax() { return 4.0; }
33
Austin Schuh96341532018-03-09 21:17:24 -080034 void Iterate(const uint32_t *unsafe_goal, bool grab_box, bool open_claw,
Austin Schuhcb091712018-02-21 20:01:55 -080035 const control_loops::ArmPosition *position,
Austin Schuh96341532018-03-09 21:17:24 -080036 const bool claw_beambreak_triggered,
37 const bool box_back_beambreak_triggered,
38 const bool intake_clear_of_box, double *proximal_output,
39 double *distal_output, bool *release_arm_brake,
40 bool *claw_closed, control_loops::ArmStatus *status);
Austin Schuhcb091712018-02-21 20:01:55 -080041
42 void Reset();
43
44 enum class State : int32_t {
45 UNINITIALIZED,
46 ZEROING,
Austin Schuh96341532018-03-09 21:17:24 -080047 DISABLED,
48 GOTO_PATH,
Austin Schuhcb091712018-02-21 20:01:55 -080049 RUNNING,
50 ESTOP,
51 };
52
Austin Schuh96341532018-03-09 21:17:24 -080053 enum class GrabState : int32_t {
54 NORMAL,
55 WAIT_FOR_BOX,
56 TALL_BOX,
57 SHORT_BOX,
58 CLAW_CLOSE,
59 OPEN_INTAKE,
60 };
61
Austin Schuhcb091712018-02-21 20:01:55 -080062 State state() const { return state_; }
Austin Schuh96341532018-03-09 21:17:24 -080063 GrabState grab_state() const { return grab_state_; }
64
65 // Returns the maximum position for the intake. This is used to override the
66 // intake position to release the box when the state machine is lifting.
67 double max_intake_override() const { return max_intake_override_; }
Austin Schuhcb091712018-02-21 20:01:55 -080068
69 private:
Austin Schuh96341532018-03-09 21:17:24 -080070 bool AtState(uint32_t state) const { return current_node_ == state; }
71 bool NearEnd(double threshold = 0.01) const {
72 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
73 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
74 follower_.path_distance_to_go() < 1e-3;
75 }
76
Austin Schuhcb091712018-02-21 20:01:55 -080077 State state_ = State::UNINITIALIZED;
78
Austin Schuh96341532018-03-09 21:17:24 -080079 GrabState grab_state_ = GrabState::NORMAL;
80
81 ::aos::monotonic_clock::time_point claw_close_start_time_ =
Austin Schuhcb091712018-02-21 20:01:55 -080082 ::aos::monotonic_clock::min_time;
83
84 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator proximal_zeroing_estimator_;
85 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator distal_zeroing_estimator_;
86
87 double proximal_offset_ = 0.0;
88 double distal_offset_ = 0.0;
89
Austin Schuh96341532018-03-09 21:17:24 -080090 bool claw_closed_ = true;
91 double max_intake_override_ = 1000.0;
92
Austin Schuhcb091712018-02-21 20:01:55 -080093 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
94
Austin Schuh7dfccf62018-03-03 21:28:14 -080095 ::std::vector<Trajectory> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -080096 SearchGraph search_graph_;
97
98 bool close_enough_for_full_power_ = false;
99
100 EKF arm_ekf_;
101 TrajectoryFollower follower_;
102
103 // Start at the 0th index.
104 uint32_t current_node_ = 0;
105
106 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
107};
108
109} // namespace arm
110} // namespace superstructure
111} // namespace control_loops
112} // namespace y2018
113
114#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_