blob: ca395cbaf9c9c88b7bc22ee2771082ee77763c00 [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),
32 kf_(dt_config_.make_kf_drivetrain_loop()) {
Austin Schuh209f1702015-11-29 17:03:00 -080033 ::aos::controls::HPolytope<0>::Init();
34}
35
Austin Schuh6197a182015-11-28 16:04:40 -080036void DrivetrainLoop::RunIteration(
Comran Morshed5323ecb2015-12-26 20:50:55 +000037 const ::frc971::control_loops::DrivetrainQueue::Goal *goal,
38 const ::frc971::control_loops::DrivetrainQueue::Position *position,
39 ::frc971::control_loops::DrivetrainQueue::Output *output,
40 ::frc971::control_loops::DrivetrainQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -070041 bool bad_pos = false;
42 if (position == nullptr) {
43 LOG_INTERVAL(no_position_);
44 bad_pos = true;
45 }
46 no_position_.Print();
47
Austin Schuh6613a072016-01-06 19:54:48 -080048 kf_.set_controller_index(dt_openloop_.controller_index());
49
50 {
51 Eigen::Matrix<double, 3, 1> Y;
52 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
53 kf_.Correct(Y);
Comran Morshed5323ecb2015-12-26 20:50:55 +000054 integrated_kf_heading_ += dt_config_.dt *
55 (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) /
56 (dt_config_.robot_radius * 2.0);
Austin Schuh6613a072016-01-06 19:54:48 -080057 }
58
Brian Silverman17f503e2015-08-02 18:17:18 -070059 bool control_loop_driving = false;
60 if (goal) {
61 double wheel = goal->steering;
62 double throttle = goal->throttle;
63 bool quickturn = goal->quickturn;
Brian Silverman17f503e2015-08-02 18:17:18 -070064 bool highgear = goal->highgear;
Brian Silverman17f503e2015-08-02 18:17:18 -070065
66 control_loop_driving = goal->control_loop_driving;
67 double left_goal = goal->left_goal;
68 double right_goal = goal->right_goal;
69
Austin Schuh64ebab22015-11-26 13:28:30 -080070 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
71 goal->right_velocity_goal);
Austin Schuh64ebab22015-11-26 13:28:30 -080072 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070073 }
74
75 if (!bad_pos) {
76 const double left_encoder = position->left_encoder;
77 const double right_encoder = position->right_encoder;
78 if (gyro_reading.FetchLatest()) {
79 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
Austin Schuh64ebab22015-11-26 13:28:30 -080080 dt_closedloop_.SetPosition(left_encoder, right_encoder,
81 gyro_reading->angle);
Austin Schuh209f1702015-11-29 17:03:00 -080082 last_gyro_heading_ = gyro_reading->angle;
83 last_gyro_rate_ = gyro_reading->velocity;
Brian Silverman17f503e2015-08-02 18:17:18 -070084 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080085 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070086 }
87 }
Austin Schuh64ebab22015-11-26 13:28:30 -080088 dt_openloop_.SetPosition(position);
89 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -070090
91 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -080092 dt_closedloop_.Update(output == NULL, true);
93 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070094 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080095 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070096 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -080097 dt_closedloop_.SetExternalMotors(output->left_voltage,
98 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -070099 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800100 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700101 }
102
103 // set the output status of the control loop state
104 if (status) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800105 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
106 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
107 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700108
Austin Schuh64ebab22015-11-26 13:28:30 -0800109 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
110 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
111 status->output_was_capped = dt_closedloop_.OutputWasCapped();
112 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
113 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700114 }
Austin Schuh209f1702015-11-29 17:03:00 -0800115
Austin Schuh209f1702015-11-29 17:03:00 -0800116 double left_voltage = 0.0;
117 double right_voltage = 0.0;
118 if (output) {
119 left_voltage = output->left_voltage;
120 right_voltage = output->right_voltage;
121 }
122
123 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
124
125 left_voltage *= scalar;
126 right_voltage *= scalar;
127
Austin Schuh209f1702015-11-29 17:03:00 -0800128 // To validate, look at the following:
129
130 // Observed - dx/dt velocity for left, right.
131
132 // Angular velocity error compared to the gyro
133 // Gyro heading vs left-right
134 // Voltage error.
135
136 Eigen::Matrix<double, 2, 1> U;
137 U << last_left_voltage_, last_right_voltage_;
138 last_left_voltage_ = left_voltage;
139 last_right_voltage_ = right_voltage;
140
141 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700142}
143
Austin Schuh6197a182015-11-28 16:04:40 -0800144} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700145} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000146} // namespace frc971