blob: cd124de9baf2dba2e11246ebb9b3b807f144bfc2 [file] [log] [blame]
Austin Schuh844960d2013-03-09 17:07:51 -08001#ifndef FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_
2#define FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_
3
4#include <memory>
5
6#include "aos/common/control_loop/ControlLoop.h"
7#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh844960d2013-03-09 17:07:51 -08008
9namespace frc971 {
10namespace control_loops {
11namespace testing {
12class WristTest_NoWindupPositive_Test;
13class WristTest_NoWindupNegative_Test;
14};
15
Austin Schuhc1f68892013-03-16 17:06:27 -070016// Note: Everything in this file assumes that there is a 1 cycle delay between
17// power being requested and it showing up at the motor. It assumes that
18// X_hat(2, 1) is the voltage being applied as well. It will go unstable if
19// that isn't true.
20
Austin Schuh844960d2013-03-09 17:07:51 -080021template<int kNumZeroSensors>
22class ZeroedJoint;
23
24// This class implements the CapU function correctly given all the extra
25// information that we know about from the wrist motor.
26template<int kNumZeroSensors>
Austin Schuhc1f68892013-03-16 17:06:27 -070027class ZeroedStateFeedbackLoop : public StateFeedbackLoop<3, 1, 1> {
Austin Schuh844960d2013-03-09 17:07:51 -080028 public:
Austin Schuhc1f68892013-03-16 17:06:27 -070029 ZeroedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1> loop,
Austin Schuh844960d2013-03-09 17:07:51 -080030 ZeroedJoint<kNumZeroSensors> *zeroed_joint)
Austin Schuhc1f68892013-03-16 17:06:27 -070031 : StateFeedbackLoop<3, 1, 1>(loop),
32 zeroed_joint_(zeroed_joint),
33 voltage_(0.0),
34 last_voltage_(0.0) {
Austin Schuh844960d2013-03-09 17:07:51 -080035 }
36
37 // Caps U, but this time respects the state of the wrist as well.
38 virtual void CapU();
Austin Schuhc1f68892013-03-16 17:06:27 -070039
40 // Returns the accumulated voltage.
41 double voltage() const { return voltage_; }
42
43 // Returns the uncapped voltage.
44 double uncapped_voltage() const { return uncapped_voltage_; }
45
46 // Zeros the accumulator.
47 void ZeroPower() { voltage_ = 0.0; }
Austin Schuh844960d2013-03-09 17:07:51 -080048 private:
49 ZeroedJoint<kNumZeroSensors> *zeroed_joint_;
Austin Schuhc1f68892013-03-16 17:06:27 -070050
51 // The accumulated voltage to apply to the motor.
52 double voltage_;
53 double last_voltage_;
54 double uncapped_voltage_;
Austin Schuh844960d2013-03-09 17:07:51 -080055};
56
57template<int kNumZeroSensors>
58void ZeroedStateFeedbackLoop<kNumZeroSensors>::CapU() {
Austin Schuhc1f68892013-03-16 17:06:27 -070059 const double old_voltage = voltage_;
60 voltage_ += U(0, 0);
61
62 uncapped_voltage_ = voltage_;
63
64 // Do all our computations with the voltage, and then compute what the delta
65 // is to make that happen.
Austin Schuh844960d2013-03-09 17:07:51 -080066 if (zeroed_joint_->state_ == ZeroedJoint<kNumZeroSensors>::READY) {
67 if (Y(0, 0) >= zeroed_joint_->config_data_.upper_limit) {
Austin Schuhc1f68892013-03-16 17:06:27 -070068 voltage_ = std::min(0.0, voltage_);
Austin Schuh844960d2013-03-09 17:07:51 -080069 }
70 if (Y(0, 0) <= zeroed_joint_->config_data_.lower_limit) {
Austin Schuhc1f68892013-03-16 17:06:27 -070071 voltage_ = std::max(0.0, voltage_);
Austin Schuh844960d2013-03-09 17:07:51 -080072 }
73 }
74
75 const bool is_ready =
76 zeroed_joint_->state_ == ZeroedJoint<kNumZeroSensors>::READY;
77 double limit = is_ready ?
78 12.0 : zeroed_joint_->config_data_.max_zeroing_voltage;
79
Austin Schuhc1f68892013-03-16 17:06:27 -070080 // Make sure that reality and the observer can't get too far off. There is a
81 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
82 // against last cycle's voltage.
83 if (X_hat(2, 0) > last_voltage_ + 2.0) {
Brian Silvermanae9d4b72013-03-16 20:11:29 -070084 //X_hat(2, 0) = last_voltage_ + 2.0;
85 voltage_ -= X_hat(2, 0) - (last_voltage_ + 2.0);
86 LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0));
Austin Schuhc1f68892013-03-16 17:06:27 -070087 } else if (X_hat(2, 0) < last_voltage_ -2.0) {
Brian Silvermanae9d4b72013-03-16 20:11:29 -070088 //X_hat(2, 0) = last_voltage_ - 2.0;
89 voltage_ += X_hat(2, 0) - (last_voltage_ - 2.0);
90 LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0));
Austin Schuhc1f68892013-03-16 17:06:27 -070091 }
92
Brian Silvermanae9d4b72013-03-16 20:11:29 -070093 voltage_ = std::min(limit, voltage_);
94 voltage_ = std::max(-limit, voltage_);
95 U(0, 0) = voltage_ - old_voltage;
96 LOG(DEBUG, "abc %f\n", X_hat(2, 0) - voltage_);
97 LOG(DEBUG, "error %f\n", X_hat(0, 0) - R(0, 0));
98
Austin Schuhc1f68892013-03-16 17:06:27 -070099 last_voltage_ = voltage_;
Austin Schuh844960d2013-03-09 17:07:51 -0800100}
101
102
103// Class to zero and control a joint with any number of zeroing sensors with a
104// state feedback controller.
105template<int kNumZeroSensors>
106class ZeroedJoint {
107 public:
108 // Sturcture to hold the hardware configuration information.
109 struct ConfigurationData {
110 // Angle at the lower hardware limit.
111 double lower_limit;
112 // Angle at the upper hardware limit.
113 double upper_limit;
114 // Speed (and direction) to move while zeroing.
115 double zeroing_speed;
Austin Schuhdd3bc412013-03-16 17:02:40 -0700116 // Speed (and direction) to move while moving off the sensor.
117 double zeroing_off_speed;
Austin Schuh844960d2013-03-09 17:07:51 -0800118 // Maximum voltage to apply when zeroing.
119 double max_zeroing_voltage;
120 // Angles where we see a positive edge from the hall effect sensors.
121 double hall_effect_start_angle[kNumZeroSensors];
122 };
123
124 // Current position data for the encoder and hall effect information.
125 struct PositionData {
126 // Current encoder position.
127 double position;
128 // Array of hall effect values.
129 bool hall_effects[kNumZeroSensors];
130 // Array of the last positive edge position for the sensors.
131 double hall_effect_positions[kNumZeroSensors];
132 };
133
Austin Schuhc1f68892013-03-16 17:06:27 -0700134 ZeroedJoint(StateFeedbackLoop<3, 1, 1> loop)
Austin Schuh844960d2013-03-09 17:07:51 -0800135 : loop_(new ZeroedStateFeedbackLoop<kNumZeroSensors>(loop, this)),
136 state_(UNINITIALIZED),
137 error_count_(0),
138 zero_offset_(0.0),
139 capped_goal_(false) {
140 }
141
142 // Copies the provided configuration data locally.
143 void set_config_data(const ConfigurationData &config_data) {
144 config_data_ = config_data;
145 }
146
147 // Clips the goal to be inside the limits and returns the clipped goal.
148 // Requires the constants to have already been fetched.
149 double ClipGoal(double goal) const {
150 return ::std::min(config_data_.upper_limit,
151 std::max(config_data_.lower_limit, goal));
152 }
153
154 // Updates the loop and state machine.
155 // position is null if the position data is stale, output_enabled is true if
156 // the output will actually go to the motors, and goal_angle and goal_velocity
157 // are the goal position and velocities.
158 double Update(const ZeroedJoint<kNumZeroSensors>::PositionData *position,
159 bool output_enabled,
160 double goal_angle, double goal_velocity);
161
162 // True if the code is zeroing.
163 bool is_zeroing() const { return state_ == ZEROING; }
164
Austin Schuhe20e93c2013-03-09 19:54:16 -0800165 // True if the code is moving off the hall effect.
166 bool is_moving_off() const { return state_ == MOVING_OFF; }
167
Austin Schuh844960d2013-03-09 17:07:51 -0800168 // True if the state machine is uninitialized.
169 bool is_uninitialized() const { return state_ == UNINITIALIZED; }
170
171 // True if the state machine is ready.
172 bool is_ready() const { return state_ == READY; }
173
174 // Returns the uncapped voltage.
175 double U_uncapped() const { return loop_->U_uncapped(0, 0); }
176
177 // True if the goal was moved to avoid goal windup.
178 bool capped_goal() const { return capped_goal_; }
179
Austin Schuhe20e93c2013-03-09 19:54:16 -0800180 // Timestamp
181 static const double dt;
182
Brian Silverman893ba072013-03-16 14:03:50 -0700183 double absolute_position() const { return loop_->X_hat(0, 0); }
184
Austin Schuh844960d2013-03-09 17:07:51 -0800185 private:
186 friend class ZeroedStateFeedbackLoop<kNumZeroSensors>;
187 // Friend the wrist test cases so that they can simulate windeup.
188 friend class testing::WristTest_NoWindupPositive_Test;
189 friend class testing::WristTest_NoWindupNegative_Test;
190
191 // The state feedback control loop to talk to.
192 ::std::unique_ptr<ZeroedStateFeedbackLoop<kNumZeroSensors>> loop_;
193
194 ConfigurationData config_data_;
195
Austin Schuhe20e93c2013-03-09 19:54:16 -0800196 // Returns the index of the first active sensor, or -1 if none are active.
197 int ActiveSensorIndex(
198 const ZeroedJoint<kNumZeroSensors>::PositionData *position) {
199 if (!position) {
200 return -1;
201 }
202 int active_index = -1;
203 for (int i = 0; i < kNumZeroSensors; ++i) {
204 if (position->hall_effects[i]) {
205 if (active_index != -1) {
206 LOG(ERROR, "More than one hall effect sensor is active\n");
207 } else {
208 active_index = i;
209 }
210 }
211 }
212 return active_index;
213 }
214 // Returns true if any of the sensors are active.
215 bool AnySensorsActive(
216 const ZeroedJoint<kNumZeroSensors>::PositionData *position) {
217 return ActiveSensorIndex(position) != -1;
218 }
219
Austin Schuh844960d2013-03-09 17:07:51 -0800220 // Enum to store the state of the internal zeroing state machine.
221 enum State {
222 UNINITIALIZED,
223 MOVING_OFF,
224 ZEROING,
225 READY,
226 ESTOP
227 };
228
229 // Internal state for zeroing.
230 State state_;
231
232 // Missed position packet count.
233 int error_count_;
234 // Offset from the raw encoder value to the absolute angle.
235 double zero_offset_;
236 // Position that gets incremented when zeroing the wrist to slowly move it to
237 // the hall effect sensor.
238 double zeroing_position_;
239 // Last position at which the hall effect sensor was off.
240 double last_off_position_;
241
242 // True if the zeroing goal was capped during this cycle.
243 bool capped_goal_;
244
Austin Schuhe20e93c2013-03-09 19:54:16 -0800245 // Returns true if number is between first and second inclusive.
246 bool is_between(double first, double second, double number) {
247 if ((number >= first || number >= second) &&
248 (number <= first || number <= second)) {
249 return true;
250 }
251 return false;
252 }
253
Austin Schuh844960d2013-03-09 17:07:51 -0800254 DISALLOW_COPY_AND_ASSIGN(ZeroedJoint);
255};
256
Austin Schuhe20e93c2013-03-09 19:54:16 -0800257template <int kNumZeroSensors>
258/*static*/ const double ZeroedJoint<kNumZeroSensors>::dt = 0.01;
259
Austin Schuh844960d2013-03-09 17:07:51 -0800260// Updates the zeroed joint controller and state machine.
261template <int kNumZeroSensors>
262double ZeroedJoint<kNumZeroSensors>::Update(
263 const ZeroedJoint<kNumZeroSensors>::PositionData *position,
264 bool output_enabled,
265 double goal_angle, double goal_velocity) {
266 // Uninitialize the bot if too many cycles pass without an encoder.
267 if (position == NULL) {
268 LOG(WARNING, "no new pos given\n");
269 error_count_++;
270 } else {
271 error_count_ = 0;
272 }
273 if (error_count_ >= 4) {
274 LOG(WARNING, "err_count is %d so forcing a re-zero\n", error_count_);
275 state_ = UNINITIALIZED;
276 }
277
278 // Compute the absolute position of the wrist.
279 double absolute_position;
280 if (position) {
281 absolute_position = position->position;
282 if (state_ == READY) {
283 absolute_position -= zero_offset_;
284 }
285 loop_->Y << absolute_position;
Austin Schuhe20e93c2013-03-09 19:54:16 -0800286 if (!AnySensorsActive(position)) {
287 last_off_position_ = position->position;
Austin Schuh844960d2013-03-09 17:07:51 -0800288 }
289 } else {
290 // Dead recon for now.
291 absolute_position = loop_->X_hat(0, 0);
292 }
293
294 switch (state_) {
295 case UNINITIALIZED:
Austin Schuhb5191b92013-03-10 18:22:24 -0700296 LOG(DEBUG, "UNINITIALIZED\n");
Austin Schuh844960d2013-03-09 17:07:51 -0800297 if (position) {
298 // Reset the zeroing goal.
299 zeroing_position_ = absolute_position;
300 // Clear the observer state.
Austin Schuhc1f68892013-03-16 17:06:27 -0700301 loop_->X_hat << absolute_position, 0.0, 0.0;
302 loop_->ZeroPower();
Austin Schuh844960d2013-03-09 17:07:51 -0800303 // Set the goal to here to make it so it doesn't move when disabled.
304 loop_->R = loop_->X_hat;
305 // Only progress if we are enabled.
306 if (::aos::robot_state->enabled) {
Austin Schuhe20e93c2013-03-09 19:54:16 -0800307 if (AnySensorsActive(position)) {
308 state_ = MOVING_OFF;
309 } else {
310 state_ = ZEROING;
Austin Schuh844960d2013-03-09 17:07:51 -0800311 }
312 }
313 }
314 break;
315 case MOVING_OFF:
Austin Schuhb5191b92013-03-10 18:22:24 -0700316 LOG(DEBUG, "MOVING_OFF\n");
Austin Schuhe20e93c2013-03-09 19:54:16 -0800317 {
318 // Move off the hall effect sensor.
319 if (!::aos::robot_state->enabled) {
320 // Start over if disabled.
321 state_ = UNINITIALIZED;
322 } else if (position && !AnySensorsActive(position)) {
323 // We are now off the sensor. Time to zero now.
324 state_ = ZEROING;
325 } else {
326 // Slowly creep off the sensor.
Austin Schuhdd3bc412013-03-16 17:02:40 -0700327 zeroing_position_ -= config_data_.zeroing_off_speed * dt;
Austin Schuhc1f68892013-03-16 17:06:27 -0700328 loop_->R << zeroing_position_, -config_data_.zeroing_off_speed, 0.0;
Austin Schuhe20e93c2013-03-09 19:54:16 -0800329 break;
330 }
Austin Schuh844960d2013-03-09 17:07:51 -0800331 }
332 case ZEROING:
Austin Schuhb5191b92013-03-10 18:22:24 -0700333 LOG(DEBUG, "ZEROING\n");
Austin Schuhe20e93c2013-03-09 19:54:16 -0800334 {
335 int active_sensor_index = ActiveSensorIndex(position);
336 if (!::aos::robot_state->enabled) {
337 // Start over if disabled.
338 state_ = UNINITIALIZED;
339 } else if (position && active_sensor_index != -1) {
340 state_ = READY;
341 // Verify that the calibration number is between the last off position
342 // and the current on position. If this is not true, move off and try
343 // again.
344 const double calibration =
345 position->hall_effect_positions[active_sensor_index];
346 if (!is_between(last_off_position_, position->position,
347 calibration)) {
348 LOG(ERROR, "Got a bogus calibration number. Trying again.\n");
349 LOG(ERROR,
350 "Last off position was %f, current is %f, calibration is %f\n",
351 last_off_position_, position->position,
352 position->hall_effect_positions[active_sensor_index]);
353 state_ = MOVING_OFF;
354 } else {
355 // Save the zero, and then offset the observer to deal with the
356 // phantom step change.
357 const double old_zero_offset = zero_offset_;
358 zero_offset_ =
359 position->hall_effect_positions[active_sensor_index] -
360 config_data_.hall_effect_start_angle[active_sensor_index];
361 loop_->X_hat(0, 0) += old_zero_offset - zero_offset_;
362 loop_->Y(0, 0) += old_zero_offset - zero_offset_;
363 }
Austin Schuh844960d2013-03-09 17:07:51 -0800364 } else {
Austin Schuhe20e93c2013-03-09 19:54:16 -0800365 // Slowly creep towards the sensor.
366 zeroing_position_ += config_data_.zeroing_speed * dt;
Austin Schuhc1f68892013-03-16 17:06:27 -0700367 loop_->R << zeroing_position_, config_data_.zeroing_speed, 0.0;
Austin Schuh844960d2013-03-09 17:07:51 -0800368 }
Austin Schuhe20e93c2013-03-09 19:54:16 -0800369 break;
Austin Schuh844960d2013-03-09 17:07:51 -0800370 }
Austin Schuh844960d2013-03-09 17:07:51 -0800371
372 case READY:
Austin Schuhb5191b92013-03-10 18:22:24 -0700373 LOG(DEBUG, "READY\n");
Austin Schuh844960d2013-03-09 17:07:51 -0800374 {
Austin Schuh844960d2013-03-09 17:07:51 -0800375 const double limited_goal = ClipGoal(goal_angle);
Austin Schuhc1f68892013-03-16 17:06:27 -0700376 loop_->R << limited_goal, goal_velocity, 0.0;
Austin Schuh844960d2013-03-09 17:07:51 -0800377 break;
378 }
379
380 case ESTOP:
Austin Schuhb5191b92013-03-10 18:22:24 -0700381 LOG(DEBUG, "ESTOP\n");
Austin Schuh844960d2013-03-09 17:07:51 -0800382 LOG(WARNING, "have already given up\n");
383 return 0.0;
384 }
385
386 // Update the observer.
Austin Schuh844960d2013-03-09 17:07:51 -0800387 loop_->Update(position != NULL, !output_enabled);
Austin Schuh844960d2013-03-09 17:07:51 -0800388
Brian Silvermanae9d4b72013-03-16 20:11:29 -0700389 LOG(DEBUG, "X_hat={%f, %f, %f}\n",
390 loop_->X_hat(0, 0), loop_->X_hat(1, 0), loop_->X_hat(2, 0));
391
Austin Schuh844960d2013-03-09 17:07:51 -0800392 capped_goal_ = false;
393 // Verify that the zeroing goal hasn't run away.
394 switch (state_) {
395 case UNINITIALIZED:
396 case READY:
397 case ESTOP:
398 // Not zeroing. No worries.
399 break;
400 case MOVING_OFF:
401 case ZEROING:
402 // Check if we have cliped and adjust the goal.
Austin Schuhc1f68892013-03-16 17:06:27 -0700403 if (loop_->uncapped_voltage() > config_data_.max_zeroing_voltage) {
404 double dx = (loop_->uncapped_voltage() -
Austin Schuh844960d2013-03-09 17:07:51 -0800405 config_data_.max_zeroing_voltage) / loop_->K(0, 0);
406 zeroing_position_ -= dx;
407 capped_goal_ = true;
Austin Schuhc1f68892013-03-16 17:06:27 -0700408 } else if(loop_->uncapped_voltage() < -config_data_.max_zeroing_voltage) {
409 double dx = (loop_->uncapped_voltage() +
Austin Schuh844960d2013-03-09 17:07:51 -0800410 config_data_.max_zeroing_voltage) / loop_->K(0, 0);
411 zeroing_position_ -= dx;
412 capped_goal_ = true;
413 }
414 break;
415 }
Austin Schuhc1f68892013-03-16 17:06:27 -0700416 return loop_->voltage();
Austin Schuh844960d2013-03-09 17:07:51 -0800417}
418
419} // namespace control_loops
420} // namespace frc971
421
422#endif // FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_