blob: 17a32b8292d0786f9df592209e08779dd8674e4b [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"
Lee Mracek8ae6c922020-01-21 21:55:17 -05005#include "aos/controls/polytope.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -07006#include "frc971/control_loops/coerce_goal.h"
Austin Schuh093535c2016-03-05 23:21:00 -08007#include "frc971/control_loops/drivetrain/gear.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 "aos/robot_state/robot_state_generated.h"
11#include "frc971/control_loops/control_loops_generated.h"
12#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
14#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
15#include "frc971/control_loops/drivetrain/drivetrain_status_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
26namespace frc971 {
27namespace control_loops {
28namespace drivetrain {
29
Austin Schuhbcce26a2018-03-26 23:41:24 -070030template <typename Scalar = double>
Comran Morshed5323ecb2015-12-26 20:50:55 +000031class PolyDrivetrain {
32 public:
Austin Schuhbcce26a2018-03-26 23:41:24 -070033 PolyDrivetrain(const DrivetrainConfig<Scalar> &dt_config,
34 StateFeedbackLoop<7, 2, 4, Scalar> *kf);
Comran Morshed5323ecb2015-12-26 20:50:55 +000035
Austin Schuhc5fceb82017-02-25 16:24:12 -080036 int controller_index() const { return loop_->index(); }
Comran Morshed5323ecb2015-12-26 20:50:55 +000037
Comran Morshed5323ecb2015-12-26 20:50:55 +000038 // Computes the speed of the motor given the hall effect position and the
39 // speed of the robot.
Austin Schuhbcce26a2018-03-26 23:41:24 -070040 Scalar MotorSpeed(const constants::ShifterHallEffect &hall_effect,
41 Scalar shifter_position, Scalar velocity, Gear gear);
Comran Morshed5323ecb2015-12-26 20:50:55 +000042
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 void SetGoal(const Scalar wheel, const Scalar throttle, const bool quickturn,
44 const bool highgear);
Comran Morshed5323ecb2015-12-26 20:50:55 +000045
46 void SetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -070047 const ::frc971::control_loops::drivetrain::Position *position,
Austin Schuh093535c2016-03-05 23:21:00 -080048 Gear left_gear, Gear right_gear);
Comran Morshed5323ecb2015-12-26 20:50:55 +000049
Lee Mracek8ae6c922020-01-21 21:55:17 -050050 Scalar FilterVelocity(Scalar throttle,
51 const Eigen::Matrix<Scalar, 2, 2> &FF) const;
Comran Morshed5323ecb2015-12-26 20:50:55 +000052
Lee Mracek8ae6c922020-01-21 21:55:17 -050053 Scalar MaxVelocity(const Eigen::Matrix<Scalar, 2, 2> &FF);
Comran Morshed5323ecb2015-12-26 20:50:55 +000054
Austin Schuheeec74a2019-01-27 20:58:59 -080055 void Update(Scalar voltage_battery);
Comran Morshed5323ecb2015-12-26 20:50:55 +000056
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 void SetOutput(::frc971::control_loops::drivetrain::OutputT *output);
Comran Morshed5323ecb2015-12-26 20:50:55 +000058
James Kuszmaulaf5dfad2020-01-03 20:02:54 -080059 flatbuffers::Offset<PolyDriveLogging> PopulateStatus(
60 flatbuffers::FlatBufferBuilder *fbb);
61
62 flatbuffers::Offset<CIMLogging> PopulateShiftingStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh41565602016-02-28 20:10:49 -080064
Austin Schuh093535c2016-03-05 23:21:00 -080065 // Computes the next state of a shifter given the current state and the
66 // requested state.
67 Gear UpdateSingleGear(Gear requested_gear, Gear current_gear);
68
Austin Schuhbcce26a2018-03-26 23:41:24 -070069 // Returns the current estimated velocity in m/s.
70 Scalar velocity() const {
Brian Silverman342d4dc2018-09-02 16:26:01 -070071 return (loop_->mutable_X_hat()(0) + loop_->mutable_X_hat()(1)) * kHalf;
Austin Schuhbcce26a2018-03-26 23:41:24 -070072 }
73
Comran Morshed5323ecb2015-12-26 20:50:55 +000074 private:
Brian Silverman342d4dc2018-09-02 16:26:01 -070075 static constexpr Scalar kZero = static_cast<Scalar>(0.0);
76 static constexpr Scalar kHalf = static_cast<Scalar>(0.5);
77 static constexpr Scalar kOne = static_cast<Scalar>(1.0);
78 static constexpr Scalar kTwo = static_cast<Scalar>(2.0);
79 static constexpr Scalar kTwelve = static_cast<Scalar>(12.0);
80
Austin Schuhbcce26a2018-03-26 23:41:24 -070081 StateFeedbackLoop<7, 2, 4, Scalar> *kf_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000082
Austin Schuhbcce26a2018-03-26 23:41:24 -070083 const ::aos::controls::HVPolytope<2, 4, 4, Scalar> U_Poly_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000084
Austin Schuhbcce26a2018-03-26 23:41:24 -070085 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2, Scalar>> loop_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000086
Austin Schuhbcce26a2018-03-26 23:41:24 -070087 const Scalar ttrust_;
88 Scalar wheel_;
89 Scalar throttle_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000090 bool quickturn_;
Austin Schuh093535c2016-03-05 23:21:00 -080091
Comran Morshed5323ecb2015-12-26 20:50:55 +000092 Gear left_gear_;
93 Gear right_gear_;
Austin Schuh093535c2016-03-05 23:21:00 -080094
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 ::frc971::control_loops::drivetrain::PositionT last_position_;
96 ::frc971::control_loops::drivetrain::PositionT position_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000097 int counter_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070098 DrivetrainConfig<Scalar> dt_config_;
Austin Schuh41565602016-02-28 20:10:49 -080099
Austin Schuhbcce26a2018-03-26 23:41:24 -0700100 Scalar goal_left_velocity_ = 0.0;
101 Scalar goal_right_velocity_ = 0.0;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700102
103 // Stored from the last iteration, for logging shifting logic.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700104 Scalar left_motor_speed_ = 0.0;
105 Scalar right_motor_speed_ = 0.0;
106 Scalar current_left_velocity_ = 0.0;
107 Scalar current_right_velocity_ = 0.0;
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800108
109 // Feedforward voltage, for logging.
110 Eigen::Matrix<Scalar, 2, 1> ff_volts_{0.0, 0.0};
Comran Morshed5323ecb2015-12-26 20:50:55 +0000111};
112
Austin Schuhbcce26a2018-03-26 23:41:24 -0700113template <typename Scalar>
114PolyDrivetrain<Scalar>::PolyDrivetrain(
115 const DrivetrainConfig<Scalar> &dt_config,
116 StateFeedbackLoop<7, 2, 4, Scalar> *kf)
117 : kf_(kf),
118 U_Poly_((Eigen::Matrix<Scalar, 4, 2>() << /*[[*/ 1, 0 /*]*/,
119 /*[*/ -1, 0 /*]*/,
120 /*[*/ 0, 1 /*]*/,
121 /*[*/ 0, -1 /*]]*/)
122 .finished(),
123 (Eigen::Matrix<Scalar, 4, 1>() << /*[[*/ 12 /*]*/,
124 /*[*/ 12 /*]*/,
125 /*[*/ 12 /*]*/,
126 /*[*/ 12 /*]]*/)
127 .finished(),
128 (Eigen::Matrix<Scalar, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/,
129 /*[*/ -12, 12, 12, -12 /*]*/)
130 .finished()),
131 loop_(new StateFeedbackLoop<2, 2, 2, Scalar>(
132 dt_config.make_v_drivetrain_loop())),
133 ttrust_(1.1),
134 wheel_(0.0),
135 throttle_(0.0),
136 quickturn_(false),
137 left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
138 right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
139 counter_(0),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 dt_config_(dt_config) {}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700141
142template <typename Scalar>
143Scalar PolyDrivetrain<Scalar>::MotorSpeed(
144 const constants::ShifterHallEffect &hall_effect, Scalar shifter_position,
145 Scalar velocity, Gear gear) {
146 const Scalar high_gear_speed =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700147 velocity /
148 static_cast<Scalar>(dt_config_.high_gear_ratio / dt_config_.wheel_radius);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700149 const Scalar low_gear_speed =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700150 velocity /
151 static_cast<Scalar>(dt_config_.low_gear_ratio / dt_config_.wheel_radius);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700152
Brian Silverman342d4dc2018-09-02 16:26:01 -0700153 if (shifter_position < static_cast<Scalar>(hall_effect.clear_low)) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700154 // We're in low gear, so return speed for that gear.
155 return low_gear_speed;
Brian Silverman342d4dc2018-09-02 16:26:01 -0700156 } else if (shifter_position > static_cast<Scalar>(hall_effect.clear_high)) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700157 // We're in high gear, so return speed for that gear.
158 return high_gear_speed;
159 }
160
161 // Not in gear, so speed-match to destination gear.
162 switch (gear) {
163 case Gear::HIGH:
164 case Gear::SHIFTING_UP:
165 return high_gear_speed;
166 case Gear::LOW:
167 case Gear::SHIFTING_DOWN:
168 default:
169 return low_gear_speed;
170 break;
171 }
172}
173
174template <typename Scalar>
175Gear PolyDrivetrain<Scalar>::UpdateSingleGear(Gear requested_gear,
176 Gear current_gear) {
177 const Gear shift_up =
178 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
179 ? Gear::SHIFTING_UP
180 : Gear::HIGH;
181 const Gear shift_down =
182 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
183 ? Gear::SHIFTING_DOWN
184 : Gear::LOW;
185 if (current_gear != requested_gear) {
186 if (IsInGear(current_gear)) {
187 if (requested_gear == Gear::HIGH) {
188 if (current_gear != Gear::HIGH) {
189 current_gear = shift_up;
190 }
191 } else {
192 if (current_gear != Gear::LOW) {
193 current_gear = shift_down;
194 }
195 }
196 } else {
197 if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) {
198 current_gear = Gear::SHIFTING_UP;
199 } else if (requested_gear == Gear::LOW &&
200 current_gear == Gear::SHIFTING_UP) {
201 current_gear = Gear::SHIFTING_DOWN;
202 }
203 }
204 }
205 return current_gear;
206}
207
208template <typename Scalar>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209void PolyDrivetrain<Scalar>::SetGoal(const Scalar wheel, const Scalar throttle,
210 const bool quickturn,
211 const bool highgear) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700212 // Apply a sin function that's scaled to make it feel better.
Brian Silverman342d4dc2018-09-02 16:26:01 -0700213 const Scalar angular_range =
214 static_cast<Scalar>(M_PI_2) * dt_config_.wheel_non_linearity;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700215
216 wheel_ = sin(angular_range * wheel) / sin(angular_range);
217 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman342d4dc2018-09-02 16:26:01 -0700218 wheel_ = kTwo * wheel - wheel_;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700219 quickturn_ = quickturn;
220
221 if (quickturn_) {
222 wheel_ *= dt_config_.quickturn_wheel_multiplier;
223 } else {
224 wheel_ *= dt_config_.wheel_multiplier;
225 }
226
Brian Silverman342d4dc2018-09-02 16:26:01 -0700227 static constexpr Scalar kThrottleDeadband = static_cast<Scalar>(0.05);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700228 if (::std::abs(throttle) < kThrottleDeadband) {
229 throttle_ = 0;
230 } else {
231 throttle_ = copysign(
Brian Silverman342d4dc2018-09-02 16:26:01 -0700232 (::std::abs(throttle) - kThrottleDeadband) / (kOne - kThrottleDeadband),
Austin Schuhbcce26a2018-03-26 23:41:24 -0700233 throttle);
234 }
235
236 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
237
238 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
239 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
240}
241
242template <typename Scalar>
243void PolyDrivetrain<Scalar>::SetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700244 const ::frc971::control_loops::drivetrain::Position *position,
Austin Schuhbcce26a2018-03-26 23:41:24 -0700245 Gear left_gear, Gear right_gear) {
246 left_gear_ = left_gear;
247 right_gear_ = right_gear;
248 last_position_ = position_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 position->UnPackTo(&position_);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700250}
251
252template <typename Scalar>
Lee Mracek8ae6c922020-01-21 21:55:17 -0500253Scalar PolyDrivetrain<Scalar>::FilterVelocity(
254 Scalar throttle, const Eigen::Matrix<Scalar, 2, 2> &FF) const {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700255 constexpr int kHighGearController = 3;
256 const Eigen::Matrix<Scalar, 2, 2> FF_high =
257 loop_->plant().coefficients(kHighGearController).B.inverse() *
258 (Eigen::Matrix<Scalar, 2, 2>::Identity() -
259 loop_->plant().coefficients(kHighGearController).A);
260
261 ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum();
262 int min_FF_sum_index;
263 const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
264 const Scalar min_K_sum = loop_->controller().K().col(min_FF_sum_index).sum();
265 const Scalar high_min_FF_sum = FF_high.col(0).sum();
266
Lee Mracek8ae6c922020-01-21 21:55:17 -0500267 const Scalar adjusted_ff_voltage = ::aos::Clip(
268 throttle * kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve);
269 return (adjusted_ff_voltage + ttrust_ * min_K_sum *
270 (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) *
271 kHalf) /
Austin Schuhbcce26a2018-03-26 23:41:24 -0700272 (ttrust_ * min_K_sum + min_FF_sum);
273}
274
275template <typename Scalar>
Lee Mracek8ae6c922020-01-21 21:55:17 -0500276Scalar PolyDrivetrain<Scalar>::MaxVelocity(
277 const Eigen::Matrix<Scalar, 2, 2> &FF) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700278 constexpr int kHighGearController = 3;
279 const Eigen::Matrix<Scalar, 2, 2> FF_high =
280 loop_->plant().coefficients(kHighGearController).B.inverse() *
281 (Eigen::Matrix<Scalar, 2, 2>::Identity() -
282 loop_->plant().coefficients(kHighGearController).A);
283
284 ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum();
285 int min_FF_sum_index;
286 const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
287 // const Scalar min_K_sum = loop_->K().col(min_FF_sum_index).sum();
288 const Scalar high_min_FF_sum = FF_high.col(0).sum();
289
290 const Scalar adjusted_ff_voltage =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700291 ::aos::Clip(kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700292 return adjusted_ff_voltage / min_FF_sum;
293}
294
295template <typename Scalar>
Austin Schuheeec74a2019-01-27 20:58:59 -0800296void PolyDrivetrain<Scalar>::Update(Scalar voltage_battery) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700297 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
Austin Schuh95771d92021-01-23 14:42:25 -0800298 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(kLeftVelocity);
299 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(kRightVelocity);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700300 }
301
302 // TODO(austin): Observer for the current velocity instead of difference
303 // calculations.
304 ++counter_;
305
306 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
307 // FF * X = U (steady state)
308 const Eigen::Matrix<Scalar, 2, 2> FF =
309 loop_->plant().B().inverse() *
310 (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A());
311
312 // Invert the plant to figure out how the velocity filter would have to
313 // work
314 // out in order to filter out the forwards negative inertia.
315 // This math assumes that the left and right power and velocity are
316 // equals,
317 // and that the plant is the same on the left and right.
Lee Mracek8ae6c922020-01-21 21:55:17 -0500318 const Scalar fvel = FilterVelocity(throttle_, FF);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700319
Brian Silverman342d4dc2018-09-02 16:26:01 -0700320 const Scalar sign_svel = wheel_ * ((fvel > kZero) ? kOne : -kOne);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700321 Scalar steering_velocity;
322 if (quickturn_) {
Lee Mracek8ae6c922020-01-21 21:55:17 -0500323 steering_velocity = wheel_ * MaxVelocity(FF);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700324 } else {
325 steering_velocity = ::std::abs(fvel) * wheel_;
326 }
327 const Scalar left_velocity = fvel - steering_velocity;
328 const Scalar right_velocity = fvel + steering_velocity;
329 goal_left_velocity_ = left_velocity;
330 goal_right_velocity_ = right_velocity;
331
332 // Integrate velocity to get the position.
333 // This position is used to get integral control.
334 loop_->mutable_R() << left_velocity, right_velocity;
335
336 if (!quickturn_) {
337 // K * R = w
338 Eigen::Matrix<Scalar, 1, 2> equality_k;
339 equality_k << 1 + sign_svel, -(1 - sign_svel);
Brian Silverman342d4dc2018-09-02 16:26:01 -0700340 const Scalar equality_w = kZero;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700341
342 // Construct a constraint on R by manipulating the constraint on U
343 ::aos::controls::HVPolytope<2, 4, 4, Scalar> R_poly_hv(
344 U_Poly_.static_H() * (loop_->controller().K() + FF),
345 U_Poly_.static_k() +
346 U_Poly_.static_H() * loop_->controller().K() * loop_->X_hat(),
347 (loop_->controller().K() + FF).inverse() *
348 ::aos::controls::ShiftPoints<2, 4, Scalar>(
349 U_Poly_.StaticVertices(),
350 loop_->controller().K() * loop_->X_hat()));
351
352 // Limit R back inside the box.
353 loop_->mutable_R() =
354 CoerceGoal<Scalar>(R_poly_hv, equality_k, equality_w, loop_->R());
355 }
356
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800357 ff_volts_ = FF * loop_->R();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700358 const Eigen::Matrix<Scalar, 2, 1> U_ideal =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800359 loop_->controller().K() * (loop_->R() - loop_->X_hat()) + ff_volts_;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700360
361 for (int i = 0; i < 2; i++) {
362 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
363 }
364
365 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800366 ff_volts_.setZero();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700367 loop_->mutable_X_hat() =
368 loop_->plant().A() * loop_->X_hat() + loop_->plant().B() * loop_->U();
369 }
370
Lee Mracek8ae6c922020-01-21 21:55:17 -0500371 // Housekeeping: set the shifting logging values to zero, because we're not
372 // shifting
Brian Silverman342d4dc2018-09-02 16:26:01 -0700373 left_motor_speed_ = kZero;
374 right_motor_speed_ = kZero;
375 current_left_velocity_ = kZero;
376 current_right_velocity_ = kZero;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700377 } else {
Austin Schuhbb735b72019-01-03 12:58:41 -0800378 const Scalar dt =
379 ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>(
380 dt_config_.dt)
381 .count();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700382 current_left_velocity_ =
Austin Schuhbb735b72019-01-03 12:58:41 -0800383 (position_.left_encoder - last_position_.left_encoder) / dt;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700384 current_right_velocity_ =
Austin Schuhbb735b72019-01-03 12:58:41 -0800385 (position_.right_encoder - last_position_.right_encoder) / dt;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700386 left_motor_speed_ =
387 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
388 current_left_velocity_, left_gear_);
389 right_motor_speed_ =
390 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
391 current_right_velocity_, right_gear_);
392
393 goal_left_velocity_ = current_left_velocity_;
394 goal_right_velocity_ = current_right_velocity_;
395
396 // Any motor is not in gear. Speed match.
397 ::Eigen::Matrix<Scalar, 1, 1> R_left;
398 ::Eigen::Matrix<Scalar, 1, 1> R_right;
399 R_left(0, 0) = left_motor_speed_;
400 R_right(0, 0) = right_motor_speed_;
401
Brian Silverman342d4dc2018-09-02 16:26:01 -0700402 const Scalar wiggle = (static_cast<Scalar>((counter_ % 30) / 15) - kHalf) *
403 static_cast<Scalar>(8.0);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700404
405 loop_->mutable_U(0, 0) = ::aos::Clip(
406 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
Brian Silverman342d4dc2018-09-02 16:26:01 -0700407 -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700408 loop_->mutable_U(1, 0) = ::aos::Clip(
409 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
Brian Silverman342d4dc2018-09-02 16:26:01 -0700410 -kTwelve, kTwelve);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800411 ff_volts_ = loop_->U();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700412#ifdef __linux__
Austin Schuheeec74a2019-01-27 20:58:59 -0800413 loop_->mutable_U() *= kTwelve / voltage_battery;
414#else
415 (void)voltage_battery;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700416#endif // __linux__
417 }
418}
419
420template <typename Scalar>
421void PolyDrivetrain<Scalar>::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422 ::frc971::control_loops::drivetrain::OutputT *output) {
423 if (output != nullptr) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700424 output->left_voltage = loop_->U(0, 0);
425 output->right_voltage = loop_->U(1, 0);
426 output->left_high = MaybeHigh(left_gear_);
427 output->right_high = MaybeHigh(right_gear_);
428 }
429}
430
431template <typename Scalar>
Lee Mracek8ae6c922020-01-21 21:55:17 -0500432flatbuffers::Offset<PolyDriveLogging> PolyDrivetrain<Scalar>::PopulateStatus(
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800433 flatbuffers::FlatBufferBuilder *fbb) {
434 PolyDriveLogging::Builder builder(*fbb);
435
436 builder.add_goal_left_velocity(goal_left_velocity_);
437 builder.add_goal_right_velocity(goal_right_velocity_);
438 builder.add_ff_left_voltage(ff_volts_(0, 0));
439 builder.add_ff_right_voltage(ff_volts_(1, 0));
440
441 return builder.Finish();
442}
443
444template <typename Scalar>
445flatbuffers::Offset<CIMLogging> PolyDrivetrain<Scalar>::PopulateShiftingStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700446 flatbuffers::FlatBufferBuilder *fbb) {
447 CIMLogging::Builder builder(*fbb);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700448
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449 builder.add_left_in_gear(IsInGear(left_gear_));
450 builder.add_left_motor_speed(left_motor_speed_);
451 builder.add_left_velocity(current_left_velocity_);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700452
Alex Perrycb7da4b2019-08-28 19:35:56 -0700453 builder.add_right_in_gear(IsInGear(right_gear_));
454 builder.add_right_motor_speed(right_motor_speed_);
455 builder.add_right_velocity(current_right_velocity_);
456
457 return builder.Finish();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700458}
459
Comran Morshed5323ecb2015-12-26 20:50:55 +0000460} // namespace drivetrain
461} // namespace control_loops
462} // namespace frc971
463
464#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_