blob: f4c608d3a56a7ba863be36faf274c9a0f15db612 [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_
3
John Park33858a32018-09-28 23:05:48 -07004#include "aos/commonmath.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -07005#include "frc971/control_loops/coerce_goal.h"
Austin Schuh093535c2016-03-05 23:21:00 -08006#include "frc971/control_loops/drivetrain/gear.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "frc971/control_loops/polytope.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -07008#ifdef __linux__
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "frc971/control_loops/control_loops_generated.h"
11#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
12#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
14#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070015#include "frc971/input/robot_state_generated.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070016#else
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "frc971/control_loops/drivetrain/drivetrain_goal_float_generated.h"
18#include "frc971/control_loops/drivetrain/drivetrain_output_float_generated.h"
19#include "frc971/control_loops/drivetrain/drivetrain_position_float_generated.h"
20#include "frc971/control_loops/drivetrain/drivetrain_status_float_generated.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070021#endif // __linux__
Comran Morshed5323ecb2015-12-26 20:50:55 +000022#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuh95771d92021-01-23 14:42:25 -080023#include "frc971/control_loops/drivetrain/drivetrain_states.h"
Lee Mracek8ae6c922020-01-21 21:55:17 -050024#include "frc971/control_loops/state_feedback_loop.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000025
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080026namespace frc971::control_loops::drivetrain {
Comran Morshed5323ecb2015-12-26 20:50:55 +000027
Austin Schuhbcce26a2018-03-26 23:41:24 -070028template <typename Scalar = double>
Comran Morshed5323ecb2015-12-26 20:50:55 +000029class PolyDrivetrain {
30 public:
Austin Schuhbcce26a2018-03-26 23:41:24 -070031 PolyDrivetrain(const DrivetrainConfig<Scalar> &dt_config,
32 StateFeedbackLoop<7, 2, 4, Scalar> *kf);
Comran Morshed5323ecb2015-12-26 20:50:55 +000033
Austin Schuhc5fceb82017-02-25 16:24:12 -080034 int controller_index() const { return loop_->index(); }
Comran Morshed5323ecb2015-12-26 20:50:55 +000035
Comran Morshed5323ecb2015-12-26 20:50:55 +000036 // Computes the speed of the motor given the hall effect position and the
37 // speed of the robot.
Austin Schuhbcce26a2018-03-26 23:41:24 -070038 Scalar MotorSpeed(const constants::ShifterHallEffect &hall_effect,
39 Scalar shifter_position, Scalar velocity, Gear gear);
Comran Morshed5323ecb2015-12-26 20:50:55 +000040
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 void SetGoal(const Scalar wheel, const Scalar throttle, const bool quickturn,
42 const bool highgear);
Comran Morshed5323ecb2015-12-26 20:50:55 +000043
44 void SetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 const ::frc971::control_loops::drivetrain::Position *position,
Austin Schuh093535c2016-03-05 23:21:00 -080046 Gear left_gear, Gear right_gear);
Comran Morshed5323ecb2015-12-26 20:50:55 +000047
Lee Mracek8ae6c922020-01-21 21:55:17 -050048 Scalar FilterVelocity(Scalar throttle,
49 const Eigen::Matrix<Scalar, 2, 2> &FF) const;
Comran Morshed5323ecb2015-12-26 20:50:55 +000050
Lee Mracek8ae6c922020-01-21 21:55:17 -050051 Scalar MaxVelocity(const Eigen::Matrix<Scalar, 2, 2> &FF);
Comran Morshed5323ecb2015-12-26 20:50:55 +000052
Austin Schuheeec74a2019-01-27 20:58:59 -080053 void Update(Scalar voltage_battery);
Comran Morshed5323ecb2015-12-26 20:50:55 +000054
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 void SetOutput(::frc971::control_loops::drivetrain::OutputT *output);
Comran Morshed5323ecb2015-12-26 20:50:55 +000056
James Kuszmaulaf5dfad2020-01-03 20:02:54 -080057 flatbuffers::Offset<PolyDriveLogging> PopulateStatus(
58 flatbuffers::FlatBufferBuilder *fbb);
59
60 flatbuffers::Offset<CIMLogging> PopulateShiftingStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh41565602016-02-28 20:10:49 -080062
Austin Schuh093535c2016-03-05 23:21:00 -080063 // Computes the next state of a shifter given the current state and the
64 // requested state.
65 Gear UpdateSingleGear(Gear requested_gear, Gear current_gear);
66
Austin Schuhbcce26a2018-03-26 23:41:24 -070067 // Returns the current estimated velocity in m/s.
68 Scalar velocity() const {
Brian Silverman342d4dc2018-09-02 16:26:01 -070069 return (loop_->mutable_X_hat()(0) + loop_->mutable_X_hat()(1)) * kHalf;
Austin Schuhbcce26a2018-03-26 23:41:24 -070070 }
71
Comran Morshed5323ecb2015-12-26 20:50:55 +000072 private:
Brian Silverman342d4dc2018-09-02 16:26:01 -070073 static constexpr Scalar kZero = static_cast<Scalar>(0.0);
74 static constexpr Scalar kHalf = static_cast<Scalar>(0.5);
75 static constexpr Scalar kOne = static_cast<Scalar>(1.0);
76 static constexpr Scalar kTwo = static_cast<Scalar>(2.0);
77 static constexpr Scalar kTwelve = static_cast<Scalar>(12.0);
78
Austin Schuhbcce26a2018-03-26 23:41:24 -070079 StateFeedbackLoop<7, 2, 4, Scalar> *kf_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000080
James Kuszmaul61750662021-06-21 21:32:33 -070081 const ::frc971::controls::HVPolytope<2, 4, 4, Scalar> U_Poly_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000082
Austin Schuhbcce26a2018-03-26 23:41:24 -070083 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2, Scalar>> loop_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000084
Austin Schuhbcce26a2018-03-26 23:41:24 -070085 const Scalar ttrust_;
86 Scalar wheel_;
87 Scalar throttle_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000088 bool quickturn_;
Austin Schuh093535c2016-03-05 23:21:00 -080089
Comran Morshed5323ecb2015-12-26 20:50:55 +000090 Gear left_gear_;
91 Gear right_gear_;
Austin Schuh093535c2016-03-05 23:21:00 -080092
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 ::frc971::control_loops::drivetrain::PositionT last_position_;
94 ::frc971::control_loops::drivetrain::PositionT position_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000095 int counter_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070096 DrivetrainConfig<Scalar> dt_config_;
Austin Schuh41565602016-02-28 20:10:49 -080097
Austin Schuhbcce26a2018-03-26 23:41:24 -070098 Scalar goal_left_velocity_ = 0.0;
99 Scalar goal_right_velocity_ = 0.0;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700100
101 // Stored from the last iteration, for logging shifting logic.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700102 Scalar left_motor_speed_ = 0.0;
103 Scalar right_motor_speed_ = 0.0;
104 Scalar current_left_velocity_ = 0.0;
105 Scalar current_right_velocity_ = 0.0;
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800106
107 // Feedforward voltage, for logging.
108 Eigen::Matrix<Scalar, 2, 1> ff_volts_{0.0, 0.0};
Comran Morshed5323ecb2015-12-26 20:50:55 +0000109};
110
Austin Schuhbcce26a2018-03-26 23:41:24 -0700111template <typename Scalar>
112PolyDrivetrain<Scalar>::PolyDrivetrain(
113 const DrivetrainConfig<Scalar> &dt_config,
114 StateFeedbackLoop<7, 2, 4, Scalar> *kf)
115 : kf_(kf),
116 U_Poly_((Eigen::Matrix<Scalar, 4, 2>() << /*[[*/ 1, 0 /*]*/,
117 /*[*/ -1, 0 /*]*/,
118 /*[*/ 0, 1 /*]*/,
119 /*[*/ 0, -1 /*]]*/)
120 .finished(),
121 (Eigen::Matrix<Scalar, 4, 1>() << /*[[*/ 12 /*]*/,
122 /*[*/ 12 /*]*/,
123 /*[*/ 12 /*]*/,
124 /*[*/ 12 /*]]*/)
125 .finished(),
126 (Eigen::Matrix<Scalar, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/,
127 /*[*/ -12, 12, 12, -12 /*]*/)
128 .finished()),
129 loop_(new StateFeedbackLoop<2, 2, 2, Scalar>(
130 dt_config.make_v_drivetrain_loop())),
131 ttrust_(1.1),
132 wheel_(0.0),
133 throttle_(0.0),
134 quickturn_(false),
135 left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
136 right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
137 counter_(0),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 dt_config_(dt_config) {}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700139
140template <typename Scalar>
141Scalar PolyDrivetrain<Scalar>::MotorSpeed(
142 const constants::ShifterHallEffect &hall_effect, Scalar shifter_position,
143 Scalar velocity, Gear gear) {
144 const Scalar high_gear_speed =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700145 velocity /
146 static_cast<Scalar>(dt_config_.high_gear_ratio / dt_config_.wheel_radius);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700147 const Scalar low_gear_speed =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700148 velocity /
149 static_cast<Scalar>(dt_config_.low_gear_ratio / dt_config_.wheel_radius);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700150
James Kuszmaul68025332024-01-20 17:06:02 -0800151 if (shifter_position < static_cast<Scalar>(hall_effect.clear_low())) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700152 // We're in low gear, so return speed for that gear.
153 return low_gear_speed;
James Kuszmaul68025332024-01-20 17:06:02 -0800154 } else if (shifter_position > static_cast<Scalar>(hall_effect.clear_high())) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700155 // We're in high gear, so return speed for that gear.
156 return high_gear_speed;
157 }
158
159 // Not in gear, so speed-match to destination gear.
160 switch (gear) {
161 case Gear::HIGH:
162 case Gear::SHIFTING_UP:
163 return high_gear_speed;
164 case Gear::LOW:
165 case Gear::SHIFTING_DOWN:
166 default:
167 return low_gear_speed;
168 break;
169 }
170}
171
172template <typename Scalar>
173Gear PolyDrivetrain<Scalar>::UpdateSingleGear(Gear requested_gear,
174 Gear current_gear) {
175 const Gear shift_up =
James Kuszmaul68025332024-01-20 17:06:02 -0800176 (dt_config_.shifter_type == ShifterType::kHallEffectShifter)
Austin Schuhbcce26a2018-03-26 23:41:24 -0700177 ? Gear::SHIFTING_UP
178 : Gear::HIGH;
179 const Gear shift_down =
James Kuszmaul68025332024-01-20 17:06:02 -0800180 (dt_config_.shifter_type == ShifterType::kHallEffectShifter)
Austin Schuhbcce26a2018-03-26 23:41:24 -0700181 ? Gear::SHIFTING_DOWN
182 : Gear::LOW;
183 if (current_gear != requested_gear) {
184 if (IsInGear(current_gear)) {
185 if (requested_gear == Gear::HIGH) {
186 if (current_gear != Gear::HIGH) {
187 current_gear = shift_up;
188 }
189 } else {
190 if (current_gear != Gear::LOW) {
191 current_gear = shift_down;
192 }
193 }
194 } else {
195 if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) {
196 current_gear = Gear::SHIFTING_UP;
197 } else if (requested_gear == Gear::LOW &&
198 current_gear == Gear::SHIFTING_UP) {
199 current_gear = Gear::SHIFTING_DOWN;
200 }
201 }
202 }
203 return current_gear;
204}
205
206template <typename Scalar>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207void PolyDrivetrain<Scalar>::SetGoal(const Scalar wheel, const Scalar throttle,
208 const bool quickturn,
209 const bool highgear) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700210 // Apply a sin function that's scaled to make it feel better.
Brian Silverman342d4dc2018-09-02 16:26:01 -0700211 const Scalar angular_range =
212 static_cast<Scalar>(M_PI_2) * dt_config_.wheel_non_linearity;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700213
214 wheel_ = sin(angular_range * wheel) / sin(angular_range);
215 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman342d4dc2018-09-02 16:26:01 -0700216 wheel_ = kTwo * wheel - wheel_;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700217 quickturn_ = quickturn;
218
219 if (quickturn_) {
220 wheel_ *= dt_config_.quickturn_wheel_multiplier;
221 } else {
222 wheel_ *= dt_config_.wheel_multiplier;
223 }
224
Brian Silverman342d4dc2018-09-02 16:26:01 -0700225 static constexpr Scalar kThrottleDeadband = static_cast<Scalar>(0.05);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700226 if (::std::abs(throttle) < kThrottleDeadband) {
227 throttle_ = 0;
228 } else {
229 throttle_ = copysign(
Brian Silverman342d4dc2018-09-02 16:26:01 -0700230 (::std::abs(throttle) - kThrottleDeadband) / (kOne - kThrottleDeadband),
Austin Schuhbcce26a2018-03-26 23:41:24 -0700231 throttle);
232 }
233
234 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
235
236 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
237 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
238}
239
240template <typename Scalar>
241void PolyDrivetrain<Scalar>::SetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700242 const ::frc971::control_loops::drivetrain::Position *position,
Austin Schuhbcce26a2018-03-26 23:41:24 -0700243 Gear left_gear, Gear right_gear) {
244 left_gear_ = left_gear;
245 right_gear_ = right_gear;
246 last_position_ = position_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 position->UnPackTo(&position_);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700248}
249
250template <typename Scalar>
Lee Mracek8ae6c922020-01-21 21:55:17 -0500251Scalar PolyDrivetrain<Scalar>::FilterVelocity(
252 Scalar throttle, const Eigen::Matrix<Scalar, 2, 2> &FF) const {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700253 constexpr int kHighGearController = 3;
254 const Eigen::Matrix<Scalar, 2, 2> FF_high =
255 loop_->plant().coefficients(kHighGearController).B.inverse() *
256 (Eigen::Matrix<Scalar, 2, 2>::Identity() -
257 loop_->plant().coefficients(kHighGearController).A);
258
259 ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum();
260 int min_FF_sum_index;
261 const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
262 const Scalar min_K_sum = loop_->controller().K().col(min_FF_sum_index).sum();
263 const Scalar high_min_FF_sum = FF_high.col(0).sum();
264
Lee Mracek8ae6c922020-01-21 21:55:17 -0500265 const Scalar adjusted_ff_voltage = ::aos::Clip(
266 throttle * kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve);
267 return (adjusted_ff_voltage + ttrust_ * min_K_sum *
268 (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) *
269 kHalf) /
Austin Schuhbcce26a2018-03-26 23:41:24 -0700270 (ttrust_ * min_K_sum + min_FF_sum);
271}
272
273template <typename Scalar>
Lee Mracek8ae6c922020-01-21 21:55:17 -0500274Scalar PolyDrivetrain<Scalar>::MaxVelocity(
275 const Eigen::Matrix<Scalar, 2, 2> &FF) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700276 constexpr int kHighGearController = 3;
277 const Eigen::Matrix<Scalar, 2, 2> FF_high =
278 loop_->plant().coefficients(kHighGearController).B.inverse() *
279 (Eigen::Matrix<Scalar, 2, 2>::Identity() -
280 loop_->plant().coefficients(kHighGearController).A);
281
282 ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum();
283 int min_FF_sum_index;
284 const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
285 // const Scalar min_K_sum = loop_->K().col(min_FF_sum_index).sum();
286 const Scalar high_min_FF_sum = FF_high.col(0).sum();
287
288 const Scalar adjusted_ff_voltage =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700289 ::aos::Clip(kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700290 return adjusted_ff_voltage / min_FF_sum;
291}
292
293template <typename Scalar>
Austin Schuheeec74a2019-01-27 20:58:59 -0800294void PolyDrivetrain<Scalar>::Update(Scalar voltage_battery) {
James Kuszmaul68025332024-01-20 17:06:02 -0800295 if (dt_config_.loop_type == LoopType::kClosedLoop) {
Austin Schuh95771d92021-01-23 14:42:25 -0800296 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(kLeftVelocity);
297 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(kRightVelocity);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700298 }
299
300 // TODO(austin): Observer for the current velocity instead of difference
301 // calculations.
302 ++counter_;
303
304 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
305 // FF * X = U (steady state)
306 const Eigen::Matrix<Scalar, 2, 2> FF =
307 loop_->plant().B().inverse() *
308 (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A());
309
310 // Invert the plant to figure out how the velocity filter would have to
311 // work
312 // out in order to filter out the forwards negative inertia.
313 // This math assumes that the left and right power and velocity are
314 // equals,
315 // and that the plant is the same on the left and right.
Lee Mracek8ae6c922020-01-21 21:55:17 -0500316 const Scalar fvel = FilterVelocity(throttle_, FF);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700317
Brian Silverman342d4dc2018-09-02 16:26:01 -0700318 const Scalar sign_svel = wheel_ * ((fvel > kZero) ? kOne : -kOne);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700319 Scalar steering_velocity;
320 if (quickturn_) {
Lee Mracek8ae6c922020-01-21 21:55:17 -0500321 steering_velocity = wheel_ * MaxVelocity(FF);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700322 } else {
323 steering_velocity = ::std::abs(fvel) * wheel_;
324 }
325 const Scalar left_velocity = fvel - steering_velocity;
326 const Scalar right_velocity = fvel + steering_velocity;
327 goal_left_velocity_ = left_velocity;
328 goal_right_velocity_ = right_velocity;
329
330 // Integrate velocity to get the position.
331 // This position is used to get integral control.
332 loop_->mutable_R() << left_velocity, right_velocity;
333
334 if (!quickturn_) {
335 // K * R = w
336 Eigen::Matrix<Scalar, 1, 2> equality_k;
337 equality_k << 1 + sign_svel, -(1 - sign_svel);
Brian Silverman342d4dc2018-09-02 16:26:01 -0700338 const Scalar equality_w = kZero;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700339
340 // Construct a constraint on R by manipulating the constraint on U
James Kuszmaul61750662021-06-21 21:32:33 -0700341 ::frc971::controls::HVPolytope<2, 4, 4, Scalar> R_poly_hv(
Austin Schuhbcce26a2018-03-26 23:41:24 -0700342 U_Poly_.static_H() * (loop_->controller().K() + FF),
343 U_Poly_.static_k() +
344 U_Poly_.static_H() * loop_->controller().K() * loop_->X_hat(),
345 (loop_->controller().K() + FF).inverse() *
James Kuszmaul61750662021-06-21 21:32:33 -0700346 ::frc971::controls::ShiftPoints<2, 4, Scalar>(
Austin Schuhbcce26a2018-03-26 23:41:24 -0700347 U_Poly_.StaticVertices(),
348 loop_->controller().K() * loop_->X_hat()));
349
350 // Limit R back inside the box.
351 loop_->mutable_R() =
352 CoerceGoal<Scalar>(R_poly_hv, equality_k, equality_w, loop_->R());
353 }
354
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800355 ff_volts_ = FF * loop_->R();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700356 const Eigen::Matrix<Scalar, 2, 1> U_ideal =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800357 loop_->controller().K() * (loop_->R() - loop_->X_hat()) + ff_volts_;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700358
359 for (int i = 0; i < 2; i++) {
360 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
361 }
362
James Kuszmaul68025332024-01-20 17:06:02 -0800363 if (dt_config_.loop_type == LoopType::kOpenLoop) {
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800364 ff_volts_.setZero();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700365 loop_->mutable_X_hat() =
366 loop_->plant().A() * loop_->X_hat() + loop_->plant().B() * loop_->U();
367 }
368
Lee Mracek8ae6c922020-01-21 21:55:17 -0500369 // Housekeeping: set the shifting logging values to zero, because we're not
370 // shifting
Brian Silverman342d4dc2018-09-02 16:26:01 -0700371 left_motor_speed_ = kZero;
372 right_motor_speed_ = kZero;
373 current_left_velocity_ = kZero;
374 current_right_velocity_ = kZero;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700375 } else {
Austin Schuhbb735b72019-01-03 12:58:41 -0800376 const Scalar dt =
377 ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>(
378 dt_config_.dt)
379 .count();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700380 current_left_velocity_ =
Austin Schuhbb735b72019-01-03 12:58:41 -0800381 (position_.left_encoder - last_position_.left_encoder) / dt;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700382 current_right_velocity_ =
Austin Schuhbb735b72019-01-03 12:58:41 -0800383 (position_.right_encoder - last_position_.right_encoder) / dt;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700384 left_motor_speed_ =
385 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
386 current_left_velocity_, left_gear_);
387 right_motor_speed_ =
388 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
389 current_right_velocity_, right_gear_);
390
391 goal_left_velocity_ = current_left_velocity_;
392 goal_right_velocity_ = current_right_velocity_;
393
394 // Any motor is not in gear. Speed match.
395 ::Eigen::Matrix<Scalar, 1, 1> R_left;
396 ::Eigen::Matrix<Scalar, 1, 1> R_right;
397 R_left(0, 0) = left_motor_speed_;
398 R_right(0, 0) = right_motor_speed_;
399
Brian Silverman342d4dc2018-09-02 16:26:01 -0700400 const Scalar wiggle = (static_cast<Scalar>((counter_ % 30) / 15) - kHalf) *
401 static_cast<Scalar>(8.0);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700402
403 loop_->mutable_U(0, 0) = ::aos::Clip(
404 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
Brian Silverman342d4dc2018-09-02 16:26:01 -0700405 -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700406 loop_->mutable_U(1, 0) = ::aos::Clip(
407 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
Brian Silverman342d4dc2018-09-02 16:26:01 -0700408 -kTwelve, kTwelve);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800409 ff_volts_ = loop_->U();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700410#ifdef __linux__
Austin Schuheeec74a2019-01-27 20:58:59 -0800411 loop_->mutable_U() *= kTwelve / voltage_battery;
412#else
413 (void)voltage_battery;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700414#endif // __linux__
415 }
416}
417
418template <typename Scalar>
419void PolyDrivetrain<Scalar>::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420 ::frc971::control_loops::drivetrain::OutputT *output) {
421 if (output != nullptr) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700422 output->left_voltage = loop_->U(0, 0);
423 output->right_voltage = loop_->U(1, 0);
424 output->left_high = MaybeHigh(left_gear_);
425 output->right_high = MaybeHigh(right_gear_);
426 }
427}
428
429template <typename Scalar>
Lee Mracek8ae6c922020-01-21 21:55:17 -0500430flatbuffers::Offset<PolyDriveLogging> PolyDrivetrain<Scalar>::PopulateStatus(
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800431 flatbuffers::FlatBufferBuilder *fbb) {
432 PolyDriveLogging::Builder builder(*fbb);
433
434 builder.add_goal_left_velocity(goal_left_velocity_);
435 builder.add_goal_right_velocity(goal_right_velocity_);
436 builder.add_ff_left_voltage(ff_volts_(0, 0));
437 builder.add_ff_right_voltage(ff_volts_(1, 0));
438
439 return builder.Finish();
440}
441
442template <typename Scalar>
443flatbuffers::Offset<CIMLogging> PolyDrivetrain<Scalar>::PopulateShiftingStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700444 flatbuffers::FlatBufferBuilder *fbb) {
445 CIMLogging::Builder builder(*fbb);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700446
Alex Perrycb7da4b2019-08-28 19:35:56 -0700447 builder.add_left_in_gear(IsInGear(left_gear_));
448 builder.add_left_motor_speed(left_motor_speed_);
449 builder.add_left_velocity(current_left_velocity_);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700450
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451 builder.add_right_in_gear(IsInGear(right_gear_));
452 builder.add_right_motor_speed(right_motor_speed_);
453 builder.add_right_velocity(current_right_velocity_);
454
455 return builder.Finish();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700456}
457
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800458} // namespace frc971::control_loops::drivetrain
Comran Morshed5323ecb2015-12-26 20:50:55 +0000459
460#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_