Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 2 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 3 | #include "aos/common/commonmath.h" |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 4 | #include "aos/common/controls/polytope.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 5 | #include "aos/common/logging/matrix_logging.h" |
| 6 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/coerce_goal.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 8 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 9 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/state_feedback_loop.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 11 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 12 | namespace frc971 { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 13 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 14 | namespace drivetrain { |
| 15 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 16 | void DrivetrainMotorsSS::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 17 | output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ || |
| 18 | ::std::abs((*U)(1, 0)) > max_voltage_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 19 | |
| 20 | if (output_was_capped_) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 21 | *U *= max_voltage_ / kf_->U_uncapped().lpNorm<Eigen::Infinity>(); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 22 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 25 | // 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 Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 29 | void DrivetrainMotorsSS::PolyCapU(Eigen::Matrix<double, 2, 1> *U) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 30 | output_was_capped_ = ::std::abs((*U)(0, 0)) > max_voltage_ || |
| 31 | ::std::abs((*U)(1, 0)) > max_voltage_; |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 32 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 33 | const Eigen::Matrix<double, 7, 1> error = kf_->R() - kf_->X_hat(); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 34 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 35 | LOG_MATRIX(DEBUG, "U_uncapped", *U); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 36 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 37 | Eigen::Matrix<double, 2, 2> position_K; |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 38 | position_K << kf_->controller().K(0, 0), kf_->controller().K(0, 2), |
| 39 | kf_->controller().K(1, 0), kf_->controller().K(1, 2); |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 40 | Eigen::Matrix<double, 2, 2> velocity_K; |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 41 | velocity_K << kf_->controller().K(0, 1), kf_->controller().K(0, 3), |
| 42 | kf_->controller().K(1, 1), kf_->controller().K(1, 3); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 43 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 44 | 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 Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 52 | |
| 53 | Eigen::Matrix<double, 2, 1> U_integral; |
| 54 | U_integral << kf_->X_hat(4, 0), kf_->X_hat(5, 0); |
| 55 | |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 56 | 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 Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 60 | (U_poly_.static_k() * max_voltage_), |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 61 | (position_K * T_).inverse() * |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 62 | ::aos::controls::ShiftPoints<2, 4, double>( |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 63 | (U_poly_.StaticVertices() * max_voltage_), |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 64 | -velocity_K * velocity_error + U_integral - kf_->ff_U())); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 65 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 66 | Eigen::Matrix<double, 2, 1> adjusted_pos_error; |
| 67 | { |
| 68 | const auto &P = drive_error; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 69 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 70 | Eigen::Matrix<double, 1, 2> L45; |
| 71 | L45 << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0)); |
| 72 | const double w45 = 0; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 73 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 74 | 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 Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 81 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 82 | 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 Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 87 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 88 | bool is_inside_h, is_inside_45; |
| 89 | const auto adjusted_pos_error_h = |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 90 | DoCoerceGoal<double>(pos_poly_hv, LH, wh, drive_error, &is_inside_h); |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 91 | const auto adjusted_pos_error_45 = |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 92 | DoCoerceGoal<double>(pos_poly_hv, L45, w45, intersection, &is_inside_45); |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 93 | if (pos_poly_hv.IsInside(intersection)) { |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 94 | 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 Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 100 | } else { |
| 101 | adjusted_pos_error = adjusted_pos_error_45; |
| 102 | } |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 103 | } else { |
| 104 | adjusted_pos_error = adjusted_pos_error_45; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 107 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 108 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 109 | *U = -U_integral + velocity_K *velocity_error + |
| 110 | position_K *T_ *adjusted_pos_error + kf_->ff_U(); |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 111 | |
| 112 | if (!output_was_capped_) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 113 | if ((*U - kf_->U_uncapped()).norm() > 0.0001) { |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 114 | LOG(FATAL, "U unnecessarily capped\n"); |
| 115 | } |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 119 | DrivetrainMotorsSS::DrivetrainMotorsSS( |
| 120 | const DrivetrainConfig<double> &dt_config, StateFeedbackLoop<7, 2, 4> *kf, |
| 121 | double *integrated_kf_heading) |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 122 | : dt_config_(dt_config), |
| 123 | kf_(kf), |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 124 | U_poly_( |
| 125 | (Eigen::Matrix<double, 4, 2>() << /*[[*/ 1, 0 /*]*/, |
| 126 | /*[*/ -1, 0 /*]*/, |
| 127 | /*[*/ 0, 1 /*]*/, |
| 128 | /*[*/ 0, -1 /*]]*/) |
| 129 | .finished(), |
| 130 | (Eigen::Matrix<double, 4, 1>() << /*[[*/ 1.0 /*]*/, |
| 131 | /*[*/ 1.0 /*]*/, |
| 132 | /*[*/ 1.0 /*]*/, |
| 133 | /*[*/ 1.0 /*]]*/) |
| 134 | .finished(), |
| 135 | (Eigen::Matrix<double, 2, 4>() << /*[[*/ 1.0, 1.0, -1.0, -1.0 /*]*/, |
| 136 | /*[*/ -1.0, 1.0, 1.0, -1.0 /*]*/) |
| 137 | .finished()), |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 138 | linear_profile_(::aos::controls::kLoopFrequency), |
| 139 | angular_profile_(::aos::controls::kLoopFrequency), |
| 140 | integrated_kf_heading_(integrated_kf_heading) { |
| 141 | ::aos::controls::HPolytope<0>::Init(); |
| 142 | T_ << 1, 1, 1, -1; |
| 143 | T_inverse_ = T_.inverse(); |
| 144 | unprofiled_goal_.setZero(); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 147 | void DrivetrainMotorsSS::SetGoal( |
| 148 | const ::frc971::control_loops::DrivetrainQueue::Goal &goal) { |
| 149 | unprofiled_goal_ << goal.left_goal, goal.left_velocity_goal, goal.right_goal, |
| 150 | goal.right_velocity_goal, 0.0, 0.0, 0.0; |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 151 | if (::std::abs(goal.max_ss_voltage) < 0.01) { |
| 152 | max_voltage_ = kMaxVoltage; |
| 153 | } else { |
| 154 | max_voltage_ = goal.max_ss_voltage; |
| 155 | } |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 156 | |
| 157 | use_profile_ = |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 158 | !kf_->controller().Kff().isZero(0) && |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 159 | (goal.linear.max_velocity != 0.0 && goal.linear.max_acceleration != 0.0 && |
| 160 | goal.angular.max_velocity != 0.0 && |
| 161 | goal.angular.max_acceleration != 0.0); |
| 162 | linear_profile_.set_maximum_velocity(goal.linear.max_velocity); |
| 163 | linear_profile_.set_maximum_acceleration(goal.linear.max_acceleration); |
| 164 | angular_profile_.set_maximum_velocity(goal.angular.max_velocity); |
| 165 | angular_profile_.set_maximum_acceleration(goal.angular.max_acceleration); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 168 | void DrivetrainMotorsSS::Update(bool enable_control_loop) { |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 169 | Eigen::Matrix<double, 2, 1> wheel_heading = |
| 170 | dt_config_.LeftRightToAngular(kf_->X_hat()); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 171 | |
| 172 | const double gyro_to_wheel_offset = |
| 173 | wheel_heading(0, 0) - *integrated_kf_heading_; |
| 174 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 175 | if (enable_control_loop) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 176 | // Update profiles. |
| 177 | Eigen::Matrix<double, 2, 1> unprofiled_linear = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 178 | dt_config_.LeftRightToLinear(unprofiled_goal_); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 179 | Eigen::Matrix<double, 2, 1> unprofiled_angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 180 | dt_config_.LeftRightToAngular(unprofiled_goal_); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 181 | |
| 182 | Eigen::Matrix<double, 2, 1> next_linear; |
| 183 | Eigen::Matrix<double, 2, 1> next_angular; |
| 184 | |
| 185 | if (use_profile_) { |
| 186 | next_linear = linear_profile_.Update(unprofiled_linear(0, 0), |
| 187 | unprofiled_linear(1, 0)); |
| 188 | next_angular = angular_profile_.Update(unprofiled_angular(0, 0), |
| 189 | unprofiled_angular(1, 0)); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 190 | } else { |
| 191 | next_angular = unprofiled_angular; |
| 192 | next_linear = unprofiled_linear; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 193 | } |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 194 | |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 195 | const double wheel_compensation_offset = |
| 196 | gyro_to_wheel_offset * dt_config_.robot_radius; |
| 197 | const double scaled_angle_delta = |
| 198 | (gyro_to_wheel_offset - last_gyro_to_wheel_offset_) * |
| 199 | dt_config_.robot_radius; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 200 | |
| 201 | kf_->mutable_next_R().block<4, 1>(0, 0) = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 202 | dt_config_.AngularLinearToLeftRight(next_linear, next_angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 203 | |
| 204 | kf_->mutable_next_R().block<3, 1>(4, 0) = |
| 205 | unprofiled_goal_.block<3, 1>(4, 0); |
| 206 | |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 207 | kf_->mutable_next_R(0, 0) -= wheel_compensation_offset; |
| 208 | kf_->mutable_next_R(2, 0) += wheel_compensation_offset; |
| 209 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 210 | if (!use_profile_) { |
| 211 | kf_->mutable_R() = kf_->next_R(); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 212 | } else { |
| 213 | kf_->mutable_R(0, 0) -= scaled_angle_delta; |
| 214 | kf_->mutable_R(2, 0) += scaled_angle_delta; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Run the controller. |
| 218 | Eigen::Matrix<double, 2, 1> U = kf_->ControllerOutput(); |
| 219 | |
| 220 | kf_->mutable_U_uncapped() = kf_->mutable_U() = U; |
| 221 | ScaleCapU(&kf_->mutable_U()); |
| 222 | |
| 223 | // Now update the feed forwards. |
| 224 | kf_->UpdateFFReference(); |
| 225 | |
| 226 | // Now, move the profile if things didn't go perfectly. |
| 227 | if (use_profile_ && |
| 228 | (kf_->U() - kf_->U_uncapped()).lpNorm<Eigen::Infinity>() > 1e-4) { |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 229 | // kf_->R() is in wheel coordinates, while the profile is in absolute |
| 230 | // coordinates. Convert back... |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 231 | linear_profile_.MoveCurrentState(dt_config_.LeftRightToLinear(kf_->R())); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 232 | |
| 233 | LOG(DEBUG, "Saturated while moving\n"); |
| 234 | |
| 235 | Eigen::Matrix<double, 2, 1> absolute_angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 236 | dt_config_.LeftRightToAngular(kf_->R()); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 237 | absolute_angular(0, 0) -= gyro_to_wheel_offset; |
| 238 | angular_profile_.MoveCurrentState(absolute_angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 239 | } |
| 240 | } else { |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 241 | Eigen::Matrix<double, 2, 1> wheel_linear = |
| 242 | dt_config_.LeftRightToLinear(kf_->X_hat()); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 243 | Eigen::Matrix<double, 2, 1> next_angular = wheel_heading; |
| 244 | next_angular(0, 0) = *integrated_kf_heading_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 245 | |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 246 | unprofiled_goal_.block<4, 1>(0, 0) = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 247 | dt_config_.AngularLinearToLeftRight(wheel_linear, next_angular); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 248 | |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 249 | auto current_linear = dt_config_.LeftRightToLinear(unprofiled_goal_); |
| 250 | auto current_angular = dt_config_.LeftRightToAngular(unprofiled_goal_); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 251 | linear_profile_.MoveCurrentState(current_linear); |
| 252 | angular_profile_.MoveCurrentState(current_angular); |
| 253 | |
| 254 | kf_->mutable_next_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0); |
| 255 | kf_->mutable_R().block<4, 1>(0, 0) = kf_->X_hat().block<4, 1>(0, 0); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 256 | } |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 257 | last_gyro_to_wheel_offset_ = gyro_to_wheel_offset; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 260 | void DrivetrainMotorsSS::SetOutput( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 261 | ::frc971::control_loops::DrivetrainQueue::Output *output) const { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 262 | if (output) { |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 263 | output->left_voltage = kf_->U(0, 0); |
| 264 | output->right_voltage = kf_->U(1, 0); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 265 | output->left_high = true; |
| 266 | output->right_high = true; |
| 267 | } |
| 268 | } |
| 269 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 270 | void DrivetrainMotorsSS::PopulateStatus( |
| 271 | ::frc971::control_loops::DrivetrainQueue::Status *status) const { |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 272 | Eigen::Matrix<double, 2, 1> profiled_linear = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 273 | dt_config_.LeftRightToLinear(kf_->next_R()); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 274 | Eigen::Matrix<double, 2, 1> profiled_angular = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 275 | dt_config_.LeftRightToAngular(kf_->next_R()); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 276 | |
| 277 | profiled_angular(0, 0) -= last_gyro_to_wheel_offset_; |
| 278 | |
| 279 | Eigen::Matrix<double, 4, 1> profiled_gyro_left_right = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 280 | dt_config_.AngularLinearToLeftRight(profiled_linear, profiled_angular); |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 281 | |
| 282 | status->profiled_left_position_goal = profiled_gyro_left_right(0, 0); |
| 283 | status->profiled_left_velocity_goal = profiled_gyro_left_right(1, 0); |
| 284 | status->profiled_right_position_goal = profiled_gyro_left_right(2, 0); |
| 285 | status->profiled_right_velocity_goal = profiled_gyro_left_right(3, 0); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 288 | } // namespace drivetrain |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 289 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 290 | } // namespace frc971 |