blob: 7bfa630b78d19ddcd29deac6ec2dd487cb2959d8 [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/controls/polytope.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +00005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/commonmath.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -07007#include "frc971/control_loops/coerce_goal.h"
Austin Schuh093535c2016-03-05 23:21:00 -08008#include "frc971/control_loops/drivetrain/gear.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -07009#ifdef __linux__
John Park33858a32018-09-28 23:05:48 -070010#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "aos/robot_state/robot_state_generated.h"
12#include "frc971/control_loops/control_loops_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
14#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
15#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
16#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070017#else
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "frc971/control_loops/drivetrain/drivetrain_goal_float_generated.h"
19#include "frc971/control_loops/drivetrain/drivetrain_output_float_generated.h"
20#include "frc971/control_loops/drivetrain/drivetrain_position_float_generated.h"
21#include "frc971/control_loops/drivetrain/drivetrain_status_float_generated.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070022#endif // __linux__
Comran Morshed5323ecb2015-12-26 20:50:55 +000023#include "frc971/control_loops/state_feedback_loop.h"
24#include "frc971/control_loops/drivetrain/drivetrain_config.h"
25
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
Austin Schuhbcce26a2018-03-26 23:41:24 -070050 Scalar FilterVelocity(Scalar throttle) const;
Comran Morshed5323ecb2015-12-26 20:50:55 +000051
Austin Schuhbcce26a2018-03-26 23:41:24 -070052 Scalar MaxVelocity();
Comran Morshed5323ecb2015-12-26 20:50:55 +000053
Austin Schuheeec74a2019-01-27 20:58:59 -080054 void Update(Scalar voltage_battery);
Comran Morshed5323ecb2015-12-26 20:50:55 +000055
Alex Perrycb7da4b2019-08-28 19:35:56 -070056 void SetOutput(::frc971::control_loops::drivetrain::OutputT *output);
Comran Morshed5323ecb2015-12-26 20:50:55 +000057
James Kuszmaulaf5dfad2020-01-03 20:02:54 -080058 flatbuffers::Offset<PolyDriveLogging> PopulateStatus(
59 flatbuffers::FlatBufferBuilder *fbb);
60
61 flatbuffers::Offset<CIMLogging> PopulateShiftingStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh41565602016-02-28 20:10:49 -080063
Austin Schuh093535c2016-03-05 23:21:00 -080064 // Computes the next state of a shifter given the current state and the
65 // requested state.
66 Gear UpdateSingleGear(Gear requested_gear, Gear current_gear);
67
Austin Schuhbcce26a2018-03-26 23:41:24 -070068 // Returns the current estimated velocity in m/s.
69 Scalar velocity() const {
Brian Silverman342d4dc2018-09-02 16:26:01 -070070 return (loop_->mutable_X_hat()(0) + loop_->mutable_X_hat()(1)) * kHalf;
Austin Schuhbcce26a2018-03-26 23:41:24 -070071 }
72
Comran Morshed5323ecb2015-12-26 20:50:55 +000073 private:
Brian Silverman342d4dc2018-09-02 16:26:01 -070074 static constexpr Scalar kZero = static_cast<Scalar>(0.0);
75 static constexpr Scalar kHalf = static_cast<Scalar>(0.5);
76 static constexpr Scalar kOne = static_cast<Scalar>(1.0);
77 static constexpr Scalar kTwo = static_cast<Scalar>(2.0);
78 static constexpr Scalar kTwelve = static_cast<Scalar>(12.0);
79
Austin Schuhbcce26a2018-03-26 23:41:24 -070080 StateFeedbackLoop<7, 2, 4, Scalar> *kf_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000081
Austin Schuhbcce26a2018-03-26 23:41:24 -070082 const ::aos::controls::HVPolytope<2, 4, 4, Scalar> U_Poly_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000083
Austin Schuhbcce26a2018-03-26 23:41:24 -070084 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2, Scalar>> loop_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000085
Austin Schuhbcce26a2018-03-26 23:41:24 -070086 const Scalar ttrust_;
87 Scalar wheel_;
88 Scalar throttle_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000089 bool quickturn_;
Austin Schuh093535c2016-03-05 23:21:00 -080090
Comran Morshed5323ecb2015-12-26 20:50:55 +000091 Gear left_gear_;
92 Gear right_gear_;
Austin Schuh093535c2016-03-05 23:21:00 -080093
Alex Perrycb7da4b2019-08-28 19:35:56 -070094 ::frc971::control_loops::drivetrain::PositionT last_position_;
95 ::frc971::control_loops::drivetrain::PositionT position_;
Comran Morshed5323ecb2015-12-26 20:50:55 +000096 int counter_;
Austin Schuhbcce26a2018-03-26 23:41:24 -070097 DrivetrainConfig<Scalar> dt_config_;
Austin Schuh41565602016-02-28 20:10:49 -080098
Austin Schuhbcce26a2018-03-26 23:41:24 -070099 Scalar goal_left_velocity_ = 0.0;
100 Scalar goal_right_velocity_ = 0.0;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700101
102 // Stored from the last iteration, for logging shifting logic.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700103 Scalar left_motor_speed_ = 0.0;
104 Scalar right_motor_speed_ = 0.0;
105 Scalar current_left_velocity_ = 0.0;
106 Scalar current_right_velocity_ = 0.0;
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800107
108 // Feedforward voltage, for logging.
109 Eigen::Matrix<Scalar, 2, 1> ff_volts_{0.0, 0.0};
Comran Morshed5323ecb2015-12-26 20:50:55 +0000110};
111
Austin Schuhbcce26a2018-03-26 23:41:24 -0700112template <typename Scalar>
113PolyDrivetrain<Scalar>::PolyDrivetrain(
114 const DrivetrainConfig<Scalar> &dt_config,
115 StateFeedbackLoop<7, 2, 4, Scalar> *kf)
116 : kf_(kf),
117 U_Poly_((Eigen::Matrix<Scalar, 4, 2>() << /*[[*/ 1, 0 /*]*/,
118 /*[*/ -1, 0 /*]*/,
119 /*[*/ 0, 1 /*]*/,
120 /*[*/ 0, -1 /*]]*/)
121 .finished(),
122 (Eigen::Matrix<Scalar, 4, 1>() << /*[[*/ 12 /*]*/,
123 /*[*/ 12 /*]*/,
124 /*[*/ 12 /*]*/,
125 /*[*/ 12 /*]]*/)
126 .finished(),
127 (Eigen::Matrix<Scalar, 2, 4>() << /*[[*/ 12, 12, -12, -12 /*]*/,
128 /*[*/ -12, 12, 12, -12 /*]*/)
129 .finished()),
130 loop_(new StateFeedbackLoop<2, 2, 2, Scalar>(
131 dt_config.make_v_drivetrain_loop())),
132 ttrust_(1.1),
133 wheel_(0.0),
134 throttle_(0.0),
135 quickturn_(false),
136 left_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
137 right_gear_(dt_config.default_high_gear ? Gear::HIGH : Gear::LOW),
138 counter_(0),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 dt_config_(dt_config) {}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700140
141template <typename Scalar>
142Scalar PolyDrivetrain<Scalar>::MotorSpeed(
143 const constants::ShifterHallEffect &hall_effect, Scalar shifter_position,
144 Scalar velocity, Gear gear) {
145 const Scalar high_gear_speed =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700146 velocity /
147 static_cast<Scalar>(dt_config_.high_gear_ratio / dt_config_.wheel_radius);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700148 const Scalar low_gear_speed =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700149 velocity /
150 static_cast<Scalar>(dt_config_.low_gear_ratio / dt_config_.wheel_radius);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700151
Brian Silverman342d4dc2018-09-02 16:26:01 -0700152 if (shifter_position < static_cast<Scalar>(hall_effect.clear_low)) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700153 // We're in low gear, so return speed for that gear.
154 return low_gear_speed;
Brian Silverman342d4dc2018-09-02 16:26:01 -0700155 } else if (shifter_position > static_cast<Scalar>(hall_effect.clear_high)) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700156 // We're in high gear, so return speed for that gear.
157 return high_gear_speed;
158 }
159
160 // Not in gear, so speed-match to destination gear.
161 switch (gear) {
162 case Gear::HIGH:
163 case Gear::SHIFTING_UP:
164 return high_gear_speed;
165 case Gear::LOW:
166 case Gear::SHIFTING_DOWN:
167 default:
168 return low_gear_speed;
169 break;
170 }
171}
172
173template <typename Scalar>
174Gear PolyDrivetrain<Scalar>::UpdateSingleGear(Gear requested_gear,
175 Gear current_gear) {
176 const Gear shift_up =
177 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
178 ? Gear::SHIFTING_UP
179 : Gear::HIGH;
180 const Gear shift_down =
181 (dt_config_.shifter_type == ShifterType::HALL_EFFECT_SHIFTER)
182 ? Gear::SHIFTING_DOWN
183 : Gear::LOW;
184 if (current_gear != requested_gear) {
185 if (IsInGear(current_gear)) {
186 if (requested_gear == Gear::HIGH) {
187 if (current_gear != Gear::HIGH) {
188 current_gear = shift_up;
189 }
190 } else {
191 if (current_gear != Gear::LOW) {
192 current_gear = shift_down;
193 }
194 }
195 } else {
196 if (requested_gear == Gear::HIGH && current_gear == Gear::SHIFTING_DOWN) {
197 current_gear = Gear::SHIFTING_UP;
198 } else if (requested_gear == Gear::LOW &&
199 current_gear == Gear::SHIFTING_UP) {
200 current_gear = Gear::SHIFTING_DOWN;
201 }
202 }
203 }
204 return current_gear;
205}
206
207template <typename Scalar>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208void PolyDrivetrain<Scalar>::SetGoal(const Scalar wheel, const Scalar throttle,
209 const bool quickturn,
210 const bool highgear) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700211 // Apply a sin function that's scaled to make it feel better.
Brian Silverman342d4dc2018-09-02 16:26:01 -0700212 const Scalar angular_range =
213 static_cast<Scalar>(M_PI_2) * dt_config_.wheel_non_linearity;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700214
215 wheel_ = sin(angular_range * wheel) / sin(angular_range);
216 wheel_ = sin(angular_range * wheel_) / sin(angular_range);
Brian Silverman342d4dc2018-09-02 16:26:01 -0700217 wheel_ = kTwo * wheel - wheel_;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700218 quickturn_ = quickturn;
219
220 if (quickturn_) {
221 wheel_ *= dt_config_.quickturn_wheel_multiplier;
222 } else {
223 wheel_ *= dt_config_.wheel_multiplier;
224 }
225
Brian Silverman342d4dc2018-09-02 16:26:01 -0700226 static constexpr Scalar kThrottleDeadband = static_cast<Scalar>(0.05);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700227 if (::std::abs(throttle) < kThrottleDeadband) {
228 throttle_ = 0;
229 } else {
230 throttle_ = copysign(
Brian Silverman342d4dc2018-09-02 16:26:01 -0700231 (::std::abs(throttle) - kThrottleDeadband) / (kOne - kThrottleDeadband),
Austin Schuhbcce26a2018-03-26 23:41:24 -0700232 throttle);
233 }
234
235 Gear requested_gear = highgear ? Gear::HIGH : Gear::LOW;
236
237 left_gear_ = UpdateSingleGear(requested_gear, left_gear_);
238 right_gear_ = UpdateSingleGear(requested_gear, right_gear_);
239}
240
241template <typename Scalar>
242void PolyDrivetrain<Scalar>::SetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700243 const ::frc971::control_loops::drivetrain::Position *position,
Austin Schuhbcce26a2018-03-26 23:41:24 -0700244 Gear left_gear, Gear right_gear) {
245 left_gear_ = left_gear;
246 right_gear_ = right_gear;
247 last_position_ = position_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 position->UnPackTo(&position_);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700249}
250
251template <typename Scalar>
252Scalar PolyDrivetrain<Scalar>::FilterVelocity(Scalar throttle) const {
253 const Eigen::Matrix<Scalar, 2, 2> FF =
254 loop_->plant().B().inverse() *
255 (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A());
256
257 constexpr int kHighGearController = 3;
258 const Eigen::Matrix<Scalar, 2, 2> FF_high =
259 loop_->plant().coefficients(kHighGearController).B.inverse() *
260 (Eigen::Matrix<Scalar, 2, 2>::Identity() -
261 loop_->plant().coefficients(kHighGearController).A);
262
263 ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum();
264 int min_FF_sum_index;
265 const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
266 const Scalar min_K_sum = loop_->controller().K().col(min_FF_sum_index).sum();
267 const Scalar high_min_FF_sum = FF_high.col(0).sum();
268
269 const Scalar adjusted_ff_voltage =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700270 ::aos::Clip(throttle * kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700271 return (adjusted_ff_voltage +
Brian Silverman342d4dc2018-09-02 16:26:01 -0700272 ttrust_ * min_K_sum * (loop_->X_hat(0, 0) + loop_->X_hat(1, 0)) *
273 kHalf) /
Austin Schuhbcce26a2018-03-26 23:41:24 -0700274 (ttrust_ * min_K_sum + min_FF_sum);
275}
276
277template <typename Scalar>
278Scalar PolyDrivetrain<Scalar>::MaxVelocity() {
279 const Eigen::Matrix<Scalar, 2, 2> FF =
280 loop_->plant().B().inverse() *
281 (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A());
282
283 constexpr int kHighGearController = 3;
284 const Eigen::Matrix<Scalar, 2, 2> FF_high =
285 loop_->plant().coefficients(kHighGearController).B.inverse() *
286 (Eigen::Matrix<Scalar, 2, 2>::Identity() -
287 loop_->plant().coefficients(kHighGearController).A);
288
289 ::Eigen::Matrix<Scalar, 1, 2> FF_sum = FF.colwise().sum();
290 int min_FF_sum_index;
291 const Scalar min_FF_sum = FF_sum.minCoeff(&min_FF_sum_index);
292 // const Scalar min_K_sum = loop_->K().col(min_FF_sum_index).sum();
293 const Scalar high_min_FF_sum = FF_high.col(0).sum();
294
295 const Scalar adjusted_ff_voltage =
Brian Silverman342d4dc2018-09-02 16:26:01 -0700296 ::aos::Clip(kTwelve * min_FF_sum / high_min_FF_sum, -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700297 return adjusted_ff_voltage / min_FF_sum;
298}
299
300template <typename Scalar>
Austin Schuheeec74a2019-01-27 20:58:59 -0800301void PolyDrivetrain<Scalar>::Update(Scalar voltage_battery) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700302 if (dt_config_.loop_type == LoopType::CLOSED_LOOP) {
303 loop_->mutable_X_hat()(0, 0) = kf_->X_hat()(1, 0);
304 loop_->mutable_X_hat()(1, 0) = kf_->X_hat()(3, 0);
305 }
306
307 // TODO(austin): Observer for the current velocity instead of difference
308 // calculations.
309 ++counter_;
310
311 if (IsInGear(left_gear_) && IsInGear(right_gear_)) {
312 // FF * X = U (steady state)
313 const Eigen::Matrix<Scalar, 2, 2> FF =
314 loop_->plant().B().inverse() *
315 (Eigen::Matrix<Scalar, 2, 2>::Identity() - loop_->plant().A());
316
317 // Invert the plant to figure out how the velocity filter would have to
318 // work
319 // out in order to filter out the forwards negative inertia.
320 // This math assumes that the left and right power and velocity are
321 // equals,
322 // and that the plant is the same on the left and right.
323 const Scalar fvel = FilterVelocity(throttle_);
324
Brian Silverman342d4dc2018-09-02 16:26:01 -0700325 const Scalar sign_svel = wheel_ * ((fvel > kZero) ? kOne : -kOne);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700326 Scalar steering_velocity;
327 if (quickturn_) {
328 steering_velocity = wheel_ * MaxVelocity();
329 } else {
330 steering_velocity = ::std::abs(fvel) * wheel_;
331 }
332 const Scalar left_velocity = fvel - steering_velocity;
333 const Scalar right_velocity = fvel + steering_velocity;
334 goal_left_velocity_ = left_velocity;
335 goal_right_velocity_ = right_velocity;
336
337 // Integrate velocity to get the position.
338 // This position is used to get integral control.
339 loop_->mutable_R() << left_velocity, right_velocity;
340
341 if (!quickturn_) {
342 // K * R = w
343 Eigen::Matrix<Scalar, 1, 2> equality_k;
344 equality_k << 1 + sign_svel, -(1 - sign_svel);
Brian Silverman342d4dc2018-09-02 16:26:01 -0700345 const Scalar equality_w = kZero;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700346
347 // Construct a constraint on R by manipulating the constraint on U
348 ::aos::controls::HVPolytope<2, 4, 4, Scalar> R_poly_hv(
349 U_Poly_.static_H() * (loop_->controller().K() + FF),
350 U_Poly_.static_k() +
351 U_Poly_.static_H() * loop_->controller().K() * loop_->X_hat(),
352 (loop_->controller().K() + FF).inverse() *
353 ::aos::controls::ShiftPoints<2, 4, Scalar>(
354 U_Poly_.StaticVertices(),
355 loop_->controller().K() * loop_->X_hat()));
356
357 // Limit R back inside the box.
358 loop_->mutable_R() =
359 CoerceGoal<Scalar>(R_poly_hv, equality_k, equality_w, loop_->R());
360 }
361
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800362 ff_volts_ = FF * loop_->R();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700363 const Eigen::Matrix<Scalar, 2, 1> U_ideal =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800364 loop_->controller().K() * (loop_->R() - loop_->X_hat()) + ff_volts_;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700365
366 for (int i = 0; i < 2; i++) {
367 loop_->mutable_U()[i] = ::aos::Clip(U_ideal[i], -12, 12);
368 }
369
370 if (dt_config_.loop_type == LoopType::OPEN_LOOP) {
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800371 ff_volts_.setZero();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700372 loop_->mutable_X_hat() =
373 loop_->plant().A() * loop_->X_hat() + loop_->plant().B() * loop_->U();
374 }
375
376 // Housekeeping: set the shifting logging values to zero, because we're not shifting
Brian Silverman342d4dc2018-09-02 16:26:01 -0700377 left_motor_speed_ = kZero;
378 right_motor_speed_ = kZero;
379 current_left_velocity_ = kZero;
380 current_right_velocity_ = kZero;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700381 } else {
Austin Schuhbb735b72019-01-03 12:58:41 -0800382 const Scalar dt =
383 ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>(
384 dt_config_.dt)
385 .count();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700386 current_left_velocity_ =
Austin Schuhbb735b72019-01-03 12:58:41 -0800387 (position_.left_encoder - last_position_.left_encoder) / dt;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700388 current_right_velocity_ =
Austin Schuhbb735b72019-01-03 12:58:41 -0800389 (position_.right_encoder - last_position_.right_encoder) / dt;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700390 left_motor_speed_ =
391 MotorSpeed(dt_config_.left_drive, position_.left_shifter_position,
392 current_left_velocity_, left_gear_);
393 right_motor_speed_ =
394 MotorSpeed(dt_config_.right_drive, position_.right_shifter_position,
395 current_right_velocity_, right_gear_);
396
397 goal_left_velocity_ = current_left_velocity_;
398 goal_right_velocity_ = current_right_velocity_;
399
400 // Any motor is not in gear. Speed match.
401 ::Eigen::Matrix<Scalar, 1, 1> R_left;
402 ::Eigen::Matrix<Scalar, 1, 1> R_right;
403 R_left(0, 0) = left_motor_speed_;
404 R_right(0, 0) = right_motor_speed_;
405
Brian Silverman342d4dc2018-09-02 16:26:01 -0700406 const Scalar wiggle = (static_cast<Scalar>((counter_ % 30) / 15) - kHalf) *
407 static_cast<Scalar>(8.0);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700408
409 loop_->mutable_U(0, 0) = ::aos::Clip(
410 (R_left / dt_config_.v)(0, 0) + (IsInGear(left_gear_) ? 0 : wiggle),
Brian Silverman342d4dc2018-09-02 16:26:01 -0700411 -kTwelve, kTwelve);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700412 loop_->mutable_U(1, 0) = ::aos::Clip(
413 (R_right / dt_config_.v)(0, 0) + (IsInGear(right_gear_) ? 0 : wiggle),
Brian Silverman342d4dc2018-09-02 16:26:01 -0700414 -kTwelve, kTwelve);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800415 ff_volts_ = loop_->U();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700416#ifdef __linux__
Austin Schuheeec74a2019-01-27 20:58:59 -0800417 loop_->mutable_U() *= kTwelve / voltage_battery;
418#else
419 (void)voltage_battery;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700420#endif // __linux__
421 }
422}
423
424template <typename Scalar>
425void PolyDrivetrain<Scalar>::SetOutput(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426 ::frc971::control_loops::drivetrain::OutputT *output) {
427 if (output != nullptr) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700428 output->left_voltage = loop_->U(0, 0);
429 output->right_voltage = loop_->U(1, 0);
430 output->left_high = MaybeHigh(left_gear_);
431 output->right_high = MaybeHigh(right_gear_);
432 }
433}
434
435template <typename Scalar>
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800436flatbuffers::Offset<PolyDriveLogging>
437PolyDrivetrain<Scalar>::PopulateStatus(
438 flatbuffers::FlatBufferBuilder *fbb) {
439 PolyDriveLogging::Builder builder(*fbb);
440
441 builder.add_goal_left_velocity(goal_left_velocity_);
442 builder.add_goal_right_velocity(goal_right_velocity_);
443 builder.add_ff_left_voltage(ff_volts_(0, 0));
444 builder.add_ff_right_voltage(ff_volts_(1, 0));
445
446 return builder.Finish();
447}
448
449template <typename Scalar>
450flatbuffers::Offset<CIMLogging> PolyDrivetrain<Scalar>::PopulateShiftingStatus(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451 flatbuffers::FlatBufferBuilder *fbb) {
452 CIMLogging::Builder builder(*fbb);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700453
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454 builder.add_left_in_gear(IsInGear(left_gear_));
455 builder.add_left_motor_speed(left_motor_speed_);
456 builder.add_left_velocity(current_left_velocity_);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700457
Alex Perrycb7da4b2019-08-28 19:35:56 -0700458 builder.add_right_in_gear(IsInGear(right_gear_));
459 builder.add_right_motor_speed(right_motor_speed_);
460 builder.add_right_velocity(current_right_velocity_);
461
462 return builder.Finish();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700463}
464
Comran Morshed5323ecb2015-12-26 20:50:55 +0000465} // namespace drivetrain
466} // namespace control_loops
467} // namespace frc971
468
469#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_