Skip to content

Commit b72c800

Browse files
authored
Update gravity to links and clean up the implementation. (#24599)
1 parent 80437a3 commit b72c800

4 files changed

Lines changed: 215 additions & 160 deletions

File tree

bindings/generated_docstrings/multibody_tree.h

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10983,12 +10983,20 @@ Parameter ``M_BP_E``:
1098310983
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1098410984
const char* doc =
1098510985
R"""(This ForceElement allows modeling the effect of a uniform gravity
10986-
field as felt by bodies on the surface of the Earth. This gravity
10987-
field acts on all bodies in the MultibodyTree model.)""";
10986+
field as felt by bodies on the surface of the Earth or elsewhere. This
10987+
gravity field acts on all bodies in the MultibodyPlant except those
10988+
whose ModelInstance has been explicitly excluded.)""";
1098810989
// Symbol: drake::multibody::UniformGravityFieldElement::CalcConservativePower
1098910990
struct /* CalcConservativePower */ {
1099010991
// Source: drake/multibody/tree/uniform_gravity_field_element.h
10991-
const char* doc = R"""()""";
10992+
const char* doc =
10993+
R"""(Returns gravitational power acting on the system. This is positive
10994+
when gravitational potential energy is decreasing. In terms of the
10995+
generalized gravity forces ``tau_g`` and the generalized velocities
10996+
``v``, the gravitational power is ``v ⋅ tau_g``.
10997+
10998+
See also:
10999+
CalcGravityGeneralizedForces())""";
1099211000
} CalcConservativePower;
1099311001
// Symbol: drake::multibody::UniformGravityFieldElement::CalcGravityGeneralizedForces
1099411002
struct /* CalcGravityGeneralizedForces */ {
@@ -11008,45 +11016,50 @@ generalized forces, like so:
1100811016
1100911017
.. code-block:: c++
1101011018
11011-
Mv̇ + C(q, v)v = tau_g(q) + tau_app
11019+
M(q)v̇ + C(q, v)v = tau_g(q) + tau_app
1101211020
1101311021
.. raw:: html
1101411022
1101511023
</details>
1101611024
11017-
where ``tau_app`` includes any other generalized forces applied on the
11018-
system.
11025+
where ``tau_app`` are generalized forces generated by any other
11026+
applied spatial forces (i.e., other than gravity forces).
11027+
11028+
Note:
11029+
Given the gravitational potential energy PE(q), the generalized
11030+
force at the iᵗʰ joint degree of freedom is tau_gᵢ = −∂(PE)/∂qᵢ.
1101911031
1102011032
Parameter ``context``:
1102111033
The context storing the state of the multibody model to which this
1102211034
element belongs.
1102311035
1102411036
Returns:
1102511037
tau_g A vector containing the generalized forces due to this
11026-
gravity field force element. The generalized forces are consistent
11027-
with the vector of generalized velocities ``v`` for the parent
11028-
MultibodyTree model so that the inner product ``v⋅tau_g``
11029-
corresponds to the power applied by the gravity forces on the
11030-
mechanical system. That is, ``v⋅tau_g > 0`` corresponds to
11031-
potential energy going into the system, as either mechanical
11032-
kinetic energy, some other potential energy, or heat, and
11033-
therefore to a decrease of potential energy.)""";
11038+
gravity field force element. The ordering of the generalized
11039+
forces is consistent with the vector of generalized velocities
11040+
``v`` for the parent MultibodyPlant so that the inner product ``v
11041+
⋅ tau_g`` is the gravitational power acting on the mechanical
11042+
system. That is, ``v ⋅ tau_g > 0`` corresponds to a decreasing
11043+
gravitational potential energy and hence an increasing kinetic or
11044+
potential energy, work done, or heat.)""";
1103411045
} CalcGravityGeneralizedForces;
1103511046
// Symbol: drake::multibody::UniformGravityFieldElement::CalcNonConservativePower
1103611047
struct /* CalcNonConservativePower */ {
1103711048
// Source: drake/multibody/tree/uniform_gravity_field_element.h
11038-
const char* doc = R"""()""";
11049+
const char* doc =
11050+
R"""(Returns zero always since gravity is conservative.)""";
1103911051
} CalcNonConservativePower;
1104011052
// Symbol: drake::multibody::UniformGravityFieldElement::CalcPotentialEnergy
1104111053
struct /* CalcPotentialEnergy */ {
1104211054
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1104311055
const char* doc =
11044-
R"""(Computes the total potential energy of all bodies in the model in this
11045-
uniform gravity field. The definition of potential energy allows to
11046-
arbitrarily choose the zero energy height. This element takes the zero
11047-
energy height to be the same as the world's height. That is, a body
11048-
will have zero potential energy when its the height of its center of
11049-
mass is at the world's origin.)""";
11056+
R"""(Computes the total gravitational potential energy of all bodies in
11057+
this MultibodyPlant except those whose model instance has been
11058+
explicitly excluded from gravity. The definition of potential energy
11059+
allows for an arbitrary choice of zero energy height. This element
11060+
takes the zero energy height to be the same as the World origin
11061+
height. That is, a body will have zero gravitational potential energy
11062+
when the height of the body's center of mass in the World is zero.)""";
1105011063
} CalcPotentialEnergy;
1105111064
// Symbol: drake::multibody::UniformGravityFieldElement::DoCalcAndAddForceContribution
1105211065
struct /* DoCalcAndAddForceContribution */ {
@@ -11068,35 +11081,37 @@ mass is at the world's origin.)""";
1106811081
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1106911082
const char* doc_0args =
1107011083
R"""(Constructs a uniform gravity field element with a default strength (on
11071-
the earth's surface) and direction (-z).)""";
11084+
the Earth's surface) and direction (-z).)""";
1107211085
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1107311086
const char* doc_1args =
11074-
R"""(Constructs a uniform gravity field element with a strength given by
11075-
the acceleration of gravity vector ``g_W``, expressed in the world
11076-
frame W.)""";
11087+
R"""(Constructs a uniform gravity field element with a strength and
11088+
direction given by the gravity acceleration vector ``g_W``, expressed
11089+
in the world frame W.)""";
1107711090
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1107811091
const char* doc_2args =
11079-
R"""(Constructs a uniform gravity field element with a strength given by
11080-
the acceleration of gravity vector ``g_W``, expressed in the world
11081-
frame W. Gravity is disabled for the set of model instances
11082-
``disabled_model_instances``.)""";
11092+
R"""(Constructs a uniform gravity field element with a strength and
11093+
direction given by the gravity acceleration vector ``g_W``, expressed
11094+
in the world frame W. Gravity is disabled for the set of model
11095+
instances ``disabled_model_instances``.
11096+
11097+
See also:
11098+
set_enabled(), is_enabled())""";
1108311099
} ctor;
1108411100
// Symbol: drake::multibody::UniformGravityFieldElement::gravity_vector
1108511101
struct /* gravity_vector */ {
1108611102
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1108711103
const char* doc =
11088-
R"""(Returns the acceleration of the gravity vector in m/s², expressed in
11089-
the world frame W.)""";
11104+
R"""(Returns the acceleration of gravity vector in m/s², expressed in the
11105+
world frame W.)""";
1109011106
} gravity_vector;
1109111107
// Symbol: drake::multibody::UniformGravityFieldElement::is_enabled
1109211108
struct /* is_enabled */ {
1109311109
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1109411110
const char* doc =
11095-
R"""(Returns:
11096-
``True`` iff gravity is enabled for ``model_instance``.
11111+
R"""(Checks whether gravity is enabled for a given model instance.
1109711112
11098-
See also:
11099-
enable(), disable().
11113+
Returns:
11114+
``True`` iff gravity is enabled for ``model_instance``.
1110011115
1110111116
Raises:
1110211117
RuntimeError if the model instance is invalid.
@@ -11109,7 +11124,8 @@ See also:
1110911124
struct /* set_enabled */ {
1111011125
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1111111126
const char* doc =
11112-
R"""(Sets is_enabled() for ``model_instance`` to ``is_enabled``.
11127+
R"""(Enables or disables gravity for a given ``model_instance``. Calls to
11128+
this function are only permitted pre-Finalize().
1111311129
1111411130
Raises:
1111511131
if the parent model is finalized.
@@ -11125,8 +11141,8 @@ R"""(Sets is_enabled() for ``model_instance`` to ``is_enabled``.
1112511141
struct /* set_gravity_vector */ {
1112611142
// Source: drake/multibody/tree/uniform_gravity_field_element.h
1112711143
const char* doc =
11128-
R"""(Sets the acceleration of gravity vector, expressed in the world frame
11129-
W in m/s².)""";
11144+
R"""(Sets the gravity acceleration vector (in m/s²), expressed in the world
11145+
frame W.)""";
1113011146
} set_gravity_vector;
1113111147
} UniformGravityFieldElement;
1113211148
// Symbol: drake::multibody::UnitInertia

multibody/plant/test/gravity_test.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ TEST_F(MultibodyPlantGravityForceTest, DisableGravity) {
135135
// The generated generalized forces (tau_gᵢ = −∂V/∂qᵢ) are thus:
136136
// tau_g₁ = −g sin(q₁) (m₁ L₁ + m₂ (d₁ + L₂ cos(q₂)))
137137
// tau_g₂ = −m₂ g L₂ sin(q₂) cos(q₁)
138-
GTEST_TEST(GravityTest, PotentialEnergyAndGravityGeneralizedForces) {
138+
GTEST_TEST(GravityTest, PotentialEnergyPowerAndGravityGeneralizedForces) {
139139
// Build a double pendulum from scratch.
140140
const double m1 = 2.0; // kg
141141
const double m2 = 1.5; // kg
@@ -195,6 +195,29 @@ GTEST_TEST(GravityTest, PotentialEnergyAndGravityGeneralizedForces) {
195195
EXPECT_NEAR(tau_g(1), expected_tau2, 1e-14);
196196
}
197197
}
198+
199+
// Conservative power P is related to potential energy V as P = -𝑑V/𝑑𝑡.
200+
// In this test, potential energy is only due to gravity and V = V(q₁,q₂), so
201+
// P = -𝑑V/𝑑𝑡 = -(∂V/q₁) q̇₁ - (∂V/q₂) q̇₂ = tau_g · v since the generalized
202+
// forces tau_g = -[∂V/q₁ ∂V/q₂] and generalized velocities are v = [q̇₁ q̇₂]ᵀ.
203+
// P is positive (increases kinetic energy) when potential energy V decreases.
204+
for (const double q1 : {M_PI / 6, -M_PI / 3}) {
205+
for (const double q2 : {M_PI / 4, -M_PI / 6}) {
206+
SCOPED_TRACE(fmt::format("q1={}, q2={}", q1, q2));
207+
joint1.set_angle(context.get(), q1);
208+
joint2.set_angle(context.get(), q2);
209+
210+
const double v1 = 1.2;
211+
const double v2 = -0.7;
212+
joint1.set_angular_rate(context.get(), v1);
213+
joint2.set_angular_rate(context.get(), v2);
214+
215+
const VectorXd tau_g = pendulum.CalcGravityGeneralizedForces(*context);
216+
const double expected_power = tau_g(0) * v1 + tau_g(1) * v2;
217+
EXPECT_NEAR(pendulum.EvalConservativePower(*context), expected_power,
218+
1e-14);
219+
}
220+
}
198221
}
199222

200223
} // namespace

0 commit comments

Comments
 (0)