blob: 14c1a42c23ffad609a5d5a68b30f3204a642624a [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
9#include "aos/common/logging/logging.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070010#include "aos/common/logging/queue_logging.h"
11#include "aos/common/logging/matrix_logging.h"
12
Comran Morshed5323ecb2015-12-26 20:50:55 +000013#include "frc971/control_loops/drivetrain/drivetrain.q.h"
14#include "frc971/control_loops/drivetrain/polydrivetrain.h"
15#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Austin Schuh05c5a612016-04-02 15:10:25 -070016#include "frc971/control_loops/drivetrain/down_estimator.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000017#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070018#include "frc971/queues/gyro.q.h"
19#include "frc971/shifter_hall_effect.h"
Austin Schuh05c5a612016-04-02 15:10:25 -070020#include "frc971/wpilib/imu.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070021
Brian Silverman17f503e2015-08-02 18:17:18 -070022using frc971::sensors::gyro_reading;
Campbell Crowley2527ed22017-02-17 21:10:02 -080023using frc971::imu_values;
Austin Schuh9993fb32017-03-15 20:17:46 -070024using ::aos::monotonic_clock;
25namespace chrono = ::std::chrono;
Brian Silverman17f503e2015-08-02 18:17:18 -070026
Comran Morshed5323ecb2015-12-26 20:50:55 +000027namespace frc971 {
Brian Silverman17f503e2015-08-02 18:17:18 -070028namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080029namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070030
Austin Schuh209f1702015-11-29 17:03:00 -080031DrivetrainLoop::DrivetrainLoop(
Austin Schuhbcce26a2018-03-26 23:41:24 -070032 const DrivetrainConfig<double> &dt_config,
Comran Morshed5323ecb2015-12-26 20:50:55 +000033 ::frc971::control_loops::DrivetrainQueue *my_drivetrain)
34 : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>(
Austin Schuh209f1702015-11-29 17:03:00 -080035 my_drivetrain),
Comran Morshed5323ecb2015-12-26 20:50:55 +000036 dt_config_(dt_config),
Austin Schuh41565602016-02-28 20:10:49 -080037 kf_(dt_config_.make_kf_drivetrain_loop()),
38 dt_openloop_(dt_config_, &kf_),
Austin Schuh093535c2016-03-05 23:21:00 -080039 dt_closedloop_(dt_config_, &kf_, &integrated_kf_heading_),
Austin Schuh05c5a612016-04-02 15:10:25 -070040 down_estimator_(MakeDownEstimatorLoop()),
Austin Schuh093535c2016-03-05 23:21:00 -080041 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
42 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
43 left_high_requested_(dt_config_.default_high_gear),
44 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuh209f1702015-11-29 17:03:00 -080045 ::aos::controls::HPolytope<0>::Init();
Austin Schuh05c5a612016-04-02 15:10:25 -070046 down_U_.setZero();
Austin Schuh209f1702015-11-29 17:03:00 -080047}
48
Austin Schuh093535c2016-03-05 23:21:00 -080049int DrivetrainLoop::ControllerIndexFromGears() {
50 if (MaybeHigh(left_gear_)) {
51 if (MaybeHigh(right_gear_)) {
52 return 3;
53 } else {
54 return 2;
55 }
56 } else {
57 if (MaybeHigh(right_gear_)) {
58 return 1;
59 } else {
60 return 0;
61 }
62 }
63}
64
65Gear ComputeGear(double shifter_position,
66 const constants::ShifterHallEffect &shifter_config,
67 bool high_requested) {
68 if (shifter_position < shifter_config.clear_low) {
69 return Gear::LOW;
70 } else if (shifter_position > shifter_config.clear_high) {
71 return Gear::HIGH;
72 } else {
73 if (high_requested) {
74 return Gear::SHIFTING_UP;
75 } else {
76 return Gear::SHIFTING_DOWN;
77 }
78 }
79}
80
Austin Schuh6197a182015-11-28 16:04:40 -080081void DrivetrainLoop::RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000082 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
83 const ::frc971::control_loops::DrivetrainQueue::Position *position,
84 ::frc971::control_loops::DrivetrainQueue::Output *output,
85 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Austin Schuh9993fb32017-03-15 20:17:46 -070086 monotonic_clock::time_point monotonic_now = monotonic_clock::now();
87
Austin Schuh5900d142016-04-03 21:35:12 -070088 if (!has_been_enabled_ && output) {
89 has_been_enabled_ = true;
90 down_estimator_.mutable_X_hat(1, 0) = 0.0;
91 }
92
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:
110 left_gear_ = ComputeGear(position->left_shifter_position,
111 dt_config_.left_drive, left_high_requested_);
112 right_gear_ = ComputeGear(position->right_shifter_position,
113 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
Austin Schuhc5fceb82017-02-25 16:24:12 -0800119 kf_.set_index(ControllerIndexFromGears());
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700120
121 // Set the gear-logging parts of the status
122 if (status) {
123 status->gear_logging.left_state = static_cast<uint32_t>(left_gear_);
124 status->gear_logging.right_state = static_cast<uint32_t>(right_gear_);
125 status->gear_logging.left_loop_high = MaybeHigh(left_gear_);
126 status->gear_logging.right_loop_high = MaybeHigh(right_gear_);
127 status->gear_logging.controller_index = kf_.index();
Austin Schuh093535c2016-03-05 23:21:00 -0800128 }
Kyle Stachowicz2f3c20f2017-07-13 16:04:05 -0700129
Campbell Crowley2527ed22017-02-17 21:10:02 -0800130 const bool is_latest_imu_values = ::frc971::imu_values.FetchLatest();
131 if (is_latest_imu_values) {
Austin Schuh05c5a612016-04-02 15:10:25 -0700132 const double rate = -::frc971::imu_values->gyro_y;
133 const double accel_squared = ::frc971::imu_values->accelerometer_x *
134 ::frc971::imu_values->accelerometer_x +
135 ::frc971::imu_values->accelerometer_y *
136 ::frc971::imu_values->accelerometer_y +
137 ::frc971::imu_values->accelerometer_z *
138 ::frc971::imu_values->accelerometer_z;
Austin Schuhf0c05762016-04-03 16:06:49 -0700139 const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x,
140 ::frc971::imu_values->accelerometer_z) +
Austin Schuh05c5a612016-04-02 15:10:25 -0700141 0.008;
Diana Burgessd0180f12018-03-21 21:24:17 -0700142
143 switch (dt_config_.imu_type) {
144 case IMUType::IMU_X:
145 last_accel_ = -::frc971::imu_values->accelerometer_x;
146 break;
147 case IMUType::IMU_Y:
148 last_accel_ = -::frc971::imu_values->accelerometer_y;
149 break;
150 }
151
Austin Schuh05c5a612016-04-02 15:10:25 -0700152 if (accel_squared > 1.03 || accel_squared < 0.97) {
153 LOG(DEBUG, "New IMU value, rejecting reading\n");
154 } else {
155 // -y is our gyro.
Austin Schuhf0c05762016-04-03 16:06:49 -0700156 // z accel is down
157 // x accel is the front of the robot pointed down.
Austin Schuh05c5a612016-04-02 15:10:25 -0700158 Eigen::Matrix<double, 1, 1> Y;
Austin Schuhdf79d112016-10-15 21:25:32 -0700159 Y(0, 0) = angle;
Austin Schuh05c5a612016-04-02 15:10:25 -0700160 down_estimator_.Correct(Y);
161 }
162
163 LOG(DEBUG,
164 "New IMU value from ADIS16448, rate is %f, angle %f, fused %f, bias "
165 "%f\n",
166 rate, angle, down_estimator_.X_hat(0, 0), down_estimator_.X_hat(1, 0));
Austin Schuhdf79d112016-10-15 21:25:32 -0700167 down_U_(0, 0) = rate;
Austin Schuh05c5a612016-04-02 15:10:25 -0700168 }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800169 down_estimator_.UpdateObserver(down_U_, ::aos::controls::kLoopFrequency);
Austin Schuh05c5a612016-04-02 15:10:25 -0700170
Austin Schuh093535c2016-03-05 23:21:00 -0800171 // TODO(austin): Signal the current gear to both loops.
172
Campbell Crowley2527ed22017-02-17 21:10:02 -0800173 switch (dt_config_.gyro_type) {
174 case GyroType::IMU_X_GYRO:
175 if (is_latest_imu_values) {
176 LOG_STRUCT(DEBUG, "using", *imu_values.get());
177 last_gyro_rate_ = imu_values->gyro_x;
Austin Schuh9993fb32017-03-15 20:17:46 -0700178 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800179 }
180 break;
181 case GyroType::IMU_Y_GYRO:
182 if (is_latest_imu_values) {
183 LOG_STRUCT(DEBUG, "using", *imu_values.get());
184 last_gyro_rate_ = imu_values->gyro_y;
Austin Schuh9993fb32017-03-15 20:17:46 -0700185 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800186 }
187 break;
188 case GyroType::IMU_Z_GYRO:
189 if (is_latest_imu_values) {
190 LOG_STRUCT(DEBUG, "using", *imu_values.get());
191 last_gyro_rate_ = imu_values->gyro_z;
Austin Schuh9993fb32017-03-15 20:17:46 -0700192 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800193 }
194 break;
195 case GyroType::SPARTAN_GYRO:
196 if (gyro_reading.FetchLatest()) {
197 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
198 last_gyro_rate_ = gyro_reading->velocity;
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::FLIPPED_SPARTAN_GYRO:
203 if (gyro_reading.FetchLatest()) {
204 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
205 last_gyro_rate_ = -gyro_reading->velocity;
Austin Schuh9993fb32017-03-15 20:17:46 -0700206 last_gyro_time_ = monotonic_now;
Campbell Crowley2527ed22017-02-17 21:10:02 -0800207 }
208 break;
209 default:
210 LOG(FATAL, "invalid gyro configured");
211 break;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800212 }
213
Austin Schuh9993fb32017-03-15 20:17:46 -0700214 if (monotonic_now > last_gyro_time_ + chrono::milliseconds(20)) {
215 last_gyro_rate_ = 0.0;
216 }
217
Austin Schuh6613a072016-01-06 19:54:48 -0800218 {
Diana Burgessd0180f12018-03-21 21:24:17 -0700219 Eigen::Matrix<double, 4, 1> Y;
220 Y << position->left_encoder, position->right_encoder, last_gyro_rate_,
221 last_accel_;
Austin Schuh6613a072016-01-06 19:54:48 -0800222 kf_.Correct(Y);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000223 integrated_kf_heading_ += dt_config_.dt *
224 (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) /
225 (dt_config_.robot_radius * 2.0);
Austin Schuh093535c2016-03-05 23:21:00 -0800226
227 // gyro_heading = (real_right - real_left) / width
228 // wheel_heading = (wheel_right - wheel_left) / width
229 // gyro_heading + offset = wheel_heading
230 // gyro_goal + offset = wheel_goal
231 // offset = wheel_heading - gyro_heading
232
233 // gyro_goal + wheel_heading - gyro_heading = wheel_goal
Austin Schuh6613a072016-01-06 19:54:48 -0800234 }
235
Austin Schuh093535c2016-03-05 23:21:00 -0800236 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
237
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 bool control_loop_driving = false;
239 if (goal) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700240 control_loop_driving = goal->control_loop_driving;
Brian Silverman17f503e2015-08-02 18:17:18 -0700241
Austin Schuh093535c2016-03-05 23:21:00 -0800242 dt_closedloop_.SetGoal(*goal);
243 dt_openloop_.SetGoal(*goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700244 }
245
Austin Schuh64ebab22015-11-26 13:28:30 -0800246 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -0700247
248 if (control_loop_driving) {
Austin Schuh093535c2016-03-05 23:21:00 -0800249 dt_closedloop_.Update(output != NULL);
250 dt_closedloop_.SetOutput(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700251 } else {
Austin Schuh093535c2016-03-05 23:21:00 -0800252 dt_openloop_.SetOutput(output);
253 // TODO(austin): Set profile to current spot.
254 dt_closedloop_.Update(false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700255 }
256
Austin Schuh093535c2016-03-05 23:21:00 -0800257 // The output should now contain the shift request.
258
Brian Silverman17f503e2015-08-02 18:17:18 -0700259 // set the output status of the control loop state
260 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800261 status->robot_speed = (kf_.X_hat(1, 0) + kf_.X_hat(3, 0)) / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700262
Austin Schuh093535c2016-03-05 23:21:00 -0800263 Eigen::Matrix<double, 2, 1> linear =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700264 dt_config_.LeftRightToLinear(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800265 Eigen::Matrix<double, 2, 1> angular =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700266 dt_config_.LeftRightToAngular(kf_.X_hat());
Austin Schuh093535c2016-03-05 23:21:00 -0800267
268 angular(0, 0) = integrated_kf_heading_;
269
270 Eigen::Matrix<double, 4, 1> gyro_left_right =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700271 dt_config_.AngularLinearToLeftRight(linear, angular);
Austin Schuh093535c2016-03-05 23:21:00 -0800272
273 status->estimated_left_position = gyro_left_right(0, 0);
274 status->estimated_right_position = gyro_left_right(2, 0);
275
276 status->estimated_left_velocity = gyro_left_right(1, 0);
277 status->estimated_right_velocity = gyro_left_right(3, 0);
278 status->output_was_capped = dt_closedloop_.output_was_capped();
279 status->uncapped_left_voltage = kf_.U_uncapped(0, 0);
280 status->uncapped_right_voltage = kf_.U_uncapped(1, 0);
281
282 status->left_voltage_error = kf_.X_hat(4, 0);
283 status->right_voltage_error = kf_.X_hat(5, 0);
284 status->estimated_angular_velocity_error = kf_.X_hat(6, 0);
285 status->estimated_heading = integrated_kf_heading_;
Austin Schuh889fee82016-04-13 22:16:36 -0700286 status->ground_angle = down_estimator_.X_hat(0, 0) + dt_config_.down_offset;
Austin Schuh41565602016-02-28 20:10:49 -0800287
288 dt_openloop_.PopulateStatus(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800289 dt_closedloop_.PopulateStatus(status);
Brian Silverman17f503e2015-08-02 18:17:18 -0700290 }
Austin Schuh209f1702015-11-29 17:03:00 -0800291
Austin Schuh209f1702015-11-29 17:03:00 -0800292 double left_voltage = 0.0;
293 double right_voltage = 0.0;
294 if (output) {
295 left_voltage = output->left_voltage;
296 right_voltage = output->right_voltage;
Austin Schuh093535c2016-03-05 23:21:00 -0800297 left_high_requested_ = output->left_high;
298 right_high_requested_ = output->right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800299 }
300
301 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
302
303 left_voltage *= scalar;
304 right_voltage *= scalar;
305
Austin Schuh209f1702015-11-29 17:03:00 -0800306 // To validate, look at the following:
307
308 // Observed - dx/dt velocity for left, right.
309
310 // Angular velocity error compared to the gyro
311 // Gyro heading vs left-right
312 // Voltage error.
313
314 Eigen::Matrix<double, 2, 1> U;
Austin Schuhdf79d112016-10-15 21:25:32 -0700315 U(0, 0) = last_left_voltage_;
316 U(1, 0) = last_right_voltage_;
Austin Schuh209f1702015-11-29 17:03:00 -0800317 last_left_voltage_ = left_voltage;
318 last_right_voltage_ = right_voltage;
319
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800320 kf_.UpdateObserver(U, ::aos::controls::kLoopFrequency);
Brian Silverman17f503e2015-08-02 18:17:18 -0700321}
322
Adam Snaiderbc918b62016-02-27 21:03:39 -0800323void DrivetrainLoop::Zero(
324 ::frc971::control_loops::DrivetrainQueue::Output *output) {
325 output->left_voltage = 0;
326 output->right_voltage = 0;
327 output->left_high = dt_config_.default_high_gear;
328 output->right_high = dt_config_.default_high_gear;
329}
330
Austin Schuh6197a182015-11-28 16:04:40 -0800331} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700332} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000333} // namespace frc971