blob: e7cfae68ed4a32d97d58b32ac38dca0939bf30ae [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;
Austin Schuh9993fb32017-03-15 20:17:46 -070024using ::aos::monotonic_clock;
25namespace chrono = ::std::chrono;
Brian Silverman17f503e2015-08-02 18:17:18 -070026
Comran Morshed5323ecb2015-12-26 20:50:55 +000027namespace frc971 {
Brian Silverman17f503e2015-08-02 18:17:18 -070028namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080029namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070030
Austin Schuh55a13dc2019-01-27 22:39:03 -080031DrivetrainLoop::DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
32 ::aos::EventLoop *event_loop,
James Kuszmaul3431d622019-02-17 17:07:44 -080033 LocalizerInterface *localizer,
Austin Schuh55a13dc2019-01-27 22:39:03 -080034 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),
James Kuszmaulef428a02019-03-02 22:19:41 -080038 localizer_control_fetcher_(event_loop->MakeFetcher<LocalizerControl>(
39 ".frc971.control_loops.drivetrain.localizer_control")),
Austin Schuh73b6e3b2019-05-27 16:37:15 -070040 imu_values_fetcher_(
41 event_loop->MakeFetcher<::frc971::IMUValues>(".frc971.imu_values")),
James Kuszmaul3431d622019-02-17 17:07:44 -080042 localizer_(localizer),
Austin Schuh41565602016-02-28 20:10:49 -080043 kf_(dt_config_.make_kf_drivetrain_loop()),
44 dt_openloop_(dt_config_, &kf_),
James Kuszmaul3431d622019-02-17 17:07:44 -080045 dt_closedloop_(dt_config_, &kf_, localizer_),
Alex Perry731b4602019-02-02 22:13:01 -080046 dt_spline_(dt_config_),
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080047 dt_line_follow_(dt_config_, localizer->target_selector()),
Austin Schuh05c5a612016-04-02 15:10:25 -070048 down_estimator_(MakeDownEstimatorLoop()),
Austin Schuh093535c2016-03-05 23:21:00 -080049 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
50 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
51 left_high_requested_(dt_config_.default_high_gear),
52 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuh209f1702015-11-29 17:03:00 -080053 ::aos::controls::HPolytope<0>::Init();
Austin Schuh05c5a612016-04-02 15:10:25 -070054 down_U_.setZero();
Austin Schuh209f1702015-11-29 17:03:00 -080055}
56
Austin Schuh093535c2016-03-05 23:21:00 -080057int DrivetrainLoop::ControllerIndexFromGears() {
58 if (MaybeHigh(left_gear_)) {
59 if (MaybeHigh(right_gear_)) {
60 return 3;
61 } else {
62 return 2;
63 }
64 } else {
65 if (MaybeHigh(right_gear_)) {
66 return 1;
67 } else {
68 return 0;
69 }
70 }
71}
72
73Gear ComputeGear(double shifter_position,
74 const constants::ShifterHallEffect &shifter_config,
75 bool high_requested) {
76 if (shifter_position < shifter_config.clear_low) {
77 return Gear::LOW;
78 } else if (shifter_position > shifter_config.clear_high) {
79 return Gear::HIGH;
80 } else {
81 if (high_requested) {
82 return Gear::SHIFTING_UP;
83 } else {
84 return Gear::SHIFTING_DOWN;
85 }
86 }
87}
88
Austin Schuh6197a182015-11-28 16:04:40 -080089void DrivetrainLoop::RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000090 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
91 const ::frc971::control_loops::DrivetrainQueue::Position *position,
92 ::frc971::control_loops::DrivetrainQueue::Output *output,
93 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Austin Schuh9993fb32017-03-15 20:17:46 -070094 monotonic_clock::time_point monotonic_now = monotonic_clock::now();
95
Austin Schuh5900d142016-04-03 21:35:12 -070096 if (!has_been_enabled_ && output) {
97 has_been_enabled_ = true;
98 down_estimator_.mutable_X_hat(1, 0) = 0.0;
99 }
100
Austin Schuh093535c2016-03-05 23:21:00 -0800101 // TODO(austin): Put gear detection logic here.
102 switch (dt_config_.shifter_type) {
103 case ShifterType::SIMPLE_SHIFTER:
104 // Force the right controller for simple shifters since we assume that
105 // gear switching is instantaneous.
106 if (left_high_requested_) {
107 left_gear_ = Gear::HIGH;
108 } else {
109 left_gear_ = Gear::LOW;
110 }
111 if (right_high_requested_) {
112 right_gear_ = Gear::HIGH;
113 } else {
114 right_gear_ = Gear::LOW;
115 }
116 break;
117 case ShifterType::HALL_EFFECT_SHIFTER:
118 left_gear_ = ComputeGear(position->left_shifter_position,
119 dt_config_.left_drive, left_high_requested_);
120 right_gear_ = ComputeGear(position->right_shifter_position,
121 dt_config_.right_drive, right_high_requested_);
122 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700123 case ShifterType::NO_SHIFTER:
124 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700126
Austin Schuhc5fceb82017-02-25 16:24:12 -0800127 kf_.set_index(ControllerIndexFromGears());
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700128
129 // Set the gear-logging parts of the status
130 if (status) {
131 status->gear_logging.left_state = static_cast<uint32_t>(left_gear_);
132 status->gear_logging.right_state = static_cast<uint32_t>(right_gear_);
133 status->gear_logging.left_loop_high = MaybeHigh(left_gear_);
134 status->gear_logging.right_loop_high = MaybeHigh(right_gear_);
135 status->gear_logging.controller_index = kf_.index();
Austin Schuh093535c2016-03-05 23:21:00 -0800136 }
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700137
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700138 const bool is_latest_imu_values = imu_values_fetcher_.Fetch();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800139 if (is_latest_imu_values) {
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700140 const double rate = -imu_values_fetcher_->gyro_y;
141 const double accel_squared =
142 ::std::pow(imu_values_fetcher_->accelerometer_x, 2.0) +
143 ::std::pow(imu_values_fetcher_->accelerometer_y, 2.0) +
144 ::std::pow(imu_values_fetcher_->accelerometer_z, 2.0);
145 const double angle = ::std::atan2(imu_values_fetcher_->accelerometer_x,
146 imu_values_fetcher_->accelerometer_z) +
Austin Schuh05c5a612016-04-02 15:10:25 -0700147 0.008;
Diana Burgessd0180f12018-03-21 21:24:17 -0700148
149 switch (dt_config_.imu_type) {
150 case IMUType::IMU_X:
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700151 last_accel_ = -imu_values_fetcher_->accelerometer_x;
Diana Burgessd0180f12018-03-21 21:24:17 -0700152 break;
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700153 case IMUType::IMU_FLIPPED_X:
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700154 last_accel_ = imu_values_fetcher_->accelerometer_x;
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700155 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700156 case IMUType::IMU_Y:
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700157 last_accel_ = -imu_values_fetcher_->accelerometer_y;
Diana Burgessd0180f12018-03-21 21:24:17 -0700158 break;
159 }
160
Austin Schuh05c5a612016-04-02 15:10:25 -0700161 if (accel_squared > 1.03 || accel_squared < 0.97) {
162 LOG(DEBUG, "New IMU value, rejecting reading\n");
163 } else {
164 // -y is our gyro.
Austin Schuhf0c05762016-04-03 16:06:49 -0700165 // z accel is down
166 // x accel is the front of the robot pointed down.
Austin Schuh05c5a612016-04-02 15:10:25 -0700167 Eigen::Matrix<double, 1, 1> Y;
Austin Schuhdf79d112016-10-15 21:25:32 -0700168 Y(0, 0) = angle;
Austin Schuh05c5a612016-04-02 15:10:25 -0700169 down_estimator_.Correct(Y);
170 }
171
172 LOG(DEBUG,
Lee Mracek28313b02019-02-07 19:23:36 -0500173 "New IMU value, rate is %f, angle %f, fused %f, bias "
Austin Schuh05c5a612016-04-02 15:10:25 -0700174 "%f\n",
Austin Schuh3a378462019-01-04 21:48:04 -0800175 rate, angle, down_estimator_.X_hat(0), down_estimator_.X_hat(1));
Austin Schuhdf79d112016-10-15 21:25:32 -0700176 down_U_(0, 0) = rate;
Austin Schuh05c5a612016-04-02 15:10:25 -0700177 }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800178 down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency);
Austin Schuh05c5a612016-04-02 15:10:25 -0700179
Austin Schuh093535c2016-03-05 23:21:00 -0800180 // TODO(austin): Signal the current gear to both loops.
181
Campbell Crowley2527ed22017-02-17 21:10:02 -0800182 switch (dt_config_.gyro_type) {
183 case GyroType::IMU_X_GYRO:
184 if (is_latest_imu_values) {
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700185 LOG_STRUCT(DEBUG, "using", *imu_values_fetcher_.get());
186 last_gyro_rate_ = imu_values_fetcher_->gyro_x;
Austin Schuh9993fb32017-03-15 20:17:46 -0700187 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800188 }
189 break;
190 case GyroType::IMU_Y_GYRO:
191 if (is_latest_imu_values) {
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700192 LOG_STRUCT(DEBUG, "using", *imu_values_fetcher_.get());
193 last_gyro_rate_ = imu_values_fetcher_->gyro_y;
Austin Schuh9993fb32017-03-15 20:17:46 -0700194 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800195 }
196 break;
197 case GyroType::IMU_Z_GYRO:
198 if (is_latest_imu_values) {
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700199 LOG_STRUCT(DEBUG, "using", *imu_values_fetcher_.get());
200 last_gyro_rate_ = imu_values_fetcher_->gyro_z;
Austin Schuh9993fb32017-03-15 20:17:46 -0700201 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800202 }
203 break;
Austin Schuh90b43b42019-01-04 07:45:05 +1100204 case GyroType::FLIPPED_IMU_Z_GYRO:
205 if (is_latest_imu_values) {
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700206 LOG_STRUCT(DEBUG, "using", *imu_values_fetcher_.get());
207 last_gyro_rate_ = -imu_values_fetcher_->gyro_z;
Austin Schuh90b43b42019-01-04 07:45:05 +1100208 last_gyro_time_ = monotonic_now;
209 }
210 break;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800211 case GyroType::SPARTAN_GYRO:
212 if (gyro_reading.FetchLatest()) {
213 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
214 last_gyro_rate_ = gyro_reading->velocity;
Austin Schuh9993fb32017-03-15 20:17:46 -0700215 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800216 }
217 break;
218 case GyroType::FLIPPED_SPARTAN_GYRO:
219 if (gyro_reading.FetchLatest()) {
220 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
221 last_gyro_rate_ = -gyro_reading->velocity;
Austin Schuh9993fb32017-03-15 20:17:46 -0700222 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800223 }
224 break;
225 default:
226 LOG(FATAL, "invalid gyro configured");
227 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800228 }
229
Austin Schuh9993fb32017-03-15 20:17:46 -0700230 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
231 last_gyro_rate_ = 0.0;
232 }
233
Austin Schuh6613a072016-01-06 19:54:48 -0800234 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700235 Eigen::Matrix<double, 4, 1> Y;
236 Y << position->left_encoder, position->right_encoder, last_gyro_rate_,
237 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800238 kf_.Correct(Y);
James Kuszmaulef428a02019-03-02 22:19:41 -0800239 // If we get a new message setting the absolute position, then reset the
240 // localizer.
241 // TODO(james): Use a watcher (instead of a fetcher) once we support it in
242 // simulation.
243 if (localizer_control_fetcher_.Fetch()) {
James Kuszmaul074429e2019-03-23 16:01:49 -0700244 LOG_STRUCT(DEBUG, "localizer_control", *localizer_control_fetcher_);
James Kuszmaul518640d2019-04-13 15:50:50 -0700245 localizer_->ResetPosition(
246 monotonic_now, localizer_control_fetcher_->x,
247 localizer_control_fetcher_->y, localizer_control_fetcher_->theta,
248 localizer_control_fetcher_->theta_uncertainty,
249 !localizer_control_fetcher_->keep_current_theta);
James Kuszmaulef428a02019-03-02 22:19:41 -0800250 }
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800251 localizer_->Update({last_last_left_voltage_, last_last_right_voltage_},
252 monotonic_now, position->left_encoder,
253 position->right_encoder, last_gyro_rate_, last_accel_);
Austin Schuh6613a072016-01-06 19:54:48 -0800254 }
255
Austin Schuh093535c2016-03-05 23:21:00 -0800256 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
257
Austin Schuh78379ea2019-01-04 20:39:45 -0800258 int controller_type = 0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700259 if (goal) {
Austin Schuh78379ea2019-01-04 20:39:45 -0800260 controller_type = goal->controller_type;
Brian Silverman17f503e2015-08-02 18:17:18 -0700261
Austin Schuh093535c2016-03-05 23:21:00 -0800262 dt_closedloop_.SetGoal(*goal);
263 dt_openloop_.SetGoal(*goal);
Alex Perry731b4602019-02-02 22:13:01 -0800264 dt_spline_.SetGoal(*goal);
James Kuszmaul38e79642019-03-09 15:48:27 -0800265 dt_line_follow_.SetGoal(monotonic_now, *goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700266 }
267
Austin Schuheeec74a2019-01-27 20:58:59 -0800268 dt_openloop_.Update(robot_state().voltage_battery);
Brian Silverman17f503e2015-08-02 18:17:18 -0700269
Austin Schuh78379ea2019-01-04 20:39:45 -0800270 dt_closedloop_.Update(output != NULL && controller_type == 1);
271
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800272 const Eigen::Matrix<double, 5, 1> trajectory_state =
273 (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(),
274 localizer_->theta(), localizer_->left_velocity(),
275 localizer_->right_velocity())
276 .finished();
277
278 dt_spline_.Update(output != NULL && controller_type == 2, trajectory_state);
279
James Kuszmaul38e79642019-03-09 15:48:27 -0800280 dt_line_follow_.Update(monotonic_now, trajectory_state);
Alex Perrya71badb2019-02-06 19:40:41 -0800281
Austin Schuh78379ea2019-01-04 20:39:45 -0800282 switch (controller_type) {
283 case 0:
284 dt_openloop_.SetOutput(output);
285 break;
286 case 1:
287 dt_closedloop_.SetOutput(output);
288 break;
Alex Perry731b4602019-02-02 22:13:01 -0800289 case 2:
290 dt_spline_.SetOutput(output);
291 break;
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800292 case 3:
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800293 if (!dt_line_follow_.SetOutput(output)) {
294 // If the line follow drivetrain was unable to execute (generally due to
295 // not having a target), execute the regular teleop drivetrain.
296 dt_openloop_.SetOutput(output);
297 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800298 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700299 }
300
Austin Schuh093535c2016-03-05 23:21:00 -0800301 // The output should now contain the shift request.
302
Brian Silverman17f503e2015-08-02 18:17:18 -0700303 // set the output status of the control loop state
304 if (status) {
Austin Schuh3a378462019-01-04 21:48:04 -0800305 status->robot_speed = (kf_.X_hat(1) + kf_.X_hat(3)) / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700306
Austin Schuh093535c2016-03-05 23:21:00 -0800307 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700308 dt_config_.LeftRightToLinear(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800309 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700310 dt_config_.LeftRightToAngular(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800311
James Kuszmaul3431d622019-02-17 17:07:44 -0800312 angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800313
314 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700315 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800316
317 status->estimated_left_position = gyro_left_right(0, 0);
318 status->estimated_right_position = gyro_left_right(2, 0);
319
320 status->estimated_left_velocity = gyro_left_right(1, 0);
321 status->estimated_right_velocity = gyro_left_right(3, 0);
322 status->output_was_capped = dt_closedloop_.output_was_capped();
323 status->uncapped_left_voltage = kf_.U_uncapped(0, 0);
324 status->uncapped_right_voltage = kf_.U_uncapped(1, 0);
325
Austin Schuh3a378462019-01-04 21:48:04 -0800326 status->left_voltage_error = kf_.X_hat(4);
327 status->right_voltage_error = kf_.X_hat(5);
328 status->estimated_angular_velocity_error = kf_.X_hat(6);
James Kuszmaul3431d622019-02-17 17:07:44 -0800329 status->estimated_heading = localizer_->theta();
Austin Schuh3a378462019-01-04 21:48:04 -0800330
James Kuszmaul3431d622019-02-17 17:07:44 -0800331 status->x = localizer_->x();
332 status->y = localizer_->y();
James Kuszmaul7f1a4082019-04-14 10:50:44 -0700333 status->theta = ::aos::math::NormalizeAngle(localizer_->theta());
Austin Schuh3a378462019-01-04 21:48:04 -0800334
335 status->ground_angle = down_estimator_.X_hat(0) + dt_config_.down_offset;
Austin Schuh41565602016-02-28 20:10:49 -0800336
337 dt_openloop_.PopulateStatus(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800338 dt_closedloop_.PopulateStatus(status);
Alex Perrye32eabc2019-02-08 19:51:19 -0800339 dt_spline_.PopulateStatus(status);
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800340 dt_line_follow_.PopulateStatus(status);
Brian Silverman17f503e2015-08-02 18:17:18 -0700341 }
Austin Schuh209f1702015-11-29 17:03:00 -0800342
Austin Schuh209f1702015-11-29 17:03:00 -0800343 double left_voltage = 0.0;
344 double right_voltage = 0.0;
345 if (output) {
346 left_voltage = output->left_voltage;
347 right_voltage = output->right_voltage;
Austin Schuh093535c2016-03-05 23:21:00 -0800348 left_high_requested_ = output->left_high;
349 right_high_requested_ = output->right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800350 }
351
Austin Schuheeec74a2019-01-27 20:58:59 -0800352 const double scalar = robot_state().voltage_battery / 12.0;
Austin Schuh209f1702015-11-29 17:03:00 -0800353
354 left_voltage *= scalar;
355 right_voltage *= scalar;
356
Austin Schuh209f1702015-11-29 17:03:00 -0800357 // To validate, look at the following:
358
359 // Observed - dx/dt velocity for left, right.
360
361 // Angular velocity error compared to the gyro
362 // Gyro heading vs left-right
363 // Voltage error.
364
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800365 last_last_left_voltage_ = last_left_voltage_;
366 last_last_right_voltage_ = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800367 Eigen::Matrix<double, 2, 1> U;
Austin Schuhdf79d112016-10-15 21:25:32 -0700368 U(0, 0) = last_left_voltage_;
369 U(1, 0) = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800370 last_left_voltage_ = left_voltage;
371 last_right_voltage_ = right_voltage;
372
Austin Schuh3a378462019-01-04 21:48:04 -0800373 last_state_ = kf_.X_hat();
374 kf_.UpdateObserver(U, dt_config_.dt);
Brian Silverman17f503e2015-08-02 18:17:18 -0700375}
376
Adam Snaiderbc918b62016-02-27 21:03:39 -0800377void DrivetrainLoop::Zero(
378 ::frc971::control_loops::DrivetrainQueue::Output *output) {
379 output->left_voltage = 0;
380 output->right_voltage = 0;
381 output->left_high = dt_config_.default_high_gear;
382 output->right_high = dt_config_.default_high_gear;
383}
384
Austin Schuh6197a182015-11-28 16:04:40 -0800385} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700386} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000387} // namespace frc971