blob: 5717f363af80b9ce2cf7a0f4b0cd38db3f31af3f [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/control_loops/drivetrain/drivetrain.h"
2
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
13#include "y2014/constants.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070014#include "y2014/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh0e997732015-11-08 15:14:53 -080015#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Austin Schuh209f1702015-11-29 17:03:00 -080016#include "y2014/control_loops/drivetrain/kalman_drivetrain_motor_plant.h"
Austin Schuh96ce8ae2015-11-26 12:46:02 -080017#include "y2014/control_loops/drivetrain/polydrivetrain.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080018#include "y2014/control_loops/drivetrain/ssdrivetrain.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070019#include "frc971/queues/gyro.q.h"
20#include "frc971/shifter_hall_effect.h"
21
22// A consistent way to mark code that goes away without shifters. It's still
23// here because we will have shifters again in the future.
24#define HAVE_SHIFTERS 1
25
26using frc971::sensors::gyro_reading;
27
Austin Schuh6197a182015-11-28 16:04:40 -080028namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070029namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080030namespace drivetrain {
Brian Silverman17f503e2015-08-02 18:17:18 -070031
Austin Schuh209f1702015-11-29 17:03:00 -080032DrivetrainLoop::DrivetrainLoop(
Brian Silvermanb601d892015-12-20 18:24:38 -050033 ::y2014::control_loops::DrivetrainQueue *my_drivetrain)
34 : aos::controls::ControlLoop<::y2014::control_loops::DrivetrainQueue>(
Austin Schuh209f1702015-11-29 17:03:00 -080035 my_drivetrain),
Austin Schuh6613a072016-01-06 19:54:48 -080036 kf_(::y2014::control_loops::drivetrain::MakeKFDrivetrainLoop()),
37 dt_openloop_(&kf_) {
Austin Schuh209f1702015-11-29 17:03:00 -080038 ::aos::controls::HPolytope<0>::Init();
39}
40
Austin Schuh6197a182015-11-28 16:04:40 -080041void DrivetrainLoop::RunIteration(
Brian Silvermanb601d892015-12-20 18:24:38 -050042 const ::y2014::control_loops::DrivetrainQueue::Goal *goal,
43 const ::y2014::control_loops::DrivetrainQueue::Position *position,
44 ::y2014::control_loops::DrivetrainQueue::Output *output,
45 ::y2014::control_loops::DrivetrainQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -070046 bool bad_pos = false;
47 if (position == nullptr) {
48 LOG_INTERVAL(no_position_);
49 bad_pos = true;
50 }
51 no_position_.Print();
52
Austin Schuh6613a072016-01-06 19:54:48 -080053 kf_.set_controller_index(dt_openloop_.controller_index());
54
55 {
56 Eigen::Matrix<double, 3, 1> Y;
57 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
58 kf_.Correct(Y);
59 integrated_kf_heading_ +=
60 kDt * (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) / (kRobotRadius * 2.0);
61 }
62
Brian Silverman17f503e2015-08-02 18:17:18 -070063 bool control_loop_driving = false;
64 if (goal) {
65 double wheel = goal->steering;
66 double throttle = goal->throttle;
67 bool quickturn = goal->quickturn;
68#if HAVE_SHIFTERS
69 bool highgear = goal->highgear;
70#endif
71
72 control_loop_driving = goal->control_loop_driving;
73 double left_goal = goal->left_goal;
74 double right_goal = goal->right_goal;
75
Austin Schuh64ebab22015-11-26 13:28:30 -080076 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
77 goal->right_velocity_goal);
Brian Silverman17f503e2015-08-02 18:17:18 -070078#if HAVE_SHIFTERS
Austin Schuh64ebab22015-11-26 13:28:30 -080079 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070080#else
Austin Schuh64ebab22015-11-26 13:28:30 -080081 dt_openloop_.SetGoal(wheel, throttle, quickturn, false);
Brian Silverman17f503e2015-08-02 18:17:18 -070082#endif
83 }
84
85 if (!bad_pos) {
86 const double left_encoder = position->left_encoder;
87 const double right_encoder = position->right_encoder;
88 if (gyro_reading.FetchLatest()) {
89 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
Austin Schuh64ebab22015-11-26 13:28:30 -080090 dt_closedloop_.SetPosition(left_encoder, right_encoder,
91 gyro_reading->angle);
Austin Schuh209f1702015-11-29 17:03:00 -080092 last_gyro_heading_ = gyro_reading->angle;
93 last_gyro_rate_ = gyro_reading->velocity;
Brian Silverman17f503e2015-08-02 18:17:18 -070094 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080095 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070096 }
97 }
Austin Schuh64ebab22015-11-26 13:28:30 -080098 dt_openloop_.SetPosition(position);
99 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -0700100
101 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800102 dt_closedloop_.Update(output == NULL, true);
103 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700104 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -0800105 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -0700106 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800107 dt_closedloop_.SetExternalMotors(output->left_voltage,
108 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -0700109 }
Austin Schuh64ebab22015-11-26 13:28:30 -0800110 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700111 }
112
113 // set the output status of the control loop state
114 if (status) {
Austin Schuh64ebab22015-11-26 13:28:30 -0800115 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
116 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
117 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700118
Austin Schuh64ebab22015-11-26 13:28:30 -0800119 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
120 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
121 status->output_was_capped = dt_closedloop_.OutputWasCapped();
122 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
123 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
Austin Schuh209f1702015-11-29 17:03:00 -0800125
126
127 double left_voltage = 0.0;
128 double right_voltage = 0.0;
129 if (output) {
130 left_voltage = output->left_voltage;
131 right_voltage = output->right_voltage;
132 }
133
134 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
135
136 left_voltage *= scalar;
137 right_voltage *= scalar;
138
Austin Schuh209f1702015-11-29 17:03:00 -0800139 // To validate, look at the following:
140
141 // Observed - dx/dt velocity for left, right.
142
143 // Angular velocity error compared to the gyro
144 // Gyro heading vs left-right
145 // Voltage error.
146
147 Eigen::Matrix<double, 2, 1> U;
148 U << last_left_voltage_, last_right_voltage_;
149 last_left_voltage_ = left_voltage;
150 last_right_voltage_ = right_voltage;
151
152 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700153}
154
Austin Schuh6197a182015-11-28 16:04:40 -0800155} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700156} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -0800157} // namespace y2014