blob: 7ceb001cc6c7d9ff81b0fa1e0e2292fe5cb614b9 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include "aos/time/time.h"
Austin Schuhcb091712018-02-21 20:01:55 -08005#include "frc971/zeroing/zeroing.h"
6#include "y2018/constants.h"
7#include "y2018/control_loops/superstructure/arm/dynamics.h"
8#include "y2018/control_loops/superstructure/arm/ekf.h"
Austin Schuh41c71e42018-04-04 20:11:20 -07009#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Austin Schuhcb091712018-02-21 20:01:55 -080010#include "y2018/control_loops/superstructure/arm/graph.h"
11#include "y2018/control_loops/superstructure/arm/trajectory.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
13#include "y2018/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuhcb091712018-02-21 20:01:55 -080014
15namespace y2018 {
16namespace control_loops {
17namespace superstructure {
18namespace arm {
19
Austin Schuhcb091712018-02-21 20:01:55 -080020class Arm {
21 public:
22 Arm();
23
Austin Schuh7a090402018-03-04 01:16:45 -080024 // If true, tune down all the constants for testing.
Austin Schuhb874fd32018-03-05 00:27:10 -080025 static constexpr bool kGrannyMode() { return false; }
Austin Schuh7a090402018-03-04 01:16:45 -080026
Austin Schuhcb091712018-02-21 20:01:55 -080027 // The operating voltage.
Austin Schuh7a090402018-03-04 01:16:45 -080028 static constexpr double kOperatingVoltage() {
Austin Schuh41c71e42018-04-04 20:11:20 -070029 return kGrannyMode() ? 5.0 : 12.0;
Austin Schuh7a090402018-03-04 01:16:45 -080030 }
Austin Schuhcb091712018-02-21 20:01:55 -080031 static constexpr double kDt() { return 0.00505; }
Austin Schuh41c71e42018-04-04 20:11:20 -070032 static constexpr double kAlpha0Max() { return kGrannyMode() ? 5.0 : 15.0; }
33 static constexpr double kAlpha1Max() { return kGrannyMode() ? 5.0 : 15.0; }
Austin Schuhb874fd32018-03-05 00:27:10 -080034
35 static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; }
36 static constexpr double kPathlessVMax() { return 5.0; }
Austin Schuh41c71e42018-04-04 20:11:20 -070037 static constexpr double kGotoPathVMax() { return 6.0; }
Austin Schuhcb091712018-02-21 20:01:55 -080038
Alex Perrycb7da4b2019-08-28 19:35:56 -070039 flatbuffers::Offset<superstructure::ArmStatus> Iterate(
40 const ::aos::monotonic_clock::time_point monotonic_now,
41 const uint32_t *unsafe_goal, bool grab_box, bool open_claw,
42 bool close_claw, const superstructure::ArmPosition *position,
43 const bool claw_beambreak_triggered,
44 const bool box_back_beambreak_triggered, const bool intake_clear_of_box,
45 bool suicide, bool trajectory_override, double *proximal_output,
46 double *distal_output, bool *release_arm_brake, bool *claw_closed,
47 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuhcb091712018-02-21 20:01:55 -080048
49 void Reset();
50
51 enum class State : int32_t {
52 UNINITIALIZED,
53 ZEROING,
Austin Schuh96341532018-03-09 21:17:24 -080054 DISABLED,
55 GOTO_PATH,
Austin Schuhcb091712018-02-21 20:01:55 -080056 RUNNING,
Austin Schuh17e484e2018-03-11 01:11:36 -080057 PREP_CLIMB,
Austin Schuhcb091712018-02-21 20:01:55 -080058 ESTOP,
59 };
60
Austin Schuh96341532018-03-09 21:17:24 -080061 enum class GrabState : int32_t {
Austin Schuhede47322018-07-08 16:04:36 -070062 NORMAL = 0,
63 WAIT_FOR_BOX = 1,
64 TALL_BOX = 2,
65 SHORT_BOX = 3,
66 CLAW_CLOSE = 4,
67 OPEN_INTAKE = 5,
Austin Schuh96341532018-03-09 21:17:24 -080068 };
69
Austin Schuhcb091712018-02-21 20:01:55 -080070 State state() const { return state_; }
Austin Schuh96341532018-03-09 21:17:24 -080071 GrabState grab_state() const { return grab_state_; }
72
Alex Perrycb7da4b2019-08-28 19:35:56 -070073 bool estopped() const { return state_ == State::ESTOP; }
74 bool zeroed() const {
75 return (proximal_zeroing_estimator_.zeroed() &&
76 distal_zeroing_estimator_.zeroed());
77 }
78
Austin Schuh96341532018-03-09 21:17:24 -080079 // Returns the maximum position for the intake. This is used to override the
80 // intake position to release the box when the state machine is lifting.
81 double max_intake_override() const { return max_intake_override_; }
Austin Schuhcb091712018-02-21 20:01:55 -080082
Austin Schuhd76546a2018-07-08 16:05:14 -070083 uint32_t current_node() const { return current_node_; }
84
85 double path_distance_to_go() { return follower_.path_distance_to_go(); }
86
Austin Schuhcb091712018-02-21 20:01:55 -080087 private:
Austin Schuh96341532018-03-09 21:17:24 -080088 bool AtState(uint32_t state) const { return current_node_ == state; }
Austin Schuh83cdd8a2018-03-21 20:49:02 -070089 bool NearEnd(double threshold = 0.03) const {
Austin Schuh96341532018-03-09 21:17:24 -080090 return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold &&
91 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold &&
92 follower_.path_distance_to_go() < 1e-3;
93 }
94
Austin Schuhcb091712018-02-21 20:01:55 -080095 State state_ = State::UNINITIALIZED;
96
Austin Schuh96341532018-03-09 21:17:24 -080097 GrabState grab_state_ = GrabState::NORMAL;
98
99 ::aos::monotonic_clock::time_point claw_close_start_time_ =
Austin Schuhcb091712018-02-21 20:01:55 -0800100 ::aos::monotonic_clock::min_time;
101
Austin Schuh72db9a12019-01-21 18:02:51 -0800102 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
103 proximal_zeroing_estimator_;
104 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator
105 distal_zeroing_estimator_;
Austin Schuhcb091712018-02-21 20:01:55 -0800106
107 double proximal_offset_ = 0.0;
108 double distal_offset_ = 0.0;
109
Austin Schuh96341532018-03-09 21:17:24 -0800110 bool claw_closed_ = true;
111 double max_intake_override_ = 1000.0;
112
Austin Schuhcb091712018-02-21 20:01:55 -0800113 const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_;
114
Austin Schuh41c71e42018-04-04 20:11:20 -0700115 double vmax_ = kVMax();
116
117 ::std::vector<TrajectoryAndParams> trajectories_;
Austin Schuhcb091712018-02-21 20:01:55 -0800118 SearchGraph search_graph_;
119
120 bool close_enough_for_full_power_ = false;
121
Austin Schuh17e484e2018-03-11 01:11:36 -0800122 int32_t climb_count_ = 0;
Austin Schuh7afcc232018-09-16 16:33:47 -0700123 int32_t brownout_count_ = 0;
Austin Schuh17e484e2018-03-11 01:11:36 -0800124
Austin Schuhcb091712018-02-21 20:01:55 -0800125 EKF arm_ekf_;
126 TrajectoryFollower follower_;
127
Austin Schuhb874fd32018-03-05 00:27:10 -0800128 const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_;
129
Austin Schuhcb091712018-02-21 20:01:55 -0800130 // Start at the 0th index.
131 uint32_t current_node_ = 0;
132
Neil Balchba9cbba2018-04-06 22:26:38 -0700133 uint32_t claw_closed_count_ = 0;
134
Austin Schuhcb091712018-02-21 20:01:55 -0800135 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
136};
137
138} // namespace arm
139} // namespace superstructure
140} // namespace control_loops
141} // namespace y2018
142
143#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_