blob: 2280648e2a32110a08b3efdad4cb20a9452bd392 [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 Schuh093535c2016-03-05 23:21:00 -080083 // TODO(austin): Put gear detection logic here.
84 switch (dt_config_.shifter_type) {
85 case ShifterType::SIMPLE_SHIFTER:
86 // Force the right controller for simple shifters since we assume that
87 // gear switching is instantaneous.
88 if (left_high_requested_) {
89 left_gear_ = Gear::HIGH;
90 } else {
91 left_gear_ = Gear::LOW;
92 }
93 if (right_high_requested_) {
94 right_gear_ = Gear::HIGH;
95 } else {
96 right_gear_ = Gear::LOW;
97 }
98 break;
99 case ShifterType::HALL_EFFECT_SHIFTER:
100 left_gear_ = ComputeGear(position->left_shifter_position,
101 dt_config_.left_drive, left_high_requested_);
102 right_gear_ = ComputeGear(position->right_shifter_position,
103 dt_config_.right_drive, right_high_requested_);
104 break;
Brian Silverman17f503e2015-08-02 18:17:18 -0700105 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700106
Austin Schuh093535c2016-03-05 23:21:00 -0800107 kf_.set_controller_index(ControllerIndexFromGears());
108 {
109 GearLogging gear_logging;
110 gear_logging.left_state = static_cast<uint32_t>(left_gear_);
111 gear_logging.right_state = static_cast<uint32_t>(right_gear_);
112 gear_logging.left_loop_high = MaybeHigh(left_gear_);
113 gear_logging.right_loop_high = MaybeHigh(right_gear_);
114 gear_logging.controller_index = kf_.controller_index();
115 LOG_STRUCT(DEBUG, "state", gear_logging);
116 }
Austin Schuh6613a072016-01-06 19:54:48 -0800117
Austin Schuh05c5a612016-04-02 15:10:25 -0700118 if (::frc971::imu_values.FetchLatest()) {
119 const double rate = -::frc971::imu_values->gyro_y;
120 const double accel_squared = ::frc971::imu_values->accelerometer_x *
121 ::frc971::imu_values->accelerometer_x +
122 ::frc971::imu_values->accelerometer_y *
123 ::frc971::imu_values->accelerometer_y +
124 ::frc971::imu_values->accelerometer_z *
125 ::frc971::imu_values->accelerometer_z;
Austin Schuhf0c05762016-04-03 16:06:49 -0700126 const double angle = ::std::atan2(::frc971::imu_values->accelerometer_x,
127 ::frc971::imu_values->accelerometer_z) +
Austin Schuh05c5a612016-04-02 15:10:25 -0700128 0.008;
129 if (accel_squared > 1.03 || accel_squared < 0.97) {
130 LOG(DEBUG, "New IMU value, rejecting reading\n");
131 } else {
132 // -y is our gyro.
Austin Schuhf0c05762016-04-03 16:06:49 -0700133 // z accel is down
134 // x accel is the front of the robot pointed down.
Austin Schuh05c5a612016-04-02 15:10:25 -0700135 Eigen::Matrix<double, 1, 1> Y;
136 Y << angle;
137 down_estimator_.Correct(Y);
138 }
139
140 LOG(DEBUG,
141 "New IMU value from ADIS16448, rate is %f, angle %f, fused %f, bias "
142 "%f\n",
143 rate, angle, down_estimator_.X_hat(0, 0), down_estimator_.X_hat(1, 0));
144 down_U_ << rate;
145 }
146 down_estimator_.UpdateObserver(down_U_);
147
Austin Schuh093535c2016-03-05 23:21:00 -0800148 // TODO(austin): Signal the current gear to both loops.
149
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800150 if (gyro_reading.FetchLatest()) {
151 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
152 last_gyro_heading_ = gyro_reading->angle;
153 last_gyro_rate_ = gyro_reading->velocity;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -0800154 }
155
Austin Schuh6613a072016-01-06 19:54:48 -0800156 {
157 Eigen::Matrix<double, 3, 1> Y;
158 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
159 kf_.Correct(Y);
Comran Morshed5323ecb2015-12-26 20:50:55 +0000160 integrated_kf_heading_ += dt_config_.dt *
161 (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) /
162 (dt_config_.robot_radius * 2.0);
Austin Schuh093535c2016-03-05 23:21:00 -0800163
164 // gyro_heading = (real_right - real_left) / width
165 // wheel_heading = (wheel_right - wheel_left) / width
166 // gyro_heading + offset = wheel_heading
167 // gyro_goal + offset = wheel_goal
168 // offset = wheel_heading - gyro_heading
169
170 // gyro_goal + wheel_heading - gyro_heading = wheel_goal
Austin Schuh6613a072016-01-06 19:54:48 -0800171 }
172
Austin Schuh093535c2016-03-05 23:21:00 -0800173 dt_openloop_.SetPosition(position, left_gear_, right_gear_);
174
Brian Silverman17f503e2015-08-02 18:17:18 -0700175 bool control_loop_driving = false;
176 if (goal) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 control_loop_driving = goal->control_loop_driving;
Brian Silverman17f503e2015-08-02 18:17:18 -0700178
Austin Schuh093535c2016-03-05 23:21:00 -0800179 dt_closedloop_.SetGoal(*goal);
180 dt_openloop_.SetGoal(*goal);
Brian Silverman17f503e2015-08-02 18:17:18 -0700181 }
182
Austin Schuh64ebab22015-11-26 13:28:30 -0800183 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -0700184
185 if (control_loop_driving) {
Austin Schuh093535c2016-03-05 23:21:00 -0800186 dt_closedloop_.Update(output != NULL);
187 dt_closedloop_.SetOutput(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 } else {
Austin Schuh093535c2016-03-05 23:21:00 -0800189 dt_openloop_.SetOutput(output);
190 // TODO(austin): Set profile to current spot.
191 dt_closedloop_.Update(false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 }
193
Austin Schuh093535c2016-03-05 23:21:00 -0800194 // The output should now contain the shift request.
195
Brian Silverman17f503e2015-08-02 18:17:18 -0700196 // set the output status of the control loop state
197 if (status) {
Austin Schuh093535c2016-03-05 23:21:00 -0800198 status->robot_speed = (kf_.X_hat(1, 0) + kf_.X_hat(3, 0)) / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700199
Austin Schuh093535c2016-03-05 23:21:00 -0800200 Eigen::Matrix<double, 2, 1> linear =
201 dt_closedloop_.LeftRightToLinear(kf_.X_hat());
202 Eigen::Matrix<double, 2, 1> angular =
203 dt_closedloop_.LeftRightToAngular(kf_.X_hat());
204
205 angular(0, 0) = integrated_kf_heading_;
206
207 Eigen::Matrix<double, 4, 1> gyro_left_right =
208 dt_closedloop_.AngularLinearToLeftRight(linear, angular);
209
210 status->estimated_left_position = gyro_left_right(0, 0);
211 status->estimated_right_position = gyro_left_right(2, 0);
212
213 status->estimated_left_velocity = gyro_left_right(1, 0);
214 status->estimated_right_velocity = gyro_left_right(3, 0);
215 status->output_was_capped = dt_closedloop_.output_was_capped();
216 status->uncapped_left_voltage = kf_.U_uncapped(0, 0);
217 status->uncapped_right_voltage = kf_.U_uncapped(1, 0);
218
219 status->left_voltage_error = kf_.X_hat(4, 0);
220 status->right_voltage_error = kf_.X_hat(5, 0);
221 status->estimated_angular_velocity_error = kf_.X_hat(6, 0);
222 status->estimated_heading = integrated_kf_heading_;
Austin Schuh41565602016-02-28 20:10:49 -0800223
224 dt_openloop_.PopulateStatus(status);
Austin Schuh093535c2016-03-05 23:21:00 -0800225 dt_closedloop_.PopulateStatus(status);
Brian Silverman17f503e2015-08-02 18:17:18 -0700226 }
Austin Schuh209f1702015-11-29 17:03:00 -0800227
Austin Schuh209f1702015-11-29 17:03:00 -0800228 double left_voltage = 0.0;
229 double right_voltage = 0.0;
230 if (output) {
231 left_voltage = output->left_voltage;
232 right_voltage = output->right_voltage;
Austin Schuh093535c2016-03-05 23:21:00 -0800233 left_high_requested_ = output->left_high;
234 right_high_requested_ = output->right_high;
Austin Schuh209f1702015-11-29 17:03:00 -0800235 }
236
237 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
238
239 left_voltage *= scalar;
240 right_voltage *= scalar;
241
Austin Schuh209f1702015-11-29 17:03:00 -0800242 // To validate, look at the following:
243
244 // Observed - dx/dt velocity for left, right.
245
246 // Angular velocity error compared to the gyro
247 // Gyro heading vs left-right
248 // Voltage error.
249
250 Eigen::Matrix<double, 2, 1> U;
251 U << last_left_voltage_, last_right_voltage_;
252 last_left_voltage_ = left_voltage;
253 last_right_voltage_ = right_voltage;
254
255 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700256}
257
Adam Snaiderbc918b62016-02-27 21:03:39 -0800258void DrivetrainLoop::Zero(
259 ::frc971::control_loops::DrivetrainQueue::Output *output) {
260 output->left_voltage = 0;
261 output->right_voltage = 0;
262 output->left_high = dt_config_.default_high_gear;
263 output->right_high = dt_config_.default_high_gear;
264}
265
Austin Schuh6197a182015-11-28 16:04:40 -0800266} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700267} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000268} // namespace frc971