blob: 5924eb1525c257276b947d113ce18ef785589dcb [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
John Park33858a32018-09-28 23:05:48 -07003#include "aos/commonmath.h"
4#include "aos/controls/polytope.h"
5#include "aos/logging/matrix_logging.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08006
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 Schuh06b65b82018-12-23 09:21:44 +110016DrivetrainMotorsSS::DrivetrainMotorsSS(
17 const DrivetrainConfig<double> &dt_config, StateFeedbackLoop<7, 2, 4> *kf,
James Kuszmaul3431d622019-02-17 17:07:44 -080018 LocalizerInterface *localizer)
Austin Schuh06b65b82018-12-23 09:21:44 +110019 : dt_config_(dt_config),
20 kf_(kf),
21 U_poly_(
22 (Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
23 /*[*/ -1, 0 /*]*/,
24 /*[*/ 0, 1 /*]*/,
25 /*[*/ 0, -1 /*]]*/)
26 .finished(),
27 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 1.0 /*]*/,
28 /*[*/ 1.0 /*]*/,
29 /*[*/ 1.0 /*]*/,
30 /*[*/ 1.0 /*]]*/)
31 .finished(),
32 (Eigen::Matrix<double, 2, 4>() << /*[[*/ 1.0, 1.0, -1.0, -1.0 /*]*/,
33 /*[*/ -1.0, 1.0, 1.0, -1.0 /*]*/)
34 .finished()),
35 linear_profile_(::aos::controls::kLoopFrequency),
36 angular_profile_(::aos::controls::kLoopFrequency),
James Kuszmaul3431d622019-02-17 17:07:44 -080037 localizer_(localizer) {
Austin Schuh06b65b82018-12-23 09:21:44 +110038 ::aos::controls::HPolytope<0>::Init();
39 T_ << 1, 1, 1, -1;
40 T_inverse_ = T_.inverse();
41 unprofiled_goal_.setZero();
42}
43
Austin Schuh093535c2016-03-05 23:21:00 -080044void DrivetrainMotorsSS::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Austin Schuh0aae9242018-03-14 19:49:44 -070045 output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ ||
46 ::std::abs((*U)(1, 0)) > max_voltage_;
Austin Schuh093535c2016-03-05 23:21:00 -080047
48 if (output_was_capped_) {
Austin Schuh0aae9242018-03-14 19:49:44 -070049 *U *= max_voltage_ / kf_->U_uncapped().lpNorm<Eigen::Infinity>();
Austin Schuh093535c2016-03-05 23:21:00 -080050 }
Austin Schuh64ebab22015-11-26 13:28:30 -080051}
52
Brian Silverman4e1b0662016-01-31 18:03:19 -050053// This intentionally runs the U-capping code even when it's unnecessary to help
54// make it more deterministic. Only running it when one or both sides want
55// out-of-range voltages could lead to things like running out of CPU under
56// certain situations, which would be bad.
Austin Schuh093535c2016-03-05 23:21:00 -080057void DrivetrainMotorsSS::PolyCapU(Eigen::Matrix<double, 2, 1> *U) {
Austin Schuh0aae9242018-03-14 19:49:44 -070058 output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ ||
59 ::std::abs((*U)(1, 0)) > max_voltage_;
Brian Silverman4e1b0662016-01-31 18:03:19 -050060
Austin Schuh093535c2016-03-05 23:21:00 -080061 const Eigen::Matrix<double, 7, 1> error = kf_->R() - kf_->X_hat();
Austin Schuh64ebab22015-11-26 13:28:30 -080062
Austin Schuhf257f3c2019-10-27 21:00:43 -070063 AOS_LOG_MATRIX(DEBUG, "U_uncapped", *U);
Austin Schuh64ebab22015-11-26 13:28:30 -080064
Brian Silverman4e1b0662016-01-31 18:03:19 -050065 Eigen::Matrix<double, 2, 2> position_K;
Austin Schuh32501832017-02-25 18:32:56 -080066 position_K << kf_->controller().K(0, 0), kf_->controller().K(0, 2),
67 kf_->controller().K(1, 0), kf_->controller().K(1, 2);
Brian Silverman4e1b0662016-01-31 18:03:19 -050068 Eigen::Matrix<double, 2, 2> velocity_K;
Austin Schuh32501832017-02-25 18:32:56 -080069 velocity_K << kf_->controller().K(0, 1), kf_->controller().K(0, 3),
70 kf_->controller().K(1, 1), kf_->controller().K(1, 3);
Austin Schuh64ebab22015-11-26 13:28:30 -080071
Brian Silverman4e1b0662016-01-31 18:03:19 -050072 Eigen::Matrix<double, 2, 1> position_error;
73 position_error << error(0, 0), error(2, 0);
74 // drive_error = [total_distance_error, left_error - right_error]
75 const auto drive_error = T_inverse_ * position_error;
76 Eigen::Matrix<double, 2, 1> velocity_error;
77 velocity_error << error(1, 0), error(3, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070078 AOS_LOG_MATRIX(DEBUG, "error", error);
Austin Schuh093535c2016-03-05 23:21:00 -080079
80 Eigen::Matrix<double, 2, 1> U_integral;
81 U_integral << kf_->X_hat(4, 0), kf_->X_hat(5, 0);
82
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070083 const ::aos::controls::HVPolytope<2, 4, 4> pos_poly_hv(
84 U_poly_.static_H() * position_K * T_,
85 U_poly_.static_H() *
86 (-velocity_K * velocity_error + U_integral - kf_->ff_U()) +
Austin Schuh0aae9242018-03-14 19:49:44 -070087 (U_poly_.static_k() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070088 (position_K * T_).inverse() *
Austin Schuhbcce26a2018-03-26 23:41:24 -070089 ::aos::controls::ShiftPoints<2, 4, double>(
Austin Schuh0aae9242018-03-14 19:49:44 -070090 (U_poly_.StaticVertices() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070091 -velocity_K * velocity_error + U_integral - kf_->ff_U()));
Austin Schuh64ebab22015-11-26 13:28:30 -080092
Brian Silverman4e1b0662016-01-31 18:03:19 -050093 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
94 {
95 const auto &P = drive_error;
Austin Schuh64ebab22015-11-26 13:28:30 -080096
Brian Silverman4e1b0662016-01-31 18:03:19 -050097 Eigen::Matrix<double, 1, 2> L45;
98 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
99 const double w45 = 0;
Austin Schuh64ebab22015-11-26 13:28:30 -0800100
Brian Silverman4e1b0662016-01-31 18:03:19 -0500101 Eigen::Matrix<double, 1, 2> LH;
102 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
103 LH << 0, 1;
104 } else {
105 LH << 1, 0;
106 }
107 const double wh = LH.dot(P);
Austin Schuh64ebab22015-11-26 13:28:30 -0800108
Brian Silverman4e1b0662016-01-31 18:03:19 -0500109 Eigen::Matrix<double, 2, 2> standard;
110 standard << L45, LH;
111 Eigen::Matrix<double, 2, 1> W;
112 W << w45, wh;
113 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
Austin Schuh64ebab22015-11-26 13:28:30 -0800114
Brian Silverman4e1b0662016-01-31 18:03:19 -0500115 bool is_inside_h, is_inside_45;
116 const auto adjusted_pos_error_h =
Austin Schuhbcce26a2018-03-26 23:41:24 -0700117 DoCoerceGoal<double>(pos_poly_hv, LH, wh, drive_error, &is_inside_h);
Brian Silverman4e1b0662016-01-31 18:03:19 -0500118 const auto adjusted_pos_error_45 =
Austin Schuhbcce26a2018-03-26 23:41:24 -0700119 DoCoerceGoal<double>(pos_poly_hv, L45, w45, intersection, &is_inside_45);
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700120 if (pos_poly_hv.IsInside(intersection)) {
Brian Silverman4e1b0662016-01-31 18:03:19 -0500121 adjusted_pos_error = adjusted_pos_error_h;
122 } else {
123 if (is_inside_h) {
124 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm() ||
125 adjusted_pos_error_45.norm() > intersection.norm()) {
126 adjusted_pos_error = adjusted_pos_error_h;
Austin Schuh64ebab22015-11-26 13:28:30 -0800127 } else {
128 adjusted_pos_error = adjusted_pos_error_45;
129 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500130 } else {
131 adjusted_pos_error = adjusted_pos_error_45;
Austin Schuh64ebab22015-11-26 13:28:30 -0800132 }
133 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500134 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800135
Austin Schuh093535c2016-03-05 23:21:00 -0800136 *U = -U_integral + velocity_K *velocity_error +
137 position_K *T_ *adjusted_pos_error + kf_->ff_U();
Brian Silverman4e1b0662016-01-31 18:03:19 -0500138
139 if (!output_was_capped_) {
Austin Schuh093535c2016-03-05 23:21:00 -0800140 if ((*U - kf_->U_uncapped()).norm() > 0.0001) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700141 AOS_LOG(FATAL, "U unnecessarily capped\n");
Brian Silverman4e1b0662016-01-31 18:03:19 -0500142 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800143 }
144}
145
Austin Schuh093535c2016-03-05 23:21:00 -0800146void DrivetrainMotorsSS::SetGoal(
147 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
Alex Perryfc83d3b2019-02-03 15:41:58 -0800148 unprofiled_goal_ << goal.left_goal, 0.0, goal.right_goal, 0.0, 0.0, 0.0, 0.0;
Austin Schuh0aae9242018-03-14 19:49:44 -0700149 if (::std::abs(goal.max_ss_voltage) < 0.01) {
150 max_voltage_ = kMaxVoltage;
151 } else {
152 max_voltage_ = goal.max_ss_voltage;
153 }
Austin Schuh093535c2016-03-05 23:21:00 -0800154
155 use_profile_ =
Austin Schuh32501832017-02-25 18:32:56 -0800156 !kf_->controller().Kff().isZero(0) &&
Austin Schuh093535c2016-03-05 23:21:00 -0800157 (goal.linear.max_velocity != 0.0 && goal.linear.max_acceleration != 0.0 &&
158 goal.angular.max_velocity != 0.0 &&
159 goal.angular.max_acceleration != 0.0);
160 linear_profile_.set_maximum_velocity(goal.linear.max_velocity);
161 linear_profile_.set_maximum_acceleration(goal.linear.max_acceleration);
162 angular_profile_.set_maximum_velocity(goal.angular.max_velocity);
163 angular_profile_.set_maximum_acceleration(goal.angular.max_acceleration);
Austin Schuh64ebab22015-11-26 13:28:30 -0800164}
165
Austin Schuh093535c2016-03-05 23:21:00 -0800166void DrivetrainMotorsSS::Update(bool enable_control_loop) {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700167 Eigen::Matrix<double, 2, 1> wheel_heading =
168 dt_config_.LeftRightToAngular(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700169
James Kuszmaul3431d622019-02-17 17:07:44 -0800170 const double gyro_to_wheel_offset = wheel_heading(0, 0) - localizer_->theta();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700171
Austin Schuh64ebab22015-11-26 13:28:30 -0800172 if (enable_control_loop) {
Austin Schuh093535c2016-03-05 23:21:00 -0800173 // Update profiles.
174 Eigen::Matrix<double, 2, 1> unprofiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700175 dt_config_.LeftRightToLinear(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800176 Eigen::Matrix<double, 2, 1> unprofiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700177 dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800178
179 Eigen::Matrix<double, 2, 1> next_linear;
180 Eigen::Matrix<double, 2, 1> next_angular;
181
182 if (use_profile_) {
183 next_linear = linear_profile_.Update(unprofiled_linear(0, 0),
184 unprofiled_linear(1, 0));
185 next_angular = angular_profile_.Update(unprofiled_angular(0, 0),
186 unprofiled_angular(1, 0));
Austin Schuh093535c2016-03-05 23:21:00 -0800187 } else {
188 next_angular = unprofiled_angular;
189 next_linear = unprofiled_linear;
Austin Schuh64ebab22015-11-26 13:28:30 -0800190 }
Austin Schuh093535c2016-03-05 23:21:00 -0800191
Austin Schuhba93d9e2016-03-18 22:38:57 -0700192 const double wheel_compensation_offset =
193 gyro_to_wheel_offset * dt_config_.robot_radius;
194 const double scaled_angle_delta =
195 (gyro_to_wheel_offset - last_gyro_to_wheel_offset_) *
196 dt_config_.robot_radius;
Austin Schuh093535c2016-03-05 23:21:00 -0800197
198 kf_->mutable_next_R().block<4, 1>(0, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700199 dt_config_.AngularLinearToLeftRight(next_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800200
201 kf_->mutable_next_R().block<3, 1>(4, 0) =
202 unprofiled_goal_.block<3, 1>(4, 0);
203
Austin Schuhba93d9e2016-03-18 22:38:57 -0700204 kf_->mutable_next_R(0, 0) -= wheel_compensation_offset;
205 kf_->mutable_next_R(2, 0) += wheel_compensation_offset;
206
Austin Schuh093535c2016-03-05 23:21:00 -0800207 if (!use_profile_) {
208 kf_->mutable_R() = kf_->next_R();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700209 } else {
210 kf_->mutable_R(0, 0) -= scaled_angle_delta;
211 kf_->mutable_R(2, 0) += scaled_angle_delta;
Austin Schuh093535c2016-03-05 23:21:00 -0800212 }
213
214 // Run the controller.
215 Eigen::Matrix<double, 2, 1> U = kf_->ControllerOutput();
216
217 kf_->mutable_U_uncapped() = kf_->mutable_U() = U;
218 ScaleCapU(&kf_->mutable_U());
219
220 // Now update the feed forwards.
221 kf_->UpdateFFReference();
222
223 // Now, move the profile if things didn't go perfectly.
224 if (use_profile_ &&
225 (kf_->U() - kf_->U_uncapped()).lpNorm<Eigen::Infinity>() > 1e-4) {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700226 // kf_->R() is in wheel coordinates, while the profile is in absolute
227 // coordinates. Convert back...
Austin Schuhd91c0d22016-10-15 21:24:28 -0700228 linear_profile_.MoveCurrentState(dt_config_.LeftRightToLinear(kf_->R()));
Austin Schuhba93d9e2016-03-18 22:38:57 -0700229
Austin Schuhf257f3c2019-10-27 21:00:43 -0700230 AOS_LOG(DEBUG, "Saturated while moving\n");
Austin Schuhba93d9e2016-03-18 22:38:57 -0700231
232 Eigen::Matrix<double, 2, 1> absolute_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700233 dt_config_.LeftRightToAngular(kf_->R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700234 absolute_angular(0, 0) -= gyro_to_wheel_offset;
235 angular_profile_.MoveCurrentState(absolute_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800236 }
237 } else {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700238 Eigen::Matrix<double, 2, 1> wheel_linear =
239 dt_config_.LeftRightToLinear(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700240 Eigen::Matrix<double, 2, 1> next_angular = wheel_heading;
James Kuszmaul3431d622019-02-17 17:07:44 -0800241 next_angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800242
Austin Schuhba93d9e2016-03-18 22:38:57 -0700243 unprofiled_goal_.block<4, 1>(0, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700244 dt_config_.AngularLinearToLeftRight(wheel_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800245
Austin Schuhd91c0d22016-10-15 21:24:28 -0700246 auto current_linear = dt_config_.LeftRightToLinear(unprofiled_goal_);
247 auto current_angular = dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700248 linear_profile_.MoveCurrentState(current_linear);
249 angular_profile_.MoveCurrentState(current_angular);
250
251 kf_->mutable_next_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
252 kf_->mutable_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
Austin Schuh64ebab22015-11-26 13:28:30 -0800253 }
Austin Schuhba93d9e2016-03-18 22:38:57 -0700254 last_gyro_to_wheel_offset_ = gyro_to_wheel_offset;
Austin Schuh64ebab22015-11-26 13:28:30 -0800255}
256
Austin Schuh093535c2016-03-05 23:21:00 -0800257void DrivetrainMotorsSS::SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000258 ::frc971::control_loops::DrivetrainQueue::Output *output) const {
Austin Schuh64ebab22015-11-26 13:28:30 -0800259 if (output) {
Austin Schuh093535c2016-03-05 23:21:00 -0800260 output->left_voltage = kf_->U(0, 0);
261 output->right_voltage = kf_->U(1, 0);
Austin Schuh64ebab22015-11-26 13:28:30 -0800262 output->left_high = true;
263 output->right_high = true;
264 }
265}
266
Austin Schuh093535c2016-03-05 23:21:00 -0800267void DrivetrainMotorsSS::PopulateStatus(
268 ::frc971::control_loops::DrivetrainQueue::Status *status) const {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700269 Eigen::Matrix<double, 2, 1> profiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700270 dt_config_.LeftRightToLinear(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700271 Eigen::Matrix<double, 2, 1> profiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700272 dt_config_.LeftRightToAngular(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700273
274 profiled_angular(0, 0) -= last_gyro_to_wheel_offset_;
275
276 Eigen::Matrix<double, 4, 1> profiled_gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700277 dt_config_.AngularLinearToLeftRight(profiled_linear, profiled_angular);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700278
279 status->profiled_left_position_goal = profiled_gyro_left_right(0, 0);
280 status->profiled_left_velocity_goal = profiled_gyro_left_right(1, 0);
281 status->profiled_right_position_goal = profiled_gyro_left_right(2, 0);
282 status->profiled_right_velocity_goal = profiled_gyro_left_right(3, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800283}
284
Austin Schuh6197a182015-11-28 16:04:40 -0800285} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -0800286} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000287} // namespace frc971