blob: 2307c572247227a2e341ed4b5425d6cba60afbd1 [file] [log] [blame]
joe93778a62014-02-15 13:22:14 -08001#ifndef FRC971_CONTROL_LOOPS_shooter_shooter_H_
2#define FRC971_CONTROL_LOOPS_shooter_shooter_H_
3
4#include <memory>
5
6#include "aos/common/control_loop/ControlLoop.h"
7#include "frc971/control_loops/state_feedback_loop.h"
Ben Fredricksonedf0e092014-02-16 10:46:50 +00008#include "aos/common/time.h"
Brian Silverman36407c92014-04-01 15:54:02 -07009#include "aos/common/util/log_interval.h"
joe93778a62014-02-15 13:22:14 -080010
Ben Fredricksonedf0e092014-02-16 10:46:50 +000011#include "frc971/constants.h"
joe93778a62014-02-15 13:22:14 -080012#include "frc971/control_loops/shooter/shooter_motor_plant.h"
13#include "frc971/control_loops/shooter/shooter.q.h"
14
joe93778a62014-02-15 13:22:14 -080015namespace frc971 {
16namespace control_loops {
17namespace testing {
Austin Schuhd34569d2014-02-18 20:26:38 -080018class ShooterTest_UnloadWindupPositive_Test;
19class ShooterTest_UnloadWindupNegative_Test;
joe93778a62014-02-15 13:22:14 -080020};
21
Ben Fredricksonedf0e092014-02-16 10:46:50 +000022using ::aos::time::Time;
23
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000024// Note: Everything in this file assumes that there is a 1 cycle delay between
25// power being requested and it showing up at the motor. It assumes that
26// X_hat(2, 1) is the voltage being applied as well. It will go unstable if
27// that isn't true.
28
29// This class implements the CapU function correctly given all the extra
30// information that we know about from the wrist motor.
31// It does not have any zeroing logic in it, only logic to deal with a delta U
32// controller.
33class ZeroedStateFeedbackLoop : public StateFeedbackLoop<3, 1, 1> {
34 public:
35 ZeroedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1> loop)
36 : StateFeedbackLoop<3, 1, 1>(loop),
37 voltage_(0.0),
38 last_voltage_(0.0),
39 uncapped_voltage_(0.0),
Austin Schuhd34569d2014-02-18 20:26:38 -080040 offset_(0.0),
41 max_voltage_(12.0),
42 capped_goal_(false) {}
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000043
44 const static int kZeroingMaxVoltage = 5;
45
46 // Caps U, but this time respects the state of the wrist as well.
47 virtual void CapU();
48
49 // Returns the accumulated voltage.
50 double voltage() const { return voltage_; }
51
52 // Returns the uncapped voltage.
53 double uncapped_voltage() const { return uncapped_voltage_; }
54
55 // Zeros the accumulator.
56 void ZeroPower() { voltage_ = 0.0; }
57
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000058 // Sets the calibration offset given the absolute angle and the corrisponding
59 // encoder value.
Austin Schuh30537882014-02-18 01:07:23 -080060 void SetCalibration(double encoder_val, double known_position);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000061
Austin Schuh30537882014-02-18 01:07:23 -080062 double offset() const { return offset_; }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000063
Austin Schuh30537882014-02-18 01:07:23 -080064 double absolute_position() const { return X_hat(0, 0) + kPositionOffset; }
Austin Schuhbe1401f2014-02-18 03:18:41 -080065 double absolute_velocity() const { return X_hat(1, 0); }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000066
Austin Schuh30537882014-02-18 01:07:23 -080067 void CorrectPosition(double position) {
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000068 Eigen::Matrix<double, 1, 1> Y;
Austin Schuh30537882014-02-18 01:07:23 -080069 Y << position + offset_ - kPositionOffset;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000070 Correct(Y);
71 }
72
Austin Schuhd34569d2014-02-18 20:26:38 -080073 // Recomputes the power goal for the current controller and position/velocity.
74 void RecalculatePowerGoal();
75
Austin Schuh30537882014-02-18 01:07:23 -080076 double goal_position() const { return R(0, 0) + kPositionOffset; }
77 double goal_velocity() const { return R(1, 0); }
78 void InitializeState(double position) {
79 X_hat(0, 0) = position - kPositionOffset;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000080 }
81
Austin Schuh30537882014-02-18 01:07:23 -080082 void SetGoalPosition(double desired_position, double desired_velocity) {
Austin Schuhbe1401f2014-02-18 03:18:41 -080083 LOG(DEBUG, "Goal position: %f Goal velocity: %f\n", desired_position, desired_velocity);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000084
Austin Schuh30537882014-02-18 01:07:23 -080085 R << desired_position - kPositionOffset, desired_velocity,
Austin Schuhbe1401f2014-02-18 03:18:41 -080086 (-A(1, 0) / A(1, 2) * (desired_position - kPositionOffset) -
87 A(1, 1) / A(1, 2) * desired_velocity);
Austin Schuh30537882014-02-18 01:07:23 -080088 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000089
Austin Schuh30537882014-02-18 01:07:23 -080090 double position() const { return X_hat(0, 0) - offset_ + kPositionOffset; }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000091
Austin Schuhd34569d2014-02-18 20:26:38 -080092 void set_max_voltage(const double max_voltage) { max_voltage_ = max_voltage; }
93 bool capped_goal() const { return capped_goal_; }
94
95 void CapGoal();
96
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000097 private:
Austin Schuh30537882014-02-18 01:07:23 -080098 // The offset between what is '0' (0 rate on the spring) and the 0 (all the
99 // way cocked).
Austin Schuh25933852014-02-23 02:04:13 -0800100 constexpr static double kPositionOffset = kMaxExtension;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000101 // The accumulated voltage to apply to the motor.
102 double voltage_;
103 double last_voltage_;
104 double uncapped_voltage_;
105 double offset_;
Austin Schuhd34569d2014-02-18 20:26:38 -0800106 double max_voltage_;
107 bool capped_goal_;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000108};
109
Austin Schuh06cbbf12014-02-22 02:07:31 -0800110const Time kUnloadTimeout = Time::InSeconds(10);
Austin Schuh492331a2014-03-02 21:13:17 -0800111const Time kLoadTimeout = Time::InSeconds(2);
Austin Schuh1a08bd82014-03-09 00:47:11 -0800112const Time kLoadProblemEndTimeout = Time::InSeconds(1.0);
Austin Schuh06cbbf12014-02-22 02:07:31 -0800113const Time kShooterBrakeSetTime = Time::InSeconds(0.05);
Austin Schuhecdb0252014-02-23 04:12:35 -0800114// Time to wait after releasing the latch piston before winching back again.
115const Time kShotEndTimeout = Time::InSeconds(0.2);
Austin Schuh06cbbf12014-02-22 02:07:31 -0800116const Time kPrepareFireEndTime = Time::InMS(40);
117
joe93778a62014-02-15 13:22:14 -0800118class ShooterMotor
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000119 : public aos::control_loops::ControlLoop<control_loops::ShooterGroup> {
joe93778a62014-02-15 13:22:14 -0800120 public:
Ben Fredrickson22c93322014-02-17 05:56:33 +0000121 explicit ShooterMotor(control_loops::ShooterGroup *my_shooter =
122 &control_loops::shooter_queue_group);
joe93778a62014-02-15 13:22:14 -0800123
124 // True if the goal was moved to avoid goal windup.
Austin Schuhd34569d2014-02-18 20:26:38 -0800125 bool capped_goal() const { return shooter_.capped_goal(); }
joe93778a62014-02-15 13:22:14 -0800126
Austin Schuh30537882014-02-18 01:07:23 -0800127 double PowerToPosition(double power);
James Kuszmauld29689f2014-03-02 13:06:54 -0800128 double PositionToPower(double position);
Ben Fredrickson4413f3b2014-02-16 23:25:36 +0000129
Ben Fredrickson22c93322014-02-17 05:56:33 +0000130 typedef enum {
131 STATE_INITIALIZE = 0,
132 STATE_REQUEST_LOAD = 1,
133 STATE_LOAD_BACKTRACK = 2,
134 STATE_LOAD = 3,
135 STATE_LOADING_PROBLEM = 4,
136 STATE_PREPARE_SHOT = 5,
137 STATE_READY = 6,
Austin Schuh30537882014-02-18 01:07:23 -0800138 STATE_FIRE = 8,
139 STATE_UNLOAD = 9,
140 STATE_UNLOAD_MOVE = 10,
141 STATE_READY_UNLOAD = 11,
142 STATE_ESTOP = 12
Ben Fredrickson22c93322014-02-17 05:56:33 +0000143 } State;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000144
Austin Schuh30537882014-02-18 01:07:23 -0800145 State state() { return state_; }
Ben Fredricksona6d77542014-02-17 07:54:43 +0000146
joe93778a62014-02-15 13:22:14 -0800147 protected:
148 virtual void RunIteration(
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000149 const ShooterGroup::Goal *goal,
150 const control_loops::ShooterGroup::Position *position,
Ben Fredrickson22c93322014-02-17 05:56:33 +0000151 ShooterGroup::Output *output, ShooterGroup::Status *status);
joe93778a62014-02-15 13:22:14 -0800152
153 private:
Brian Silvermand1e65b92014-03-08 17:07:14 -0800154 // We have to override this to keep the pistons in the correct positions.
155 virtual void ZeroOutputs();
156
joe93778a62014-02-15 13:22:14 -0800157 // Friend the test classes for acces to the internal state.
Austin Schuhd34569d2014-02-18 20:26:38 -0800158 friend class testing::ShooterTest_UnloadWindupPositive_Test;
159 friend class testing::ShooterTest_UnloadWindupNegative_Test;
joe93778a62014-02-15 13:22:14 -0800160
Austin Schuh30537882014-02-18 01:07:23 -0800161 // Enter state STATE_UNLOAD
162 void Unload() {
163 state_ = STATE_UNLOAD;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800164 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800165 }
166 // Enter state STATE_LOAD
167 void Load() {
168 state_ = STATE_LOAD;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800169 load_timeout_ = Time::Now() + kLoadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800170 }
171
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000172 control_loops::ShooterGroup::Position last_position_;
173
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000174 ZeroedStateFeedbackLoop shooter_;
joe93778a62014-02-15 13:22:14 -0800175
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000176 // state machine state
177 State state_;
178
179 // time to giving up on loading problem
180 Time loading_problem_end_time_;
181
Austin Schuh30537882014-02-18 01:07:23 -0800182 // The end time when loading for it to timeout.
183 Time load_timeout_;
184
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000185 // wait for brake to set
186 Time shooter_brake_set_time_;
James Kuszmaul7290a942014-02-26 20:04:13 -0800187
Austin Schuh30537882014-02-18 01:07:23 -0800188 // The timeout for unloading.
189 Time unload_timeout_;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000190
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000191 // time that shot must have completed
192 Time shot_end_time_;
193
194 // track cycles that we are stuck to detect errors
195 int cycles_not_moved_;
196
Austin Schuh30537882014-02-18 01:07:23 -0800197 double firing_starting_position_;
198
199 // True if the latch should be engaged and the brake should be engaged.
200 bool latch_piston_;
201 bool brake_piston_;
202 int32_t last_distal_posedge_count_;
203 int32_t last_proximal_posedge_count_;
Austin Schuh4edad872014-03-02 11:56:12 -0800204 uint32_t shot_count_;
Austin Schuh492331a2014-03-02 21:13:17 -0800205 bool zeroed_;
206 int distal_posedge_validation_cycles_left_;
207 int proximal_posedge_validation_cycles_left_;
Austin Schuh2e976812014-03-05 19:56:58 -0800208 bool last_distal_current_;
209 bool last_proximal_current_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000210
Brian Silverman36407c92014-04-01 15:54:02 -0700211 ::aos::util::SimpleLogInterval motors_off_log_ =
212 ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.05),
213 WARNING, "motors disabled");
214
joe93778a62014-02-15 13:22:14 -0800215 DISALLOW_COPY_AND_ASSIGN(ShooterMotor);
216};
217
218} // namespace control_loops
219} // namespace frc971
220
221#endif // FRC971_CONTROL_LOOPS_shooter_shooter_H_