blob: 3e58d091b95f3ce8469721717cd7b8000b65cfb7 [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"
Austin Schuh64ebab22015-11-26 13:28:30 -08005
Austin Schuh64ebab22015-11-26 13:28:30 -08006#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +00007#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
9#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
10#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh0aae9242018-03-14 19:49:44 -070011#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080012
Comran Morshed5323ecb2015-12-26 20:50:55 +000013namespace frc971 {
Austin Schuh64ebab22015-11-26 13:28:30 -080014namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080015namespace drivetrain {
16
Austin Schuh06b65b82018-12-23 09:21:44 +110017DrivetrainMotorsSS::DrivetrainMotorsSS(
18 const DrivetrainConfig<double> &dt_config, StateFeedbackLoop<7, 2, 4> *kf,
James Kuszmaul3431d622019-02-17 17:07:44 -080019 LocalizerInterface *localizer)
Austin Schuh06b65b82018-12-23 09:21:44 +110020 : dt_config_(dt_config),
21 kf_(kf),
22 U_poly_(
23 (Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/,
24 /*[*/ -1, 0 /*]*/,
25 /*[*/ 0, 1 /*]*/,
26 /*[*/ 0, -1 /*]]*/)
27 .finished(),
28 (Eigen::Matrix<double, 4, 1>() << /*[[*/ 1.0 /*]*/,
29 /*[*/ 1.0 /*]*/,
30 /*[*/ 1.0 /*]*/,
31 /*[*/ 1.0 /*]]*/)
32 .finished(),
33 (Eigen::Matrix<double, 2, 4>() << /*[[*/ 1.0, 1.0, -1.0, -1.0 /*]*/,
34 /*[*/ -1.0, 1.0, 1.0, -1.0 /*]*/)
35 .finished()),
36 linear_profile_(::aos::controls::kLoopFrequency),
37 angular_profile_(::aos::controls::kLoopFrequency),
James Kuszmaul3431d622019-02-17 17:07:44 -080038 localizer_(localizer) {
Austin Schuh06b65b82018-12-23 09:21:44 +110039 ::aos::controls::HPolytope<0>::Init();
40 T_ << 1, 1, 1, -1;
41 T_inverse_ = T_.inverse();
42 unprofiled_goal_.setZero();
43}
44
Austin Schuh093535c2016-03-05 23:21:00 -080045void DrivetrainMotorsSS::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Austin Schuh0aae9242018-03-14 19:49:44 -070046 output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ ||
47 ::std::abs((*U)(1, 0)) > max_voltage_;
Austin Schuh093535c2016-03-05 23:21:00 -080048
49 if (output_was_capped_) {
Austin Schuh0aae9242018-03-14 19:49:44 -070050 *U *= max_voltage_ / kf_->U_uncapped().lpNorm<Eigen::Infinity>();
Austin Schuh093535c2016-03-05 23:21:00 -080051 }
Austin Schuh64ebab22015-11-26 13:28:30 -080052}
53
Brian Silverman4e1b0662016-01-31 18:03:19 -050054// This intentionally runs the U-capping code even when it's unnecessary to help
55// make it more deterministic. Only running it when one or both sides want
56// out-of-range voltages could lead to things like running out of CPU under
57// certain situations, which would be bad.
Austin Schuh093535c2016-03-05 23:21:00 -080058void DrivetrainMotorsSS::PolyCapU(Eigen::Matrix<double, 2, 1> *U) {
Austin Schuh0aae9242018-03-14 19:49:44 -070059 output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ ||
60 ::std::abs((*U)(1, 0)) > max_voltage_;
Brian Silverman4e1b0662016-01-31 18:03:19 -050061
Austin Schuh093535c2016-03-05 23:21:00 -080062 const Eigen::Matrix<double, 7, 1> error = kf_->R() - kf_->X_hat();
Austin Schuh64ebab22015-11-26 13:28:30 -080063
Brian Silverman4e1b0662016-01-31 18:03:19 -050064 Eigen::Matrix<double, 2, 2> position_K;
Austin Schuh95771d92021-01-23 14:42:25 -080065 position_K << kf_->controller().K(kLeftVoltage, kLeftPosition),
66 kf_->controller().K(kLeftVoltage, kRightPosition),
67 kf_->controller().K(kRightVoltage, kLeftPosition),
68 kf_->controller().K(kRightVoltage, kRightPosition);
Brian Silverman4e1b0662016-01-31 18:03:19 -050069 Eigen::Matrix<double, 2, 2> velocity_K;
Austin Schuh95771d92021-01-23 14:42:25 -080070 velocity_K << kf_->controller().K(kLeftVoltage, kLeftVelocity),
71 kf_->controller().K(kLeftVoltage, kRightVelocity),
72 kf_->controller().K(kRightVoltage, kLeftVelocity),
73 kf_->controller().K(kRightVoltage, kRightVelocity);
Austin Schuh64ebab22015-11-26 13:28:30 -080074
Austin Schuh5be6c522021-01-23 14:43:02 -080075 Eigen::Matrix<double, 2, 2> error_K;
76 error_K << kf_->controller().K(kLeftVoltage, kLeftError), 0.0, 0.0,
77 kf_->controller().K(kRightVoltage, kRightError);
78
Brian Silverman4e1b0662016-01-31 18:03:19 -050079 Eigen::Matrix<double, 2, 1> position_error;
Austin Schuh95771d92021-01-23 14:42:25 -080080 position_error << error(kLeftPosition), error(kRightPosition);
Brian Silverman4e1b0662016-01-31 18:03:19 -050081 // drive_error = [total_distance_error, left_error - right_error]
82 const auto drive_error = T_inverse_ * position_error;
83 Eigen::Matrix<double, 2, 1> velocity_error;
Austin Schuh95771d92021-01-23 14:42:25 -080084 velocity_error << error(kLeftVelocity), error(kRightVelocity);
Austin Schuh093535c2016-03-05 23:21:00 -080085
Austin Schuh5be6c522021-01-23 14:43:02 -080086 Eigen::Matrix<double, 2, 1> U_integral =
87 error_K * Eigen::Matrix<double, 2, 1>(kf_->X_hat(kLeftError),
88 kf_->X_hat(kRightError));
Austin Schuh093535c2016-03-05 23:21:00 -080089
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070090 const ::aos::controls::HVPolytope<2, 4, 4> pos_poly_hv(
91 U_poly_.static_H() * position_K * T_,
92 U_poly_.static_H() *
93 (-velocity_K * velocity_error + U_integral - kf_->ff_U()) +
Austin Schuh0aae9242018-03-14 19:49:44 -070094 (U_poly_.static_k() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070095 (position_K * T_).inverse() *
Austin Schuhbcce26a2018-03-26 23:41:24 -070096 ::aos::controls::ShiftPoints<2, 4, double>(
Austin Schuh0aae9242018-03-14 19:49:44 -070097 (U_poly_.StaticVertices() * max_voltage_),
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070098 -velocity_K * velocity_error + U_integral - kf_->ff_U()));
Austin Schuh64ebab22015-11-26 13:28:30 -080099
Brian Silverman4e1b0662016-01-31 18:03:19 -0500100 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
101 {
102 const auto &P = drive_error;
Austin Schuh64ebab22015-11-26 13:28:30 -0800103
Brian Silverman4e1b0662016-01-31 18:03:19 -0500104 Eigen::Matrix<double, 1, 2> L45;
105 L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
106 const double w45 = 0;
Austin Schuh64ebab22015-11-26 13:28:30 -0800107
Brian Silverman4e1b0662016-01-31 18:03:19 -0500108 Eigen::Matrix<double, 1, 2> LH;
109 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
110 LH << 0, 1;
111 } else {
112 LH << 1, 0;
113 }
114 const double wh = LH.dot(P);
Austin Schuh64ebab22015-11-26 13:28:30 -0800115
Brian Silverman4e1b0662016-01-31 18:03:19 -0500116 Eigen::Matrix<double, 2, 2> standard;
117 standard << L45, LH;
118 Eigen::Matrix<double, 2, 1> W;
119 W << w45, wh;
120 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
Austin Schuh64ebab22015-11-26 13:28:30 -0800121
Brian Silverman4e1b0662016-01-31 18:03:19 -0500122 bool is_inside_h, is_inside_45;
123 const auto adjusted_pos_error_h =
Austin Schuhbcce26a2018-03-26 23:41:24 -0700124 DoCoerceGoal<double>(pos_poly_hv, LH, wh, drive_error, &is_inside_h);
Austin Schuhd749d932020-12-30 21:38:40 -0800125 const auto adjusted_pos_error_45 = DoCoerceGoal<double>(
126 pos_poly_hv, L45, w45, intersection, &is_inside_45);
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700127 if (pos_poly_hv.IsInside(intersection)) {
Brian Silverman4e1b0662016-01-31 18:03:19 -0500128 adjusted_pos_error = adjusted_pos_error_h;
129 } else {
130 if (is_inside_h) {
131 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm() ||
132 adjusted_pos_error_45.norm() > intersection.norm()) {
133 adjusted_pos_error = adjusted_pos_error_h;
Austin Schuh64ebab22015-11-26 13:28:30 -0800134 } else {
135 adjusted_pos_error = adjusted_pos_error_45;
136 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500137 } else {
138 adjusted_pos_error = adjusted_pos_error_45;
Austin Schuh64ebab22015-11-26 13:28:30 -0800139 }
140 }
Brian Silverman4e1b0662016-01-31 18:03:19 -0500141 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800142
Austin Schuhd749d932020-12-30 21:38:40 -0800143 *U = -U_integral + velocity_K * velocity_error +
144 position_K * T_ * adjusted_pos_error + kf_->ff_U();
Brian Silverman4e1b0662016-01-31 18:03:19 -0500145
146 if (!output_was_capped_) {
Austin Schuh093535c2016-03-05 23:21:00 -0800147 if ((*U - kf_->U_uncapped()).norm() > 0.0001) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700148 AOS_LOG(FATAL, "U unnecessarily capped\n");
Brian Silverman4e1b0662016-01-31 18:03:19 -0500149 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800150 }
151}
152
Austin Schuh093535c2016-03-05 23:21:00 -0800153void DrivetrainMotorsSS::SetGoal(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154 const ::frc971::control_loops::drivetrain::Goal *goal) {
155 unprofiled_goal_ << goal->left_goal(), 0.0, goal->right_goal(), 0.0, 0.0, 0.0,
156 0.0;
157 if (!goal->has_max_ss_voltage()) {
Austin Schuh0aae9242018-03-14 19:49:44 -0700158 max_voltage_ = kMaxVoltage;
159 } else {
Milind Upadhyay10143452020-12-19 17:25:48 -0800160 max_voltage_ = goal->max_ss_voltage();
Austin Schuh0aae9242018-03-14 19:49:44 -0700161 }
Austin Schuh093535c2016-03-05 23:21:00 -0800162
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 use_profile_ = !kf_->controller().Kff().isZero(0) &&
164 (goal->has_linear() && goal->has_angular() &&
165 goal->linear()->has_max_velocity() &&
166 goal->linear()->has_max_acceleration() &&
167 goal->angular()->has_max_velocity() &&
168 goal->angular()->has_max_acceleration());
169 if (goal->has_linear()) {
170 linear_profile_.set_maximum_velocity(goal->linear()->max_velocity());
171 linear_profile_.set_maximum_acceleration(
172 goal->linear()->max_acceleration());
173 }
174 if (goal->has_angular()) {
175 angular_profile_.set_maximum_velocity(goal->angular()->max_velocity());
176 angular_profile_.set_maximum_acceleration(
177 goal->angular()->max_acceleration());
178 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800179}
180
Austin Schuh093535c2016-03-05 23:21:00 -0800181void DrivetrainMotorsSS::Update(bool enable_control_loop) {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700182 Eigen::Matrix<double, 2, 1> wheel_heading =
183 dt_config_.LeftRightToAngular(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700184
James Kuszmaul3431d622019-02-17 17:07:44 -0800185 const double gyro_to_wheel_offset = wheel_heading(0, 0) - localizer_->theta();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700186
Austin Schuh64ebab22015-11-26 13:28:30 -0800187 if (enable_control_loop) {
Austin Schuh093535c2016-03-05 23:21:00 -0800188 // Update profiles.
189 Eigen::Matrix<double, 2, 1> unprofiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700190 dt_config_.LeftRightToLinear(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800191 Eigen::Matrix<double, 2, 1> unprofiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700192 dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuh093535c2016-03-05 23:21:00 -0800193
194 Eigen::Matrix<double, 2, 1> next_linear;
195 Eigen::Matrix<double, 2, 1> next_angular;
196
197 if (use_profile_) {
198 next_linear = linear_profile_.Update(unprofiled_linear(0, 0),
199 unprofiled_linear(1, 0));
200 next_angular = angular_profile_.Update(unprofiled_angular(0, 0),
201 unprofiled_angular(1, 0));
Austin Schuh093535c2016-03-05 23:21:00 -0800202 } else {
203 next_angular = unprofiled_angular;
204 next_linear = unprofiled_linear;
Austin Schuh64ebab22015-11-26 13:28:30 -0800205 }
Austin Schuh093535c2016-03-05 23:21:00 -0800206
Austin Schuhba93d9e2016-03-18 22:38:57 -0700207 const double wheel_compensation_offset =
208 gyro_to_wheel_offset * dt_config_.robot_radius;
209 const double scaled_angle_delta =
210 (gyro_to_wheel_offset - last_gyro_to_wheel_offset_) *
211 dt_config_.robot_radius;
Austin Schuh093535c2016-03-05 23:21:00 -0800212
Austin Schuh95771d92021-01-23 14:42:25 -0800213 kf_->mutable_next_R().block<4, 1>(kLeftPosition, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700214 dt_config_.AngularLinearToLeftRight(next_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800215
Austin Schuh95771d92021-01-23 14:42:25 -0800216 kf_->mutable_next_R().block<3, 1>(kLeftError, 0) =
217 unprofiled_goal_.block<3, 1>(kLeftError, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800218
Austin Schuhba93d9e2016-03-18 22:38:57 -0700219 kf_->mutable_next_R(0, 0) -= wheel_compensation_offset;
220 kf_->mutable_next_R(2, 0) += wheel_compensation_offset;
221
Austin Schuh093535c2016-03-05 23:21:00 -0800222 if (!use_profile_) {
223 kf_->mutable_R() = kf_->next_R();
Austin Schuhba93d9e2016-03-18 22:38:57 -0700224 } else {
225 kf_->mutable_R(0, 0) -= scaled_angle_delta;
226 kf_->mutable_R(2, 0) += scaled_angle_delta;
Austin Schuh093535c2016-03-05 23:21:00 -0800227 }
228
229 // Run the controller.
230 Eigen::Matrix<double, 2, 1> U = kf_->ControllerOutput();
231
232 kf_->mutable_U_uncapped() = kf_->mutable_U() = U;
233 ScaleCapU(&kf_->mutable_U());
234
235 // Now update the feed forwards.
236 kf_->UpdateFFReference();
237
238 // Now, move the profile if things didn't go perfectly.
239 if (use_profile_ &&
240 (kf_->U() - kf_->U_uncapped()).lpNorm<Eigen::Infinity>() > 1e-4) {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700241 // kf_->R() is in wheel coordinates, while the profile is in absolute
242 // coordinates. Convert back...
Austin Schuhd91c0d22016-10-15 21:24:28 -0700243 linear_profile_.MoveCurrentState(dt_config_.LeftRightToLinear(kf_->R()));
Austin Schuhba93d9e2016-03-18 22:38:57 -0700244
Austin Schuhf257f3c2019-10-27 21:00:43 -0700245 AOS_LOG(DEBUG, "Saturated while moving\n");
Austin Schuhba93d9e2016-03-18 22:38:57 -0700246
247 Eigen::Matrix<double, 2, 1> absolute_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700248 dt_config_.LeftRightToAngular(kf_->R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700249 absolute_angular(0, 0) -= gyro_to_wheel_offset;
250 angular_profile_.MoveCurrentState(absolute_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800251 }
252 } else {
Austin Schuhd91c0d22016-10-15 21:24:28 -0700253 Eigen::Matrix<double, 2, 1> wheel_linear =
254 dt_config_.LeftRightToLinear(kf_->X_hat());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700255 Eigen::Matrix<double, 2, 1> next_angular = wheel_heading;
James Kuszmaul3431d622019-02-17 17:07:44 -0800256 next_angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800257
Austin Schuhba93d9e2016-03-18 22:38:57 -0700258 unprofiled_goal_.block<4, 1>(0, 0) =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700259 dt_config_.AngularLinearToLeftRight(wheel_linear, next_angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800260
Austin Schuhd91c0d22016-10-15 21:24:28 -0700261 auto current_linear = dt_config_.LeftRightToLinear(unprofiled_goal_);
262 auto current_angular = dt_config_.LeftRightToAngular(unprofiled_goal_);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700263 linear_profile_.MoveCurrentState(current_linear);
264 angular_profile_.MoveCurrentState(current_angular);
265
266 kf_->mutable_next_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
267 kf_->mutable_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0);
Austin Schuh64ebab22015-11-26 13:28:30 -0800268 }
Austin Schuhba93d9e2016-03-18 22:38:57 -0700269 last_gyro_to_wheel_offset_ = gyro_to_wheel_offset;
Austin Schuh64ebab22015-11-26 13:28:30 -0800270}
271
Austin Schuh093535c2016-03-05 23:21:00 -0800272void DrivetrainMotorsSS::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 ::frc971::control_loops::drivetrain::OutputT *output) const {
Austin Schuh64ebab22015-11-26 13:28:30 -0800274 if (output) {
Austin Schuh95771d92021-01-23 14:42:25 -0800275 output->left_voltage = kf_->U(kLeftVoltage);
276 output->right_voltage = kf_->U(kRightVoltage);
Austin Schuh64ebab22015-11-26 13:28:30 -0800277 output->left_high = true;
278 output->right_high = true;
279 }
280}
281
Austin Schuh093535c2016-03-05 23:21:00 -0800282void DrivetrainMotorsSS::PopulateStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 ::frc971::control_loops::drivetrain::StatusBuilder *builder) const {
Austin Schuhba93d9e2016-03-18 22:38:57 -0700284 Eigen::Matrix<double, 2, 1> profiled_linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700285 dt_config_.LeftRightToLinear(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700286 Eigen::Matrix<double, 2, 1> profiled_angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700287 dt_config_.LeftRightToAngular(kf_->next_R());
Austin Schuhba93d9e2016-03-18 22:38:57 -0700288
289 profiled_angular(0, 0) -= last_gyro_to_wheel_offset_;
290
291 Eigen::Matrix<double, 4, 1> profiled_gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700292 dt_config_.AngularLinearToLeftRight(profiled_linear, profiled_angular);
Austin Schuhba93d9e2016-03-18 22:38:57 -0700293
Austin Schuh95771d92021-01-23 14:42:25 -0800294 builder->add_profiled_left_position_goal(
295 profiled_gyro_left_right(kLeftPosition));
296 builder->add_profiled_left_velocity_goal(
297 profiled_gyro_left_right(kLeftVelocity));
298 builder->add_profiled_right_position_goal(
299 profiled_gyro_left_right(kRightPosition));
300 builder->add_profiled_right_velocity_goal(
301 profiled_gyro_left_right(kRightVelocity));
Austin Schuh093535c2016-03-05 23:21:00 -0800302}
303
Austin Schuh6197a182015-11-28 16:04:40 -0800304} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -0800305} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000306} // namespace frc971