blob: 98974a51ca6267daddf06f5dff9255846523b099 [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),
36 kf_(::y2014::control_loops::drivetrain::MakeKFDrivetrainLoop()) {
37 ::aos::controls::HPolytope<0>::Init();
38}
39
Austin Schuh6197a182015-11-28 16:04:40 -080040void DrivetrainLoop::RunIteration(
Brian Silvermanb601d892015-12-20 18:24:38 -050041 const ::y2014::control_loops::DrivetrainQueue::Goal *goal,
42 const ::y2014::control_loops::DrivetrainQueue::Position *position,
43 ::y2014::control_loops::DrivetrainQueue::Output *output,
44 ::y2014::control_loops::DrivetrainQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -070045 bool bad_pos = false;
46 if (position == nullptr) {
47 LOG_INTERVAL(no_position_);
48 bad_pos = true;
49 }
50 no_position_.Print();
51
52 bool control_loop_driving = false;
53 if (goal) {
54 double wheel = goal->steering;
55 double throttle = goal->throttle;
56 bool quickturn = goal->quickturn;
57#if HAVE_SHIFTERS
58 bool highgear = goal->highgear;
59#endif
60
61 control_loop_driving = goal->control_loop_driving;
62 double left_goal = goal->left_goal;
63 double right_goal = goal->right_goal;
64
Austin Schuh64ebab22015-11-26 13:28:30 -080065 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
66 goal->right_velocity_goal);
Brian Silverman17f503e2015-08-02 18:17:18 -070067#if HAVE_SHIFTERS
Austin Schuh64ebab22015-11-26 13:28:30 -080068 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
Brian Silverman17f503e2015-08-02 18:17:18 -070069#else
Austin Schuh64ebab22015-11-26 13:28:30 -080070 dt_openloop_.SetGoal(wheel, throttle, quickturn, false);
Brian Silverman17f503e2015-08-02 18:17:18 -070071#endif
72 }
73
74 if (!bad_pos) {
75 const double left_encoder = position->left_encoder;
76 const double right_encoder = position->right_encoder;
77 if (gyro_reading.FetchLatest()) {
78 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
Austin Schuh64ebab22015-11-26 13:28:30 -080079 dt_closedloop_.SetPosition(left_encoder, right_encoder,
80 gyro_reading->angle);
Austin Schuh209f1702015-11-29 17:03:00 -080081 last_gyro_heading_ = gyro_reading->angle;
82 last_gyro_rate_ = gyro_reading->velocity;
Brian Silverman17f503e2015-08-02 18:17:18 -070083 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080084 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -070085 }
86 }
Austin Schuh64ebab22015-11-26 13:28:30 -080087 dt_openloop_.SetPosition(position);
88 dt_openloop_.Update();
Brian Silverman17f503e2015-08-02 18:17:18 -070089
90 if (control_loop_driving) {
Austin Schuh64ebab22015-11-26 13:28:30 -080091 dt_closedloop_.Update(output == NULL, true);
92 dt_closedloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070093 } else {
Austin Schuh64ebab22015-11-26 13:28:30 -080094 dt_openloop_.SendMotors(output);
Brian Silverman17f503e2015-08-02 18:17:18 -070095 if (output) {
Austin Schuh64ebab22015-11-26 13:28:30 -080096 dt_closedloop_.SetExternalMotors(output->left_voltage,
97 output->right_voltage);
Brian Silverman17f503e2015-08-02 18:17:18 -070098 }
Austin Schuh64ebab22015-11-26 13:28:30 -080099 dt_closedloop_.Update(output == NULL, false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700100 }
101
102 // set the output status of the control loop state
103 if (status) {
104 bool done = false;
105 if (goal) {
106 done = ((::std::abs(goal->left_goal -
Austin Schuh64ebab22015-11-26 13:28:30 -0800107 dt_closedloop_.GetEstimatedLeftEncoder()) <
Brian Silverman17f503e2015-08-02 18:17:18 -0700108 constants::GetValues().drivetrain_done_distance) &&
109 (::std::abs(goal->right_goal -
Austin Schuh64ebab22015-11-26 13:28:30 -0800110 dt_closedloop_.GetEstimatedRightEncoder()) <
Brian Silverman17f503e2015-08-02 18:17:18 -0700111 constants::GetValues().drivetrain_done_distance));
112 }
113 status->is_done = done;
Austin Schuh64ebab22015-11-26 13:28:30 -0800114 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
115 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
116 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
Brian Silverman17f503e2015-08-02 18:17:18 -0700117
Austin Schuh64ebab22015-11-26 13:28:30 -0800118 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
119 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
120 status->output_was_capped = dt_closedloop_.OutputWasCapped();
121 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
122 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 }
Austin Schuh209f1702015-11-29 17:03:00 -0800124
125
126 double left_voltage = 0.0;
127 double right_voltage = 0.0;
128 if (output) {
129 left_voltage = output->left_voltage;
130 right_voltage = output->right_voltage;
131 }
132
133 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
134
135 left_voltage *= scalar;
136 right_voltage *= scalar;
137
138 kf_.set_controller_index(dt_openloop_.controller_index());
139
140 Eigen::Matrix<double, 3, 1> Y;
141 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
142 kf_.Correct(Y);
143 integrated_kf_heading_ +=
144 kDt * (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) / (kRobotRadius * 2.0);
145
146 // To validate, look at the following:
147
148 // Observed - dx/dt velocity for left, right.
149
150 // Angular velocity error compared to the gyro
151 // Gyro heading vs left-right
152 // Voltage error.
153
154 Eigen::Matrix<double, 2, 1> U;
155 U << last_left_voltage_, last_right_voltage_;
156 last_left_voltage_ = left_voltage;
157 last_right_voltage_ = right_voltage;
158
159 kf_.UpdateObserver(U);
Brian Silverman17f503e2015-08-02 18:17:18 -0700160}
161
Austin Schuh6197a182015-11-28 16:04:40 -0800162} // namespace drivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -0700163} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -0800164} // namespace y2014