blob: b33c48d3501ef6e700dd5908dcf20cd95fed560d [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
Brian Silverman17f503e2015-08-02 18:17:18 -07003#include <sched.h>
Austin Schuhd749d932020-12-30 21:38:40 -08004#include <stdio.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07005#include <cmath>
6#include <memory>
7#include "Eigen/Dense"
8
John Park33858a32018-09-28 23:05:48 -07009#include "aos/logging/logging.h"
Austin Schuh3a378462019-01-04 21:48:04 -080010#include "frc971/control_loops/drivetrain/down_estimator.h"
Austin Schuh3a378462019-01-04 21:48:04 -080011#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#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"
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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "frc971/queues/gyro_generated.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070020#include "frc971/shifter_hall_effect.h"
Austin Schuhac17fba2020-03-28 15:55:33 -070021#include "frc971/wpilib/imu_batch_generated.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070022
Austin Schuh9993fb32017-03-15 20:17:46 -070023using ::aos::monotonic_clock;
24namespace chrono = ::std::chrono;
Brian Silverman17f503e2015-08-02 18:17:18 -070025
Comran Morshed5323ecb2015-12-26 20:50:55 +000026namespace frc971 {
Brian Silverman17f503e2015-08-02 18:17:18 -070027namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080028namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070029
Austin Schuhfb0f35c2021-02-14 21:25:29 -080030DrivetrainFilters::DrivetrainFilters(const DrivetrainConfig<double> &dt_config,
31 ::aos::EventLoop *event_loop,
32 LocalizerInterface *localizer)
33 : dt_config_(dt_config),
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 localizer_control_fetcher_(
35 event_loop->MakeFetcher<LocalizerControl>("/drivetrain")),
Austin Schuh73b6e3b2019-05-27 16:37:15 -070036 imu_values_fetcher_(
Austin Schuhac17fba2020-03-28 15:55:33 -070037 event_loop->MakeFetcher<::frc971::IMUValuesBatch>("/drivetrain")),
Austin Schuh1ea89bb2019-05-27 16:59:59 -070038 gyro_reading_fetcher_(
39 event_loop->MakeFetcher<::frc971::sensors::GyroReading>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 "/drivetrain")),
Austin Schuhfb0f35c2021-02-14 21:25:29 -080041 down_estimator_(dt_config_),
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()),
Austin Schuh093535c2016-03-05 23:21:00 -080044 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
45 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
46 left_high_requested_(dt_config_.default_high_gear),
47 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuhfb0f35c2021-02-14 21:25:29 -080048 last_voltage_.setZero();
49 last_last_voltage_.setZero();
50 aos::controls::HPolytope<0>::Init();
James Kuszmaul30aca502020-01-19 15:05:33 -080051 event_loop->OnRun([this]() {
52 // On the first fetch, make sure that we are caught all the way up to the
53 // present.
54 imu_values_fetcher_.Fetch();
55 });
James Kuszmaul2215d922020-02-11 17:17:26 -080056 if (dt_config.is_simulated) {
57 down_estimator_.assume_perfect_gravity();
58 }
Austin Schuh209f1702015-11-29 17:03:00 -080059}
60
Austin Schuhfb0f35c2021-02-14 21:25:29 -080061flatbuffers::Offset<LocalizerState> DrivetrainFilters::PopulateLocalizerState(
62 flatbuffers::FlatBufferBuilder *fbb) {
63 return localizer_->PopulateStatus(fbb);
64}
65flatbuffers::Offset<ImuZeroerState> DrivetrainFilters::PopulateImuZeroerState(
66 flatbuffers::FlatBufferBuilder *fbb) {
67 return imu_zeroer_.PopulateStatus(fbb);
Austin Schuh093535c2016-03-05 23:21:00 -080068}
69
Austin Schuhfb0f35c2021-02-14 21:25:29 -080070flatbuffers::Offset<DownEstimatorState>
71DrivetrainFilters::PopulateDownEstimatorState(
72 flatbuffers::FlatBufferBuilder *fbb,
73 aos::monotonic_clock::time_point monotonic_now) {
74 return down_estimator_.PopulateStatus(fbb, monotonic_now);
Austin Schuh093535c2016-03-05 23:21:00 -080075}
76
Austin Schuhfb0f35c2021-02-14 21:25:29 -080077void DrivetrainFilters::Reset(aos::monotonic_clock::time_point monotonic_now,
78 const drivetrain::Position *position) {
79 // If all the sensors got reset (e.g., due to wpilib_interface restarting),
80 // reset the localizer and down estimator to avoid weird jumps in the
81 // filters.
82 down_estimator_.Reset();
83 // Just reset the localizer to the current state, except for the encoders.
84 LocalizerInterface::Ekf::State X_hat = localizer_->Xhat();
85 X_hat(LocalizerInterface::StateIdx::kLeftEncoder) = position->left_encoder();
86 X_hat(LocalizerInterface::StateIdx::kRightEncoder) =
87 position->right_encoder();
88 localizer_->Reset(monotonic_now, X_hat);
89}
Austin Schuh9993fb32017-03-15 20:17:46 -070090
Austin Schuhfb0f35c2021-02-14 21:25:29 -080091void DrivetrainFilters::Correct(aos::monotonic_clock::time_point monotonic_now,
92 const drivetrain::Position *position) {
Austin Schuh093535c2016-03-05 23:21:00 -080093 // TODO(austin): Put gear detection logic here.
94 switch (dt_config_.shifter_type) {
95 case ShifterType::SIMPLE_SHIFTER:
96 // Force the right controller for simple shifters since we assume that
97 // gear switching is instantaneous.
98 if (left_high_requested_) {
99 left_gear_ = Gear::HIGH;
100 } else {
101 left_gear_ = Gear::LOW;
102 }
103 if (right_high_requested_) {
104 right_gear_ = Gear::HIGH;
105 } else {
106 right_gear_ = Gear::LOW;
107 }
108 break;
109 case ShifterType::HALL_EFFECT_SHIFTER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700110 left_gear_ = ComputeGear(position->left_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800111 dt_config_.left_drive, left_high_requested_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 right_gear_ = ComputeGear(position->right_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800113 dt_config_.right_drive, right_high_requested_);
114 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700115 case ShifterType::NO_SHIFTER:
116 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700117 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700118
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800119 while (imu_values_fetcher_.FetchNext()) {
Austin Schuhac17fba2020-03-28 15:55:33 -0700120 CHECK(imu_values_fetcher_->has_readings());
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800121 last_gyro_time_ = monotonic_now;
Austin Schuhac17fba2020-03-28 15:55:33 -0700122 for (const IMUValues *value : *imu_values_fetcher_->readings()) {
123 imu_zeroer_.InsertMeasurement(*value);
124 if (!imu_zeroer_.Zeroed()) {
125 continue;
126 }
127 const aos::monotonic_clock::time_point reading_time(
128 std::chrono::nanoseconds(value->monotonic_timestamp_ns()));
129 if (last_imu_update_ == aos::monotonic_clock::min_time) {
130 last_imu_update_ = reading_time;
131 }
132 down_estimator_.Predict(imu_zeroer_.ZeroedGyro(),
133 imu_zeroer_.ZeroedAccel(),
134 reading_time - last_imu_update_);
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800135 last_imu_update_ = reading_time;
136 }
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800137 }
Diana Burgessd0180f12018-03-21 21:24:17 -0700138
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800139 bool got_imu_reading = false;
140 if (imu_values_fetcher_.get() != nullptr) {
James Kuszmaulb7f45bb2020-02-26 20:27:48 -0800141 imu_zeroer_.ProcessMeasurements();
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800142 got_imu_reading = true;
Austin Schuhac17fba2020-03-28 15:55:33 -0700143 CHECK(imu_values_fetcher_->has_readings());
144 const IMUValues *value = imu_values_fetcher_->readings()->Get(
145 imu_values_fetcher_->readings()->size() - 1);
Diana Burgessd0180f12018-03-21 21:24:17 -0700146 switch (dt_config_.imu_type) {
147 case IMUType::IMU_X:
Austin Schuhac17fba2020-03-28 15:55:33 -0700148 last_accel_ = -value->accelerometer_x();
Diana Burgessd0180f12018-03-21 21:24:17 -0700149 break;
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700150 case IMUType::IMU_FLIPPED_X:
Austin Schuhac17fba2020-03-28 15:55:33 -0700151 last_accel_ = value->accelerometer_x();
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700152 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700153 case IMUType::IMU_Y:
Austin Schuhac17fba2020-03-28 15:55:33 -0700154 last_accel_ = -value->accelerometer_y();
Diana Burgessd0180f12018-03-21 21:24:17 -0700155 break;
James Kuszmaul7f55f072020-03-01 10:21:26 -0800156 case IMUType::IMU_Z:
Austin Schuhac17fba2020-03-28 15:55:33 -0700157 last_accel_ = value->accelerometer_z();
James Kuszmaul7f55f072020-03-01 10:21:26 -0800158 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700159 }
Austin Schuh05c5a612016-04-02 15:10:25 -0700160 }
Austin Schuh05c5a612016-04-02 15:10:25 -0700161
Austin Schuh093535c2016-03-05 23:21:00 -0800162 // TODO(austin): Signal the current gear to both loops.
163
Campbell Crowley2527ed22017-02-17 21:10:02 -0800164 switch (dt_config_.gyro_type) {
165 case GyroType::IMU_X_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800166 if (got_imu_reading) {
167 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().x();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800168 }
169 break;
170 case GyroType::IMU_Y_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800171 if (got_imu_reading) {
172 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().y();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800173 }
174 break;
175 case GyroType::IMU_Z_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800176 if (got_imu_reading) {
177 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().z();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800178 }
179 break;
Austin Schuh90b43b42019-01-04 07:45:05 +1100180 case GyroType::FLIPPED_IMU_Z_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800181 if (got_imu_reading) {
182 last_gyro_rate_ = -imu_zeroer_.ZeroedGyro().z();
Austin Schuh90b43b42019-01-04 07:45:05 +1100183 }
184 break;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800185 case GyroType::SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700186 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 last_gyro_rate_ = gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700188 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800189 }
190 break;
191 case GyroType::FLIPPED_SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700192 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 last_gyro_rate_ = -gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700194 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800195 }
196 break;
197 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700198 AOS_LOG(FATAL, "invalid gyro configured");
Campbell Crowley2527ed22017-02-17 21:10:02 -0800199 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800200 }
201
Austin Schuh9993fb32017-03-15 20:17:46 -0700202 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
203 last_gyro_rate_ = 0.0;
204 }
205
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800206 localizer_->Update(
207 {last_last_voltage_(kLeftVoltage), last_last_voltage_(kRightVoltage)},
208 monotonic_now, position->left_encoder(), position->right_encoder(),
209 down_estimator_.avg_recent_yaw_rates(),
210 down_estimator_.avg_recent_accel());
211
212 // If we get a new message setting the absolute position, then reset the
213 // localizer.
214 if (localizer_control_fetcher_.Fetch()) {
215 VLOG(1) << "localizer_control "
216 << aos::FlatbufferToJson(localizer_control_fetcher_.get());
217 localizer_->ResetPosition(
218 monotonic_now, localizer_control_fetcher_->x(),
219 localizer_control_fetcher_->y(), localizer_control_fetcher_->theta(),
220 localizer_control_fetcher_->theta_uncertainty(),
221 !localizer_control_fetcher_->keep_current_theta());
222 }
223
224 kf_.set_index(ControllerIndexFromGears());
225
Austin Schuh6613a072016-01-06 19:54:48 -0800226 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700227 Eigen::Matrix<double, 4, 1> Y;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700228 Y << position->left_encoder(), position->right_encoder(), last_gyro_rate_,
Diana Burgessd0180f12018-03-21 21:24:17 -0700229 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800230 kf_.Correct(Y);
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800231 }
232}
233
234Eigen::Matrix<double, 2, 1> DrivetrainFilters::VoltageError() const {
235 static_assert(kLeftError + 1 == kRightError);
236 Eigen::Matrix<double, 2, 2> error_K;
237 error_K << kf_.controller().K(kLeftVoltage, kLeftError), 0.0, 0.0,
238 kf_.controller().K(kRightVoltage, kRightError);
239 const Eigen::Matrix<double, 2, 1> voltage_error =
240 error_K * kf_.X_hat().block<2, 1>(kLeftError, 0);
241 return voltage_error;
242}
243
244void DrivetrainFilters::UpdateObserver(Eigen::Matrix<double, 2, 1> U) {
245 last_last_voltage_ = last_voltage_;
246
247 kf_.UpdateObserver(last_voltage_, dt_config_.dt);
248
249 last_voltage_ = U;
250}
251
252int DrivetrainFilters::ControllerIndexFromGears() const {
253 if (MaybeHigh(left_gear_)) {
254 if (MaybeHigh(right_gear_)) {
255 return 3;
256 } else {
257 return 2;
258 }
259 } else {
260 if (MaybeHigh(right_gear_)) {
261 return 1;
262 } else {
263 return 0;
James Kuszmaul891f4f12020-10-31 17:13:23 -0700264 }
Austin Schuh6613a072016-01-06 19:54:48 -0800265 }
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800266}
267flatbuffers::Offset<GearLogging> DrivetrainFilters::CreateGearLogging(
268 flatbuffers::FlatBufferBuilder *fbb) const {
269 GearLogging::Builder gear_logging_builder(*fbb);
270 gear_logging_builder.add_left_state(static_cast<uint32_t>(left_gear_));
271 gear_logging_builder.add_right_state(static_cast<uint32_t>(right_gear_));
272 gear_logging_builder.add_left_loop_high(MaybeHigh(left_gear_));
273 gear_logging_builder.add_right_loop_high(MaybeHigh(right_gear_));
274 gear_logging_builder.add_controller_index(ControllerIndexFromGears());
275 return gear_logging_builder.Finish();
276}
Austin Schuh6613a072016-01-06 19:54:48 -0800277
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800278Gear DrivetrainFilters::ComputeGear(
279 double shifter_position, const constants::ShifterHallEffect &shifter_config,
280 bool high_requested) const {
281 if (shifter_position < shifter_config.clear_low) {
282 return Gear::LOW;
283 } else if (shifter_position > shifter_config.clear_high) {
284 return Gear::HIGH;
285 } else {
286 if (high_requested) {
287 return Gear::SHIFTING_UP;
288 } else {
289 return Gear::SHIFTING_DOWN;
290 }
291 }
292}
293
294DrivetrainLoop::DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
295 ::aos::EventLoop *event_loop,
296 LocalizerInterface *localizer,
297 const ::std::string &name)
298 : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
299 name),
300 dt_config_(dt_config),
301 filters_(dt_config, event_loop, localizer),
302 dt_openloop_(dt_config_, filters_.kf()),
303 dt_closedloop_(dt_config_, filters_.kf(), localizer),
304 dt_spline_(dt_config_),
305 dt_line_follow_(dt_config_, localizer->target_selector()) {
306 event_loop->SetRuntimeRealtimePriority(30);
307}
308
309void DrivetrainLoop::RunIteration(
310 const drivetrain::Goal *goal, const drivetrain::Position *position,
311 aos::Sender<drivetrain::Output>::Builder *output,
312 aos::Sender<drivetrain::Status>::Builder *status) {
313 const monotonic_clock::time_point monotonic_now =
314 event_loop()->monotonic_now();
315
316 if (!has_been_enabled_ && output) {
317 has_been_enabled_ = true;
318 }
319
320 if (WasReset()) {
321 filters_.Reset(monotonic_now, position);
322 }
323
324 filters_.Correct(monotonic_now, position);
325
326 // Set the gear-logging parts of the status
327 CHECK(status);
328 flatbuffers::Offset<GearLogging> gear_logging_offset =
329 filters_.CreateGearLogging(status->fbb());
330
331 dt_openloop_.SetPosition(position, filters_.left_gear(),
332 filters_.right_gear());
Austin Schuh093535c2016-03-05 23:21:00 -0800333
Austin Schuh872723c2019-12-25 14:38:09 -0800334 ControllerType controller_type = ControllerType::POLYDRIVE;
Brian Silverman17f503e2015-08-02 18:17:18 -0700335 if (goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 controller_type = goal->controller_type();
Brian Silverman17f503e2015-08-02 18:17:18 -0700337
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 dt_closedloop_.SetGoal(goal);
339 dt_openloop_.SetGoal(goal->wheel(), goal->throttle(), goal->quickturn(),
340 goal->highgear());
341 dt_spline_.SetGoal(goal);
342 dt_line_follow_.SetGoal(monotonic_now, goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700343 }
344
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 dt_openloop_.Update(robot_state().voltage_battery());
Brian Silverman17f503e2015-08-02 18:17:18 -0700346
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 dt_closedloop_.Update(output != nullptr &&
Austin Schuh872723c2019-12-25 14:38:09 -0800348 controller_type == ControllerType::MOTION_PROFILE);
Austin Schuh78379ea2019-01-04 20:39:45 -0800349
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800350 const Eigen::Matrix<double, 5, 1> trajectory_state =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800351 filters_.trajectory_state();
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800352
James Kuszmaul897ef1a2020-10-25 17:51:10 -0700353 {
354 // TODO(james): The regular Kalman Filter's voltage error terms are
355 // currently unusable--either don't use voltage error at all for the spline
356 // following code, or use the EKF's voltage error estimates.
357 const Eigen::Matrix<double, 2, 1> voltage_error =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800358 0 * filters_.VoltageError();
James Kuszmaul897ef1a2020-10-25 17:51:10 -0700359 dt_spline_.Update(
360 output != nullptr && controller_type == ControllerType::SPLINE_FOLLOWER,
361 trajectory_state, voltage_error);
362 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800363
James Kuszmaul38e79642019-03-09 15:48:27 -0800364 dt_line_follow_.Update(monotonic_now, trajectory_state);
Alex Perrya71badb2019-02-06 19:40:41 -0800365
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 OutputT output_struct;
367
Austin Schuh78379ea2019-01-04 20:39:45 -0800368 switch (controller_type) {
Austin Schuh872723c2019-12-25 14:38:09 -0800369 case ControllerType::POLYDRIVE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800371 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800372 case ControllerType::MOTION_PROFILE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700373 dt_closedloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800374 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800375 case ControllerType::SPLINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700376 dt_spline_.SetOutput(output != nullptr ? &output_struct : nullptr);
Alex Perry731b4602019-02-02 22:13:01 -0800377 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800378 case ControllerType::LINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700379 if (!dt_line_follow_.SetOutput(output != nullptr ? &output_struct
380 : nullptr)) {
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800381 // If the line follow drivetrain was unable to execute (generally due to
382 // not having a target), execute the regular teleop drivetrain.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700383 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800384 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800385 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700386 }
387
Austin Schuh093535c2016-03-05 23:21:00 -0800388 // The output should now contain the shift request.
389
Brian Silverman17f503e2015-08-02 18:17:18 -0700390 // set the output status of the control loop state
391 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800392 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800393 dt_config_.LeftRightToLinear(filters_.DrivetrainXHat());
Austin Schuh093535c2016-03-05 23:21:00 -0800394 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800395 dt_config_.LeftRightToAngular(filters_.DrivetrainXHat());
Austin Schuh093535c2016-03-05 23:21:00 -0800396
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800397 angular(0, 0) = filters_.localizer_theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800398
399 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700400 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800401
Alex Perrycb7da4b2019-08-28 19:35:56 -0700402 const flatbuffers::Offset<CIMLogging> cim_logging_offset =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800403 dt_openloop_.PopulateShiftingStatus(status->fbb());
404
405 const flatbuffers::Offset<PolyDriveLogging> poly_drive_logging_offset =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700406 dt_openloop_.PopulateStatus(status->fbb());
Austin Schuh093535c2016-03-05 23:21:00 -0800407
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800408 const flatbuffers::Offset<DownEstimatorState> down_estimator_state_offset =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800409 filters_.PopulateDownEstimatorState(status->fbb(), monotonic_now);
James Kuszmaul2215d922020-02-11 17:17:26 -0800410
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800411 const flatbuffers::Offset<LocalizerState> localizer_offset =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800412 filters_.PopulateLocalizerState(status->fbb());
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800413
James Kuszmaul2215d922020-02-11 17:17:26 -0800414 const flatbuffers::Offset<ImuZeroerState> zeroer_offset =
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800415 filters_.PopulateImuZeroerState(status->fbb());
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800416
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 flatbuffers::Offset<LineFollowLogging> line_follow_logging_offset =
418 dt_line_follow_.PopulateStatus(status);
419 flatbuffers::Offset<TrajectoryLogging> trajectory_logging_offset =
420 dt_spline_.MakeTrajectoryLogging(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800421
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800422 Status::Builder builder = status->MakeBuilder<Status>();
Austin Schuh3a378462019-01-04 21:48:04 -0800423
Alex Perrycb7da4b2019-08-28 19:35:56 -0700424 dt_closedloop_.PopulateStatus(&builder);
Austin Schuh3a378462019-01-04 21:48:04 -0800425
Austin Schuh95771d92021-01-23 14:42:25 -0800426 builder.add_estimated_left_position(gyro_left_right(kLeftPosition));
427 builder.add_estimated_right_position(gyro_left_right(kRightPosition));
Austin Schuh41565602016-02-28 20:10:49 -0800428
Austin Schuh95771d92021-01-23 14:42:25 -0800429 builder.add_estimated_left_velocity(gyro_left_right(kLeftVelocity));
430 builder.add_estimated_right_velocity(gyro_left_right(kRightVelocity));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700431
432 if (dt_spline_.enable()) {
433 dt_spline_.PopulateStatus(&builder);
434 } else {
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800435 builder.add_robot_speed((filters_.DrivetrainXHat(kLeftVelocity) +
436 filters_.DrivetrainXHat(kRightVelocity)) /
437 2.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700438 builder.add_output_was_capped(dt_closedloop_.output_was_capped());
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800439 builder.add_uncapped_left_voltage(
440 filters_.DrivetrainUUncapped(kLeftVoltage));
441 builder.add_uncapped_right_voltage(
442 filters_.DrivetrainUUncapped(kRightVoltage));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700443 }
444
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800445 builder.add_left_voltage_error(filters_.DrivetrainXHat(kLeftError));
446 builder.add_right_voltage_error(filters_.DrivetrainXHat(kRightError));
447 builder.add_estimated_angular_velocity_error(
448 filters_.DrivetrainXHat(kAngularError));
449 builder.add_estimated_heading(filters_.localizer_theta());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800451 builder.add_x(filters_.x());
452 builder.add_y(filters_.y());
453 builder.add_theta(::aos::math::NormalizeAngle(filters_.localizer_theta()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454
Alex Perrycb7da4b2019-08-28 19:35:56 -0700455 builder.add_cim_logging(cim_logging_offset);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800456 builder.add_poly_drive_logging(poly_drive_logging_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457 builder.add_gear_logging(gear_logging_offset);
458 builder.add_line_follow_logging(line_follow_logging_offset);
459 builder.add_trajectory_logging(trajectory_logging_offset);
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800460 builder.add_down_estimator(down_estimator_state_offset);
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800461 builder.add_localizer(localizer_offset);
James Kuszmaul2215d922020-02-11 17:17:26 -0800462 builder.add_zeroing(zeroer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700463 status->Send(builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700464 }
Austin Schuh209f1702015-11-29 17:03:00 -0800465
Austin Schuh209f1702015-11-29 17:03:00 -0800466 double left_voltage = 0.0;
467 double right_voltage = 0.0;
468 if (output) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700469 left_voltage = output_struct.left_voltage;
470 right_voltage = output_struct.right_voltage;
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800471 filters_.set_left_high_requested(output_struct.left_high);
472 filters_.set_right_high_requested(output_struct.right_high);
Austin Schuh209f1702015-11-29 17:03:00 -0800473 }
474
Alex Perrycb7da4b2019-08-28 19:35:56 -0700475 const double scalar = robot_state().voltage_battery() / 12.0;
Austin Schuh209f1702015-11-29 17:03:00 -0800476
477 left_voltage *= scalar;
478 right_voltage *= scalar;
479
Austin Schuh209f1702015-11-29 17:03:00 -0800480 // To validate, look at the following:
481
482 // Observed - dx/dt velocity for left, right.
483
484 // Angular velocity error compared to the gyro
485 // Gyro heading vs left-right
486 // Voltage error.
487
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800488 {
489 Eigen::Matrix<double, 2, 1> U;
490 U(kLeftVoltage) = left_voltage;
491 U(kRightVoltage) = right_voltage;
492 filters_.UpdateObserver(U);
493 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700494
495 if (output) {
496 output->Send(Output::Pack(*output->fbb(), &output_struct));
497 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700498}
499
Alex Perrycb7da4b2019-08-28 19:35:56 -0700500flatbuffers::Offset<Output> DrivetrainLoop::Zero(
501 aos::Sender<Output>::Builder *output) {
502 Output::Builder builder = output->MakeBuilder<Output>();
503 builder.add_left_voltage(0);
504 builder.add_right_voltage(0);
505 builder.add_left_high(dt_config_.default_high_gear);
506 builder.add_right_high(dt_config_.default_high_gear);
507 return builder.Finish();
Adam Snaiderbc918b62016-02-27 21:03:39 -0800508}
509
Austin Schuh6197a182015-11-28 16:04:40 -0800510} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700511} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000512} // namespace frc971