blob: 594175c5cf4aaa48681b41f63f4bc1538dc633b0 [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"
James Kuszmaul61750662021-06-21 21:32:33 -07004#include "frc971/control_loops/polytope.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08005#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +00006#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
8#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
9#include "frc971/control_loops/drivetrain/drivetrain_status_generated.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()),
James Kuszmaul61750662021-06-21 21:32:33 -070035 linear_profile_(::frc971::controls::kLoopFrequency),
36 angular_profile_(::frc971::controls::kLoopFrequency),
James Kuszmaul3431d622019-02-17 17:07:44 -080037 localizer_(localizer) {
James Kuszmaul61750662021-06-21 21:32:33 -070038 ::frc971::controls::HPolytope<0>::Init();
Austin Schuh06b65b82018-12-23 09:21:44 +110039 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
Brian Silverman4e1b0662016-01-31 18:03:19 -050063 Eigen::Matrix<double, 2, 2> position_K;
Austin Schuh95771d92021-01-23 14:42:25 -080064 position_K << kf_->controller().K(kLeftVoltage, kLeftPosition),
65 kf_->controller().K(kLeftVoltage, kRightPosition),
66 kf_->controller().K(kRightVoltage, kLeftPosition),
67 kf_->controller().K(kRightVoltage, kRightPosition);
Brian Silverman4e1b0662016-01-31 18:03:19 -050068 Eigen::Matrix<double, 2, 2> velocity_K;
Austin Schuh95771d92021-01-23 14:42:25 -080069 velocity_K << kf_->controller().K(kLeftVoltage, kLeftVelocity),
70 kf_->controller().K(kLeftVoltage, kRightVelocity),
71 kf_->controller().K(kRightVoltage, kLeftVelocity),
72 kf_->controller().K(kRightVoltage, kRightVelocity);
Austin Schuh64ebab22015-11-26 13:28:30 -080073
Austin Schuh5be6c522021-01-23 14:43:02 -080074 Eigen::Matrix<double, 2, 2> error_K;
75 error_K << kf_->controller().K(kLeftVoltage, kLeftError), 0.0, 0.0,
76 kf_->controller().K(kRightVoltage, kRightError);
77
Brian Silverman4e1b0662016-01-31 18:03:19 -050078 Eigen::Matrix<double, 2, 1> position_error;
Austin Schuh95771d92021-01-23 14:42:25 -080079 position_error << error(kLeftPosition), error(kRightPosition);
Brian Silverman4e1b0662016-01-31 18:03:19 -050080 // drive_error = [total_distance_error, left_error - right_error]
81 const auto drive_error = T_inverse_ * position_error;
82 Eigen::Matrix<double, 2, 1> velocity_error;
Austin Schuh95771d92021-01-23 14:42:25 -080083 velocity_error << error(kLeftVelocity), error(kRightVelocity);
Austin Schuh093535c2016-03-05 23:21:00 -080084
Austin Schuh5be6c522021-01-23 14:43:02 -080085 Eigen::Matrix<double, 2, 1> U_integral =
86 error_K * Eigen::Matrix<double, 2, 1>(kf_->X_hat(kLeftError),
87 kf_->X_hat(kRightError));
Austin Schuh093535c2016-03-05 23:21:00 -080088
James Kuszmaul61750662021-06-21 21:32:33 -070089 const ::frc971::controls::HVPolytope<2, 4, 4> pos_poly_hv(
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070090 U_poly_.static_H() * position_K * T_,
91 U_poly_.static_H() *
92 (-velocity_K * velocity_error + U_integral - kf_->ff_U()) +
Austin Schuh0aae9242018-03-14 19:49:44 -070093 (U_poly_.static_k() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070094 (position_K * T_).inverse() *
James Kuszmaul61750662021-06-21 21:32:33 -070095 ::frc971::controls::ShiftPoints<2, 4, double>(
Austin Schuh0aae9242018-03-14 19:49:44 -070096 (U_poly_.StaticVertices() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070097 -velocity_K * velocity_error + U_integral - kf_->ff_U()));
Austin Schuh64ebab22015-11-26 13:28:30 -080098
Brian Silverman4e1b0662016-01-31 18:03:19 -050099 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
100 {
101 const auto &P = drive_error;
Austin Schuh64ebab22015-11-26 13:28:30 -0800102
Brian Silverman4e1b0662016-01-31 18:03:19 -0500103 Eigen::Matrix<double, 1, 2> L45;
104 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
105 const double w45 = 0;
Austin Schuh64ebab22015-11-26 13:28:30 -0800106
Brian Silverman4e1b0662016-01-31 18:03:19 -0500107 Eigen::Matrix<double, 1, 2> LH;
108 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
109 LH << 0, 1;
110 } else {
111 LH << 1, 0;
112 }
113 const double wh = LH.dot(P);
Austin Schuh64ebab22015-11-26 13:28:30 -0800114
Brian Silverman4e1b0662016-01-31 18:03:19 -0500115 Eigen::Matrix<double, 2, 2> standard;
116 standard << L45, LH;
117 Eigen::Matrix<double, 2, 1> W;
118 W << w45, wh;
119 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
Austin Schuh64ebab22015-11-26 13:28:30 -0800120
Brian Silverman4e1b0662016-01-31 18:03:19 -0500121 bool is_inside_h, is_inside_45;
122 const auto adjusted_pos_error_h =
Austin Schuhbcce26a2018-03-26 23:41:24 -0700123 DoCoerceGoal<double>(pos_poly_hv, LH, wh, drive_error, &is_inside_h);
Austin Schuhd749d932020-12-30 21:38:40 -0800124 const auto adjusted_pos_error_45 = DoCoerceGoal<double>(
125 pos_poly_hv, L45, w45, intersection, &is_inside_45);
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700126 if (pos_poly_hv.IsInside(intersection)) {
Brian Silverman4e1b0662016-01-31 18:03:19 -0500127 adjusted_pos_error = adjusted_pos_error_h;
128 } else {
129 if (is_inside_h) {
130 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm() ||
131 adjusted_pos_error_45.norm() > intersection.norm()) {
132 adjusted_pos_error = adjusted_pos_error_h;
Austin Schuh64ebab22015-11-26 13:28:30 -0800133 } else {
134 adjusted_pos_error = adjusted_pos_error_45;
135 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500136 } else {
137 adjusted_pos_error = adjusted_pos_error_45;
Austin Schuh64ebab22015-11-26 13:28:30 -0800138 }
139 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500140 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800141
Austin Schuhd749d932020-12-30 21:38:40 -0800142 *U = -U_integral + velocity_K * velocity_error +
143 position_K * T_ * adjusted_pos_error + kf_->ff_U();
Brian Silverman4e1b0662016-01-31 18:03:19 -0500144
145 if (!output_was_capped_) {
Austin Schuh093535c2016-03-05 23:21:00 -0800146 if ((*U - kf_->U_uncapped()).norm() > 0.0001) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700147 AOS_LOG(FATAL, "U unnecessarily capped\n");
Brian Silverman4e1b0662016-01-31 18:03:19 -0500148 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800149 }
150}
151
Austin Schuh093535c2016-03-05 23:21:00 -0800152void DrivetrainMotorsSS::SetGoal(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 const ::frc971::control_loops::drivetrain::Goal *goal) {
154 unprofiled_goal_ << goal->left_goal(), 0.0, goal->right_goal(), 0.0, 0.0, 0.0,
155 0.0;
156 if (!goal->has_max_ss_voltage()) {
Austin Schuh0aae9242018-03-14 19:49:44 -0700157 max_voltage_ = kMaxVoltage;
158 } else {
Milind Upadhyay10143452020-12-19 17:25:48 -0800159 max_voltage_ = goal->max_ss_voltage();
Austin Schuh0aae9242018-03-14 19:49:44 -0700160 }
Austin Schuh093535c2016-03-05 23:21:00 -0800161
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 use_profile_ = !kf_->controller().Kff().isZero(0) &&
163 (goal->has_linear() && goal->has_angular() &&
164 goal->linear()->has_max_velocity() &&
165 goal->linear()->has_max_acceleration() &&
166 goal->angular()->has_max_velocity() &&
167 goal->angular()->has_max_acceleration());
168 if (goal->has_linear()) {
169 linear_profile_.set_maximum_velocity(goal->linear()->max_velocity());
170 linear_profile_.set_maximum_acceleration(
171 goal->linear()->max_acceleration());
172 }
173 if (goal->has_angular()) {
174 angular_profile_.set_maximum_velocity(goal->angular()->max_velocity());
175 angular_profile_.set_maximum_acceleration(
176 goal->angular()->max_acceleration());
177 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800178}
179
Austin Schuh093535c2016-03-05 23:21:00 -0800180void DrivetrainMotorsSS::Update(bool enable_control_loop) {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700181 Eigen::Matrix<double, 2, 1> wheel_heading =
182 dt_config_.LeftRightToAngular(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700183
James Kuszmaul3431d622019-02-17 17:07:44 -0800184 const double gyro_to_wheel_offset = wheel_heading(0, 0) - localizer_->theta();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700185
Austin Schuh64ebab22015-11-26 13:28:30 -0800186 if (enable_control_loop) {
Austin Schuh093535c2016-03-05 23:21:00 -0800187 // Update profiles.
188 Eigen::Matrix<double, 2, 1> unprofiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700189 dt_config_.LeftRightToLinear(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800190 Eigen::Matrix<double, 2, 1> unprofiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700191 dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800192
193 Eigen::Matrix<double, 2, 1> next_linear;
194 Eigen::Matrix<double, 2, 1> next_angular;
195
196 if (use_profile_) {
197 next_linear = linear_profile_.Update(unprofiled_linear(0, 0),
198 unprofiled_linear(1, 0));
199 next_angular = angular_profile_.Update(unprofiled_angular(0, 0),
200 unprofiled_angular(1, 0));
Austin Schuh093535c2016-03-05 23:21:00 -0800201 } else {
202 next_angular = unprofiled_angular;
203 next_linear = unprofiled_linear;
Austin Schuh64ebab22015-11-26 13:28:30 -0800204 }
Austin Schuh093535c2016-03-05 23:21:00 -0800205
Austin Schuhba93d9e2016-03-18 22:38:57 -0700206 const double wheel_compensation_offset =
207 gyro_to_wheel_offset * dt_config_.robot_radius;
208 const double scaled_angle_delta =
209 (gyro_to_wheel_offset - last_gyro_to_wheel_offset_) *
210 dt_config_.robot_radius;
Austin Schuh093535c2016-03-05 23:21:00 -0800211
Austin Schuh95771d92021-01-23 14:42:25 -0800212 kf_->mutable_next_R().block<4, 1>(kLeftPosition, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700213 dt_config_.AngularLinearToLeftRight(next_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800214
Austin Schuh95771d92021-01-23 14:42:25 -0800215 kf_->mutable_next_R().block<3, 1>(kLeftError, 0) =
216 unprofiled_goal_.block<3, 1>(kLeftError, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800217
Austin Schuhba93d9e2016-03-18 22:38:57 -0700218 kf_->mutable_next_R(0, 0) -= wheel_compensation_offset;
219 kf_->mutable_next_R(2, 0) += wheel_compensation_offset;
220
Austin Schuh093535c2016-03-05 23:21:00 -0800221 if (!use_profile_) {
222 kf_->mutable_R() = kf_->next_R();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700223 } else {
224 kf_->mutable_R(0, 0) -= scaled_angle_delta;
225 kf_->mutable_R(2, 0) += scaled_angle_delta;
Austin Schuh093535c2016-03-05 23:21:00 -0800226 }
227
228 // Run the controller.
229 Eigen::Matrix<double, 2, 1> U = kf_->ControllerOutput();
230
231 kf_->mutable_U_uncapped() = kf_->mutable_U() = U;
232 ScaleCapU(&kf_->mutable_U());
233
234 // Now update the feed forwards.
235 kf_->UpdateFFReference();
236
237 // Now, move the profile if things didn't go perfectly.
238 if (use_profile_ &&
239 (kf_->U() - kf_->U_uncapped()).lpNorm<Eigen::Infinity>() > 1e-4) {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700240 // kf_->R() is in wheel coordinates, while the profile is in absolute
241 // coordinates. Convert back...
Austin Schuhd91c0d22016-10-15 21:24:28 -0700242 linear_profile_.MoveCurrentState(dt_config_.LeftRightToLinear(kf_->R()));
Austin Schuhba93d9e2016-03-18 22:38:57 -0700243
Austin Schuhf257f3c2019-10-27 21:00:43 -0700244 AOS_LOG(DEBUG, "Saturated while moving\n");
Austin Schuhba93d9e2016-03-18 22:38:57 -0700245
246 Eigen::Matrix<double, 2, 1> absolute_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700247 dt_config_.LeftRightToAngular(kf_->R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700248 absolute_angular(0, 0) -= gyro_to_wheel_offset;
249 angular_profile_.MoveCurrentState(absolute_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800250 }
251 } else {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700252 Eigen::Matrix<double, 2, 1> wheel_linear =
253 dt_config_.LeftRightToLinear(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700254 Eigen::Matrix<double, 2, 1> next_angular = wheel_heading;
James Kuszmaul3431d622019-02-17 17:07:44 -0800255 next_angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800256
Austin Schuhba93d9e2016-03-18 22:38:57 -0700257 unprofiled_goal_.block<4, 1>(0, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700258 dt_config_.AngularLinearToLeftRight(wheel_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800259
Austin Schuhd91c0d22016-10-15 21:24:28 -0700260 auto current_linear = dt_config_.LeftRightToLinear(unprofiled_goal_);
261 auto current_angular = dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700262 linear_profile_.MoveCurrentState(current_linear);
263 angular_profile_.MoveCurrentState(current_angular);
264
265 kf_->mutable_next_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
266 kf_->mutable_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
Austin Schuh64ebab22015-11-26 13:28:30 -0800267 }
Austin Schuhba93d9e2016-03-18 22:38:57 -0700268 last_gyro_to_wheel_offset_ = gyro_to_wheel_offset;
Austin Schuh64ebab22015-11-26 13:28:30 -0800269}
270
Austin Schuh093535c2016-03-05 23:21:00 -0800271void DrivetrainMotorsSS::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700272 ::frc971::control_loops::drivetrain::OutputT *output) const {
Austin Schuh64ebab22015-11-26 13:28:30 -0800273 if (output) {
Austin Schuh95771d92021-01-23 14:42:25 -0800274 output->left_voltage = kf_->U(kLeftVoltage);
275 output->right_voltage = kf_->U(kRightVoltage);
Austin Schuh64ebab22015-11-26 13:28:30 -0800276 output->left_high = true;
277 output->right_high = true;
278 }
279}
280
Austin Schuh093535c2016-03-05 23:21:00 -0800281void DrivetrainMotorsSS::PopulateStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282 ::frc971::control_loops::drivetrain::StatusBuilder *builder) const {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700283 Eigen::Matrix<double, 2, 1> profiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700284 dt_config_.LeftRightToLinear(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700285 Eigen::Matrix<double, 2, 1> profiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700286 dt_config_.LeftRightToAngular(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700287
288 profiled_angular(0, 0) -= last_gyro_to_wheel_offset_;
289
290 Eigen::Matrix<double, 4, 1> profiled_gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700291 dt_config_.AngularLinearToLeftRight(profiled_linear, profiled_angular);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700292
Austin Schuh95771d92021-01-23 14:42:25 -0800293 builder->add_profiled_left_position_goal(
294 profiled_gyro_left_right(kLeftPosition));
295 builder->add_profiled_left_velocity_goal(
296 profiled_gyro_left_right(kLeftVelocity));
297 builder->add_profiled_right_position_goal(
298 profiled_gyro_left_right(kRightPosition));
299 builder->add_profiled_right_velocity_goal(
300 profiled_gyro_left_right(kRightVelocity));
Austin Schuh093535c2016-03-05 23:21:00 -0800301}
302
Austin Schuh6197a182015-11-28 16:04:40 -0800303} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -0800304} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000305} // namespace frc971