Skip to content

Commit f600a44

Browse files
authored
Backport nonbreaking changes from ionic (#2552)
* Model.hh: add ModelByName and ModelCount APIs Backport nested model accessor APIs from #2494. * Add WrenchMeasured component * Physics.cc: fix typo in comment * sensor_TEST.py: move PreUpdate callback to Update The test's PreUpdate callback assumes that it executes after the ForceTorque::PreUpdate, so just move it to Update to gurantee it. Also fix a spelling error in the callback variable names. * fix spelling in on_post_update_cb * backport minor fixes to force-torque test * Include System.hh from some systems, add comment Signed-off-by: Steve Peters <[email protected]>
1 parent 1784944 commit f600a44

File tree

10 files changed

+128
-13
lines changed

10 files changed

+128
-13
lines changed

include/gz/sim/Model.hh

+14
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ namespace gz
137137
public: sim::Entity LinkByName(const EntityComponentManager &_ecm,
138138
const std::string &_name) const;
139139

140+
/// \brief Get the ID of a nested model entity which is an immediate
141+
/// child of this model.
142+
/// \param[in] _ecm Entity-component manager.
143+
/// \param[in] _name Nested model name.
144+
/// \return Nested model entity.
145+
public: sim::Entity ModelByName(const EntityComponentManager &_ecm,
146+
const std::string &_name) const;
147+
140148
/// \brief Get all joints which are immediate children of this model.
141149
/// \param[in] _ecm Entity-component manager.
142150
/// \return All joints in this model.
@@ -167,6 +175,12 @@ namespace gz
167175
/// \return Number of links in this model.
168176
public: uint64_t LinkCount(const EntityComponentManager &_ecm) const;
169177

178+
/// \brief Get the number of nested models which are immediate children
179+
/// of this model.
180+
/// \param[in] _ecm Entity-component manager.
181+
/// \return Number of nested models in this model.
182+
public: uint64_t ModelCount(const EntityComponentManager &_ecm) const;
183+
170184
/// \brief Set a command to change the model's pose.
171185
/// \param[in] _ecm Entity-component manager.
172186
/// \param[in] _pose New model pose.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (C) 2024 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#ifndef GZ_SIM_COMPONENTS_WRENCHMEASURED_HH_
18+
#define GZ_SIM_COMPONENTS_WRENCHMEASURED_HH_
19+
20+
#include <gz/msgs/wrench.pb.h>
21+
22+
#include <gz/sim/components/Factory.hh>
23+
#include <gz/sim/components/Component.hh>
24+
#include <gz/sim/components/Serialization.hh>
25+
#include <gz/sim/config.hh>
26+
27+
namespace gz
28+
{
29+
namespace sim
30+
{
31+
// Inline bracket to help doxygen filtering.
32+
inline namespace GZ_SIM_VERSION_NAMESPACE {
33+
34+
namespace components
35+
{
36+
/// \brief Wrench measured by a ForceTorqueSensor in SI units (Nm for torque,
37+
/// N for force).
38+
/// The wrench is expressed in the Sensor frame and the force component is
39+
/// applied at the sensor origin.
40+
/// \note The term Wrench is used here to mean a pair of 3D vectors representing
41+
/// torque and force quantities expressed in a given frame and where the force
42+
/// is applied at the origin of the frame. This is different from the Wrench
43+
/// used in screw theory.
44+
/// \note The value of force_offset in msgs::Wrench is ignored for this
45+
/// component. The force is assumed to be applied at the origin of the sensor
46+
/// frame.
47+
using WrenchMeasured =
48+
Component<msgs::Wrench, class WrenchMeasuredTag,
49+
serializers::MsgSerializer>;
50+
GZ_SIM_REGISTER_COMPONENT("gz_sim_components.WrenchMeasured",
51+
WrenchMeasured)
52+
} // namespace components
53+
}
54+
}
55+
}
56+
57+
#endif

python/test/sensor_TEST.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def test_model(self):
3333
file_path = os.path.dirname(os.path.realpath(__file__))
3434
fixture = TestFixture(os.path.join(file_path, 'joint_test.sdf'))
3535

36-
def on_post_udpate_cb(_info, _ecm):
36+
def on_post_update_cb(_info, _ecm):
3737
self.post_iterations += 1
3838

39-
def on_pre_udpate_cb(_info, _ecm):
40-
self.pre_iterations += 1
39+
def on_update_cb(_info, _ecm):
40+
self.iterations += 1
4141
world_e = world_entity(_ecm)
4242
self.assertNotEqual(K_NULL_ENTITY, world_e)
4343
w = World(world_e)
@@ -53,19 +53,19 @@ def on_pre_udpate_cb(_info, _ecm):
5353
# Pose Test
5454
self.assertEqual(Pose3d(0, 1, 0, 0, 0, 0), sensor.pose(_ecm))
5555
# Topic Test
56-
if self.pre_iterations <= 1:
56+
if self.iterations <= 1:
5757
self.assertEqual(None, sensor.topic(_ecm))
5858
else:
5959
self.assertEqual('sensor_topic_test', sensor.topic(_ecm))
6060
# Parent Test
6161
self.assertEqual(j.entity(), sensor.parent(_ecm))
6262

63-
def on_udpate_cb(_info, _ecm):
64-
self.iterations += 1
63+
def on_pre_update_cb(_info, _ecm):
64+
self.pre_iterations += 1
6565

66-
fixture.on_post_update(on_post_udpate_cb)
67-
fixture.on_update(on_udpate_cb)
68-
fixture.on_pre_update(on_pre_udpate_cb)
66+
fixture.on_post_update(on_post_update_cb)
67+
fixture.on_update(on_update_cb)
68+
fixture.on_pre_update(on_pre_update_cb)
6969
fixture.finalize()
7070

7171
server = fixture.server()

src/Model.cc

+16
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ Entity Model::LinkByName(const EntityComponentManager &_ecm,
148148
components::Link());
149149
}
150150

151+
//////////////////////////////////////////////////
152+
Entity Model::ModelByName(const EntityComponentManager &_ecm,
153+
const std::string &_name) const
154+
{
155+
return _ecm.EntityByComponents(
156+
components::ParentEntity(this->dataPtr->id),
157+
components::Name(_name),
158+
components::Model());
159+
}
160+
151161
//////////////////////////////////////////////////
152162
std::vector<Entity> Model::Joints(const EntityComponentManager &_ecm) const
153163
{
@@ -184,6 +194,12 @@ uint64_t Model::LinkCount(const EntityComponentManager &_ecm) const
184194
return this->Links(_ecm).size();
185195
}
186196

197+
//////////////////////////////////////////////////
198+
uint64_t Model::ModelCount(const EntityComponentManager &_ecm) const
199+
{
200+
return this->Models(_ecm).size();
201+
}
202+
187203
//////////////////////////////////////////////////
188204
void Model::SetWorldPoseCmd(EntityComponentManager &_ecm,
189205
const math::Pose3d &_pose)

src/SystemManager.cc

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ const std::vector<ISystemConfigure *>& SystemManager::SystemsConfigure()
307307
return this->systemsConfigure;
308308
}
309309

310+
//////////////////////////////////////////////////
310311
const std::vector<ISystemConfigureParameters *>&
311312
SystemManager::SystemsConfigureParameters()
312313
{

src/systems/force_torque/ForceTorque.cc

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "gz/sim/components/Sensor.hh"
4646
#include "gz/sim/components/World.hh"
4747
#include "gz/sim/EntityComponentManager.hh"
48+
#include "gz/sim/System.hh"
4849
#include "gz/sim/Util.hh"
4950

5051
using namespace gz;

src/systems/physics/Physics.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393

9494
#include "gz/sim/EntityComponentManager.hh"
9595
#include "gz/sim/Model.hh"
96+
#include "gz/sim/System.hh"
9697
#include "gz/sim/Util.hh"
9798

9899
// Components
@@ -432,7 +433,7 @@ class gz::sim::systems::PhysicsPrivate
432433
}
433434
return true;
434435
}};
435-
/// \brief msgs::Contacts equality comparison function.
436+
/// \brief msgs::Wrench equality comparison function.
436437
public: std::function<bool(const msgs::Wrench &, const msgs::Wrench &)>
437438
wrenchEql{
438439
[](const msgs::Wrench &_a, const msgs::Wrench &_b)

src/systems/user_commands/UserCommands.cc

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "gz/sim/EntityComponentManager.hh"
8181
#include "gz/sim/Model.hh"
8282
#include "gz/sim/SdfEntityCreator.hh"
83+
#include "gz/sim/System.hh"
8384
#include "gz/sim/Util.hh"
8485
#include "gz/sim/World.hh"
8586
#include "gz/sim/components/ContactSensorData.hh"

test/integration/force_torque_system.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ForceTorqueTest : public InternalFixture<::testing::Test>
4343
};
4444

4545
/////////////////////////////////////////////////
46-
TEST_F(ForceTorqueTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MeasureWeight))
46+
TEST_F(ForceTorqueTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MeasureWeightTopic))
4747
{
4848
using namespace std::chrono_literals;
4949
// Start server
@@ -59,8 +59,8 @@ TEST_F(ForceTorqueTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MeasureWeight))
5959

6060
// Having iters exactly in sync with update rate can lead to a race condition
6161
// in the test between simulation and transport
62-
size_t iters = 999u;
63-
size_t updates = 100u;
62+
const size_t iters = 999u;
63+
const size_t updates = 100u;
6464

6565
std::vector<msgs::Wrench> wrenches;
6666
wrenches.reserve(updates);

test/integration/model.cc

+24
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ TEST_F(ModelIntegrationTest, SourceFilePath)
164164
EXPECT_EQ("/tmp/path", model.SourceFilePath(ecm));
165165
}
166166

167+
//////////////////////////////////////////////////
168+
TEST_F(ModelIntegrationTest, ModelByName)
169+
{
170+
EntityComponentManager ecm;
171+
172+
// Model
173+
auto eModel = ecm.CreateEntity();
174+
Model model(eModel);
175+
EXPECT_EQ(eModel, model.Entity());
176+
EXPECT_EQ(0u, model.ModelCount(ecm));
177+
178+
// Nested Model
179+
auto eNestedModel = ecm.CreateEntity();
180+
ecm.CreateComponent<components::Model>(eNestedModel, components::Model());
181+
ecm.CreateComponent<components::ParentEntity>(eNestedModel,
182+
components::ParentEntity(eModel));
183+
ecm.CreateComponent<components::Name>(eNestedModel,
184+
components::Name("nested_model_name"));
185+
186+
// Check model
187+
EXPECT_EQ(eNestedModel, model.ModelByName(ecm, "nested_model_name"));
188+
EXPECT_EQ(1u, model.ModelCount(ecm));
189+
}
190+
167191
//////////////////////////////////////////////////
168192
TEST_F(ModelIntegrationTest, LinkByName)
169193
{

0 commit comments

Comments
 (0)