blob: 12d2cbc5ee5e23298b0c890373c9cd7e5b685d40 [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"
16#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070017#include "frc971/queues/gyro.q.h"
18#include "frc971/shifter_hall_effect.h"
19
Brian Silverman17f503e2015-08-02 18:17:18 -070020using frc971::sensors::gyro_reading;
21
Comran Morshed5323ecb2015-12-26 20:50:55 +000022namespace frc971 {
Brian Silverman17f503e2015-08-02 18:17:18 -070023namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080024namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070025
Austin Schuh209f1702015-11-29 17:03:00 -080026DrivetrainLoop::DrivetrainLoop(
Comran Morshed5323ecb2015-12-26 20:50:55 +000027 const DrivetrainConfig &dt_config,
28 ::frc971::control_loops::DrivetrainQueue *my_drivetrain)
29 : aos::controls::ControlLoop<::frc971::control_loops::DrivetrainQueue>(
Austin Schuh209f1702015-11-29 17:03:00 -080030 my_drivetrain),
Comran Morshed5323ecb2015-12-26 20:50:55 +000031 dt_config_(dt_config),
Austin Schuh41565602016-02-28 20:10:49 -080032 kf_(dt_config_.make_kf_drivetrain_loop()),
33 dt_openloop_(dt_config_, &kf_),
34 dt_closedloop_(dt_config_) {
Austin Schuh209f1702015-11-29 17:03:00 -080035 ::aos::controls::HPolytope<0>::Init();
36}
37
Austin Schuh6197a182015-11-28 16:04:40 -080038void DrivetrainLoop::RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000039 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
40 const ::frc971::control_loops::DrivetrainQueue::Position *position,
41 ::frc971::control_loops::DrivetrainQueue::Output *output,
42 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -070043 bool bad_pos = false;
44 if (position == nullptr) {
45 LOG_INTERVAL(no_position_);
46 bad_pos = true;
47 }
48 no_position_.Print();
49
Austin Schuh6613a072016-01-06 19:54:48 -080050 kf_.set_controller_index(dt_openloop_.controller_index());
51
Austin Schuh9ab4d9d2016-02-16 23:55:44 -080052 bool gyro_valid = false;
53 if (gyro_reading.FetchLatest()) {
54 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
55 last_gyro_heading_ = gyro_reading->angle;
56 last_gyro_rate_ = gyro_reading->velocity;
57 gyro_valid = true;
58 }
59
Austin Schuh6613a072016-01-06 19:54:48 -080060 {
61 Eigen::Matrix<double, 3, 1> Y;
62 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
63 kf_.Correct(Y);
Comran Morshed5323ecb2015-12-26 20:50:55 +000064 integrated_kf_heading_ += dt_config_.dt *
65 (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) /
66 (dt_config_.robot_radius * 2.0);
Austin Schuh6613a072016-01-06 19:54:48 -080067 }
68
Brian Silverman17f503e2015-08-02 18:17:18 -070069 bool control_loop_driving = false;
70 if (goal) {
71 double wheel = goal->steering;
72 double throttle = goal->throttle;
73 bool quickturn = goal->quickturn;
Brian Silverman17f503e2015-08-02 18:17:18 -070074 bool highgear = goal->highgear;
Brian Silverman17f503e2015-08-02 18:17:18 -070075
76 control_loop_driving = goal->control_loop_driving;
77 double left_goal = goal->left_goal;
78 double right_goal = goal->right_goal;
79
Austin Schuh64ebab22015-11-26 13:28:30 -080080 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
81 goal->right_velocity_goal);
Austin Schuh64ebab22015-11-26 13:28:30 -080082 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070083 }
84
85 if (!bad_pos) {
86 const double left_encoder = position->left_encoder;
87 const double right_encoder = position->right_encoder;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -080088 if (gyro_valid) {
Austin Schuh64ebab22015-11-26 13:28:30 -080089 dt_closedloop_.SetPosition(left_encoder, right_encoder,
90 gyro_reading->angle);
Brian Silverman17f503e2015-08-02 18:17:18 -070091 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080092 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070093 }
94 }
Austin Schuh64ebab22015-11-26 13:28:30 -080095 dt_openloop_.SetPosition(position);
96 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -070097
98 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -080099 dt_closedloop_.Update(output == NULL, true);
100 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700101 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -0800102 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700103 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800104 dt_closedloop_.SetExternalMotors(output->left_voltage,
105 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -0700106 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800107 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700108 }
109
110 // set the output status of the control loop state
111 if (status) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800112 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
113 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
114 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700115
Austin Schuh64ebab22015-11-26 13:28:30 -0800116 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
117 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
118 status->output_was_capped = dt_closedloop_.OutputWasCapped();
119 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
120 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Austin Schuh41565602016-02-28 20:10:49 -0800121
122 dt_openloop_.PopulateStatus(status);
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 }
Austin Schuh209f1702015-11-29 17:03:00 -0800124
Austin Schuh209f1702015-11-29 17:03:00 -0800125 double left_voltage = 0.0;
126 double right_voltage = 0.0;
127 if (output) {
128 left_voltage = output->left_voltage;
129 right_voltage = output->right_voltage;
130 }
131
132 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
133
134 left_voltage *= scalar;
135 right_voltage *= scalar;
136
Austin Schuh209f1702015-11-29 17:03:00 -0800137 // To validate, look at the following:
138
139 // Observed - dx/dt velocity for left, right.
140
141 // Angular velocity error compared to the gyro
142 // Gyro heading vs left-right
143 // Voltage error.
144
145 Eigen::Matrix<double, 2, 1> U;
146 U << last_left_voltage_, last_right_voltage_;
147 last_left_voltage_ = left_voltage;
148 last_right_voltage_ = right_voltage;
149
150 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700151}
152
Adam Snaiderbc918b62016-02-27 21:03:39 -0800153void DrivetrainLoop::Zero(
154 ::frc971::control_loops::DrivetrainQueue::Output *output) {
155 output->left_voltage = 0;
156 output->right_voltage = 0;
157 output->left_high = dt_config_.default_high_gear;
158 output->right_high = dt_config_.default_high_gear;
159}
160
Austin Schuh6197a182015-11-28 16:04:40 -0800161} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700162} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000163} // namespace frc971