blob: edde3c77151f4e1f1c809f49a5132c7ed91d95ef [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 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_(
Austin Schuhac17fba2020-03-28 15:55:33 -070040 event_loop->MakeFetcher<::frc971::IMUValuesBatch>("/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 Kuszmaul7f55f072020-03-01 10:21:26 -080044 down_estimator_(dt_config),
James Kuszmaul3431d622019-02-17 17:07:44 -080045 localizer_(localizer),
Austin Schuh41565602016-02-28 20:10:49 -080046 kf_(dt_config_.make_kf_drivetrain_loop()),
47 dt_openloop_(dt_config_, &kf_),
James Kuszmaul3431d622019-02-17 17:07:44 -080048 dt_closedloop_(dt_config_, &kf_, localizer_),
Alex Perry731b4602019-02-02 22:13:01 -080049 dt_spline_(dt_config_),
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080050 dt_line_follow_(dt_config_, localizer->target_selector()),
Austin Schuh093535c2016-03-05 23:21:00 -080051 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
52 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
53 left_high_requested_(dt_config_.default_high_gear),
54 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuh209f1702015-11-29 17:03:00 -080055 ::aos::controls::HPolytope<0>::Init();
Austin Schuh59dddac2019-12-22 16:44:49 -080056 event_loop->SetRuntimeRealtimePriority(30);
James Kuszmaul30aca502020-01-19 15:05:33 -080057 event_loop->OnRun([this]() {
58 // On the first fetch, make sure that we are caught all the way up to the
59 // present.
60 imu_values_fetcher_.Fetch();
61 });
James Kuszmaul2215d922020-02-11 17:17:26 -080062 if (dt_config.is_simulated) {
63 down_estimator_.assume_perfect_gravity();
64 }
Austin Schuh209f1702015-11-29 17:03:00 -080065}
66
Austin Schuh093535c2016-03-05 23:21:00 -080067int DrivetrainLoop::ControllerIndexFromGears() {
68 if (MaybeHigh(left_gear_)) {
69 if (MaybeHigh(right_gear_)) {
70 return 3;
71 } else {
72 return 2;
73 }
74 } else {
75 if (MaybeHigh(right_gear_)) {
76 return 1;
77 } else {
78 return 0;
79 }
80 }
81}
82
83Gear ComputeGear(double shifter_position,
84 const constants::ShifterHallEffect &shifter_config,
85 bool high_requested) {
86 if (shifter_position < shifter_config.clear_low) {
87 return Gear::LOW;
88 } else if (shifter_position > shifter_config.clear_high) {
89 return Gear::HIGH;
90 } else {
91 if (high_requested) {
92 return Gear::SHIFTING_UP;
93 } else {
94 return Gear::SHIFTING_DOWN;
95 }
96 }
97}
98
Austin Schuh6197a182015-11-28 16:04:40 -080099void DrivetrainLoop::RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100 const drivetrain::Goal *goal, const drivetrain::Position *position,
101 aos::Sender<drivetrain::Output>::Builder *output,
102 aos::Sender<drivetrain::Status>::Builder *status) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700103 const monotonic_clock::time_point monotonic_now =
104 event_loop()->monotonic_now();
Austin Schuh9993fb32017-03-15 20:17:46 -0700105
Austin Schuh5900d142016-04-03 21:35:12 -0700106 if (!has_been_enabled_ && output) {
107 has_been_enabled_ = true;
Austin Schuh5900d142016-04-03 21:35:12 -0700108 }
109
James Kuszmaulbcd96fc2020-10-12 20:29:32 -0700110 if (WasReset()) {
111 // If all the sensors got reset (e.g., due to wpilib_interface restarting),
112 // reset the localizer and down estimator to avoid weird jumps in the
113 // filters.
114 down_estimator_.Reset();
115 // Just reset the localizer to the current state, except for the encoders.
116 LocalizerInterface::Ekf::State X_hat = localizer_->Xhat();
Austin Schuhd749d932020-12-30 21:38:40 -0800117 X_hat(LocalizerInterface::StateIdx::kLeftEncoder) =
118 position->left_encoder();
James Kuszmaulbcd96fc2020-10-12 20:29:32 -0700119 X_hat(LocalizerInterface::StateIdx::kRightEncoder) =
120 position->right_encoder();
121 localizer_->Reset(monotonic_now, X_hat);
122 }
123
Austin Schuh093535c2016-03-05 23:21:00 -0800124 // TODO(austin): Put gear detection logic here.
125 switch (dt_config_.shifter_type) {
126 case ShifterType::SIMPLE_SHIFTER:
127 // Force the right controller for simple shifters since we assume that
128 // gear switching is instantaneous.
129 if (left_high_requested_) {
130 left_gear_ = Gear::HIGH;
131 } else {
132 left_gear_ = Gear::LOW;
133 }
134 if (right_high_requested_) {
135 right_gear_ = Gear::HIGH;
136 } else {
137 right_gear_ = Gear::LOW;
138 }
139 break;
140 case ShifterType::HALL_EFFECT_SHIFTER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 left_gear_ = ComputeGear(position->left_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800142 dt_config_.left_drive, left_high_requested_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 right_gear_ = ComputeGear(position->right_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800144 dt_config_.right_drive, right_high_requested_);
145 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700146 case ShifterType::NO_SHIFTER:
147 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700149
Austin Schuhc5fceb82017-02-25 16:24:12 -0800150 kf_.set_index(ControllerIndexFromGears());
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700151
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 flatbuffers::Offset<GearLogging> gear_logging_offset;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700153 // Set the gear-logging parts of the status
154 if (status) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155 GearLogging::Builder gear_logging_builder =
156 status->MakeBuilder<GearLogging>();
157 gear_logging_builder.add_left_state(static_cast<uint32_t>(left_gear_));
158 gear_logging_builder.add_right_state(static_cast<uint32_t>(right_gear_));
159 gear_logging_builder.add_left_loop_high(MaybeHigh(left_gear_));
160 gear_logging_builder.add_right_loop_high(MaybeHigh(right_gear_));
161 gear_logging_builder.add_controller_index(kf_.index());
162 gear_logging_offset = gear_logging_builder.Finish();
Austin Schuh093535c2016-03-05 23:21:00 -0800163 }
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700164
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800165 while (imu_values_fetcher_.FetchNext()) {
Austin Schuhac17fba2020-03-28 15:55:33 -0700166 CHECK(imu_values_fetcher_->has_readings());
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800167 last_gyro_time_ = monotonic_now;
Austin Schuhac17fba2020-03-28 15:55:33 -0700168 for (const IMUValues *value : *imu_values_fetcher_->readings()) {
169 imu_zeroer_.InsertMeasurement(*value);
170 if (!imu_zeroer_.Zeroed()) {
171 continue;
172 }
173 const aos::monotonic_clock::time_point reading_time(
174 std::chrono::nanoseconds(value->monotonic_timestamp_ns()));
175 if (last_imu_update_ == aos::monotonic_clock::min_time) {
176 last_imu_update_ = reading_time;
177 }
178 down_estimator_.Predict(imu_zeroer_.ZeroedGyro(),
179 imu_zeroer_.ZeroedAccel(),
180 reading_time - last_imu_update_);
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800181 last_imu_update_ = reading_time;
182 }
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800183 }
Diana Burgessd0180f12018-03-21 21:24:17 -0700184
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800185 bool got_imu_reading = false;
186 if (imu_values_fetcher_.get() != nullptr) {
James Kuszmaulb7f45bb2020-02-26 20:27:48 -0800187 imu_zeroer_.ProcessMeasurements();
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800188 got_imu_reading = true;
Austin Schuhac17fba2020-03-28 15:55:33 -0700189 CHECK(imu_values_fetcher_->has_readings());
190 const IMUValues *value = imu_values_fetcher_->readings()->Get(
191 imu_values_fetcher_->readings()->size() - 1);
Diana Burgessd0180f12018-03-21 21:24:17 -0700192 switch (dt_config_.imu_type) {
193 case IMUType::IMU_X:
Austin Schuhac17fba2020-03-28 15:55:33 -0700194 last_accel_ = -value->accelerometer_x();
Diana Burgessd0180f12018-03-21 21:24:17 -0700195 break;
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700196 case IMUType::IMU_FLIPPED_X:
Austin Schuhac17fba2020-03-28 15:55:33 -0700197 last_accel_ = value->accelerometer_x();
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700198 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700199 case IMUType::IMU_Y:
Austin Schuhac17fba2020-03-28 15:55:33 -0700200 last_accel_ = -value->accelerometer_y();
Diana Burgessd0180f12018-03-21 21:24:17 -0700201 break;
James Kuszmaul7f55f072020-03-01 10:21:26 -0800202 case IMUType::IMU_Z:
Austin Schuhac17fba2020-03-28 15:55:33 -0700203 last_accel_ = value->accelerometer_z();
James Kuszmaul7f55f072020-03-01 10:21:26 -0800204 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700205 }
Austin Schuh05c5a612016-04-02 15:10:25 -0700206 }
Austin Schuh05c5a612016-04-02 15:10:25 -0700207
Austin Schuh093535c2016-03-05 23:21:00 -0800208 // TODO(austin): Signal the current gear to both loops.
209
Campbell Crowley2527ed22017-02-17 21:10:02 -0800210 switch (dt_config_.gyro_type) {
211 case GyroType::IMU_X_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800212 if (got_imu_reading) {
213 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().x();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800214 }
215 break;
216 case GyroType::IMU_Y_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800217 if (got_imu_reading) {
218 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().y();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800219 }
220 break;
221 case GyroType::IMU_Z_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800222 if (got_imu_reading) {
223 last_gyro_rate_ = imu_zeroer_.ZeroedGyro().z();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800224 }
225 break;
Austin Schuh90b43b42019-01-04 07:45:05 +1100226 case GyroType::FLIPPED_IMU_Z_GYRO:
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800227 if (got_imu_reading) {
228 last_gyro_rate_ = -imu_zeroer_.ZeroedGyro().z();
Austin Schuh90b43b42019-01-04 07:45:05 +1100229 }
230 break;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800231 case GyroType::SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700232 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700233 last_gyro_rate_ = gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700234 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800235 }
236 break;
237 case GyroType::FLIPPED_SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700238 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239 last_gyro_rate_ = -gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700240 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800241 }
242 break;
243 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700244 AOS_LOG(FATAL, "invalid gyro configured");
Campbell Crowley2527ed22017-02-17 21:10:02 -0800245 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800246 }
247
Austin Schuh9993fb32017-03-15 20:17:46 -0700248 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
249 last_gyro_rate_ = 0.0;
250 }
251
Austin Schuh6613a072016-01-06 19:54:48 -0800252 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700253 Eigen::Matrix<double, 4, 1> Y;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254 Y << position->left_encoder(), position->right_encoder(), last_gyro_rate_,
Diana Burgessd0180f12018-03-21 21:24:17 -0700255 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800256 kf_.Correct(Y);
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800257 localizer_->Update({last_last_left_voltage_, last_last_right_voltage_},
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258 monotonic_now, position->left_encoder(),
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800259 position->right_encoder(),
260 down_estimator_.avg_recent_yaw_rates(),
261 down_estimator_.avg_recent_accel());
James Kuszmaul891f4f12020-10-31 17:13:23 -0700262 // If we get a new message setting the absolute position, then reset the
263 // localizer.
264 if (localizer_control_fetcher_.Fetch()) {
265 VLOG(1) << "localizer_control "
266 << aos::FlatbufferToJson(localizer_control_fetcher_.get());
267 localizer_->ResetPosition(
Austin Schuhd749d932020-12-30 21:38:40 -0800268 monotonic_now, localizer_control_fetcher_->x(),
269 localizer_control_fetcher_->y(), localizer_control_fetcher_->theta(),
James Kuszmaul891f4f12020-10-31 17:13:23 -0700270 localizer_control_fetcher_->theta_uncertainty(),
271 !localizer_control_fetcher_->keep_current_theta());
272 }
Austin Schuh6613a072016-01-06 19:54:48 -0800273 }
274
Austin Schuh093535c2016-03-05 23:21:00 -0800275 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
276
Austin Schuh872723c2019-12-25 14:38:09 -0800277 ControllerType controller_type = ControllerType::POLYDRIVE;
Brian Silverman17f503e2015-08-02 18:17:18 -0700278 if (goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279 controller_type = goal->controller_type();
Brian Silverman17f503e2015-08-02 18:17:18 -0700280
Alex Perrycb7da4b2019-08-28 19:35:56 -0700281 dt_closedloop_.SetGoal(goal);
282 dt_openloop_.SetGoal(goal->wheel(), goal->throttle(), goal->quickturn(),
283 goal->highgear());
284 dt_spline_.SetGoal(goal);
285 dt_line_follow_.SetGoal(monotonic_now, goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700286 }
287
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 dt_openloop_.Update(robot_state().voltage_battery());
Brian Silverman17f503e2015-08-02 18:17:18 -0700289
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290 dt_closedloop_.Update(output != nullptr &&
Austin Schuh872723c2019-12-25 14:38:09 -0800291 controller_type == ControllerType::MOTION_PROFILE);
Austin Schuh78379ea2019-01-04 20:39:45 -0800292
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800293 const Eigen::Matrix<double, 5, 1> trajectory_state =
294 (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(),
295 localizer_->theta(), localizer_->left_velocity(),
296 localizer_->right_velocity())
297 .finished();
298
James Kuszmaul897ef1a2020-10-25 17:51:10 -0700299 {
300 // TODO(james): The regular Kalman Filter's voltage error terms are
301 // currently unusable--either don't use voltage error at all for the spline
302 // following code, or use the EKF's voltage error estimates.
303 const Eigen::Matrix<double, 2, 1> voltage_error =
Austin Schuh95771d92021-01-23 14:42:25 -0800304 0 * kf_.X_hat().block<2, 1>(kLeftError, 0);
James Kuszmaul897ef1a2020-10-25 17:51:10 -0700305 dt_spline_.Update(
306 output != nullptr && controller_type == ControllerType::SPLINE_FOLLOWER,
307 trajectory_state, voltage_error);
308 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800309
James Kuszmaul38e79642019-03-09 15:48:27 -0800310 dt_line_follow_.Update(monotonic_now, trajectory_state);
Alex Perrya71badb2019-02-06 19:40:41 -0800311
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312 OutputT output_struct;
313
Austin Schuh78379ea2019-01-04 20:39:45 -0800314 switch (controller_type) {
Austin Schuh872723c2019-12-25 14:38:09 -0800315 case ControllerType::POLYDRIVE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700316 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800317 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800318 case ControllerType::MOTION_PROFILE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319 dt_closedloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800320 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800321 case ControllerType::SPLINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 dt_spline_.SetOutput(output != nullptr ? &output_struct : nullptr);
Alex Perry731b4602019-02-02 22:13:01 -0800323 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800324 case ControllerType::LINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325 if (!dt_line_follow_.SetOutput(output != nullptr ? &output_struct
326 : nullptr)) {
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800327 // If the line follow drivetrain was unable to execute (generally due to
328 // not having a target), execute the regular teleop drivetrain.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800330 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800331 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700332 }
333
Austin Schuh093535c2016-03-05 23:21:00 -0800334 // The output should now contain the shift request.
335
Brian Silverman17f503e2015-08-02 18:17:18 -0700336 // set the output status of the control loop state
337 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800338 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700339 dt_config_.LeftRightToLinear(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800340 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700341 dt_config_.LeftRightToAngular(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800342
James Kuszmaul3431d622019-02-17 17:07:44 -0800343 angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800344
345 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700346 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800347
Alex Perrycb7da4b2019-08-28 19:35:56 -0700348 const flatbuffers::Offset<CIMLogging> cim_logging_offset =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800349 dt_openloop_.PopulateShiftingStatus(status->fbb());
350
351 const flatbuffers::Offset<PolyDriveLogging> poly_drive_logging_offset =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700352 dt_openloop_.PopulateStatus(status->fbb());
Austin Schuh093535c2016-03-05 23:21:00 -0800353
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800354 const flatbuffers::Offset<DownEstimatorState> down_estimator_state_offset =
James Kuszmaul2215d922020-02-11 17:17:26 -0800355 down_estimator_.PopulateStatus(status->fbb(), monotonic_now);
356
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800357 const flatbuffers::Offset<LocalizerState> localizer_offset =
358 localizer_->PopulateStatus(status->fbb());
359
James Kuszmaul2215d922020-02-11 17:17:26 -0800360 const flatbuffers::Offset<ImuZeroerState> zeroer_offset =
361 imu_zeroer_.PopulateStatus(status->fbb());
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800362
Alex Perrycb7da4b2019-08-28 19:35:56 -0700363 flatbuffers::Offset<LineFollowLogging> line_follow_logging_offset =
364 dt_line_follow_.PopulateStatus(status);
365 flatbuffers::Offset<TrajectoryLogging> trajectory_logging_offset =
366 dt_spline_.MakeTrajectoryLogging(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800367
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 StatusBuilder builder = status->MakeBuilder<Status>();
Austin Schuh3a378462019-01-04 21:48:04 -0800369
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370 dt_closedloop_.PopulateStatus(&builder);
Austin Schuh3a378462019-01-04 21:48:04 -0800371
Austin Schuh95771d92021-01-23 14:42:25 -0800372 builder.add_estimated_left_position(gyro_left_right(kLeftPosition));
373 builder.add_estimated_right_position(gyro_left_right(kRightPosition));
Austin Schuh41565602016-02-28 20:10:49 -0800374
Austin Schuh95771d92021-01-23 14:42:25 -0800375 builder.add_estimated_left_velocity(gyro_left_right(kLeftVelocity));
376 builder.add_estimated_right_velocity(gyro_left_right(kRightVelocity));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377
378 if (dt_spline_.enable()) {
379 dt_spline_.PopulateStatus(&builder);
380 } else {
Austin Schuh95771d92021-01-23 14:42:25 -0800381 builder.add_robot_speed(
382 (kf_.X_hat(kLeftVelocity) + kf_.X_hat(kRightVelocity)) / 2.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700383 builder.add_output_was_capped(dt_closedloop_.output_was_capped());
Austin Schuh95771d92021-01-23 14:42:25 -0800384 builder.add_uncapped_left_voltage(kf_.U_uncapped(kLeftVoltage));
385 builder.add_uncapped_right_voltage(kf_.U_uncapped(kRightVoltage));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700386 }
387
Austin Schuh95771d92021-01-23 14:42:25 -0800388 builder.add_left_voltage_error(kf_.X_hat(kLeftError));
389 builder.add_right_voltage_error(kf_.X_hat(kRightError));
390 builder.add_estimated_angular_velocity_error(kf_.X_hat(kAngularError));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700391 builder.add_estimated_heading(localizer_->theta());
392
393 builder.add_x(localizer_->x());
394 builder.add_y(localizer_->y());
395 builder.add_theta(::aos::math::NormalizeAngle(localizer_->theta()));
396
Alex Perrycb7da4b2019-08-28 19:35:56 -0700397 builder.add_cim_logging(cim_logging_offset);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800398 builder.add_poly_drive_logging(poly_drive_logging_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700399 builder.add_gear_logging(gear_logging_offset);
400 builder.add_line_follow_logging(line_follow_logging_offset);
401 builder.add_trajectory_logging(trajectory_logging_offset);
James Kuszmaul3e1bb272020-01-17 18:38:19 -0800402 builder.add_down_estimator(down_estimator_state_offset);
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800403 builder.add_localizer(localizer_offset);
James Kuszmaul2215d922020-02-11 17:17:26 -0800404 builder.add_zeroing(zeroer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700405 status->Send(builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700406 }
Austin Schuh209f1702015-11-29 17:03:00 -0800407
Austin Schuh209f1702015-11-29 17:03:00 -0800408 double left_voltage = 0.0;
409 double right_voltage = 0.0;
410 if (output) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700411 left_voltage = output_struct.left_voltage;
412 right_voltage = output_struct.right_voltage;
413 left_high_requested_ = output_struct.left_high;
414 right_high_requested_ = output_struct.right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800415 }
416
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 const double scalar = robot_state().voltage_battery() / 12.0;
Austin Schuh209f1702015-11-29 17:03:00 -0800418
419 left_voltage *= scalar;
420 right_voltage *= scalar;
421
Austin Schuh209f1702015-11-29 17:03:00 -0800422 // To validate, look at the following:
423
424 // Observed - dx/dt velocity for left, right.
425
426 // Angular velocity error compared to the gyro
427 // Gyro heading vs left-right
428 // Voltage error.
429
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800430 last_last_left_voltage_ = last_left_voltage_;
431 last_last_right_voltage_ = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800432 Eigen::Matrix<double, 2, 1> U;
Austin Schuh95771d92021-01-23 14:42:25 -0800433 U(kLeftVoltage) = last_left_voltage_;
434 U(kRightVoltage) = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800435 last_left_voltage_ = left_voltage;
436 last_right_voltage_ = right_voltage;
437
Austin Schuh3a378462019-01-04 21:48:04 -0800438 last_state_ = kf_.X_hat();
439 kf_.UpdateObserver(U, dt_config_.dt);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700440
441 if (output) {
442 output->Send(Output::Pack(*output->fbb(), &output_struct));
443 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700444}
445
Alex Perrycb7da4b2019-08-28 19:35:56 -0700446flatbuffers::Offset<Output> DrivetrainLoop::Zero(
447 aos::Sender<Output>::Builder *output) {
448 Output::Builder builder = output->MakeBuilder<Output>();
449 builder.add_left_voltage(0);
450 builder.add_right_voltage(0);
451 builder.add_left_high(dt_config_.default_high_gear);
452 builder.add_right_high(dt_config_.default_high_gear);
453 return builder.Finish();
Adam Snaiderbc918b62016-02-27 21:03:39 -0800454}
455
Austin Schuh6197a182015-11-28 16:04:40 -0800456} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700457} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000458} // namespace frc971