blob: 5757b58eb567e5310025bbaf93d0ba4de7cbb51e [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;
23
Comran Morshed5323ecb2015-12-26 20:50:55 +000024namespace frc971 {
Brian Silverman17f503e2015-08-02 18:17:18 -070025namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080026namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070027
Austin Schuh209f1702015-11-29 17:03:00 -080028DrivetrainLoop::DrivetrainLoop(
Comran Morshed5323ecb2015-12-26 20:50:55 +000029 const DrivetrainConfig &dt_config,
30 ::frc971::control_loops::DrivetrainQueue *my_drivetrain)
31 : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>(
Austin Schuh209f1702015-11-29 17:03:00 -080032 my_drivetrain),
Comran Morshed5323ecb2015-12-26 20:50:55 +000033 dt_config_(dt_config),
Austin Schuh41565602016-02-28 20:10:49 -080034 kf_(dt_config_.make_kf_drivetrain_loop()),
35 dt_openloop_(dt_config_, &kf_),
Austin Schuh093535c2016-03-05 23:21:00 -080036 dt_closedloop_(dt_config_, &kf_, &integrated_kf_heading_),
Austin Schuh05c5a612016-04-02 15:10:25 -070037 down_estimator_(MakeDownEstimatorLoop()),
Austin Schuh093535c2016-03-05 23:21:00 -080038 left_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
39 right_gear_(dt_config_.default_high_gear ? Gear::HIGH : Gear::LOW),
40 left_high_requested_(dt_config_.default_high_gear),
41 right_high_requested_(dt_config_.default_high_gear) {
Austin Schuh209f1702015-11-29 17:03:00 -080042 ::aos::controls::HPolytope<0>::Init();
Austin Schuh05c5a612016-04-02 15:10:25 -070043 down_U_.setZero();
Austin Schuh209f1702015-11-29 17:03:00 -080044}
45
Austin Schuh093535c2016-03-05 23:21:00 -080046int DrivetrainLoop::ControllerIndexFromGears() {
47 if (MaybeHigh(left_gear_)) {
48 if (MaybeHigh(right_gear_)) {
49 return 3;
50 } else {
51 return 2;
52 }
53 } else {
54 if (MaybeHigh(right_gear_)) {
55 return 1;
56 } else {
57 return 0;
58 }
59 }
60}
61
62Gear ComputeGear(double shifter_position,
63 const constants::ShifterHallEffect &shifter_config,
64 bool high_requested) {
65 if (shifter_position < shifter_config.clear_low) {
66 return Gear::LOW;
67 } else if (shifter_position > shifter_config.clear_high) {
68 return Gear::HIGH;
69 } else {
70 if (high_requested) {
71 return Gear::SHIFTING_UP;
72 } else {
73 return Gear::SHIFTING_DOWN;
74 }
75 }
76}
77
Austin Schuh6197a182015-11-28 16:04:40 -080078void DrivetrainLoop::RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000079 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
80 const ::frc971::control_loops::DrivetrainQueue::Position *position,
81 ::frc971::control_loops::DrivetrainQueue::Output *output,
82 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Austin Schuh5900d142016-04-03 21:35:12 -070083 if (!has_been_enabled_ && output) {
84 has_been_enabled_ = true;
85 down_estimator_.mutable_X_hat(1, 0) = 0.0;
86 }
87
Austin Schuh093535c2016-03-05 23:21:00 -080088 // TODO(austin): Put gear detection logic here.
89 switch (dt_config_.shifter_type) {
90 case ShifterType::SIMPLE_SHIFTER:
91 // Force the right controller for simple shifters since we assume that
92 // gear switching is instantaneous.
93 if (left_high_requested_) {
94 left_gear_ = Gear::HIGH;
95 } else {
96 left_gear_ = Gear::LOW;
97 }
98 if (right_high_requested_) {
99 right_gear_ = Gear::HIGH;
100 } else {
101 right_gear_ = Gear::LOW;
102 }
103 break;
104 case ShifterType::HALL_EFFECT_SHIFTER:
105 left_gear_ = ComputeGear(position->left_shifter_position,
106 dt_config_.left_drive, left_high_requested_);
107 right_gear_ = ComputeGear(position->right_shifter_position,
108 dt_config_.right_drive, right_high_requested_);
109 break;
Adam Snaider18f44172016-10-22 15:30:21 -0700110 case ShifterType::NO_SHIFTER:
111 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700112 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700113
Austin Schuh093535c2016-03-05 23:21:00 -0800114 kf_.set_controller_index(ControllerIndexFromGears());
115 {
116 GearLogging gear_logging;
117 gear_logging.left_state = static_cast<uint32_t>(left_gear_);
118 gear_logging.right_state = static_cast<uint32_t>(right_gear_);
119 gear_logging.left_loop_high = MaybeHigh(left_gear_);
120 gear_logging.right_loop_high = MaybeHigh(right_gear_);
121 gear_logging.controller_index = kf_.controller_index();
122 LOG_STRUCT(DEBUG, "state", gear_logging);
123 }
Austin Schuh6613a072016-01-06 19:54:48 -0800124
Austin Schuh05c5a612016-04-02 15:10:25 -0700125 if (::frc971::imu_values.FetchLatest()) {
126 const double rate = -::frc971::imu_values->gyro_y;
127 const double accel_squared = ::frc971::imu_values->accelerometer_x *
128 ::frc971::imu_values->accelerometer_x +
129 ::frc971::imu_values->accelerometer_y *
130 ::frc971::imu_values->accelerometer_y +
131 ::frc971::imu_values->accelerometer_z *
132 ::frc971::imu_values->accelerometer_z;
Austin Schuhf0c05762016-04-03 16:06:49 -0700133 const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x,
134 ::frc971::imu_values->accelerometer_z) +
Austin Schuh05c5a612016-04-02 15:10:25 -0700135 0.008;
136 if (accel_squared > 1.03 || accel_squared < 0.97) {
137 LOG(DEBUG, "New IMU value, rejecting reading\n");
138 } else {
139 // -y is our gyro.
Austin Schuhf0c05762016-04-03 16:06:49 -0700140 // z accel is down
141 // x accel is the front of the robot pointed down.
Austin Schuh05c5a612016-04-02 15:10:25 -0700142 Eigen::Matrix<double, 1, 1> Y;
143 Y << angle;
144 down_estimator_.Correct(Y);
145 }
146
147 LOG(DEBUG,
148 "New IMU value from ADIS16448, rate is %f, angle %f, fused %f, bias "
149 "%f\n",
150 rate, angle, down_estimator_.X_hat(0, 0), down_estimator_.X_hat(1, 0));
151 down_U_ << rate;
152 }
153 down_estimator_.UpdateObserver(down_U_);
154
Austin Schuh093535c2016-03-05 23:21:00 -0800155 // TODO(austin): Signal the current gear to both loops.
156
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800157 if (gyro_reading.FetchLatest()) {
158 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
159 last_gyro_heading_ = gyro_reading->angle;
160 last_gyro_rate_ = gyro_reading->velocity;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800161 }
162
Austin Schuh6613a072016-01-06 19:54:48 -0800163 {
164 Eigen::Matrix<double, 3, 1> Y;
165 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
166 kf_.Correct(Y);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000167 integrated_kf_heading_ += dt_config_.dt *
168 (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) /
169 (dt_config_.robot_radius * 2.0);
Austin Schuh093535c2016-03-05 23:21:00 -0800170
171 // gyro_heading = (real_right - real_left) / width
172 // wheel_heading = (wheel_right - wheel_left) / width
173 // gyro_heading + offset = wheel_heading
174 // gyro_goal + offset = wheel_goal
175 // offset = wheel_heading - gyro_heading
176
177 // gyro_goal + wheel_heading - gyro_heading = wheel_goal
Austin Schuh6613a072016-01-06 19:54:48 -0800178 }
179
Austin Schuh093535c2016-03-05 23:21:00 -0800180 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
181
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 bool control_loop_driving = false;
183 if (goal) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700184 control_loop_driving = goal->control_loop_driving;
Brian Silverman17f503e2015-08-02 18:17:18 -0700185
Austin Schuh093535c2016-03-05 23:21:00 -0800186 dt_closedloop_.SetGoal(*goal);
187 dt_openloop_.SetGoal(*goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 }
189
Austin Schuh64ebab22015-11-26 13:28:30 -0800190 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -0700191
192 if (control_loop_driving) {
Austin Schuh093535c2016-03-05 23:21:00 -0800193 dt_closedloop_.Update(output != NULL);
194 dt_closedloop_.SetOutput(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700195 } else {
Austin Schuh093535c2016-03-05 23:21:00 -0800196 dt_openloop_.SetOutput(output);
197 // TODO(austin): Set profile to current spot.
198 dt_closedloop_.Update(false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700199 }
200
Austin Schuh093535c2016-03-05 23:21:00 -0800201 // The output should now contain the shift request.
202
Brian Silverman17f503e2015-08-02 18:17:18 -0700203 // set the output status of the control loop state
204 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800205 status->robot_speed = (kf_.X_hat(1, 0) + kf_.X_hat(3, 0)) / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700206
Austin Schuh093535c2016-03-05 23:21:00 -0800207 Eigen::Matrix<double, 2, 1> linear =
208 dt_closedloop_.LeftRightToLinear(kf_.X_hat());
209 Eigen::Matrix<double, 2, 1> angular =
210 dt_closedloop_.LeftRightToAngular(kf_.X_hat());
211
212 angular(0, 0) = integrated_kf_heading_;
213
214 Eigen::Matrix<double, 4, 1> gyro_left_right =
215 dt_closedloop_.AngularLinearToLeftRight(linear, angular);
216
217 status->estimated_left_position = gyro_left_right(0, 0);
218 status->estimated_right_position = gyro_left_right(2, 0);
219
220 status->estimated_left_velocity = gyro_left_right(1, 0);
221 status->estimated_right_velocity = gyro_left_right(3, 0);
222 status->output_was_capped = dt_closedloop_.output_was_capped();
223 status->uncapped_left_voltage = kf_.U_uncapped(0, 0);
224 status->uncapped_right_voltage = kf_.U_uncapped(1, 0);
225
226 status->left_voltage_error = kf_.X_hat(4, 0);
227 status->right_voltage_error = kf_.X_hat(5, 0);
228 status->estimated_angular_velocity_error = kf_.X_hat(6, 0);
229 status->estimated_heading = integrated_kf_heading_;
Austin Schuh889fee82016-04-13 22:16:36 -0700230 status->ground_angle = down_estimator_.X_hat(0, 0) + dt_config_.down_offset;
Austin Schuh41565602016-02-28 20:10:49 -0800231
232 dt_openloop_.PopulateStatus(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800233 dt_closedloop_.PopulateStatus(status);
Brian Silverman17f503e2015-08-02 18:17:18 -0700234 }
Austin Schuh209f1702015-11-29 17:03:00 -0800235
Austin Schuh209f1702015-11-29 17:03:00 -0800236 double left_voltage = 0.0;
237 double right_voltage = 0.0;
238 if (output) {
239 left_voltage = output->left_voltage;
240 right_voltage = output->right_voltage;
Austin Schuh093535c2016-03-05 23:21:00 -0800241 left_high_requested_ = output->left_high;
242 right_high_requested_ = output->right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800243 }
244
245 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
246
247 left_voltage *= scalar;
248 right_voltage *= scalar;
249
Austin Schuh209f1702015-11-29 17:03:00 -0800250 // To validate, look at the following:
251
252 // Observed - dx/dt velocity for left, right.
253
254 // Angular velocity error compared to the gyro
255 // Gyro heading vs left-right
256 // Voltage error.
257
258 Eigen::Matrix<double, 2, 1> U;
259 U << last_left_voltage_, last_right_voltage_;
260 last_left_voltage_ = left_voltage;
261 last_right_voltage_ = right_voltage;
262
263 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700264}
265
Adam Snaiderbc918b62016-02-27 21:03:39 -0800266void DrivetrainLoop::Zero(
267 ::frc971::control_loops::DrivetrainQueue::Output *output) {
268 output->left_voltage = 0;
269 output->right_voltage = 0;
270 output->left_high = dt_config_.default_high_gear;
271 output->right_high = dt_config_.default_high_gear;
272}
273
Austin Schuh6197a182015-11-28 16:04:40 -0800274} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700275} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000276} // namespace frc971