Skip to content

Commit f497a47

Browse files
mergify[bot]IceShuttlearjo129
authored
Gravity set command fixed (#3189) (#3190)
This pull requests solves the [issue](gazebosim/gz-physics#766) in gz-physics but since it requires changes in this repo instead of gz-physics I am opening this pr here. With this gazebo now supports changing gravity at runtime by running 'gz service -s /world/world_demo/set_physics --reqtype gz.msgs.Physics --reptype gz.msgs.Boolean --timeout 2000 --req 'gravity { x: 0 y: 0 z: -2.8 }' --------- (cherry picked from commit d2780bb) Signed-off-by: Shivang <[email protected]> Signed-off-by: Arjo Chakravarty <[email protected]> Co-authored-by: Shivang <[email protected]> Co-authored-by: Arjo Chakravarty <[email protected]>
1 parent 1dcbdf7 commit f497a47

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/SimulationRunner.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <pybind11/pybind11.h>
2626
#endif
2727

28+
#include <gz/math/Vector3.hh>
2829
#include <gz/msgs/boolean.pb.h>
2930
#include <gz/msgs/clock.pb.h>
3031
#include <gz/msgs/gui.pb.h>
@@ -46,6 +47,7 @@
4647
#include "gz/sim/components/Sensor.hh"
4748
#include "gz/sim/components/Visual.hh"
4849
#include "gz/sim/components/World.hh"
50+
#include "gz/sim/components/Gravity.hh"
4951
#include "gz/sim/components/ParentEntity.hh"
5052
#include "gz/sim/components/Physics.hh"
5153
#include "gz/sim/components/PhysicsCmd.hh"
@@ -403,10 +405,26 @@ void SimulationRunner::UpdatePhysicsParams()
403405
{
404406
return;
405407
}
408+
const auto& physicsParams = physicsCmdComp->Data();
409+
410+
auto gravityComp =
411+
this->entityCompMgr.Component<components::Gravity>(worldEntity);
412+
if (gravityComp)
413+
{
414+
const gz::math::Vector3<double> newGravity =
415+
{
416+
physicsParams.gravity().x(),
417+
physicsParams.gravity().y(),
418+
physicsParams.gravity().z()
419+
};
420+
gravityComp->Data() = newGravity;
421+
this->entityCompMgr.SetChanged(worldEntity, components::Gravity::typeId,
422+
ComponentState::OneTimeChange);
423+
}
424+
406425
auto physicsComp =
407426
this->entityCompMgr.Component<components::Physics>(worldEntity);
408427

409-
const auto& physicsParams = physicsCmdComp->Data();
410428
const auto newStepSize =
411429
std::chrono::duration<double>(physicsParams.max_step_size());
412430
const double newRTF = physicsParams.real_time_factor();

src/systems/physics/Physics.cc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <gz/msgs/Utility.hh>
2424

2525
#include <algorithm>
26-
#include <iostream>
2726
#include <deque>
2827
#include <map>
2928
#include <set>
@@ -483,6 +482,13 @@ class gz::sim::systems::PhysicsPrivate
483482
//////////////////////////////////////////////////
484483

485484
//////////////////////////////////////////////////
485+
486+
// Gravity
487+
public: struct GravityFeatureList
488+
: physics::FeatureList<
489+
MinimumFeatureList,
490+
physics::Gravity>{};
491+
486492
// Slip Compliance
487493

488494
/// \brief Feature list to process `FrictionPyramidSlipCompliance` components.
@@ -694,6 +700,7 @@ class gz::sim::systems::PhysicsPrivate
694700
MinimumFeatureList,
695701
CollisionFeatureList,
696702
ContactFeatureList,
703+
GravityFeatureList,
697704
RayIntersectionFeatureList,
698705
SetContactPropertiesCallbackFeatureList,
699706
NestedModelFeatureList,
@@ -2197,6 +2204,31 @@ void PhysicsPrivate::RemovePhysicsEntities(const EntityComponentManager &_ecm)
21972204
void PhysicsPrivate::UpdatePhysics(EntityComponentManager &_ecm)
21982205
{
21992206
GZ_PROFILE("PhysicsPrivate::UpdatePhysics");
2207+
// Gravity state
2208+
_ecm.Each<components::Gravity>(
2209+
[&](const Entity & _entity, const components::Gravity *_gravity)
2210+
{
2211+
auto gravityFeature =
2212+
this->entityWorldMap.EntityCast<GravityFeatureList>(_entity);
2213+
if (!gravityFeature)
2214+
{
2215+
static bool informed{false};
2216+
if (!informed)
2217+
{
2218+
gzdbg << "Attempting to set physics options, but the "
2219+
<< "physics engine doesn't support feature "
2220+
<< "[GravityFeature]. Options will be ignored."
2221+
<< std::endl;
2222+
informed = true;
2223+
}
2224+
return false;
2225+
}
2226+
auto new_grav = _gravity->Data();
2227+
gravityFeature->SetGravity({new_grav.X(), new_grav.Y(), new_grav.Z()});
2228+
return true;
2229+
});
2230+
2231+
22002232
// Battery state
22012233
_ecm.Each<components::BatterySoC>(
22022234
[&](const Entity & _entity, const components::BatterySoC *_bat)

test/integration/user_commands.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include <future>
19+
#include <gz/msgs/details/vector3d.pb.h>
1920
#include <string>
2021

2122
#include <gtest/gtest.h>
@@ -37,6 +38,7 @@
3738
#include <gz/transport/Node.hh>
3839
#include <gz/utils/ExtraTestMacros.hh>
3940

41+
#include "gz/sim/components/Gravity.hh"
4042
#include "gz/sim/components/Light.hh"
4143
#include "gz/sim/components/Link.hh"
4244
#include "gz/sim/components/Material.hh"
@@ -1584,15 +1586,30 @@ TEST_F(UserCommandsTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(Physics))
15841586
auto worldEntity = ecm->EntityByComponents(components::World());
15851587
EXPECT_NE(kNullEntity, worldEntity);
15861588
auto physicsComp = ecm->Component<components::Physics>(worldEntity);
1589+
auto gravityComp = ecm->Component<components::Gravity>(worldEntity);
15871590
ASSERT_NE(nullptr, physicsComp);
1591+
ASSERT_NE(nullptr, gravityComp);
15881592
EXPECT_DOUBLE_EQ(0.001, physicsComp->Data().MaxStepSize());
15891593
EXPECT_DOUBLE_EQ(0.0, physicsComp->Data().RealTimeFactor());
15901594

1595+
auto gravity = gravityComp->Data();
1596+
EXPECT_DOUBLE_EQ(0.0, gravity.X());
1597+
EXPECT_DOUBLE_EQ(0.0, gravity.Y());
1598+
EXPECT_DOUBLE_EQ(-9.8, gravity.Z());
1599+
15911600
// Set physics properties
15921601
msgs::Physics req;
15931602
req.set_max_step_size(0.123);
15941603
req.set_real_time_factor(4.567);
15951604

1605+
gz::msgs::Vector3d new_gravity;
1606+
new_gravity.set_x(1.0);
1607+
new_gravity.set_y(3.0);
1608+
new_gravity.set_z(5.0);
1609+
req.mutable_gravity()->set_x(new_gravity.x());
1610+
req.mutable_gravity()->set_y(new_gravity.y());
1611+
req.mutable_gravity()->set_z(new_gravity.z());
1612+
15961613
msgs::Boolean res;
15971614
bool result;
15981615
unsigned int timeout = 5000;
@@ -1607,6 +1624,13 @@ TEST_F(UserCommandsTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(Physics))
16071624
// in the second one it is processed
16081625
server.Run(true, 2, false);
16091626

1627+
// Check updated gravity
1628+
gravityComp = ecm->Component<components::Gravity>(worldEntity);
1629+
gravity = gravityComp->Data();
1630+
EXPECT_DOUBLE_EQ(new_gravity.x(), gravity.X());
1631+
EXPECT_DOUBLE_EQ(new_gravity.y(), gravity.Y());
1632+
EXPECT_DOUBLE_EQ(new_gravity.z(), gravity.Z());
1633+
16101634
// Check updated physics properties
16111635
physicsComp = ecm->Component<components::Physics>(worldEntity);
16121636
EXPECT_DOUBLE_EQ(0.123, physicsComp->Data().MaxStepSize());

0 commit comments

Comments
 (0)