blob: d097ec82258b61170c8b0610fe99eed38a139442 [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#include "frc971/control_loops/drivetrain/drivetrain.h"
Brian Silverman17f503e2015-08-02 18:17:18 -07002
3#include <stdio.h>
4#include <sched.h>
5#include <cmath>
6#include <memory>
7#include "Eigen/Dense"
8
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
10#include "aos/logging/queue_logging.h"
11#include "aos/logging/matrix_logging.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070012
Austin Schuh3a378462019-01-04 21:48:04 -080013#include "frc971/control_loops/drivetrain/down_estimator.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000014#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh3a378462019-01-04 21:48:04 -080015#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000016#include "frc971/control_loops/drivetrain/polydrivetrain.h"
17#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Austin Schuh3a378462019-01-04 21:48:04 -080018#include "frc971/control_loops/runge_kutta.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070019#include "frc971/queues/gyro.q.h"
20#include "frc971/shifter_hall_effect.h"
Austin Schuh05c5a612016-04-02 15:10:25 -070021#include "frc971/wpilib/imu.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070022
Brian Silverman17f503e2015-08-02 18:17:18 -070023using frc971::sensors::gyro_reading;
Campbell Crowley2527ed22017-02-17 21:10:02 -080024using frc971::imu_values;
Austin Schuh9993fb32017-03-15 20:17:46 -070025using ::aos::monotonic_clock;
26namespace chrono = ::std::chrono;
Brian Silverman17f503e2015-08-02 18:17:18 -070027
Comran Morshed5323ecb2015-12-26 20:50:55 +000028namespace frc971 {
Brian Silverman17f503e2015-08-02 18:17:18 -070029namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080030namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070031
Austin Schuh55a13dc2019-01-27 22:39:03 -080032DrivetrainLoop::DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
33 ::aos::EventLoop *event_loop,
34 const ::std::string &name)
Comran Morshed5323ecb2015-12-26 20:50:55 +000035 : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>(
Austin Schuh55a13dc2019-01-27 22:39:03 -080036 event_loop, name),
Comran Morshed5323ecb2015-12-26 20:50:55 +000037 dt_config_(dt_config),
Austin Schuh41565602016-02-28 20:10:49 -080038 kf_(dt_config_.make_kf_drivetrain_loop()),
39 dt_openloop_(dt_config_, &kf_),
Austin Schuh093535c2016-03-05 23:21:00 -080040 dt_closedloop_(dt_config_, &kf_, &integrated_kf_heading_),
Alex Perry731b4602019-02-02 22:13:01 -080041 dt_spline_(dt_config_),
Austin Schuh05c5a612016-04-02 15:10:25 -070042 down_estimator_(MakeDownEstimatorLoop()),
Austin Schuh093535c2016-03-05 23:21:00 -080043 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
44 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
45 left_high_requested_(dt_config_.default_high_gear),
46 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuh209f1702015-11-29 17:03:00 -080047 ::aos::controls::HPolytope<0>::Init();
Austin Schuh05c5a612016-04-02 15:10:25 -070048 down_U_.setZero();
Austin Schuh209f1702015-11-29 17:03:00 -080049}
50
Austin Schuh093535c2016-03-05 23:21:00 -080051int DrivetrainLoop::ControllerIndexFromGears() {
52 if (MaybeHigh(left_gear_)) {
53 if (MaybeHigh(right_gear_)) {
54 return 3;
55 } else {
56 return 2;
57 }
58 } else {
59 if (MaybeHigh(right_gear_)) {
60 return 1;
61 } else {
62 return 0;
63 }
64 }
65}
66
67Gear ComputeGear(double shifter_position,
68 const constants::ShifterHallEffect &shifter_config,
69 bool high_requested) {
70 if (shifter_position < shifter_config.clear_low) {
71 return Gear::LOW;
72 } else if (shifter_position > shifter_config.clear_high) {
73 return Gear::HIGH;
74 } else {
75 if (high_requested) {
76 return Gear::SHIFTING_UP;
77 } else {
78 return Gear::SHIFTING_DOWN;
79 }
80 }
81}
82
Austin Schuh3a378462019-01-04 21:48:04 -080083::Eigen::Matrix<double, 3, 1> DrivetrainLoop::PredictState(
84 const ::Eigen::Matrix<double, 3, 1> &xytheta_state,
85 const ::Eigen::Matrix<double, 7, 1> &state,
86 const ::Eigen::Matrix<double, 7, 1> &previous_state) const {
87 const double dt =
88 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
89 dt_config_.dt)
90 .count();
91
92 const double distance_traveled =
93 (state(0) + state(2)) / 2.0 -
94 (previous_state(0) + previous_state(2)) / 2.0;
95
96 const double omega0 =
97 (previous_state(3) - previous_state(1)) / (dt_config_.robot_radius * 2.0);
98 const double omega1 = (state(3) - state(1)) / (dt_config_.robot_radius * 2.0);
99 const double alpha = (omega1 - omega0) / dt;
100
101 const double velocity_start = (previous_state(3) + previous_state(1)) / 2.0;
102 const double velocity_end = (state(3) + state(1)) / 2.0;
103
104 const double acceleration = (velocity_end - velocity_start) / dt;
105 const double velocity_offset =
106 distance_traveled / dt - 0.5 * acceleration * dt - velocity_start;
107 const double velocity0 = velocity_start + velocity_offset;
108
109 // TODO(austin): Substep 10x here. This is super important! ?
110 return RungeKutta(
111 [&dt, &velocity0, &acceleration, &omega0, &alpha](
112 double t, const ::Eigen::Matrix<double, 3, 1> &X) {
113 const double velocity1 = velocity0 + acceleration * t;
114 const double omega1 = omega0 + alpha * t;
115 const double theta = X(2);
116
117 return (::Eigen::Matrix<double, 3, 1>()
118 << ::std::cos(theta) * velocity1,
119 ::std::sin(theta) * velocity1, omega1)
120 .finished();
121 },
122 xytheta_state, 0.0,
123 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
124 dt_config_.dt)
125 .count());
126}
127
Austin Schuh6197a182015-11-28 16:04:40 -0800128void DrivetrainLoop::RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +0000129 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
130 const ::frc971::control_loops::DrivetrainQueue::Position *position,
131 ::frc971::control_loops::DrivetrainQueue::Output *output,
132 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Austin Schuh9993fb32017-03-15 20:17:46 -0700133 monotonic_clock::time_point monotonic_now = monotonic_clock::now();
134
Austin Schuh5900d142016-04-03 21:35:12 -0700135 if (!has_been_enabled_ && output) {
136 has_been_enabled_ = true;
137 down_estimator_.mutable_X_hat(1, 0) = 0.0;
138 }
139
Austin Schuh093535c2016-03-05 23:21:00 -0800140 // TODO(austin): Put gear detection logic here.
141 switch (dt_config_.shifter_type) {
142 case ShifterType::SIMPLE_SHIFTER:
143 // Force the right controller for simple shifters since we assume that
144 // gear switching is instantaneous.
145 if (left_high_requested_) {
146 left_gear_ = Gear::HIGH;
147 } else {
148 left_gear_ = Gear::LOW;
149 }
150 if (right_high_requested_) {
151 right_gear_ = Gear::HIGH;
152 } else {
153 right_gear_ = Gear::LOW;
154 }
155 break;
156 case ShifterType::HALL_EFFECT_SHIFTER:
157 left_gear_ = ComputeGear(position->left_shifter_position,
158 dt_config_.left_drive, left_high_requested_);
159 right_gear_ = ComputeGear(position->right_shifter_position,
160 dt_config_.right_drive, right_high_requested_);
161 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700162 case ShifterType::NO_SHIFTER:
163 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700165
Austin Schuhc5fceb82017-02-25 16:24:12 -0800166 kf_.set_index(ControllerIndexFromGears());
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700167
168 // Set the gear-logging parts of the status
169 if (status) {
170 status->gear_logging.left_state = static_cast<uint32_t>(left_gear_);
171 status->gear_logging.right_state = static_cast<uint32_t>(right_gear_);
172 status->gear_logging.left_loop_high = MaybeHigh(left_gear_);
173 status->gear_logging.right_loop_high = MaybeHigh(right_gear_);
174 status->gear_logging.controller_index = kf_.index();
Austin Schuh093535c2016-03-05 23:21:00 -0800175 }
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700176
Campbell Crowley2527ed22017-02-17 21:10:02 -0800177 const bool is_latest_imu_values = ::frc971::imu_values.FetchLatest();
178 if (is_latest_imu_values) {
Austin Schuh05c5a612016-04-02 15:10:25 -0700179 const double rate = -::frc971::imu_values->gyro_y;
180 const double accel_squared = ::frc971::imu_values->accelerometer_x *
181 ::frc971::imu_values->accelerometer_x +
182 ::frc971::imu_values->accelerometer_y *
183 ::frc971::imu_values->accelerometer_y +
184 ::frc971::imu_values->accelerometer_z *
185 ::frc971::imu_values->accelerometer_z;
Austin Schuhf0c05762016-04-03 16:06:49 -0700186 const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x,
187 ::frc971::imu_values->accelerometer_z) +
Austin Schuh05c5a612016-04-02 15:10:25 -0700188 0.008;
Diana Burgessd0180f12018-03-21 21:24:17 -0700189
190 switch (dt_config_.imu_type) {
191 case IMUType::IMU_X:
192 last_accel_ = -::frc971::imu_values->accelerometer_x;
193 break;
194 case IMUType::IMU_Y:
195 last_accel_ = -::frc971::imu_values->accelerometer_y;
196 break;
197 }
198
Austin Schuh05c5a612016-04-02 15:10:25 -0700199 if (accel_squared > 1.03 || accel_squared < 0.97) {
200 LOG(DEBUG, "New IMU value, rejecting reading\n");
201 } else {
202 // -y is our gyro.
Austin Schuhf0c05762016-04-03 16:06:49 -0700203 // z accel is down
204 // x accel is the front of the robot pointed down.
Austin Schuh05c5a612016-04-02 15:10:25 -0700205 Eigen::Matrix<double, 1, 1> Y;
Austin Schuhdf79d112016-10-15 21:25:32 -0700206 Y(0, 0) = angle;
Austin Schuh05c5a612016-04-02 15:10:25 -0700207 down_estimator_.Correct(Y);
208 }
209
210 LOG(DEBUG,
Lee Mracek28313b02019-02-07 19:23:36 -0500211 "New IMU value, rate is %f, angle %f, fused %f, bias "
Austin Schuh05c5a612016-04-02 15:10:25 -0700212 "%f\n",
Austin Schuh3a378462019-01-04 21:48:04 -0800213 rate, angle, down_estimator_.X_hat(0), down_estimator_.X_hat(1));
Austin Schuhdf79d112016-10-15 21:25:32 -0700214 down_U_(0, 0) = rate;
Austin Schuh05c5a612016-04-02 15:10:25 -0700215 }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800216 down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency);
Austin Schuh05c5a612016-04-02 15:10:25 -0700217
Austin Schuh093535c2016-03-05 23:21:00 -0800218 // TODO(austin): Signal the current gear to both loops.
219
Campbell Crowley2527ed22017-02-17 21:10:02 -0800220 switch (dt_config_.gyro_type) {
221 case GyroType::IMU_X_GYRO:
222 if (is_latest_imu_values) {
223 LOG_STRUCT(DEBUG, "using", *imu_values.get());
224 last_gyro_rate_ = imu_values->gyro_x;
Austin Schuh9993fb32017-03-15 20:17:46 -0700225 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800226 }
227 break;
228 case GyroType::IMU_Y_GYRO:
229 if (is_latest_imu_values) {
230 LOG_STRUCT(DEBUG, "using", *imu_values.get());
231 last_gyro_rate_ = imu_values->gyro_y;
Austin Schuh9993fb32017-03-15 20:17:46 -0700232 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800233 }
234 break;
235 case GyroType::IMU_Z_GYRO:
236 if (is_latest_imu_values) {
237 LOG_STRUCT(DEBUG, "using", *imu_values.get());
238 last_gyro_rate_ = imu_values->gyro_z;
Austin Schuh9993fb32017-03-15 20:17:46 -0700239 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800240 }
241 break;
Austin Schuh90b43b42019-01-04 07:45:05 +1100242 case GyroType::FLIPPED_IMU_Z_GYRO:
243 if (is_latest_imu_values) {
244 LOG_STRUCT(DEBUG, "using", *imu_values.get());
245 last_gyro_rate_ = -imu_values->gyro_z;
246 last_gyro_time_ = monotonic_now;
247 }
248 break;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800249 case GyroType::SPARTAN_GYRO:
250 if (gyro_reading.FetchLatest()) {
251 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
252 last_gyro_rate_ = gyro_reading->velocity;
Austin Schuh9993fb32017-03-15 20:17:46 -0700253 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800254 }
255 break;
256 case GyroType::FLIPPED_SPARTAN_GYRO:
257 if (gyro_reading.FetchLatest()) {
258 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
259 last_gyro_rate_ = -gyro_reading->velocity;
Austin Schuh9993fb32017-03-15 20:17:46 -0700260 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800261 }
262 break;
263 default:
264 LOG(FATAL, "invalid gyro configured");
265 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800266 }
267
Austin Schuh9993fb32017-03-15 20:17:46 -0700268 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
269 last_gyro_rate_ = 0.0;
270 }
271
Austin Schuh6613a072016-01-06 19:54:48 -0800272 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700273 Eigen::Matrix<double, 4, 1> Y;
274 Y << position->left_encoder, position->right_encoder, last_gyro_rate_,
275 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800276 kf_.Correct(Y);
Austin Schuh3a378462019-01-04 21:48:04 -0800277
278 // We are going to choose to integrate velocity to get position by assuming
279 // that velocity is a linear function of time. For drivetrains with large
280 // amounts of mass, we won't get large changes in acceleration over a 5 ms
281 // timestep. Do note, the only place that this matters is when we are
282 // talking about the curvature errors introduced by integration. The
283 // velocities are scaled such that the distance traveled is correct.
284 //
285 // We want to do this after the kalman filter runs so we take into account
286 // the encoder and gyro corrections.
287 //
288 // Start by computing the beginning and ending linear and angular
289 // velocities.
290 // To handle 0 velocity well, compute the offset required to be added to
291 // both velocities to make the robot travel the correct distance.
292
293 xytheta_state_.block<3, 1>(0, 0) = PredictState(
294 xytheta_state_.block<3, 1>(0, 0), kf_.X_hat(), last_state_);
295
296 // Use trapezoidal integration for the gyro heading since it's more
297 // accurate.
298 const double average_angular_velocity =
299 ((kf_.X_hat(3) - kf_.X_hat(1)) + (last_state_(3) - last_state_(1))) /
300 2.0 / (dt_config_.robot_radius * 2.0);
301
Austin Schuhbb735b72019-01-03 12:58:41 -0800302 integrated_kf_heading_ +=
Austin Schuh3a378462019-01-04 21:48:04 -0800303 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
304 dt_config_.dt)
305 .count() *
306 average_angular_velocity;
307
308 // Copy over the gyro heading.
309 xytheta_state_(2) = integrated_kf_heading_;
310 // Copy over the velocities heading.
311 xytheta_state_(3) = kf_.X_hat(1);
312 xytheta_state_(4) = kf_.X_hat(3);
313 // Copy over the voltage errors.
314 xytheta_state_.block<2, 1>(5, 0) = kf_.X_hat().block<2, 1>(4, 0);
Austin Schuh093535c2016-03-05 23:21:00 -0800315
316 // gyro_heading = (real_right - real_left) / width
317 // wheel_heading = (wheel_right - wheel_left) / width
318 // gyro_heading + offset = wheel_heading
319 // gyro_goal + offset = wheel_goal
320 // offset = wheel_heading - gyro_heading
321
322 // gyro_goal + wheel_heading - gyro_heading = wheel_goal
Austin Schuh6613a072016-01-06 19:54:48 -0800323 }
324
Austin Schuh093535c2016-03-05 23:21:00 -0800325 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
326
Austin Schuh78379ea2019-01-04 20:39:45 -0800327 int controller_type = 0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700328 if (goal) {
Austin Schuh78379ea2019-01-04 20:39:45 -0800329 controller_type = goal->controller_type;
Brian Silverman17f503e2015-08-02 18:17:18 -0700330
Austin Schuh093535c2016-03-05 23:21:00 -0800331 dt_closedloop_.SetGoal(*goal);
332 dt_openloop_.SetGoal(*goal);
Alex Perry731b4602019-02-02 22:13:01 -0800333 dt_spline_.SetGoal(*goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700334 }
335
Austin Schuheeec74a2019-01-27 20:58:59 -0800336 dt_openloop_.Update(robot_state().voltage_battery);
Brian Silverman17f503e2015-08-02 18:17:18 -0700337
Austin Schuh78379ea2019-01-04 20:39:45 -0800338 dt_closedloop_.Update(output != NULL && controller_type == 1);
339
Alex Perrye32eabc2019-02-08 19:51:19 -0800340 dt_spline_.Update(output != NULL && controller_type == 2,
341 xytheta_state_.block<5, 1>(0, 0));
Alex Perrya71badb2019-02-06 19:40:41 -0800342
Austin Schuh78379ea2019-01-04 20:39:45 -0800343 switch (controller_type) {
344 case 0:
345 dt_openloop_.SetOutput(output);
346 break;
347 case 1:
348 dt_closedloop_.SetOutput(output);
349 break;
Alex Perry731b4602019-02-02 22:13:01 -0800350 case 2:
351 dt_spline_.SetOutput(output);
352 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700353 }
354
Austin Schuh093535c2016-03-05 23:21:00 -0800355 // The output should now contain the shift request.
356
Brian Silverman17f503e2015-08-02 18:17:18 -0700357 // set the output status of the control loop state
358 if (status) {
Austin Schuh3a378462019-01-04 21:48:04 -0800359 status->robot_speed = (kf_.X_hat(1) + kf_.X_hat(3)) / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700360
Austin Schuh093535c2016-03-05 23:21:00 -0800361 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700362 dt_config_.LeftRightToLinear(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800363 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700364 dt_config_.LeftRightToAngular(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800365
366 angular(0, 0) = integrated_kf_heading_;
367
368 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700369 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800370
371 status->estimated_left_position = gyro_left_right(0, 0);
372 status->estimated_right_position = gyro_left_right(2, 0);
373
374 status->estimated_left_velocity = gyro_left_right(1, 0);
375 status->estimated_right_velocity = gyro_left_right(3, 0);
376 status->output_was_capped = dt_closedloop_.output_was_capped();
377 status->uncapped_left_voltage = kf_.U_uncapped(0, 0);
378 status->uncapped_right_voltage = kf_.U_uncapped(1, 0);
379
Austin Schuh3a378462019-01-04 21:48:04 -0800380 status->left_voltage_error = kf_.X_hat(4);
381 status->right_voltage_error = kf_.X_hat(5);
382 status->estimated_angular_velocity_error = kf_.X_hat(6);
Austin Schuh093535c2016-03-05 23:21:00 -0800383 status->estimated_heading = integrated_kf_heading_;
Austin Schuh3a378462019-01-04 21:48:04 -0800384
385 status->x = xytheta_state_(0);
386 status->y = xytheta_state_(1);
387 status->theta = xytheta_state_(2);
388
389 status->ground_angle = down_estimator_.X_hat(0) + dt_config_.down_offset;
Austin Schuh41565602016-02-28 20:10:49 -0800390
391 dt_openloop_.PopulateStatus(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800392 dt_closedloop_.PopulateStatus(status);
Alex Perrye32eabc2019-02-08 19:51:19 -0800393 dt_spline_.PopulateStatus(status);
Brian Silverman17f503e2015-08-02 18:17:18 -0700394 }
Austin Schuh209f1702015-11-29 17:03:00 -0800395
Austin Schuh209f1702015-11-29 17:03:00 -0800396 double left_voltage = 0.0;
397 double right_voltage = 0.0;
398 if (output) {
399 left_voltage = output->left_voltage;
400 right_voltage = output->right_voltage;
Austin Schuh093535c2016-03-05 23:21:00 -0800401 left_high_requested_ = output->left_high;
402 right_high_requested_ = output->right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800403 }
404
Austin Schuheeec74a2019-01-27 20:58:59 -0800405 const double scalar = robot_state().voltage_battery / 12.0;
Austin Schuh209f1702015-11-29 17:03:00 -0800406
407 left_voltage *= scalar;
408 right_voltage *= scalar;
409
Austin Schuh209f1702015-11-29 17:03:00 -0800410 // To validate, look at the following:
411
412 // Observed - dx/dt velocity for left, right.
413
414 // Angular velocity error compared to the gyro
415 // Gyro heading vs left-right
416 // Voltage error.
417
418 Eigen::Matrix<double, 2, 1> U;
Austin Schuhdf79d112016-10-15 21:25:32 -0700419 U(0, 0) = last_left_voltage_;
420 U(1, 0) = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800421 last_left_voltage_ = left_voltage;
422 last_right_voltage_ = right_voltage;
423
Austin Schuh3a378462019-01-04 21:48:04 -0800424 last_state_ = kf_.X_hat();
425 kf_.UpdateObserver(U, dt_config_.dt);
Brian Silverman17f503e2015-08-02 18:17:18 -0700426}
427
Adam Snaiderbc918b62016-02-27 21:03:39 -0800428void DrivetrainLoop::Zero(
429 ::frc971::control_loops::DrivetrainQueue::Output *output) {
430 output->left_voltage = 0;
431 output->right_voltage = 0;
432 output->left_high = dt_config_.default_high_gear;
433 output->right_high = dt_config_.default_high_gear;
434}
435
Austin Schuh6197a182015-11-28 16:04:40 -0800436} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700437} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000438} // namespace frc971