blob: 77c52c46e464ba671951b2bef1d4ac4800dc679b [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"
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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "frc971/wpilib/imu_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 Schuh55a13dc2019-01-27 22:39:03 -080030DrivetrainLoop::DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
31 ::aos::EventLoop *event_loop,
James Kuszmaul3431d622019-02-17 17:07:44 -080032 LocalizerInterface *localizer,
Austin Schuh55a13dc2019-01-27 22:39:03 -080033 const ::std::string &name)
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
35 name),
Comran Morshed5323ecb2015-12-26 20:50:55 +000036 dt_config_(dt_config),
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 localizer_control_fetcher_(
38 event_loop->MakeFetcher<LocalizerControl>("/drivetrain")),
Austin Schuh73b6e3b2019-05-27 16:37:15 -070039 imu_values_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 event_loop->MakeFetcher<::frc971::IMUValues>("/drivetrain")),
Austin Schuh1ea89bb2019-05-27 16:59:59 -070041 gyro_reading_fetcher_(
42 event_loop->MakeFetcher<::frc971::sensors::GyroReading>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 "/drivetrain")),
James Kuszmaul3431d622019-02-17 17:07:44 -080044 localizer_(localizer),
Austin Schuh41565602016-02-28 20:10:49 -080045 kf_(dt_config_.make_kf_drivetrain_loop()),
46 dt_openloop_(dt_config_, &kf_),
James Kuszmaul3431d622019-02-17 17:07:44 -080047 dt_closedloop_(dt_config_, &kf_, localizer_),
Alex Perry731b4602019-02-02 22:13:01 -080048 dt_spline_(dt_config_),
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080049 dt_line_follow_(dt_config_, localizer->target_selector()),
Austin Schuh093535c2016-03-05 23:21:00 -080050 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
51 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
52 left_high_requested_(dt_config_.default_high_gear),
53 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuh209f1702015-11-29 17:03:00 -080054 ::aos::controls::HPolytope<0>::Init();
Austin Schuh59dddac2019-12-22 16:44:49 -080055 event_loop->SetRuntimeRealtimePriority(30);
James Kuszmaul30aca502020-01-19 15:05:33 -080056 event_loop->OnRun([this]() {
57 // On the first fetch, make sure that we are caught all the way up to the
58 // present.
59 imu_values_fetcher_.Fetch();
60 });
James Kuszmaul2215d922020-02-11 17:17:26 -080061 if (dt_config.is_simulated) {
62 down_estimator_.assume_perfect_gravity();
63 }
Austin Schuh209f1702015-11-29 17:03:00 -080064}
65
Austin Schuh093535c2016-03-05 23:21:00 -080066int DrivetrainLoop::ControllerIndexFromGears() {
67 if (MaybeHigh(left_gear_)) {
68 if (MaybeHigh(right_gear_)) {
69 return 3;
70 } else {
71 return 2;
72 }
73 } else {
74 if (MaybeHigh(right_gear_)) {
75 return 1;
76 } else {
77 return 0;
78 }
79 }
80}
81
82Gear ComputeGear(double shifter_position,
83 const constants::ShifterHallEffect &shifter_config,
84 bool high_requested) {
85 if (shifter_position < shifter_config.clear_low) {
86 return Gear::LOW;
87 } else if (shifter_position > shifter_config.clear_high) {
88 return Gear::HIGH;
89 } else {
90 if (high_requested) {
91 return Gear::SHIFTING_UP;
92 } else {
93 return Gear::SHIFTING_DOWN;
94 }
95 }
96}
97
Austin Schuh6197a182015-11-28 16:04:40 -080098void DrivetrainLoop::RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -070099 const drivetrain::Goal *goal, const drivetrain::Position *position,
100 aos::Sender<drivetrain::Output>::Builder *output,
101 aos::Sender<drivetrain::Status>::Builder *status) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700102 const monotonic_clock::time_point monotonic_now =
103 event_loop()->monotonic_now();
Austin Schuh9993fb32017-03-15 20:17:46 -0700104
Austin Schuh5900d142016-04-03 21:35:12 -0700105 if (!has_been_enabled_ && output) {
106 has_been_enabled_ = true;
Austin Schuh5900d142016-04-03 21:35:12 -0700107 }
108
Austin Schuh093535c2016-03-05 23:21:00 -0800109 // TODO(austin): Put gear detection logic here.
110 switch (dt_config_.shifter_type) {
111 case ShifterType::SIMPLE_SHIFTER:
112 // Force the right controller for simple shifters since we assume that
113 // gear switching is instantaneous.
114 if (left_high_requested_) {
115 left_gear_ = Gear::HIGH;
116 } else {
117 left_gear_ = Gear::LOW;
118 }
119 if (right_high_requested_) {
120 right_gear_ = Gear::HIGH;
121 } else {
122 right_gear_ = Gear::LOW;
123 }
124 break;
125 case ShifterType::HALL_EFFECT_SHIFTER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 left_gear_ = ComputeGear(position->left_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800127 dt_config_.left_drive, left_high_requested_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128 right_gear_ = ComputeGear(position->right_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800129 dt_config_.right_drive, right_high_requested_);
130 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700131 case ShifterType::NO_SHIFTER:
132 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700133 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700134
Austin Schuhc5fceb82017-02-25 16:24:12 -0800135 kf_.set_index(ControllerIndexFromGears());
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700136
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 flatbuffers::Offset<GearLogging> gear_logging_offset;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700138 // Set the gear-logging parts of the status
139 if (status) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 GearLogging::Builder gear_logging_builder =
141 status->MakeBuilder<GearLogging>();
142 gear_logging_builder.add_left_state(static_cast<uint32_t>(left_gear_));
143 gear_logging_builder.add_right_state(static_cast<uint32_t>(right_gear_));
144 gear_logging_builder.add_left_loop_high(MaybeHigh(left_gear_));
145 gear_logging_builder.add_right_loop_high(MaybeHigh(right_gear_));
146 gear_logging_builder.add_controller_index(kf_.index());
147 gear_logging_offset = gear_logging_builder.Finish();
Austin Schuh093535c2016-03-05 23:21:00 -0800148 }
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700149
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800150 while (imu_values_fetcher_.FetchNext()) {
James Kuszmaulb7f45bb2020-02-26 20:27:48 -0800151 imu_zeroer_.InsertMeasurement(*imu_values_fetcher_);
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800152 last_gyro_time_ = monotonic_now;
James Kuszmaulb1e29372020-02-11 16:55:36 -0800153 if (!imu_zeroer_.Zeroed()) {
154 continue;
155 }
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800156 aos::monotonic_clock::time_point reading_time(std::chrono::nanoseconds(
157 imu_values_fetcher_->monotonic_timestamp_ns()));
158 if (last_imu_update_ == aos::monotonic_clock::min_time) {
159 last_imu_update_ = reading_time;
160 }
161 down_estimator_.Predict(imu_zeroer_.ZeroedGyro(), imu_zeroer_.ZeroedAccel(),
162 reading_time - last_imu_update_);
163 last_imu_update_ = reading_time;
164 }
Diana Burgessd0180f12018-03-21 21:24:17 -0700165
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800166 bool got_imu_reading = false;
167 if (imu_values_fetcher_.get() != nullptr) {
James Kuszmaulb7f45bb2020-02-26 20:27:48 -0800168 imu_zeroer_.ProcessMeasurements();
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800169 got_imu_reading = true;
Diana Burgessd0180f12018-03-21 21:24:17 -0700170 switch (dt_config_.imu_type) {
171 case IMUType::IMU_X:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700172 last_accel_ = -imu_values_fetcher_->accelerometer_x();
Diana Burgessd0180f12018-03-21 21:24:17 -0700173 break;
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700174 case IMUType::IMU_FLIPPED_X:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 last_accel_ = imu_values_fetcher_->accelerometer_x();
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700176 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700177 case IMUType::IMU_Y:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178 last_accel_ = -imu_values_fetcher_->accelerometer_y();
Diana Burgessd0180f12018-03-21 21:24:17 -0700179 break;
180 }
Austin Schuh05c5a612016-04-02 15:10:25 -0700181 }
Austin Schuh05c5a612016-04-02 15:10:25 -0700182
Austin Schuh093535c2016-03-05 23:21:00 -0800183 // TODO(austin): Signal the current gear to both loops.
184
Campbell Crowley2527ed22017-02-17 21:10:02 -0800185 switch (dt_config_.gyro_type) {
186 case GyroType::IMU_X_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800187 if (got_imu_reading) {
188 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().x();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800189 }
190 break;
191 case GyroType::IMU_Y_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800192 if (got_imu_reading) {
193 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().y();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800194 }
195 break;
196 case GyroType::IMU_Z_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800197 if (got_imu_reading) {
198 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().z();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800199 }
200 break;
Austin Schuh90b43b42019-01-04 07:45:05 +1100201 case GyroType::FLIPPED_IMU_Z_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800202 if (got_imu_reading) {
203 last_gyro_rate_ = -imu_zeroer_.ZeroedGyro().z();
Austin Schuh90b43b42019-01-04 07:45:05 +1100204 }
205 break;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800206 case GyroType::SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700207 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208 last_gyro_rate_ = gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700209 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800210 }
211 break;
212 case GyroType::FLIPPED_SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700213 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214 last_gyro_rate_ = -gyro_reading_fetcher_->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 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700219 AOS_LOG(FATAL, "invalid gyro configured");
Campbell Crowley2527ed22017-02-17 21:10:02 -0800220 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800221 }
222
Austin Schuh9993fb32017-03-15 20:17:46 -0700223 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
224 last_gyro_rate_ = 0.0;
225 }
226
Austin Schuh6613a072016-01-06 19:54:48 -0800227 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700228 Eigen::Matrix<double, 4, 1> Y;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700229 Y << position->left_encoder(), position->right_encoder(), last_gyro_rate_,
Diana Burgessd0180f12018-03-21 21:24:17 -0700230 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800231 kf_.Correct(Y);
James Kuszmaulef428a02019-03-02 22:19:41 -0800232 // If we get a new message setting the absolute position, then reset the
233 // localizer.
234 // TODO(james): Use a watcher (instead of a fetcher) once we support it in
235 // simulation.
236 if (localizer_control_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 VLOG(1) << "localizer_control "
238 << aos::FlatbufferToJson(localizer_control_fetcher_.get());
James Kuszmaul518640d2019-04-13 15:50:50 -0700239 localizer_->ResetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700240 monotonic_now, localizer_control_fetcher_->x(),
241 localizer_control_fetcher_->y(), localizer_control_fetcher_->theta(),
242 localizer_control_fetcher_->theta_uncertainty(),
243 !localizer_control_fetcher_->keep_current_theta());
James Kuszmaulef428a02019-03-02 22:19:41 -0800244 }
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800245 localizer_->Update({last_last_left_voltage_, last_last_right_voltage_},
Alex Perrycb7da4b2019-08-28 19:35:56 -0700246 monotonic_now, position->left_encoder(),
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800247 position->right_encoder(),
248 down_estimator_.avg_recent_yaw_rates(),
249 down_estimator_.avg_recent_accel());
Austin Schuh6613a072016-01-06 19:54:48 -0800250 }
251
Austin Schuh093535c2016-03-05 23:21:00 -0800252 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
253
Austin Schuh872723c2019-12-25 14:38:09 -0800254 ControllerType controller_type = ControllerType::POLYDRIVE;
Brian Silverman17f503e2015-08-02 18:17:18 -0700255 if (goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700256 controller_type = goal->controller_type();
Brian Silverman17f503e2015-08-02 18:17:18 -0700257
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258 dt_closedloop_.SetGoal(goal);
259 dt_openloop_.SetGoal(goal->wheel(), goal->throttle(), goal->quickturn(),
260 goal->highgear());
261 dt_spline_.SetGoal(goal);
262 dt_line_follow_.SetGoal(monotonic_now, goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700263 }
264
Alex Perrycb7da4b2019-08-28 19:35:56 -0700265 dt_openloop_.Update(robot_state().voltage_battery());
Brian Silverman17f503e2015-08-02 18:17:18 -0700266
Alex Perrycb7da4b2019-08-28 19:35:56 -0700267 dt_closedloop_.Update(output != nullptr &&
Austin Schuh872723c2019-12-25 14:38:09 -0800268 controller_type == ControllerType::MOTION_PROFILE);
Austin Schuh78379ea2019-01-04 20:39:45 -0800269
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800270 const Eigen::Matrix<double, 5, 1> trajectory_state =
271 (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(),
272 localizer_->theta(), localizer_->left_velocity(),
273 localizer_->right_velocity())
274 .finished();
275
Alex Perrycb7da4b2019-08-28 19:35:56 -0700276 dt_spline_.Update(
Austin Schuh872723c2019-12-25 14:38:09 -0800277 output != nullptr && controller_type == ControllerType::SPLINE_FOLLOWER,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700278 trajectory_state);
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800279
James Kuszmaul38e79642019-03-09 15:48:27 -0800280 dt_line_follow_.Update(monotonic_now, trajectory_state);
Alex Perrya71badb2019-02-06 19:40:41 -0800281
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282 OutputT output_struct;
283
Austin Schuh78379ea2019-01-04 20:39:45 -0800284 switch (controller_type) {
Austin Schuh872723c2019-12-25 14:38:09 -0800285 case ControllerType::POLYDRIVE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800287 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800288 case ControllerType::MOTION_PROFILE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289 dt_closedloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800290 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800291 case ControllerType::SPLINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700292 dt_spline_.SetOutput(output != nullptr ? &output_struct : nullptr);
Alex Perry731b4602019-02-02 22:13:01 -0800293 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800294 case ControllerType::LINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295 if (!dt_line_follow_.SetOutput(output != nullptr ? &output_struct
296 : nullptr)) {
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800297 // If the line follow drivetrain was unable to execute (generally due to
298 // not having a target), execute the regular teleop drivetrain.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800300 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800301 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700302 }
303
Austin Schuh093535c2016-03-05 23:21:00 -0800304 // The output should now contain the shift request.
305
Brian Silverman17f503e2015-08-02 18:17:18 -0700306 // set the output status of the control loop state
307 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800308 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700309 dt_config_.LeftRightToLinear(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800310 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700311 dt_config_.LeftRightToAngular(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800312
James Kuszmaul3431d622019-02-17 17:07:44 -0800313 angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800314
315 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700316 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800317
Alex Perrycb7da4b2019-08-28 19:35:56 -0700318 const flatbuffers::Offset<CIMLogging> cim_logging_offset =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800319 dt_openloop_.PopulateShiftingStatus(status->fbb());
320
321 const flatbuffers::Offset<PolyDriveLogging> poly_drive_logging_offset =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 dt_openloop_.PopulateStatus(status->fbb());
Austin Schuh093535c2016-03-05 23:21:00 -0800323
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800324 const flatbuffers::Offset<DownEstimatorState> down_estimator_state_offset =
James Kuszmaul2215d922020-02-11 17:17:26 -0800325 down_estimator_.PopulateStatus(status->fbb(), monotonic_now);
326
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800327 const flatbuffers::Offset<LocalizerState> localizer_offset =
328 localizer_->PopulateStatus(status->fbb());
329
James Kuszmaul2215d922020-02-11 17:17:26 -0800330 const flatbuffers::Offset<ImuZeroerState> zeroer_offset =
331 imu_zeroer_.PopulateStatus(status->fbb());
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800332
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333 flatbuffers::Offset<LineFollowLogging> line_follow_logging_offset =
334 dt_line_follow_.PopulateStatus(status);
335 flatbuffers::Offset<TrajectoryLogging> trajectory_logging_offset =
336 dt_spline_.MakeTrajectoryLogging(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800337
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 StatusBuilder builder = status->MakeBuilder<Status>();
Austin Schuh3a378462019-01-04 21:48:04 -0800339
Alex Perrycb7da4b2019-08-28 19:35:56 -0700340 dt_closedloop_.PopulateStatus(&builder);
Austin Schuh3a378462019-01-04 21:48:04 -0800341
Alex Perrycb7da4b2019-08-28 19:35:56 -0700342 builder.add_estimated_left_position(gyro_left_right(0, 0));
343 builder.add_estimated_right_position(gyro_left_right(2, 0));
Austin Schuh41565602016-02-28 20:10:49 -0800344
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 builder.add_estimated_left_velocity(gyro_left_right(1, 0));
346 builder.add_estimated_right_velocity(gyro_left_right(3, 0));
347
348 if (dt_spline_.enable()) {
349 dt_spline_.PopulateStatus(&builder);
350 } else {
351 builder.add_robot_speed((kf_.X_hat(1) + kf_.X_hat(3)) / 2.0);
352 builder.add_output_was_capped(dt_closedloop_.output_was_capped());
353 builder.add_uncapped_left_voltage(kf_.U_uncapped(0, 0));
354 builder.add_uncapped_right_voltage(kf_.U_uncapped(1, 0));
355 }
356
357 builder.add_left_voltage_error(kf_.X_hat(4));
358 builder.add_right_voltage_error(kf_.X_hat(5));
359 builder.add_estimated_angular_velocity_error(kf_.X_hat(6));
360 builder.add_estimated_heading(localizer_->theta());
361
362 builder.add_x(localizer_->x());
363 builder.add_y(localizer_->y());
364 builder.add_theta(::aos::math::NormalizeAngle(localizer_->theta()));
365
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 builder.add_cim_logging(cim_logging_offset);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800367 builder.add_poly_drive_logging(poly_drive_logging_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 builder.add_gear_logging(gear_logging_offset);
369 builder.add_line_follow_logging(line_follow_logging_offset);
370 builder.add_trajectory_logging(trajectory_logging_offset);
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800371 builder.add_down_estimator(down_estimator_state_offset);
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800372 builder.add_localizer(localizer_offset);
James Kuszmaul2215d922020-02-11 17:17:26 -0800373 builder.add_zeroing(zeroer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700374 status->Send(builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700375 }
Austin Schuh209f1702015-11-29 17:03:00 -0800376
Austin Schuh209f1702015-11-29 17:03:00 -0800377 double left_voltage = 0.0;
378 double right_voltage = 0.0;
379 if (output) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700380 left_voltage = output_struct.left_voltage;
381 right_voltage = output_struct.right_voltage;
382 left_high_requested_ = output_struct.left_high;
383 right_high_requested_ = output_struct.right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800384 }
385
Alex Perrycb7da4b2019-08-28 19:35:56 -0700386 const double scalar = robot_state().voltage_battery() / 12.0;
Austin Schuh209f1702015-11-29 17:03:00 -0800387
388 left_voltage *= scalar;
389 right_voltage *= scalar;
390
Austin Schuh209f1702015-11-29 17:03:00 -0800391 // To validate, look at the following:
392
393 // Observed - dx/dt velocity for left, right.
394
395 // Angular velocity error compared to the gyro
396 // Gyro heading vs left-right
397 // Voltage error.
398
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800399 last_last_left_voltage_ = last_left_voltage_;
400 last_last_right_voltage_ = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800401 Eigen::Matrix<double, 2, 1> U;
Austin Schuhdf79d112016-10-15 21:25:32 -0700402 U(0, 0) = last_left_voltage_;
403 U(1, 0) = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800404 last_left_voltage_ = left_voltage;
405 last_right_voltage_ = right_voltage;
406
Austin Schuh3a378462019-01-04 21:48:04 -0800407 last_state_ = kf_.X_hat();
408 kf_.UpdateObserver(U, dt_config_.dt);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700409
410 if (output) {
411 output->Send(Output::Pack(*output->fbb(), &output_struct));
412 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700413}
414
Alex Perrycb7da4b2019-08-28 19:35:56 -0700415flatbuffers::Offset<Output> DrivetrainLoop::Zero(
416 aos::Sender<Output>::Builder *output) {
417 Output::Builder builder = output->MakeBuilder<Output>();
418 builder.add_left_voltage(0);
419 builder.add_right_voltage(0);
420 builder.add_left_high(dt_config_.default_high_gear);
421 builder.add_right_high(dt_config_.default_high_gear);
422 return builder.Finish();
Adam Snaiderbc918b62016-02-27 21:03:39 -0800423}
424
Austin Schuh6197a182015-11-28 16:04:40 -0800425} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700426} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000427} // namespace frc971