blob: cdbd6fe88f077d3aa77b19eae70fb2503f2a549d [file] [log] [blame]
Austin Schuh74455862018-02-17 17:14:59 -08001#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_DYNAMICS_H_
2#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_DYNAMICS_H_
3
4#include "Eigen/Dense"
5
6#include "frc971/control_loops/runge_kutta.h"
Austin Schuh87dc7bb2018-10-29 21:53:23 -07007#include "gflags/gflags.h"
Austin Schuhcb091712018-02-21 20:01:55 -08008
9DECLARE_bool(gravity);
Austin Schuh74455862018-02-17 17:14:59 -080010
11namespace y2018 {
12namespace control_loops {
13namespace superstructure {
14namespace arm {
15
16// This class captures the dynamics of our system. It doesn't actually need to
17// store state yet, so everything can be constexpr and/or static.
18class Dynamics {
19 public:
20 // Below, 1 refers to the proximal joint, and 2 refers to the distal joint.
21 // Length of the joints in meters.
22 static constexpr double kL1 = 46.25 * 0.0254;
23 static constexpr double kL2 = 41.80 * 0.0254;
24
25 // Mass of the joints in kilograms.
26 static constexpr double kM1 = 9.34 / 2.2;
27 static constexpr double kM2 = 9.77 / 2.2;
28
29 // Moment of inertia of the joints in kg m^2
30 static constexpr double kJ1 = 2957.05 * 0.0002932545454545454;
31 static constexpr double kJ2 = 2824.70 * 0.0002932545454545454;
32
33 // Radius of the center of mass of the joints in meters.
34 static constexpr double r1 = 21.64 * 0.0254;
35 static constexpr double r2 = 26.70 * 0.0254;
36
37 // Gear ratios for the two joints.
38 static constexpr double kG1 = 140.0;
39 static constexpr double kG2 = 90.0;
40
41 // MiniCIM motor constants.
Austin Schuhcb091712018-02-21 20:01:55 -080042 static constexpr double kEfficiencyTweak = 0.95;
43 static constexpr double kStallTorque = 1.41 * kEfficiencyTweak;
Austin Schuh74455862018-02-17 17:14:59 -080044 static constexpr double kFreeSpeed = (5840.0 / 60.0) * 2.0 * M_PI;
45 static constexpr double kStallCurrent = 89.0;
46 static constexpr double kResistance = 12.0 / kStallCurrent;
47 static constexpr double Kv = kFreeSpeed / 12.0;
48 static constexpr double Kt = kStallTorque / kStallCurrent;
49
50 // Number of motors on the distal joint.
51 static constexpr double kNumDistalMotors = 2.0;
52
53 static constexpr double kAlpha = kJ1 + r1 * r1 * kM1 + kL1 * kL1 * kM2;
54 static constexpr double kBeta = kL1 * r2 * kM2;
55 static constexpr double kGamma = kJ2 + r2 * r2 * kM2;
56
57 // K3, K4 matricies described below.
58 static const ::Eigen::Matrix<double, 2, 2> K3;
Austin Schuh8e99b822018-02-18 00:15:36 -080059 static const ::Eigen::Matrix<double, 2, 2> K3_inverse;
Austin Schuh74455862018-02-17 17:14:59 -080060 static const ::Eigen::Matrix<double, 2, 2> K4;
61
62 // Generates K1-2 for the arm ODE.
63 // K1 * d^2 theta / dt^2 + K2 * d theta / dt = K3 * V - K4 * d theta/dt
64 // These matricies are missing the velocity factor for K2[1, 0], and K2[0, 1].
65 // You probbaly want MatriciesForState.
66 static void NormilizedMatriciesForState(
67 const ::Eigen::Matrix<double, 4, 1> &X,
68 ::Eigen::Matrix<double, 2, 2> *K1_result,
69 ::Eigen::Matrix<double, 2, 2> *K2_result) {
70 const double angle = X(0, 0) - X(2, 0);
71 const double s = ::std::sin(angle);
72 const double c = ::std::cos(angle);
73 *K1_result << kAlpha, c * kBeta, c * kBeta, kGamma;
74 *K2_result << 0.0, s * kBeta, -s * kBeta, 0.0;
75 }
76
77 // Generates K1-2 for the arm ODE.
78 // K1 * d^2 theta / dt^2 + K2 * d theta / dt = K3 * V - K4 * d theta/dt
79 static void MatriciesForState(const ::Eigen::Matrix<double, 4, 1> &X,
80 ::Eigen::Matrix<double, 2, 2> *K1_result,
81 ::Eigen::Matrix<double, 2, 2> *K2_result) {
82 NormilizedMatriciesForState(X, K1_result, K2_result);
83 (*K2_result)(1, 0) *= X(1, 0);
84 (*K2_result)(0, 1) *= X(3, 0);
85 }
86
87 // TODO(austin): We may want a way to provide K1 and K2 to save CPU cycles.
88
89 // Calculates the acceleration given the current state and control input.
90 static const ::Eigen::Matrix<double, 4, 1> Acceleration(
91 const ::Eigen::Matrix<double, 4, 1> &X,
92 const ::Eigen::Matrix<double, 2, 1> &U) {
93 ::Eigen::Matrix<double, 2, 2> K1;
94 ::Eigen::Matrix<double, 2, 2> K2;
95
96 MatriciesForState(X, &K1, &K2);
97
98 const ::Eigen::Matrix<double, 2, 1> velocity =
99 (::Eigen::Matrix<double, 2, 1>() << X(1, 0), X(3, 0)).finished();
100
101 const ::Eigen::Matrix<double, 2, 1> torque = K3 * U - K4 * velocity;
Austin Schuhcb091712018-02-21 20:01:55 -0800102 const ::Eigen::Matrix<double, 2, 1> gravity_torque = GravityTorque(X);
Austin Schuh74455862018-02-17 17:14:59 -0800103
104 const ::Eigen::Matrix<double, 2, 1> accel =
Austin Schuhcb091712018-02-21 20:01:55 -0800105 K1.inverse() * (torque + gravity_torque - K2 * velocity);
Austin Schuh74455862018-02-17 17:14:59 -0800106
107 return (::Eigen::Matrix<double, 4, 1>() << X(1, 0), accel(0, 0), X(3, 0),
108 accel(1, 0))
109 .finished();
110 }
111
Austin Schuh54e5bb52018-02-19 01:09:18 -0800112 // Calculates the acceleration given the current augmented kalman filter state
113 // and control input.
114 static const ::Eigen::Matrix<double, 6, 1> EKFAcceleration(
115 const ::Eigen::Matrix<double, 6, 1> &X,
116 const ::Eigen::Matrix<double, 2, 1> &U) {
117 ::Eigen::Matrix<double, 2, 2> K1;
118 ::Eigen::Matrix<double, 2, 2> K2;
119
120 MatriciesForState(X.block<4, 1>(0, 0), &K1, &K2);
121
122 const ::Eigen::Matrix<double, 2, 1> velocity =
123 (::Eigen::Matrix<double, 2, 1>() << X(1, 0), X(3, 0)).finished();
124
125 const ::Eigen::Matrix<double, 2, 1> torque =
126 K3 *
127 (U +
128 (::Eigen::Matrix<double, 2, 1>() << X(4, 0), X(5, 0)).finished()) -
129 K4 * velocity;
Austin Schuhcb091712018-02-21 20:01:55 -0800130 const ::Eigen::Matrix<double, 2, 1> gravity_torque =
131 GravityTorque(X.block<4, 1>(0, 0));
Austin Schuh54e5bb52018-02-19 01:09:18 -0800132
133 const ::Eigen::Matrix<double, 2, 1> accel =
Austin Schuhcb091712018-02-21 20:01:55 -0800134 K1.inverse() * (torque + gravity_torque - K2 * velocity);
Austin Schuh54e5bb52018-02-19 01:09:18 -0800135
136 return (::Eigen::Matrix<double, 6, 1>() << X(1, 0), accel(0, 0), X(3, 0),
137 accel(1, 0), 0.0, 0.0)
138 .finished();
139 }
140
Austin Schuh74455862018-02-17 17:14:59 -0800141 // Calculates the voltage required to follow the trajectory. This requires
142 // knowing the current state, desired angular velocity and acceleration.
143 static const ::Eigen::Matrix<double, 2, 1> FF_U(
144 const ::Eigen::Matrix<double, 4, 1> &X,
145 const ::Eigen::Matrix<double, 2, 1> &omega_t,
146 const ::Eigen::Matrix<double, 2, 1> &alpha_t) {
147 ::Eigen::Matrix<double, 2, 2> K1;
148 ::Eigen::Matrix<double, 2, 2> K2;
149
150 MatriciesForState(X, &K1, &K2);
151
Austin Schuhcb091712018-02-21 20:01:55 -0800152 const ::Eigen::Matrix<double, 2, 1> gravity_torque = GravityTorque(X);
153
154 return K3_inverse *
155 (K1 * alpha_t + K2 * omega_t + K4 * omega_t - gravity_torque);
156 }
157
158 static ::Eigen::Matrix<double, 2, 1> GravityTorque(
159 const ::Eigen::Matrix<double, 4, 1> &X) {
160 constexpr double kAccelDueToGravity = 9.8 * kEfficiencyTweak;
161 return (::Eigen::Matrix<double, 2, 1>() << (r1 * kM1 + kL1 * kM2) *
162 ::std::sin(X(0)) *
163 kAccelDueToGravity,
164 r2 * kM2 * ::std::sin(X(2)) * kAccelDueToGravity)
165 .finished() *
166 (FLAGS_gravity ? 1.0 : 0.0);
Austin Schuh74455862018-02-17 17:14:59 -0800167 }
168
169 static const ::Eigen::Matrix<double, 4, 1> UnboundedDiscreteDynamics(
170 const ::Eigen::Matrix<double, 4, 1> &X,
171 const ::Eigen::Matrix<double, 2, 1> &U, double dt) {
172 return ::frc971::control_loops::RungeKutta(Dynamics::Acceleration, X, U,
173 dt);
174 }
Austin Schuh54e5bb52018-02-19 01:09:18 -0800175
176 static const ::Eigen::Matrix<double, 6, 1> UnboundedEKFDiscreteDynamics(
177 const ::Eigen::Matrix<double, 6, 1> &X,
178 const ::Eigen::Matrix<double, 2, 1> &U, double dt) {
179 return ::frc971::control_loops::RungeKutta(Dynamics::EKFAcceleration, X, U,
180 dt);
181 }
Austin Schuh74455862018-02-17 17:14:59 -0800182};
183
184} // namespace arm
185} // namespace superstructure
186} // namespace control_loops
187} // namespace y2018
188
189#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_DYNAMICS_H_