blob: ed990346c3edd158d15d4a5f0c3c4d947078d84a [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08002
Austin Schuh64ebab22015-11-26 13:28:30 -08003#include "aos/common/commonmath.h"
Austin Schuh0aae9242018-03-14 19:49:44 -07004#include "aos/common/controls/polytope.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08005#include "aos/common/logging/matrix_logging.h"
6
Austin Schuh64ebab22015-11-26 13:28:30 -08007#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +00008#include "frc971/control_loops/drivetrain/drivetrain.q.h"
9#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuh0aae9242018-03-14 19:49:44 -070010#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080011
Comran Morshed5323ecb2015-12-26 20:50:55 +000012namespace frc971 {
Austin Schuh64ebab22015-11-26 13:28:30 -080013namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080014namespace drivetrain {
15
Austin Schuh093535c2016-03-05 23:21:00 -080016void DrivetrainMotorsSS::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Austin Schuh0aae9242018-03-14 19:49:44 -070017 output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ ||
18 ::std::abs((*U)(1, 0)) > max_voltage_;
Austin Schuh093535c2016-03-05 23:21:00 -080019
20 if (output_was_capped_) {
Austin Schuh0aae9242018-03-14 19:49:44 -070021 *U *= max_voltage_ / kf_->U_uncapped().lpNorm<Eigen::Infinity>();
Austin Schuh093535c2016-03-05 23:21:00 -080022 }
Austin Schuh64ebab22015-11-26 13:28:30 -080023}
24
Brian Silverman4e1b0662016-01-31 18:03:19 -050025// This intentionally runs the U-capping code even when it's unnecessary to help
26// make it more deterministic. Only running it when one or both sides want
27// out-of-range voltages could lead to things like running out of CPU under
28// certain situations, which would be bad.
Austin Schuh093535c2016-03-05 23:21:00 -080029void DrivetrainMotorsSS::PolyCapU(Eigen::Matrix<double, 2, 1> *U) {
Austin Schuh0aae9242018-03-14 19:49:44 -070030 output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ ||
31 ::std::abs((*U)(1, 0)) > max_voltage_;
Brian Silverman4e1b0662016-01-31 18:03:19 -050032
Austin Schuh093535c2016-03-05 23:21:00 -080033 const Eigen::Matrix<double, 7, 1> error = kf_->R() - kf_->X_hat();
Austin Schuh64ebab22015-11-26 13:28:30 -080034
Austin Schuh093535c2016-03-05 23:21:00 -080035 LOG_MATRIX(DEBUG, "U_uncapped", *U);
Austin Schuh64ebab22015-11-26 13:28:30 -080036
Brian Silverman4e1b0662016-01-31 18:03:19 -050037 Eigen::Matrix<double, 2, 2> position_K;
Austin Schuh32501832017-02-25 18:32:56 -080038 position_K << kf_->controller().K(0, 0), kf_->controller().K(0, 2),
39 kf_->controller().K(1, 0), kf_->controller().K(1, 2);
Brian Silverman4e1b0662016-01-31 18:03:19 -050040 Eigen::Matrix<double, 2, 2> velocity_K;
Austin Schuh32501832017-02-25 18:32:56 -080041 velocity_K << kf_->controller().K(0, 1), kf_->controller().K(0, 3),
42 kf_->controller().K(1, 1), kf_->controller().K(1, 3);
Austin Schuh64ebab22015-11-26 13:28:30 -080043
Brian Silverman4e1b0662016-01-31 18:03:19 -050044 Eigen::Matrix<double, 2, 1> position_error;
45 position_error << error(0, 0), error(2, 0);
46 // drive_error = [total_distance_error, left_error - right_error]
47 const auto drive_error = T_inverse_ * position_error;
48 Eigen::Matrix<double, 2, 1> velocity_error;
49 velocity_error << error(1, 0), error(3, 0);
50 LOG_MATRIX(DEBUG, "error", error);
Austin Schuh64ebab22015-11-26 13:28:30 -080051
Austin Schuh093535c2016-03-05 23:21:00 -080052
53 Eigen::Matrix<double, 2, 1> U_integral;
54 U_integral << kf_->X_hat(4, 0), kf_->X_hat(5, 0);
55
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070056 const ::aos::controls::HVPolytope<2, 4, 4> pos_poly_hv(
57 U_poly_.static_H() * position_K * T_,
58 U_poly_.static_H() *
59 (-velocity_K * velocity_error + U_integral - kf_->ff_U()) +
Austin Schuh0aae9242018-03-14 19:49:44 -070060 (U_poly_.static_k() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070061 (position_K * T_).inverse() *
62 ::aos::controls::ShiftPoints<2, 4>(
Austin Schuh0aae9242018-03-14 19:49:44 -070063 (U_poly_.StaticVertices() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070064 -velocity_K * velocity_error + U_integral - kf_->ff_U()));
Austin Schuh64ebab22015-11-26 13:28:30 -080065
Brian Silverman4e1b0662016-01-31 18:03:19 -050066 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
67 {
68 const auto &P = drive_error;
Austin Schuh64ebab22015-11-26 13:28:30 -080069
Brian Silverman4e1b0662016-01-31 18:03:19 -050070 Eigen::Matrix<double, 1, 2> L45;
71 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
72 const double w45 = 0;
Austin Schuh64ebab22015-11-26 13:28:30 -080073
Brian Silverman4e1b0662016-01-31 18:03:19 -050074 Eigen::Matrix<double, 1, 2> LH;
75 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
76 LH << 0, 1;
77 } else {
78 LH << 1, 0;
79 }
80 const double wh = LH.dot(P);
Austin Schuh64ebab22015-11-26 13:28:30 -080081
Brian Silverman4e1b0662016-01-31 18:03:19 -050082 Eigen::Matrix<double, 2, 2> standard;
83 standard << L45, LH;
84 Eigen::Matrix<double, 2, 1> W;
85 W << w45, wh;
86 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
Austin Schuh64ebab22015-11-26 13:28:30 -080087
Brian Silverman4e1b0662016-01-31 18:03:19 -050088 bool is_inside_h, is_inside_45;
89 const auto adjusted_pos_error_h =
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070090 DoCoerceGoal(pos_poly_hv, LH, wh, drive_error, &is_inside_h);
Brian Silverman4e1b0662016-01-31 18:03:19 -050091 const auto adjusted_pos_error_45 =
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070092 DoCoerceGoal(pos_poly_hv, L45, w45, intersection, &is_inside_45);
93 if (pos_poly_hv.IsInside(intersection)) {
Brian Silverman4e1b0662016-01-31 18:03:19 -050094 adjusted_pos_error = adjusted_pos_error_h;
95 } else {
96 if (is_inside_h) {
97 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm() ||
98 adjusted_pos_error_45.norm() > intersection.norm()) {
99 adjusted_pos_error = adjusted_pos_error_h;
Austin Schuh64ebab22015-11-26 13:28:30 -0800100 } else {
101 adjusted_pos_error = adjusted_pos_error_45;
102 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500103 } else {
104 adjusted_pos_error = adjusted_pos_error_45;
Austin Schuh64ebab22015-11-26 13:28:30 -0800105 }
106 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500107 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800108
Austin Schuh093535c2016-03-05 23:21:00 -0800109 *U = -U_integral + velocity_K *velocity_error +
110 position_K *T_ *adjusted_pos_error + kf_->ff_U();
Brian Silverman4e1b0662016-01-31 18:03:19 -0500111
112 if (!output_was_capped_) {
Austin Schuh093535c2016-03-05 23:21:00 -0800113 if ((*U - kf_->U_uncapped()).norm() > 0.0001) {
Brian Silverman4e1b0662016-01-31 18:03:19 -0500114 LOG(FATAL, "U unnecessarily capped\n");
115 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800116 }
117}
118
Austin Schuh093535c2016-03-05 23:21:00 -0800119DrivetrainMotorsSS::DrivetrainMotorsSS(const DrivetrainConfig &dt_config,
120 StateFeedbackLoop<7, 2, 3> *kf,
121 double *integrated_kf_heading)
122 : dt_config_(dt_config),
123 kf_(kf),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700124 U_poly_((Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
125 /*[*/ -1, 0 /*]*/,
126 /*[*/ 0, 1 /*]*/,
127 /*[*/ 0, -1 /*]]*/).finished(),
Austin Schuh0aae9242018-03-14 19:49:44 -0700128 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 1.0 /*]*/,
129 /*[*/ 1.0 /*]*/,
130 /*[*/ 1.0 /*]*/,
131 /*[*/ 1.0 /*]]*/).finished(),
132 (Eigen::Matrix<double, 2, 4>() << /*[[*/ 1.0, 1.0, -1.0, -1.0 /*]*/,
133 /*[*/ -1.0, 1.0, 1.0, -1.0 /*]*/).finished()),
Austin Schuh093535c2016-03-05 23:21:00 -0800134 linear_profile_(::aos::controls::kLoopFrequency),
135 angular_profile_(::aos::controls::kLoopFrequency),
136 integrated_kf_heading_(integrated_kf_heading) {
137 ::aos::controls::HPolytope<0>::Init();
138 T_ << 1, 1, 1, -1;
139 T_inverse_ = T_.inverse();
140 unprofiled_goal_.setZero();
Austin Schuh64ebab22015-11-26 13:28:30 -0800141}
142
Austin Schuh093535c2016-03-05 23:21:00 -0800143void DrivetrainMotorsSS::SetGoal(
144 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
145 unprofiled_goal_ << goal.left_goal, goal.left_velocity_goal, goal.right_goal,
146 goal.right_velocity_goal, 0.0, 0.0, 0.0;
Austin Schuh0aae9242018-03-14 19:49:44 -0700147 if (::std::abs(goal.max_ss_voltage) < 0.01) {
148 max_voltage_ = kMaxVoltage;
149 } else {
150 max_voltage_ = goal.max_ss_voltage;
151 }
Austin Schuh093535c2016-03-05 23:21:00 -0800152
153 use_profile_ =
Austin Schuh32501832017-02-25 18:32:56 -0800154 !kf_->controller().Kff().isZero(0) &&
Austin Schuh093535c2016-03-05 23:21:00 -0800155 (goal.linear.max_velocity != 0.0 && goal.linear.max_acceleration != 0.0 &&
156 goal.angular.max_velocity != 0.0 &&
157 goal.angular.max_acceleration != 0.0);
158 linear_profile_.set_maximum_velocity(goal.linear.max_velocity);
159 linear_profile_.set_maximum_acceleration(goal.linear.max_acceleration);
160 angular_profile_.set_maximum_velocity(goal.angular.max_velocity);
161 angular_profile_.set_maximum_acceleration(goal.angular.max_acceleration);
Austin Schuh64ebab22015-11-26 13:28:30 -0800162}
163
Austin Schuh093535c2016-03-05 23:21:00 -0800164void DrivetrainMotorsSS::Update(bool enable_control_loop) {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700165 Eigen::Matrix<double, 2, 1> wheel_heading =
166 dt_config_.LeftRightToAngular(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700167
168 const double gyro_to_wheel_offset =
169 wheel_heading(0, 0) - *integrated_kf_heading_;
170
Austin Schuh64ebab22015-11-26 13:28:30 -0800171 if (enable_control_loop) {
Austin Schuh093535c2016-03-05 23:21:00 -0800172 // Update profiles.
173 Eigen::Matrix<double, 2, 1> unprofiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700174 dt_config_.LeftRightToLinear(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800175 Eigen::Matrix<double, 2, 1> unprofiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700176 dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800177
178 Eigen::Matrix<double, 2, 1> next_linear;
179 Eigen::Matrix<double, 2, 1> next_angular;
180
181 if (use_profile_) {
182 next_linear = linear_profile_.Update(unprofiled_linear(0, 0),
183 unprofiled_linear(1, 0));
184 next_angular = angular_profile_.Update(unprofiled_angular(0, 0),
185 unprofiled_angular(1, 0));
Austin Schuh093535c2016-03-05 23:21:00 -0800186 } else {
187 next_angular = unprofiled_angular;
188 next_linear = unprofiled_linear;
Austin Schuh64ebab22015-11-26 13:28:30 -0800189 }
Austin Schuh093535c2016-03-05 23:21:00 -0800190
Austin Schuhba93d9e2016-03-18 22:38:57 -0700191 const double wheel_compensation_offset =
192 gyro_to_wheel_offset * dt_config_.robot_radius;
193 const double scaled_angle_delta =
194 (gyro_to_wheel_offset - last_gyro_to_wheel_offset_) *
195 dt_config_.robot_radius;
Austin Schuh093535c2016-03-05 23:21:00 -0800196
197 kf_->mutable_next_R().block<4, 1>(0, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700198 dt_config_.AngularLinearToLeftRight(next_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800199
200 kf_->mutable_next_R().block<3, 1>(4, 0) =
201 unprofiled_goal_.block<3, 1>(4, 0);
202
Austin Schuhba93d9e2016-03-18 22:38:57 -0700203 kf_->mutable_next_R(0, 0) -= wheel_compensation_offset;
204 kf_->mutable_next_R(2, 0) += wheel_compensation_offset;
205
Austin Schuh093535c2016-03-05 23:21:00 -0800206 if (!use_profile_) {
207 kf_->mutable_R() = kf_->next_R();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700208 } else {
209 kf_->mutable_R(0, 0) -= scaled_angle_delta;
210 kf_->mutable_R(2, 0) += scaled_angle_delta;
Austin Schuh093535c2016-03-05 23:21:00 -0800211 }
212
213 // Run the controller.
214 Eigen::Matrix<double, 2, 1> U = kf_->ControllerOutput();
215
216 kf_->mutable_U_uncapped() = kf_->mutable_U() = U;
217 ScaleCapU(&kf_->mutable_U());
218
219 // Now update the feed forwards.
220 kf_->UpdateFFReference();
221
222 // Now, move the profile if things didn't go perfectly.
223 if (use_profile_ &&
224 (kf_->U() - kf_->U_uncapped()).lpNorm<Eigen::Infinity>() > 1e-4) {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700225 // kf_->R() is in wheel coordinates, while the profile is in absolute
226 // coordinates. Convert back...
Austin Schuhd91c0d22016-10-15 21:24:28 -0700227 linear_profile_.MoveCurrentState(dt_config_.LeftRightToLinear(kf_->R()));
Austin Schuhba93d9e2016-03-18 22:38:57 -0700228
229 LOG(DEBUG, "Saturated while moving\n");
230
231 Eigen::Matrix<double, 2, 1> absolute_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700232 dt_config_.LeftRightToAngular(kf_->R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700233 absolute_angular(0, 0) -= gyro_to_wheel_offset;
234 angular_profile_.MoveCurrentState(absolute_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800235 }
236 } else {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700237 Eigen::Matrix<double, 2, 1> wheel_linear =
238 dt_config_.LeftRightToLinear(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700239 Eigen::Matrix<double, 2, 1> next_angular = wheel_heading;
240 next_angular(0, 0) = *integrated_kf_heading_;
Austin Schuh093535c2016-03-05 23:21:00 -0800241
Austin Schuhba93d9e2016-03-18 22:38:57 -0700242 unprofiled_goal_.block<4, 1>(0, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700243 dt_config_.AngularLinearToLeftRight(wheel_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800244
Austin Schuhd91c0d22016-10-15 21:24:28 -0700245 auto current_linear = dt_config_.LeftRightToLinear(unprofiled_goal_);
246 auto current_angular = dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700247 linear_profile_.MoveCurrentState(current_linear);
248 angular_profile_.MoveCurrentState(current_angular);
249
250 kf_->mutable_next_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
251 kf_->mutable_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
Austin Schuh64ebab22015-11-26 13:28:30 -0800252 }
Austin Schuhba93d9e2016-03-18 22:38:57 -0700253 last_gyro_to_wheel_offset_ = gyro_to_wheel_offset;
Austin Schuh64ebab22015-11-26 13:28:30 -0800254}
255
Austin Schuh093535c2016-03-05 23:21:00 -0800256void DrivetrainMotorsSS::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000257 ::frc971::control_loops::DrivetrainQueue::Output *output) const {
Austin Schuh64ebab22015-11-26 13:28:30 -0800258 if (output) {
Austin Schuh093535c2016-03-05 23:21:00 -0800259 output->left_voltage = kf_->U(0, 0);
260 output->right_voltage = kf_->U(1, 0);
Austin Schuh64ebab22015-11-26 13:28:30 -0800261 output->left_high = true;
262 output->right_high = true;
263 }
264}
265
Austin Schuh093535c2016-03-05 23:21:00 -0800266void DrivetrainMotorsSS::PopulateStatus(
267 ::frc971::control_loops::DrivetrainQueue::Status *status) const {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700268 Eigen::Matrix<double, 2, 1> profiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700269 dt_config_.LeftRightToLinear(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700270 Eigen::Matrix<double, 2, 1> profiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700271 dt_config_.LeftRightToAngular(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700272
273 profiled_angular(0, 0) -= last_gyro_to_wheel_offset_;
274
275 Eigen::Matrix<double, 4, 1> profiled_gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700276 dt_config_.AngularLinearToLeftRight(profiled_linear, profiled_angular);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700277
278 status->profiled_left_position_goal = profiled_gyro_left_right(0, 0);
279 status->profiled_left_velocity_goal = profiled_gyro_left_right(1, 0);
280 status->profiled_right_position_goal = profiled_gyro_left_right(2, 0);
281 status->profiled_right_velocity_goal = profiled_gyro_left_right(3, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800282}
283
Austin Schuh6197a182015-11-28 16:04:40 -0800284} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -0800285} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000286} // namespace frc971