blob: 51c6f035d0fdd490277a171e70b095348b10b23a [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 Schuh05c5a612016-04-02 15:10:25 -070050 down_estimator_(MakeDownEstimatorLoop()),
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 Schuh05c5a612016-04-02 15:10:25 -070056 down_U_.setZero();
Austin Schuh59dddac2019-12-22 16:44:49 -080057 event_loop->SetRuntimeRealtimePriority(30);
Austin Schuh209f1702015-11-29 17:03:00 -080058}
59
Austin Schuh093535c2016-03-05 23:21:00 -080060int DrivetrainLoop::ControllerIndexFromGears() {
61 if (MaybeHigh(left_gear_)) {
62 if (MaybeHigh(right_gear_)) {
63 return 3;
64 } else {
65 return 2;
66 }
67 } else {
68 if (MaybeHigh(right_gear_)) {
69 return 1;
70 } else {
71 return 0;
72 }
73 }
74}
75
76Gear ComputeGear(double shifter_position,
77 const constants::ShifterHallEffect &shifter_config,
78 bool high_requested) {
79 if (shifter_position < shifter_config.clear_low) {
80 return Gear::LOW;
81 } else if (shifter_position > shifter_config.clear_high) {
82 return Gear::HIGH;
83 } else {
84 if (high_requested) {
85 return Gear::SHIFTING_UP;
86 } else {
87 return Gear::SHIFTING_DOWN;
88 }
89 }
90}
91
Austin Schuh6197a182015-11-28 16:04:40 -080092void DrivetrainLoop::RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 const drivetrain::Goal *goal, const drivetrain::Position *position,
94 aos::Sender<drivetrain::Output>::Builder *output,
95 aos::Sender<drivetrain::Status>::Builder *status) {
Austin Schuh9fe68f72019-08-10 19:32:03 -070096 const monotonic_clock::time_point monotonic_now =
97 event_loop()->monotonic_now();
Austin Schuh9993fb32017-03-15 20:17:46 -070098
Austin Schuh5900d142016-04-03 21:35:12 -070099 if (!has_been_enabled_ && output) {
100 has_been_enabled_ = true;
101 down_estimator_.mutable_X_hat(1, 0) = 0.0;
102 }
103
Austin Schuh093535c2016-03-05 23:21:00 -0800104 // TODO(austin): Put gear detection logic here.
105 switch (dt_config_.shifter_type) {
106 case ShifterType::SIMPLE_SHIFTER:
107 // Force the right controller for simple shifters since we assume that
108 // gear switching is instantaneous.
109 if (left_high_requested_) {
110 left_gear_ = Gear::HIGH;
111 } else {
112 left_gear_ = Gear::LOW;
113 }
114 if (right_high_requested_) {
115 right_gear_ = Gear::HIGH;
116 } else {
117 right_gear_ = Gear::LOW;
118 }
119 break;
120 case ShifterType::HALL_EFFECT_SHIFTER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700121 left_gear_ = ComputeGear(position->left_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800122 dt_config_.left_drive, left_high_requested_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 right_gear_ = ComputeGear(position->right_shifter_position(),
Austin Schuh093535c2016-03-05 23:21:00 -0800124 dt_config_.right_drive, right_high_requested_);
125 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700126 case ShifterType::NO_SHIFTER:
127 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700129
Austin Schuhc5fceb82017-02-25 16:24:12 -0800130 kf_.set_index(ControllerIndexFromGears());
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700131
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 flatbuffers::Offset<GearLogging> gear_logging_offset;
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700133 // Set the gear-logging parts of the status
134 if (status) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700135 GearLogging::Builder gear_logging_builder =
136 status->MakeBuilder<GearLogging>();
137 gear_logging_builder.add_left_state(static_cast<uint32_t>(left_gear_));
138 gear_logging_builder.add_right_state(static_cast<uint32_t>(right_gear_));
139 gear_logging_builder.add_left_loop_high(MaybeHigh(left_gear_));
140 gear_logging_builder.add_right_loop_high(MaybeHigh(right_gear_));
141 gear_logging_builder.add_controller_index(kf_.index());
142 gear_logging_offset = gear_logging_builder.Finish();
Austin Schuh093535c2016-03-05 23:21:00 -0800143 }
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700144
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700145 const bool is_latest_imu_values = imu_values_fetcher_.Fetch();
Campbell Crowley2527ed22017-02-17 21:10:02 -0800146 if (is_latest_imu_values) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 const double rate = -imu_values_fetcher_->gyro_y();
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700148 const double accel_squared =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149 ::std::pow(imu_values_fetcher_->accelerometer_x(), 2.0) +
150 ::std::pow(imu_values_fetcher_->accelerometer_y(), 2.0) +
151 ::std::pow(imu_values_fetcher_->accelerometer_z(), 2.0);
152 const double angle = ::std::atan2(imu_values_fetcher_->accelerometer_x(),
153 imu_values_fetcher_->accelerometer_z()) +
Austin Schuh05c5a612016-04-02 15:10:25 -0700154 0.008;
Diana Burgessd0180f12018-03-21 21:24:17 -0700155
156 switch (dt_config_.imu_type) {
157 case IMUType::IMU_X:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158 last_accel_ = -imu_values_fetcher_->accelerometer_x();
Diana Burgessd0180f12018-03-21 21:24:17 -0700159 break;
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700160 case IMUType::IMU_FLIPPED_X:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 last_accel_ = imu_values_fetcher_->accelerometer_x();
Austin Schuhe7f6ddf2019-03-22 20:29:49 -0700162 break;
Diana Burgessd0180f12018-03-21 21:24:17 -0700163 case IMUType::IMU_Y:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 last_accel_ = -imu_values_fetcher_->accelerometer_y();
Diana Burgessd0180f12018-03-21 21:24:17 -0700165 break;
166 }
167
Austin Schuh05c5a612016-04-02 15:10:25 -0700168 if (accel_squared > 1.03 || accel_squared < 0.97) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700169 AOS_LOG(DEBUG, "New IMU value, rejecting reading\n");
Austin Schuh05c5a612016-04-02 15:10:25 -0700170 } else {
171 // -y is our gyro.
Austin Schuhf0c05762016-04-03 16:06:49 -0700172 // z accel is down
173 // x accel is the front of the robot pointed down.
Austin Schuh05c5a612016-04-02 15:10:25 -0700174 Eigen::Matrix<double, 1, 1> Y;
Austin Schuhdf79d112016-10-15 21:25:32 -0700175 Y(0, 0) = angle;
Austin Schuh05c5a612016-04-02 15:10:25 -0700176 down_estimator_.Correct(Y);
177 }
178
Austin Schuhf257f3c2019-10-27 21:00:43 -0700179 AOS_LOG(DEBUG,
180 "New IMU value, rate is %f, angle %f, fused %f, bias "
181 "%f\n",
182 rate, angle, down_estimator_.X_hat(0), down_estimator_.X_hat(1));
Austin Schuhdf79d112016-10-15 21:25:32 -0700183 down_U_(0, 0) = rate;
Austin Schuh05c5a612016-04-02 15:10:25 -0700184 }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800185 down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency);
Austin Schuh05c5a612016-04-02 15:10:25 -0700186
Austin Schuh093535c2016-03-05 23:21:00 -0800187 // TODO(austin): Signal the current gear to both loops.
188
Campbell Crowley2527ed22017-02-17 21:10:02 -0800189 switch (dt_config_.gyro_type) {
190 case GyroType::IMU_X_GYRO:
191 if (is_latest_imu_values) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 last_gyro_rate_ = imu_values_fetcher_->gyro_x();
Austin Schuh9993fb32017-03-15 20:17:46 -0700193 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800194 }
195 break;
196 case GyroType::IMU_Y_GYRO:
197 if (is_latest_imu_values) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700198 last_gyro_rate_ = imu_values_fetcher_->gyro_y();
Austin Schuh9993fb32017-03-15 20:17:46 -0700199 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800200 }
201 break;
202 case GyroType::IMU_Z_GYRO:
203 if (is_latest_imu_values) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 last_gyro_rate_ = imu_values_fetcher_->gyro_z();
Austin Schuh9993fb32017-03-15 20:17:46 -0700205 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800206 }
207 break;
Austin Schuh90b43b42019-01-04 07:45:05 +1100208 case GyroType::FLIPPED_IMU_Z_GYRO:
209 if (is_latest_imu_values) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210 last_gyro_rate_ = -imu_values_fetcher_->gyro_z();
Austin Schuh90b43b42019-01-04 07:45:05 +1100211 last_gyro_time_ = monotonic_now;
212 }
213 break;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800214 case GyroType::SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700215 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700216 last_gyro_rate_ = gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700217 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800218 }
219 break;
220 case GyroType::FLIPPED_SPARTAN_GYRO:
Austin Schuh1ea89bb2019-05-27 16:59:59 -0700221 if (gyro_reading_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 last_gyro_rate_ = -gyro_reading_fetcher_->velocity();
Austin Schuh9993fb32017-03-15 20:17:46 -0700223 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800224 }
225 break;
226 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700227 AOS_LOG(FATAL, "invalid gyro configured");
Campbell Crowley2527ed22017-02-17 21:10:02 -0800228 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800229 }
230
Austin Schuh9993fb32017-03-15 20:17:46 -0700231 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
232 last_gyro_rate_ = 0.0;
233 }
234
Austin Schuh6613a072016-01-06 19:54:48 -0800235 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700236 Eigen::Matrix<double, 4, 1> Y;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 Y << position->left_encoder(), position->right_encoder(), last_gyro_rate_,
Diana Burgessd0180f12018-03-21 21:24:17 -0700238 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800239 kf_.Correct(Y);
James Kuszmaulef428a02019-03-02 22:19:41 -0800240 // If we get a new message setting the absolute position, then reset the
241 // localizer.
242 // TODO(james): Use a watcher (instead of a fetcher) once we support it in
243 // simulation.
244 if (localizer_control_fetcher_.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245 VLOG(1) << "localizer_control "
246 << aos::FlatbufferToJson(localizer_control_fetcher_.get());
James Kuszmaul518640d2019-04-13 15:50:50 -0700247 localizer_->ResetPosition(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 monotonic_now, localizer_control_fetcher_->x(),
249 localizer_control_fetcher_->y(), localizer_control_fetcher_->theta(),
250 localizer_control_fetcher_->theta_uncertainty(),
251 !localizer_control_fetcher_->keep_current_theta());
James Kuszmaulef428a02019-03-02 22:19:41 -0800252 }
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800253 localizer_->Update({last_last_left_voltage_, last_last_right_voltage_},
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254 monotonic_now, position->left_encoder(),
255 position->right_encoder(), last_gyro_rate_, last_accel_);
Austin Schuh6613a072016-01-06 19:54:48 -0800256 }
257
Austin Schuh093535c2016-03-05 23:21:00 -0800258 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
259
Austin Schuh872723c2019-12-25 14:38:09 -0800260 ControllerType controller_type = ControllerType::POLYDRIVE;
Brian Silverman17f503e2015-08-02 18:17:18 -0700261 if (goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262 controller_type = goal->controller_type();
Brian Silverman17f503e2015-08-02 18:17:18 -0700263
Alex Perrycb7da4b2019-08-28 19:35:56 -0700264 dt_closedloop_.SetGoal(goal);
265 dt_openloop_.SetGoal(goal->wheel(), goal->throttle(), goal->quickturn(),
266 goal->highgear());
267 dt_spline_.SetGoal(goal);
268 dt_line_follow_.SetGoal(monotonic_now, goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700269 }
270
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 dt_openloop_.Update(robot_state().voltage_battery());
Brian Silverman17f503e2015-08-02 18:17:18 -0700272
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 dt_closedloop_.Update(output != nullptr &&
Austin Schuh872723c2019-12-25 14:38:09 -0800274 controller_type == ControllerType::MOTION_PROFILE);
Austin Schuh78379ea2019-01-04 20:39:45 -0800275
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800276 const Eigen::Matrix<double, 5, 1> trajectory_state =
277 (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(),
278 localizer_->theta(), localizer_->left_velocity(),
279 localizer_->right_velocity())
280 .finished();
281
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282 dt_spline_.Update(
Austin Schuh872723c2019-12-25 14:38:09 -0800283 output != nullptr && controller_type == ControllerType::SPLINE_FOLLOWER,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 trajectory_state);
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800285
James Kuszmaul38e79642019-03-09 15:48:27 -0800286 dt_line_follow_.Update(monotonic_now, trajectory_state);
Alex Perrya71badb2019-02-06 19:40:41 -0800287
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 OutputT output_struct;
289
Austin Schuh78379ea2019-01-04 20:39:45 -0800290 switch (controller_type) {
Austin Schuh872723c2019-12-25 14:38:09 -0800291 case ControllerType::POLYDRIVE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700292 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800293 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800294 case ControllerType::MOTION_PROFILE:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295 dt_closedloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
Austin Schuh78379ea2019-01-04 20:39:45 -0800296 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800297 case ControllerType::SPLINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 dt_spline_.SetOutput(output != nullptr ? &output_struct : nullptr);
Alex Perry731b4602019-02-02 22:13:01 -0800299 break;
Austin Schuh872723c2019-12-25 14:38:09 -0800300 case ControllerType::LINE_FOLLOWER:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 if (!dt_line_follow_.SetOutput(output != nullptr ? &output_struct
302 : nullptr)) {
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800303 // If the line follow drivetrain was unable to execute (generally due to
304 // not having a target), execute the regular teleop drivetrain.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700305 dt_openloop_.SetOutput(output != nullptr ? &output_struct : nullptr);
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800306 }
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800307 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700308 }
309
Austin Schuh093535c2016-03-05 23:21:00 -0800310 // The output should now contain the shift request.
311
Brian Silverman17f503e2015-08-02 18:17:18 -0700312 // set the output status of the control loop state
313 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800314 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700315 dt_config_.LeftRightToLinear(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800316 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700317 dt_config_.LeftRightToAngular(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800318
James Kuszmaul3431d622019-02-17 17:07:44 -0800319 angular(0, 0) = localizer_->theta();
Austin Schuh093535c2016-03-05 23:21:00 -0800320
321 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700322 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800323
Alex Perrycb7da4b2019-08-28 19:35:56 -0700324 const flatbuffers::Offset<CIMLogging> cim_logging_offset =
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800325 dt_openloop_.PopulateShiftingStatus(status->fbb());
326
327 const flatbuffers::Offset<PolyDriveLogging> poly_drive_logging_offset =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 dt_openloop_.PopulateStatus(status->fbb());
Austin Schuh093535c2016-03-05 23:21:00 -0800329
Alex Perrycb7da4b2019-08-28 19:35:56 -0700330 flatbuffers::Offset<LineFollowLogging> line_follow_logging_offset =
331 dt_line_follow_.PopulateStatus(status);
332 flatbuffers::Offset<TrajectoryLogging> trajectory_logging_offset =
333 dt_spline_.MakeTrajectoryLogging(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800334
Alex Perrycb7da4b2019-08-28 19:35:56 -0700335 StatusBuilder builder = status->MakeBuilder<Status>();
Austin Schuh3a378462019-01-04 21:48:04 -0800336
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 dt_closedloop_.PopulateStatus(&builder);
Austin Schuh3a378462019-01-04 21:48:04 -0800338
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 builder.add_estimated_left_position(gyro_left_right(0, 0));
340 builder.add_estimated_right_position(gyro_left_right(2, 0));
Austin Schuh41565602016-02-28 20:10:49 -0800341
Alex Perrycb7da4b2019-08-28 19:35:56 -0700342 builder.add_estimated_left_velocity(gyro_left_right(1, 0));
343 builder.add_estimated_right_velocity(gyro_left_right(3, 0));
344
345 if (dt_spline_.enable()) {
346 dt_spline_.PopulateStatus(&builder);
347 } else {
348 builder.add_robot_speed((kf_.X_hat(1) + kf_.X_hat(3)) / 2.0);
349 builder.add_output_was_capped(dt_closedloop_.output_was_capped());
350 builder.add_uncapped_left_voltage(kf_.U_uncapped(0, 0));
351 builder.add_uncapped_right_voltage(kf_.U_uncapped(1, 0));
352 }
353
354 builder.add_left_voltage_error(kf_.X_hat(4));
355 builder.add_right_voltage_error(kf_.X_hat(5));
356 builder.add_estimated_angular_velocity_error(kf_.X_hat(6));
357 builder.add_estimated_heading(localizer_->theta());
358
359 builder.add_x(localizer_->x());
360 builder.add_y(localizer_->y());
361 builder.add_theta(::aos::math::NormalizeAngle(localizer_->theta()));
362
363 builder.add_ground_angle(down_estimator_.X_hat(0) + dt_config_.down_offset);
364 builder.add_cim_logging(cim_logging_offset);
James Kuszmaulaf5dfad2020-01-03 20:02:54 -0800365 builder.add_poly_drive_logging(poly_drive_logging_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 builder.add_gear_logging(gear_logging_offset);
367 builder.add_line_follow_logging(line_follow_logging_offset);
368 builder.add_trajectory_logging(trajectory_logging_offset);
369 status->Send(builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700370 }
Austin Schuh209f1702015-11-29 17:03:00 -0800371
Austin Schuh209f1702015-11-29 17:03:00 -0800372 double left_voltage = 0.0;
373 double right_voltage = 0.0;
374 if (output) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700375 left_voltage = output_struct.left_voltage;
376 right_voltage = output_struct.right_voltage;
377 left_high_requested_ = output_struct.left_high;
378 right_high_requested_ = output_struct.right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800379 }
380
Alex Perrycb7da4b2019-08-28 19:35:56 -0700381 const double scalar = robot_state().voltage_battery() / 12.0;
Austin Schuh209f1702015-11-29 17:03:00 -0800382
383 left_voltage *= scalar;
384 right_voltage *= scalar;
385
Austin Schuh209f1702015-11-29 17:03:00 -0800386 // To validate, look at the following:
387
388 // Observed - dx/dt velocity for left, right.
389
390 // Angular velocity error compared to the gyro
391 // Gyro heading vs left-right
392 // Voltage error.
393
James Kuszmaulf3add3c2019-03-02 14:55:00 -0800394 last_last_left_voltage_ = last_left_voltage_;
395 last_last_right_voltage_ = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800396 Eigen::Matrix<double, 2, 1> U;
Austin Schuhdf79d112016-10-15 21:25:32 -0700397 U(0, 0) = last_left_voltage_;
398 U(1, 0) = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800399 last_left_voltage_ = left_voltage;
400 last_right_voltage_ = right_voltage;
401
Austin Schuh3a378462019-01-04 21:48:04 -0800402 last_state_ = kf_.X_hat();
403 kf_.UpdateObserver(U, dt_config_.dt);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700404
405 if (output) {
406 output->Send(Output::Pack(*output->fbb(), &output_struct));
407 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700408}
409
Alex Perrycb7da4b2019-08-28 19:35:56 -0700410flatbuffers::Offset<Output> DrivetrainLoop::Zero(
411 aos::Sender<Output>::Builder *output) {
412 Output::Builder builder = output->MakeBuilder<Output>();
413 builder.add_left_voltage(0);
414 builder.add_right_voltage(0);
415 builder.add_left_high(dt_config_.default_high_gear);
416 builder.add_right_high(dt_config_.default_high_gear);
417 return builder.Finish();
Adam Snaiderbc918b62016-02-27 21:03:39 -0800418}
419
Austin Schuh6197a182015-11-28 16:04:40 -0800420} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700421} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000422} // namespace frc971