blob: 50c5df7463102ed98d2ae74209788902dc1d73fa [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
Austin Schuh9ab4d9d2016-02-16 23:55:44 -080050 bool gyro_valid = false;
51 if (gyro_reading.FetchLatest()) {
52 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
53 last_gyro_heading_ = gyro_reading->angle;
54 last_gyro_rate_ = gyro_reading->velocity;
55 gyro_valid = true;
56 }
57
Austin Schuh6613a072016-01-06 19:54:48 -080058 {
59 Eigen::Matrix<double, 3, 1> Y;
60 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
61 kf_.Correct(Y);
Comran Morshed5323ecb2015-12-26 20:50:55 +000062 integrated_kf_heading_ += dt_config_.dt *
63 (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) /
64 (dt_config_.robot_radius * 2.0);
Austin Schuh6613a072016-01-06 19:54:48 -080065 }
66
Brian Silverman17f503e2015-08-02 18:17:18 -070067 bool control_loop_driving = false;
68 if (goal) {
69 double wheel = goal->steering;
70 double throttle = goal->throttle;
71 bool quickturn = goal->quickturn;
Brian Silverman17f503e2015-08-02 18:17:18 -070072 bool highgear = goal->highgear;
Brian Silverman17f503e2015-08-02 18:17:18 -070073
74 control_loop_driving = goal->control_loop_driving;
75 double left_goal = goal->left_goal;
76 double right_goal = goal->right_goal;
77
Austin Schuh64ebab22015-11-26 13:28:30 -080078 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
79 goal->right_velocity_goal);
Austin Schuh64ebab22015-11-26 13:28:30 -080080 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070081 }
82
83 if (!bad_pos) {
84 const double left_encoder = position->left_encoder;
85 const double right_encoder = position->right_encoder;
Austin Schuh9ab4d9d2016-02-16 23:55:44 -080086 if (gyro_valid) {
Austin Schuh64ebab22015-11-26 13:28:30 -080087 dt_closedloop_.SetPosition(left_encoder, right_encoder,
88 gyro_reading->angle);
Brian Silverman17f503e2015-08-02 18:17:18 -070089 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080090 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070091 }
92 }
Austin Schuh64ebab22015-11-26 13:28:30 -080093 dt_openloop_.SetPosition(position);
94 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -070095
96 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -080097 dt_closedloop_.Update(output == NULL, true);
98 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070099 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -0800100 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700101 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800102 dt_closedloop_.SetExternalMotors(output->left_voltage,
103 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -0700104 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800105 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700106 }
107
108 // set the output status of the control loop state
109 if (status) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800110 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
111 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
112 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700113
Austin Schuh64ebab22015-11-26 13:28:30 -0800114 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
115 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
116 status->output_was_capped = dt_closedloop_.OutputWasCapped();
117 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
118 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700119 }
Austin Schuh209f1702015-11-29 17:03:00 -0800120
Austin Schuh209f1702015-11-29 17:03:00 -0800121 double left_voltage = 0.0;
122 double right_voltage = 0.0;
123 if (output) {
124 left_voltage = output->left_voltage;
125 right_voltage = output->right_voltage;
126 }
127
128 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
129
130 left_voltage *= scalar;
131 right_voltage *= scalar;
132
Austin Schuh209f1702015-11-29 17:03:00 -0800133 // To validate, look at the following:
134
135 // Observed - dx/dt velocity for left, right.
136
137 // Angular velocity error compared to the gyro
138 // Gyro heading vs left-right
139 // Voltage error.
140
141 Eigen::Matrix<double, 2, 1> U;
142 U << last_left_voltage_, last_right_voltage_;
143 last_left_voltage_ = left_voltage;
144 last_right_voltage_ = right_voltage;
145
146 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700147}
148
Austin Schuh6197a182015-11-28 16:04:40 -0800149} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700150} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +0000151} // namespace frc971