blob: e97e44097e80d5f3b7d722b5bcd95d20cc2d9e6d [file] [log] [blame]
Brian Silvermanc71537c2016-01-01 13:43:14 -08001#include "y2012/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"
10#include "aos/common/logging/queue_logging.h"
11#include "aos/common/logging/matrix_logging.h"
12
13#include "y2012/control_loops/drivetrain/drivetrain.q.h"
14#include "y2012/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
15#include "y2012/control_loops/drivetrain/kalman_drivetrain_motor_plant.h"
16#include "y2012/control_loops/drivetrain/polydrivetrain.h"
17#include "y2012/control_loops/drivetrain/ssdrivetrain.h"
18#include "frc971/queues/gyro.q.h"
19
20// A consistent way to mark code that goes away without shifters. It's still
21// here because we will have shifters again in the future.
22#define HAVE_SHIFTERS 1
23
24using frc971::sensors::gyro_reading;
25
26namespace y2012 {
27namespace control_loops {
28namespace drivetrain {
29
30DrivetrainLoop::DrivetrainLoop(
31 ::y2012::control_loops::DrivetrainQueue *my_drivetrain)
32 : aos::controls::ControlLoop<::y2012::control_loops::DrivetrainQueue>(
33 my_drivetrain),
34 kf_(::y2012::control_loops::drivetrain::MakeKFDrivetrainLoop()) {
35 ::aos::controls::HPolytope<0>::Init();
36}
37
38void DrivetrainLoop::RunIteration(
39 const ::y2012::control_loops::DrivetrainQueue::Goal *goal,
40 const ::y2012::control_loops::DrivetrainQueue::Position *position,
41 ::y2012::control_loops::DrivetrainQueue::Output *output,
42 ::y2012::control_loops::DrivetrainQueue::Status *status) {
43 bool bad_pos = false;
44 if (position == nullptr) {
45 LOG_INTERVAL(no_position_);
46 bad_pos = true;
47 }
48 no_position_.Print();
49
50 bool control_loop_driving = false;
51 if (goal) {
52 double wheel = goal->steering;
53 double throttle = goal->throttle;
54 bool quickturn = goal->quickturn;
55#if HAVE_SHIFTERS
56 bool highgear = goal->highgear;
57#endif
58
59 control_loop_driving = goal->control_loop_driving;
60 double left_goal = goal->left_goal;
61 double right_goal = goal->right_goal;
62
63 dt_closedloop_.SetGoal(left_goal, goal->left_velocity_goal, right_goal,
64 goal->right_velocity_goal);
65#if HAVE_SHIFTERS
66 dt_openloop_.SetGoal(wheel, throttle, quickturn, highgear);
67#else
68 dt_openloop_.SetGoal(wheel, throttle, quickturn, false);
69#endif
70 }
71
72 if (!bad_pos) {
73 const double left_encoder = position->left_encoder;
74 const double right_encoder = position->right_encoder;
75 if (gyro_reading.FetchLatest()) {
76 LOG_STRUCT(DEBUG, "using", *gyro_reading.get());
77 dt_closedloop_.SetPosition(left_encoder, right_encoder,
78 gyro_reading->angle);
79 last_gyro_heading_ = gyro_reading->angle;
80 last_gyro_rate_ = gyro_reading->velocity;
81 } else {
82 dt_closedloop_.SetRawPosition(left_encoder, right_encoder);
83 }
84 }
85 dt_openloop_.SetPosition(position);
86 dt_openloop_.Update();
87
88 if (control_loop_driving) {
89 dt_closedloop_.Update(output == NULL, true);
90 dt_closedloop_.SendMotors(output);
91 } else {
92 dt_openloop_.SendMotors(output);
93 if (output) {
94 dt_closedloop_.SetExternalMotors(output->left_voltage,
95 output->right_voltage);
96 }
97 dt_closedloop_.Update(output == NULL, false);
98 }
99
100 // set the output status of the control loop state
101 if (status) {
102 status->robot_speed = dt_closedloop_.GetEstimatedRobotSpeed();
103 status->filtered_left_position = dt_closedloop_.GetEstimatedLeftEncoder();
104 status->filtered_right_position = dt_closedloop_.GetEstimatedRightEncoder();
105
106 status->filtered_left_velocity = dt_closedloop_.loop().X_hat(1, 0);
107 status->filtered_right_velocity = dt_closedloop_.loop().X_hat(3, 0);
108 status->output_was_capped = dt_closedloop_.OutputWasCapped();
109 status->uncapped_left_voltage = dt_closedloop_.loop().U_uncapped(0, 0);
110 status->uncapped_right_voltage = dt_closedloop_.loop().U_uncapped(1, 0);
111 }
112
113
114 double left_voltage = 0.0;
115 double right_voltage = 0.0;
116 if (output) {
117 left_voltage = output->left_voltage;
118 right_voltage = output->right_voltage;
119 }
120
121 const double scalar = ::aos::robot_state->voltage_battery / 12.0;
122
123 left_voltage *= scalar;
124 right_voltage *= scalar;
125
126 kf_.set_controller_index(dt_openloop_.controller_index());
127
128 Eigen::Matrix<double, 3, 1> Y;
129 Y << position->left_encoder, position->right_encoder, last_gyro_rate_;
130 kf_.Correct(Y);
131 integrated_kf_heading_ +=
132 kDt * (kf_.X_hat(3, 0) - kf_.X_hat(1, 0)) / (kRobotRadius * 2.0);
133
134 // To validate, look at the following:
135
136 // Observed - dx/dt velocity for left, right.
137
138 // Angular velocity error compared to the gyro
139 // Gyro heading vs left-right
140 // Voltage error.
141
142 Eigen::Matrix<double, 2, 1> U;
143 U << last_left_voltage_, last_right_voltage_;
144 last_left_voltage_ = left_voltage;
145 last_right_voltage_ = right_voltage;
146
147 kf_.UpdateObserver(U);
148}
149
150} // namespace drivetrain
151} // namespace control_loops
152} // namespace y2012