blob: 8cd6b398ec2878a6678df10d60f87f8b3775c636 [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 Schuh20177c92019-07-07 20:48:24 -070037 void Iterate(const ::aos::monotonic_clock::time_point monotonic_now,
38 const uint32_t *unsafe_goal, bool grab_box, bool open_claw,
Neil Balchba9cbba2018-04-06 22:26:38 -070039 bool close_claw, const control_loops::ArmPosition *position,
Austin Schuh96341532018-03-09 21:17:24 -080040 const bool claw_beambreak_triggered,
41 const bool box_back_beambreak_triggered,
Austin Schuh20177c92019-07-07 20:48:24 -070042 const bool intake_clear_of_box, bool suicide,
43 bool trajectory_override, 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
Austin Schuh72db9a12019-01-21 18:02:51 -080094 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
95 proximal_zeroing_estimator_;
96 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
97 distal_zeroing_estimator_;
Austin Schuhcb091712018-02-21 20:01:55 -080098
99 double proximal_offset_ = 0.0;
100 double distal_offset_ = 0.0;
101
Austin Schuh96341532018-03-09 21:17:24 -0800102 bool claw_closed_ = true;
103 double max_intake_override_ = 1000.0;
104
Austin Schuhcb091712018-02-21 20:01:55 -0800105 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
106
Austin Schuh41c71e42018-04-04 20:11:20 -0700107 double vmax_ = kVMax();
108
109 ::std::vector<TrajectoryAndParams> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -0800110 SearchGraph search_graph_;
111
112 bool close_enough_for_full_power_ = false;
113
Austin Schuh17e484e2018-03-11 01:11:36 -0800114 int32_t climb_count_ = 0;
Austin Schuh7afcc232018-09-16 16:33:47 -0700115 int32_t brownout_count_ = 0;
Austin Schuh17e484e2018-03-11 01:11:36 -0800116
Austin Schuhcb091712018-02-21 20:01:55 -0800117 EKF arm_ekf_;
118 TrajectoryFollower follower_;
119
Austin Schuhb874fd32018-03-05 00:27:10 -0800120 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
121
Austin Schuhcb091712018-02-21 20:01:55 -0800122 // Start at the 0th index.
123 uint32_t current_node_ = 0;
124
Neil Balchba9cbba2018-04-06 22:26:38 -0700125 uint32_t claw_closed_count_ = 0;
126
Austin Schuhcb091712018-02-21 20:01:55 -0800127 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
128};
129
130} // namespace arm
131} // namespace superstructure
132} // namespace control_loops
133} // namespace y2018
134
135#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_