-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
acrobot_plant.cc
231 lines (191 loc) · 7.65 KB
/
acrobot_plant.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "drake/examples/acrobot/acrobot_plant.h"
#include <cmath>
#include <vector>
#include "drake/common/default_scalars.h"
#include "drake/common/drake_throw.h"
#include "drake/systems/controllers/linear_quadratic_regulator.h"
#include "drake/systems/framework/diagram.h"
#include "drake/systems/framework/diagram_builder.h"
#include "drake/systems/sensors/rotary_encoders.h"
using std::sin;
using std::cos;
namespace drake {
namespace examples {
namespace acrobot {
template <typename T>
AcrobotPlant<T>::AcrobotPlant()
: systems::LeafSystem<T>(systems::SystemTypeTag<AcrobotPlant>{}) {
this->DeclareNumericParameter(AcrobotParams<T>());
this->DeclareVectorInputPort("elbow_torque", AcrobotInput<T>());
auto state_index = this->DeclareContinuousState(
AcrobotState<T>(), 2 /* num_q */, 2 /* num_v */, 0 /* num_z */);
this->DeclareStateOutputPort("acrobot_state", state_index);
}
template <typename T>
template <typename U>
AcrobotPlant<T>::AcrobotPlant(const AcrobotPlant<U>&) : AcrobotPlant<T>() {}
template <typename T>
void AcrobotPlant<T>::SetMITAcrobotParameters(systems::Parameters<T>*
parameters) const {
auto* p = dynamic_cast<AcrobotParams<T>*>(parameters);
DRAKE_ASSERT(p != nullptr);
p->set_m1(2.4367);
p->set_m2(0.6178);
p->set_l1(0.2563);
p->set_lc1(1.6738);
p->set_lc2(1.5651);
p->set_Ic1(-4.7443); // Notes: Yikes! Negative inertias (from sysid).
p->set_Ic2(-1.0068);
p->set_b1(0.0320);
p->set_b2(0.0413);
// Note: parameters are identified in a way that torque has the unit of
// current (Amps), in order to simplify the implementation of torque
// constraint on motors. Therefore, some of the numbers here have incorrect
// units.
}
template <typename T>
Matrix2<T> AcrobotPlant<T>::MassMatrix(
const systems::Context<T> &context) const {
const AcrobotState<T>& state = get_state(context);
const AcrobotParams<T>& p = get_parameters(context);
const T c2 = cos(state.theta2());
const T I1 = p.Ic1() + p.m1() * p.lc1() * p.lc1();
const T I2 = p.Ic2() + p.m2() * p.lc2() * p.lc2();
const T m2l1lc2 = p.m2() * p.l1() * p.lc2();
const T m12 = I2 + m2l1lc2 * c2;
Matrix2<T> M;
M << I1 + I2 + p.m2() * p.l1() * p.l1() + 2 * m2l1lc2 * c2, m12, m12,
I2;
return M;
}
template <typename T>
Vector2<T> AcrobotPlant<T>::DynamicsBiasTerm(const systems::Context<T> &
context)
const {
const AcrobotState<T>& state = get_state(context);
const AcrobotParams<T>& p = get_parameters(context);
const T s1 = sin(state.theta1()), s2 = sin(state.theta2());
const T s12 = sin(state.theta1() + state.theta2());
const T m2l1lc2 = p.m2() * p.l1() * p.lc2();
Vector2<T> bias;
// C(q,v)*v terms.
bias << -2 * m2l1lc2 * s2 * state.theta2dot() * state.theta1dot() +
-m2l1lc2 * s2 * state.theta2dot() * state.theta2dot(),
m2l1lc2 * s2 * state.theta1dot() * state.theta1dot();
// -τ_g(q) terms.
bias(0) += p.gravity() * p.m1() * p.lc1() * s1 +
p.gravity() * p.m2() * (p.l1() * s1 + p.lc2() * s12);
bias(1) += p.gravity() * p.m2() * p.lc2() * s12;
// Damping terms.
bias(0) += p.b1() * state.theta1dot();
bias(1) += p.b2() * state.theta2dot();
return bias;
}
// Compute the actual physics.
template <typename T>
void AcrobotPlant<T>::DoCalcTimeDerivatives(
const systems::Context<T>& context,
systems::ContinuousState<T>* derivatives) const {
const AcrobotState<T>& state = get_state(context);
const T& tau = get_tau(context);
const Matrix2<T> M = MassMatrix(context);
const Vector2<T> bias = DynamicsBiasTerm(context);
const Vector2<T> B(0, 1); // input matrix
Vector4<T> xdot;
xdot << state.theta1dot(), state.theta2dot(),
M.inverse() * (B * tau - bias);
derivatives->SetFromVector(xdot);
}
template <typename T>
void AcrobotPlant<T>::DoCalcImplicitTimeDerivativesResidual(
const systems::Context<T>& context,
const systems::ContinuousState<T>& proposed_derivatives,
EigenPtr<VectorX<T>> residual) const {
DRAKE_DEMAND(residual != nullptr);
const AcrobotState<T>& state = get_state(context);
const T& tau = get_tau(context);
const Matrix2<T> M = MassMatrix(context);
const Vector2<T> bias = DynamicsBiasTerm(context);
const Vector2<T> B(0, 1); // input matrix
const auto& proposed_qdot = proposed_derivatives.get_generalized_position();
const auto proposed_vdot =
proposed_derivatives.get_generalized_velocity().CopyToVector();
*residual << proposed_qdot[0] - state.theta1dot(),
proposed_qdot[1] - state.theta2dot(),
M * proposed_vdot - (B * tau - bias);
}
template <typename T>
T AcrobotPlant<T>::DoCalcKineticEnergy(
const systems::Context<T>& context) const {
const AcrobotState<T>& state = dynamic_cast<const AcrobotState<T>&>(
context.get_continuous_state_vector());
Matrix2<T> M = MassMatrix(context);
Vector2<T> qdot(state.theta1dot(), state.theta2dot());
return 0.5 * qdot.transpose() * M * qdot;
}
template <typename T>
T AcrobotPlant<T>::DoCalcPotentialEnergy(
const systems::Context<T>& context) const {
const AcrobotState<T>& state = get_state(context);
const AcrobotParams<T>& p = get_parameters(context);
using std::cos;
const T c1 = cos(state.theta1());
const T c12 = cos(state.theta1() + state.theta2());
return -p.m1() * p.gravity() * p.lc1() * c1 -
p.m2() * p.gravity() * (p.l1() * c1 + p.lc2() * c12);
}
template <typename T>
AcrobotWEncoder<T>::AcrobotWEncoder(bool acrobot_state_as_second_output) {
systems::DiagramBuilder<T> builder;
acrobot_plant_ = builder.template AddSystem<AcrobotPlant<T>>();
acrobot_plant_->set_name("acrobot_plant");
auto encoder =
builder.template AddSystem<systems::sensors::RotaryEncoders<T>>(
4, std::vector<int>{0, 1});
encoder->set_name("encoder");
builder.Cascade(*acrobot_plant_, *encoder);
builder.ExportInput(acrobot_plant_->get_input_port(0), "elbow_torque");
builder.ExportOutput(encoder->get_output_port(), "measured_joint_positions");
if (acrobot_state_as_second_output)
builder.ExportOutput(acrobot_plant_->get_output_port(0), "acrobot_state");
builder.BuildInto(this);
}
template <typename T>
AcrobotState<T>& AcrobotWEncoder<T>::get_mutable_acrobot_state(
systems::Context<T>* context) const {
AcrobotState<T>* x = dynamic_cast<AcrobotState<T>*>(
&this->GetMutableSubsystemContext(*acrobot_plant_, context)
.get_mutable_continuous_state_vector());
DRAKE_DEMAND(x != nullptr);
return *x;
}
std::unique_ptr<systems::AffineSystem<double>> BalancingLQRController(
const AcrobotPlant<double>& acrobot) {
auto context = acrobot.CreateDefaultContext();
// Set nominal torque to zero.
acrobot.GetInputPort("elbow_torque").FixValue(context.get(), 0.0);
// Set nominal state to the upright fixed point.
AcrobotState<double>* x = dynamic_cast<AcrobotState<double>*>(
&context->get_mutable_continuous_state_vector());
DRAKE_ASSERT(x != nullptr);
x->set_theta1(M_PI);
x->set_theta2(0.0);
x->set_theta1dot(0.0);
x->set_theta2dot(0.0);
// Setup LQR Cost matrices (penalize position error 10x more than velocity
// to roughly address difference in units, using sqrt(g/l) as the time
// constant.
Eigen::Matrix4d Q = Eigen::Matrix4d::Identity();
Q(0, 0) = 10;
Q(1, 1) = 10;
Vector1d R = Vector1d::Constant(1);
return systems::controllers::LinearQuadraticRegulator(acrobot, *context, Q,
R);
}
} // namespace acrobot
} // namespace examples
} // namespace drake
DRAKE_DEFINE_CLASS_TEMPLATE_INSTANTIATIONS_ON_DEFAULT_SCALARS(
class ::drake::examples::acrobot::AcrobotPlant)
DRAKE_DEFINE_CLASS_TEMPLATE_INSTANTIATIONS_ON_DEFAULT_NONSYMBOLIC_SCALARS(
class ::drake::examples::acrobot::AcrobotWEncoder)