blob: 8e1860fefaa3d06e2b908459294fee759e5756ac [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 Schuhb874fd32018-03-05 00:27:10 -080029 static constexpr double kAlpha0Max() { return kGrannyMode() ? 10.0 : 15.0; }
30 static constexpr double kAlpha1Max() { return kGrannyMode() ? 10.0 : 15.0; }
31
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,
41 bool *claw_closed, control_loops::ArmStatus *status);
Austin Schuhcb091712018-02-21 20:01:55 -080042
43 void Reset();
44
45 enum class State : int32_t {
46 UNINITIALIZED,
47 ZEROING,
Austin Schuh96341532018-03-09 21:17:24 -080048 DISABLED,
49 GOTO_PATH,
Austin Schuhcb091712018-02-21 20:01:55 -080050 RUNNING,
51 ESTOP,
52 };
53
Austin Schuh96341532018-03-09 21:17:24 -080054 enum class GrabState : int32_t {
55 NORMAL,
56 WAIT_FOR_BOX,
57 TALL_BOX,
58 SHORT_BOX,
59 CLAW_CLOSE,
60 OPEN_INTAKE,
61 };
62
Austin Schuhcb091712018-02-21 20:01:55 -080063 State state() const { return state_; }
Austin Schuh96341532018-03-09 21:17:24 -080064 GrabState grab_state() const { return grab_state_; }
65
66 // Returns the maximum position for the intake. This is used to override the
67 // intake position to release the box when the state machine is lifting.
68 double max_intake_override() const { return max_intake_override_; }
Austin Schuhcb091712018-02-21 20:01:55 -080069
70 private:
Austin Schuh96341532018-03-09 21:17:24 -080071 bool AtState(uint32_t state) const { return current_node_ == state; }
Austin Schuhb874fd32018-03-05 00:27:10 -080072 bool NearEnd(double threshold = 0.02) const {
Austin Schuh96341532018-03-09 21:17:24 -080073 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
74 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
75 follower_.path_distance_to_go() < 1e-3;
76 }
77
Austin Schuhcb091712018-02-21 20:01:55 -080078 State state_ = State::UNINITIALIZED;
79
Austin Schuh96341532018-03-09 21:17:24 -080080 GrabState grab_state_ = GrabState::NORMAL;
81
82 ::aos::monotonic_clock::time_point claw_close_start_time_ =
Austin Schuhcb091712018-02-21 20:01:55 -080083 ::aos::monotonic_clock::min_time;
84
85 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator proximal_zeroing_estimator_;
86 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator distal_zeroing_estimator_;
87
88 double proximal_offset_ = 0.0;
89 double distal_offset_ = 0.0;
90
Austin Schuh96341532018-03-09 21:17:24 -080091 bool claw_closed_ = true;
92 double max_intake_override_ = 1000.0;
93
Austin Schuhcb091712018-02-21 20:01:55 -080094 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
95
Austin Schuh7dfccf62018-03-03 21:28:14 -080096 ::std::vector<Trajectory> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -080097 SearchGraph search_graph_;
98
99 bool close_enough_for_full_power_ = false;
100
101 EKF arm_ekf_;
102 TrajectoryFollower follower_;
103
Austin Schuhb874fd32018-03-05 00:27:10 -0800104 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
105
Austin Schuhcb091712018-02-21 20:01:55 -0800106 // Start at the 0th index.
107 uint32_t current_node_ = 0;
108
109 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
110};
111
112} // namespace arm
113} // namespace superstructure
114} // namespace control_loops
115} // namespace y2018
116
117#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_