blob: 01e95e343f4b8fb74a0ddbaad7af23daf1e761d6 [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 voltage_ = std::min(limit, voltage_);
81 voltage_ = std::max(-limit, voltage_);
82 U(0, 0) = voltage_ - old_voltage;
83
84 // Make sure that reality and the observer can't get too far off. There is a
85 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
86 // against last cycle's voltage.
87 if (X_hat(2, 0) > last_voltage_ + 2.0) {
88 X_hat(2, 0) = last_voltage_ + 2.0;
89 } else if (X_hat(2, 0) < last_voltage_ -2.0) {
90 X_hat(2, 0) = last_voltage_ - 2.0;
91 }
92
93 last_voltage_ = voltage_;
Austin Schuh844960d2013-03-09 17:07:51 -080094}
95
96
97// Class to zero and control a joint with any number of zeroing sensors with a
98// state feedback controller.
99template<int kNumZeroSensors>
100class ZeroedJoint {
101 public:
102 // Sturcture to hold the hardware configuration information.
103 struct ConfigurationData {
104 // Angle at the lower hardware limit.
105 double lower_limit;
106 // Angle at the upper hardware limit.
107 double upper_limit;
108 // Speed (and direction) to move while zeroing.
109 double zeroing_speed;
Austin Schuhdd3bc412013-03-16 17:02:40 -0700110 // Speed (and direction) to move while moving off the sensor.
111 double zeroing_off_speed;
Austin Schuh844960d2013-03-09 17:07:51 -0800112 // Maximum voltage to apply when zeroing.
113 double max_zeroing_voltage;
114 // Angles where we see a positive edge from the hall effect sensors.
115 double hall_effect_start_angle[kNumZeroSensors];
116 };
117
118 // Current position data for the encoder and hall effect information.
119 struct PositionData {
120 // Current encoder position.
121 double position;
122 // Array of hall effect values.
123 bool hall_effects[kNumZeroSensors];
124 // Array of the last positive edge position for the sensors.
125 double hall_effect_positions[kNumZeroSensors];
126 };
127
Austin Schuhc1f68892013-03-16 17:06:27 -0700128 ZeroedJoint(StateFeedbackLoop<3, 1, 1> loop)
Austin Schuh844960d2013-03-09 17:07:51 -0800129 : loop_(new ZeroedStateFeedbackLoop<kNumZeroSensors>(loop, this)),
130 state_(UNINITIALIZED),
131 error_count_(0),
132 zero_offset_(0.0),
133 capped_goal_(false) {
134 }
135
136 // Copies the provided configuration data locally.
137 void set_config_data(const ConfigurationData &config_data) {
138 config_data_ = config_data;
139 }
140
141 // Clips the goal to be inside the limits and returns the clipped goal.
142 // Requires the constants to have already been fetched.
143 double ClipGoal(double goal) const {
144 return ::std::min(config_data_.upper_limit,
145 std::max(config_data_.lower_limit, goal));
146 }
147
148 // Updates the loop and state machine.
149 // position is null if the position data is stale, output_enabled is true if
150 // the output will actually go to the motors, and goal_angle and goal_velocity
151 // are the goal position and velocities.
152 double Update(const ZeroedJoint<kNumZeroSensors>::PositionData *position,
153 bool output_enabled,
154 double goal_angle, double goal_velocity);
155
156 // True if the code is zeroing.
157 bool is_zeroing() const { return state_ == ZEROING; }
158
Austin Schuhe20e93c2013-03-09 19:54:16 -0800159 // True if the code is moving off the hall effect.
160 bool is_moving_off() const { return state_ == MOVING_OFF; }
161
Austin Schuh844960d2013-03-09 17:07:51 -0800162 // True if the state machine is uninitialized.
163 bool is_uninitialized() const { return state_ == UNINITIALIZED; }
164
165 // True if the state machine is ready.
166 bool is_ready() const { return state_ == READY; }
167
168 // Returns the uncapped voltage.
169 double U_uncapped() const { return loop_->U_uncapped(0, 0); }
170
171 // True if the goal was moved to avoid goal windup.
172 bool capped_goal() const { return capped_goal_; }
173
Austin Schuhe20e93c2013-03-09 19:54:16 -0800174 // Timestamp
175 static const double dt;
176
Austin Schuh844960d2013-03-09 17:07:51 -0800177 private:
178 friend class ZeroedStateFeedbackLoop<kNumZeroSensors>;
179 // Friend the wrist test cases so that they can simulate windeup.
180 friend class testing::WristTest_NoWindupPositive_Test;
181 friend class testing::WristTest_NoWindupNegative_Test;
182
183 // The state feedback control loop to talk to.
184 ::std::unique_ptr<ZeroedStateFeedbackLoop<kNumZeroSensors>> loop_;
185
186 ConfigurationData config_data_;
187
Austin Schuhe20e93c2013-03-09 19:54:16 -0800188 // Returns the index of the first active sensor, or -1 if none are active.
189 int ActiveSensorIndex(
190 const ZeroedJoint<kNumZeroSensors>::PositionData *position) {
191 if (!position) {
192 return -1;
193 }
194 int active_index = -1;
195 for (int i = 0; i < kNumZeroSensors; ++i) {
196 if (position->hall_effects[i]) {
197 if (active_index != -1) {
198 LOG(ERROR, "More than one hall effect sensor is active\n");
199 } else {
200 active_index = i;
201 }
202 }
203 }
204 return active_index;
205 }
206 // Returns true if any of the sensors are active.
207 bool AnySensorsActive(
208 const ZeroedJoint<kNumZeroSensors>::PositionData *position) {
209 return ActiveSensorIndex(position) != -1;
210 }
211
Austin Schuh844960d2013-03-09 17:07:51 -0800212 // Enum to store the state of the internal zeroing state machine.
213 enum State {
214 UNINITIALIZED,
215 MOVING_OFF,
216 ZEROING,
217 READY,
218 ESTOP
219 };
220
221 // Internal state for zeroing.
222 State state_;
223
224 // Missed position packet count.
225 int error_count_;
226 // Offset from the raw encoder value to the absolute angle.
227 double zero_offset_;
228 // Position that gets incremented when zeroing the wrist to slowly move it to
229 // the hall effect sensor.
230 double zeroing_position_;
231 // Last position at which the hall effect sensor was off.
232 double last_off_position_;
233
234 // True if the zeroing goal was capped during this cycle.
235 bool capped_goal_;
236
Austin Schuhe20e93c2013-03-09 19:54:16 -0800237 // Returns true if number is between first and second inclusive.
238 bool is_between(double first, double second, double number) {
239 if ((number >= first || number >= second) &&
240 (number <= first || number <= second)) {
241 return true;
242 }
243 return false;
244 }
245
Austin Schuh844960d2013-03-09 17:07:51 -0800246 DISALLOW_COPY_AND_ASSIGN(ZeroedJoint);
247};
248
Austin Schuhe20e93c2013-03-09 19:54:16 -0800249template <int kNumZeroSensors>
250/*static*/ const double ZeroedJoint<kNumZeroSensors>::dt = 0.01;
251
Austin Schuh844960d2013-03-09 17:07:51 -0800252// Updates the zeroed joint controller and state machine.
253template <int kNumZeroSensors>
254double ZeroedJoint<kNumZeroSensors>::Update(
255 const ZeroedJoint<kNumZeroSensors>::PositionData *position,
256 bool output_enabled,
257 double goal_angle, double goal_velocity) {
258 // Uninitialize the bot if too many cycles pass without an encoder.
259 if (position == NULL) {
260 LOG(WARNING, "no new pos given\n");
261 error_count_++;
262 } else {
263 error_count_ = 0;
264 }
265 if (error_count_ >= 4) {
266 LOG(WARNING, "err_count is %d so forcing a re-zero\n", error_count_);
267 state_ = UNINITIALIZED;
268 }
269
270 // Compute the absolute position of the wrist.
271 double absolute_position;
272 if (position) {
273 absolute_position = position->position;
274 if (state_ == READY) {
275 absolute_position -= zero_offset_;
276 }
277 loop_->Y << absolute_position;
Austin Schuhe20e93c2013-03-09 19:54:16 -0800278 if (!AnySensorsActive(position)) {
279 last_off_position_ = position->position;
Austin Schuh844960d2013-03-09 17:07:51 -0800280 }
281 } else {
282 // Dead recon for now.
283 absolute_position = loop_->X_hat(0, 0);
284 }
285
286 switch (state_) {
287 case UNINITIALIZED:
Austin Schuhb5191b92013-03-10 18:22:24 -0700288 LOG(DEBUG, "UNINITIALIZED\n");
Austin Schuh844960d2013-03-09 17:07:51 -0800289 if (position) {
290 // Reset the zeroing goal.
291 zeroing_position_ = absolute_position;
292 // Clear the observer state.
Austin Schuhc1f68892013-03-16 17:06:27 -0700293 loop_->X_hat << absolute_position, 0.0, 0.0;
294 loop_->ZeroPower();
Austin Schuh844960d2013-03-09 17:07:51 -0800295 // Set the goal to here to make it so it doesn't move when disabled.
296 loop_->R = loop_->X_hat;
297 // Only progress if we are enabled.
298 if (::aos::robot_state->enabled) {
Austin Schuhe20e93c2013-03-09 19:54:16 -0800299 if (AnySensorsActive(position)) {
300 state_ = MOVING_OFF;
301 } else {
302 state_ = ZEROING;
Austin Schuh844960d2013-03-09 17:07:51 -0800303 }
304 }
305 }
306 break;
307 case MOVING_OFF:
Austin Schuhb5191b92013-03-10 18:22:24 -0700308 LOG(DEBUG, "MOVING_OFF\n");
Austin Schuhe20e93c2013-03-09 19:54:16 -0800309 {
310 // Move off the hall effect sensor.
311 if (!::aos::robot_state->enabled) {
312 // Start over if disabled.
313 state_ = UNINITIALIZED;
314 } else if (position && !AnySensorsActive(position)) {
315 // We are now off the sensor. Time to zero now.
316 state_ = ZEROING;
317 } else {
318 // Slowly creep off the sensor.
Austin Schuhdd3bc412013-03-16 17:02:40 -0700319 zeroing_position_ -= config_data_.zeroing_off_speed * dt;
Austin Schuhc1f68892013-03-16 17:06:27 -0700320 loop_->R << zeroing_position_, -config_data_.zeroing_off_speed, 0.0;
Austin Schuhe20e93c2013-03-09 19:54:16 -0800321 break;
322 }
Austin Schuh844960d2013-03-09 17:07:51 -0800323 }
324 case ZEROING:
Austin Schuhb5191b92013-03-10 18:22:24 -0700325 LOG(DEBUG, "ZEROING\n");
Austin Schuhe20e93c2013-03-09 19:54:16 -0800326 {
327 int active_sensor_index = ActiveSensorIndex(position);
328 if (!::aos::robot_state->enabled) {
329 // Start over if disabled.
330 state_ = UNINITIALIZED;
331 } else if (position && active_sensor_index != -1) {
332 state_ = READY;
333 // Verify that the calibration number is between the last off position
334 // and the current on position. If this is not true, move off and try
335 // again.
336 const double calibration =
337 position->hall_effect_positions[active_sensor_index];
338 if (!is_between(last_off_position_, position->position,
339 calibration)) {
340 LOG(ERROR, "Got a bogus calibration number. Trying again.\n");
341 LOG(ERROR,
342 "Last off position was %f, current is %f, calibration is %f\n",
343 last_off_position_, position->position,
344 position->hall_effect_positions[active_sensor_index]);
345 state_ = MOVING_OFF;
346 } else {
347 // Save the zero, and then offset the observer to deal with the
348 // phantom step change.
349 const double old_zero_offset = zero_offset_;
350 zero_offset_ =
351 position->hall_effect_positions[active_sensor_index] -
352 config_data_.hall_effect_start_angle[active_sensor_index];
353 loop_->X_hat(0, 0) += old_zero_offset - zero_offset_;
354 loop_->Y(0, 0) += old_zero_offset - zero_offset_;
355 }
Austin Schuh844960d2013-03-09 17:07:51 -0800356 } else {
Austin Schuhe20e93c2013-03-09 19:54:16 -0800357 // Slowly creep towards the sensor.
358 zeroing_position_ += config_data_.zeroing_speed * dt;
Austin Schuhc1f68892013-03-16 17:06:27 -0700359 loop_->R << zeroing_position_, config_data_.zeroing_speed, 0.0;
Austin Schuh844960d2013-03-09 17:07:51 -0800360 }
Austin Schuhe20e93c2013-03-09 19:54:16 -0800361 break;
Austin Schuh844960d2013-03-09 17:07:51 -0800362 }
Austin Schuh844960d2013-03-09 17:07:51 -0800363
364 case READY:
Austin Schuhb5191b92013-03-10 18:22:24 -0700365 LOG(DEBUG, "READY\n");
Austin Schuh844960d2013-03-09 17:07:51 -0800366 {
Austin Schuh844960d2013-03-09 17:07:51 -0800367 const double limited_goal = ClipGoal(goal_angle);
Austin Schuhc1f68892013-03-16 17:06:27 -0700368 loop_->R << limited_goal, goal_velocity, 0.0;
Austin Schuh844960d2013-03-09 17:07:51 -0800369 break;
370 }
371
372 case ESTOP:
Austin Schuhb5191b92013-03-10 18:22:24 -0700373 LOG(DEBUG, "ESTOP\n");
Austin Schuh844960d2013-03-09 17:07:51 -0800374 LOG(WARNING, "have already given up\n");
375 return 0.0;
376 }
377
378 // Update the observer.
Austin Schuh844960d2013-03-09 17:07:51 -0800379 loop_->Update(position != NULL, !output_enabled);
Austin Schuh844960d2013-03-09 17:07:51 -0800380
381 capped_goal_ = false;
382 // Verify that the zeroing goal hasn't run away.
383 switch (state_) {
384 case UNINITIALIZED:
385 case READY:
386 case ESTOP:
387 // Not zeroing. No worries.
388 break;
389 case MOVING_OFF:
390 case ZEROING:
391 // Check if we have cliped and adjust the goal.
Austin Schuhc1f68892013-03-16 17:06:27 -0700392 if (loop_->uncapped_voltage() > config_data_.max_zeroing_voltage) {
393 double dx = (loop_->uncapped_voltage() -
Austin Schuh844960d2013-03-09 17:07:51 -0800394 config_data_.max_zeroing_voltage) / loop_->K(0, 0);
395 zeroing_position_ -= dx;
396 capped_goal_ = true;
Austin Schuhc1f68892013-03-16 17:06:27 -0700397 } else if(loop_->uncapped_voltage() < -config_data_.max_zeroing_voltage) {
398 double dx = (loop_->uncapped_voltage() +
Austin Schuh844960d2013-03-09 17:07:51 -0800399 config_data_.max_zeroing_voltage) / loop_->K(0, 0);
400 zeroing_position_ -= dx;
401 capped_goal_ = true;
402 }
403 break;
404 }
Austin Schuhc1f68892013-03-16 17:06:27 -0700405 return loop_->voltage();
Austin Schuh844960d2013-03-09 17:07:51 -0800406}
407
408} // namespace control_loops
409} // namespace frc971
410
411#endif // FRC971_CONTROL_LOOPS_ZEROED_JOINT_H_