Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds bindings for the Sensor convenience class #2042

Merged
merged 49 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bd9846e
Adds bindings for the world convenience class
Voldivh Jul 18, 2023
1db3735
Adds initial tests
Voldivh Jul 18, 2023
df9608e
Use alias on bindings
Voldivh Jul 19, 2023
40423f8
Adds test to this class
Voldivh Jul 25, 2023
4a013a6
Adds missing dependency for CI
Voldivh Jul 26, 2023
55e5bb7
Adds dependency to packages.apt
Voldivh Jul 26, 2023
863b61b
Adds newline at the end of file
Voldivh Jul 26, 2023
e7c955b
Renames the world sdf file
Voldivh Jul 27, 2023
cbcdf3d
Adds the rest of the test
Voldivh Aug 1, 2023
aa435d6
Removes trailing whitespace
Voldivh Aug 1, 2023
a97358d
Adds import dependency files
Voldivh Aug 8, 2023
99c9de0
Adds newline at end of files
Voldivh Aug 8, 2023
786edbf
Address reviewer feedback
mjcarroll Jul 18, 2023
4289f00
Include correct export header
mjcarroll Jul 18, 2023
5b48e80
Adds model bindings
Voldivh Jul 18, 2023
db2d815
Use alias on bindings
Voldivh Jul 19, 2023
1aeeb53
Adds test and fixes documentation
Voldivh Jul 26, 2023
4989d23
Fixes identation on sdf file
Voldivh Jul 27, 2023
af115a1
Modifies the use of import dependencies
Voldivh Aug 8, 2023
8824ad0
Adds the bindings for the joint class
Voldivh Jul 19, 2023
e68a009
Adds test for the bindings
Voldivh Jul 27, 2023
80a0d5e
Adds empty line at the end of file
Voldivh Jul 27, 2023
916f061
Give executable permission to test
Voldivh Jul 27, 2023
4858939
Modifies documentation
Voldivh Jul 27, 2023
37b883f
Modifies velocity test
Voldivh Aug 1, 2023
6795ee4
Modifies import dependencies
Voldivh Aug 8, 2023
9ad75c4
Adds bindings for the Sensor class
Voldivh Jul 19, 2023
0c3652e
Adds test for the bindings
Voldivh Jul 27, 2023
2aa880c
Adds whitespace at the end of comment
Voldivh Jul 27, 2023
4050b98
Modifies import dependencies
Voldivh Aug 8, 2023
7de886c
Adds copy constructor
Voldivh Aug 10, 2023
49dfce9
Adds copy constructor
Voldivh Aug 10, 2023
0608135
Removes whitespace
Voldivh Aug 10, 2023
35a7f66
Removes whitespace
Voldivh Aug 10, 2023
462a9a9
Merge branch 'main' into voldivh/python_bindings_joint
Voldivh Aug 15, 2023
5237f1b
Modifies the use of the null entity variable
Voldivh Aug 15, 2023
578651c
Adds bindings for the Sensor class
Voldivh Jul 19, 2023
84f8c89
Adds test for the bindings
Voldivh Jul 27, 2023
e1e8168
Adds whitespace at the end of comment
Voldivh Jul 27, 2023
7ffc6a8
Modifies import dependencies
Voldivh Aug 8, 2023
fea97b4
Adds copy constructor
Voldivh Aug 10, 2023
aea15ab
Removes whitespace
Voldivh Aug 10, 2023
4a1e4f2
Modifies the use of the null entity variable
Voldivh Aug 15, 2023
bdb4d6f
Resolve merge conflicts
Voldivh Aug 15, 2023
b01d182
Actually resolves the merge conflict
Voldivh Aug 15, 2023
cc8a985
Merge branch 'main' into voldivh/python_bindings_sensor
Voldivh Aug 17, 2023
0d70b41
Modifies the type of sensor to torque_force
Voldivh Aug 17, 2023
4a8b23f
Removes unused library import
Voldivh Aug 17, 2023
d17a53d
Adds plugins for sensor topic
Voldivh Aug 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pybind11_add_module(${BINDINGS_MODULE_NAME} MODULE
src/gz/sim/Link.cc
src/gz/sim/Model.cc
src/gz/sim/TestFixture.cc
src/gz/sim/Sensor.cc
src/gz/sim/Server.cc
src/gz/sim/ServerConfig.cc
src/gz/sim/UpdateInfo.cc
Expand Down Expand Up @@ -92,6 +93,7 @@ if (BUILD_TESTING)
joint_TEST
link_TEST
model_TEST
sensor_TEST
testFixture_TEST
world_TEST
)
Expand Down
71 changes: 71 additions & 0 deletions python/src/gz/sim/Sensor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "Sensor.hh"

namespace py = pybind11;

namespace gz
{
namespace sim
{
namespace python
{
void defineSimSensor(py::object module)
{
py::class_<gz::sim::Sensor>(module, "Sensor")
.def(py::init<gz::sim::Entity>())
.def(py::init<gz::sim::Sensor>())
.def("entity", &gz::sim::Sensor::Entity,
"Get the entity which this sensor is related to.")
.def("reset_entity", &gz::sim::Sensor::ResetEntity,
"Reset Entity to a new one.")
.def("valid", &gz::sim::Sensor::Valid,
py::arg("ecm"),

Check warning on line 41 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L41

Added line #L41 was not covered by tests
"Check whether this sensor correctly refers to an entity that"
"has a components::Sensor.")
Voldivh marked this conversation as resolved.
Show resolved Hide resolved
.def("name", &gz::sim::Sensor::Name,
py::arg("ecm"),

Check warning on line 45 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L45

Added line #L45 was not covered by tests
"Get the sensor's unscoped name.")
.def("pose", &gz::sim::Sensor::Pose,
py::arg("ecm"),

Check warning on line 48 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L48

Added line #L48 was not covered by tests
"Get the pose of the sensor. "
"If the sensor has a trajectory, this will only return the origin"
"pose of the trajectory and not the actual world pose of the sensor.")
.def("topic", &gz::sim::Sensor::Topic,
py::arg("ecm"),

Check warning on line 53 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L53

Added line #L53 was not covered by tests
"Get the topic of the sensor.")
.def("parent", &gz::sim::Sensor::Parent,
py::arg("ecm"),

Check warning on line 56 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L56

Added line #L56 was not covered by tests
"Get the parent entity. This can be a link or a joint.")
.def("__copy__",
[](const gz::sim::Sensor &self)

Check warning on line 59 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L59

Added line #L59 was not covered by tests
{
return gz::sim::Sensor(self);

Check warning on line 61 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L61

Added line #L61 was not covered by tests
})
.def("__deepcopy__",
[](const gz::sim::Sensor &self, pybind11::dict)

Check warning on line 64 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L64

Added line #L64 was not covered by tests
{
return gz::sim::Sensor(self);

Check warning on line 66 in python/src/gz/sim/Sensor.cc

View check run for this annotation

Codecov / codecov/patch

python/src/gz/sim/Sensor.cc#L66

Added line #L66 was not covered by tests
});
}
} // namespace python
} // namespace sim
} // namespace gz
40 changes: 40 additions & 0 deletions python/src/gz/sim/Sensor.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef GZ_SIM_PYTHON__SENSOR_HH_
#define GZ_SIM_PYTHON__SENSOR_HH_

#include <pybind11/pybind11.h>

#include <gz/sim/Sensor.hh>

namespace gz
{
namespace sim
{
namespace python
{
/// Define a pybind11 wrapper for a gz::sim::Sensor
/**
* \param[in] module a pybind11 module to add the definition to
*/
void
defineSimSensor(pybind11::object module);
} // namespace python
} // namespace sim
} // namespace gz

#endif // GZ_SIM_PYTHON__SENSOR_HH_
2 changes: 2 additions & 0 deletions python/src/gz/sim/_gz_sim_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Joint.hh"
#include "Link.hh"
#include "Model.hh"
#include "Sensor.hh"
#include "Server.hh"
#include "ServerConfig.hh"
#include "TestFixture.hh"
Expand All @@ -38,6 +39,7 @@ PYBIND11_MODULE(BINDINGS_MODULE_NAME, m) {
gz::sim::python::defineSimJoint(m);
gz::sim::python::defineSimLink(m);
gz::sim::python::defineSimModel(m);
gz::sim::python::defineSimSensor(m);
gz::sim::python::defineSimServer(m);
gz::sim::python::defineSimServerConfig(m);
gz::sim::python::defineSimTestFixture(m);
Expand Down
3 changes: 3 additions & 0 deletions python/test/joint_test.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
<axis>
<xyz>0 0 1</xyz>
</axis>

<sensor type="force_torque" name="sensor_test">
<pose>0 1 0 0 0 0</pose>
<topic>sensor_topic_test</topic>
</sensor>
</joint>
</model>
Expand Down
79 changes: 79 additions & 0 deletions python/test/sensor_TEST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
# Copyright (C) 2023 Open Source Robotics Foundation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

from gz.common import set_verbosity
from gz_test_deps.sim import (K_NULL_ENTITY, TestFixture,
Joint, Model, Sensor, World, world_entity)
from gz_test_deps.math import Pose3d


class TestSensor(unittest.TestCase):
post_iterations = 0
iterations = 0
pre_iterations = 0

def test_model(self):
set_verbosity(4)

file_path = os.path.dirname(os.path.realpath(__file__))
fixture = TestFixture(os.path.join(file_path, 'joint_test.sdf'))

def on_post_udpate_cb(_info, _ecm):
self.post_iterations += 1

def on_pre_udpate_cb(_info, _ecm):
self.pre_iterations += 1
world_e = world_entity(_ecm)
self.assertNotEqual(K_NULL_ENTITY, world_e)
w = World(world_e)
m = Model(w.model_by_name(_ecm, 'model_test'))
j = Joint(m.joint_by_name(_ecm, 'joint_test'))
sensor = Sensor(j.sensor_by_name(_ecm, 'sensor_test'))
# Entity Test
self.assertNotEqual(K_NULL_ENTITY, sensor.entity())
# Valid Test
self.assertTrue(sensor.valid(_ecm))
# Name Test
self.assertEqual('sensor_test', sensor.name(_ecm))
# Pose Test
self.assertEqual(Pose3d(0, 1, 0, 0, 0, 0), sensor.pose(_ecm))
# Topic Test
# This is None because the entity does not have a
# components::SensorTopic component.
azeey marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(None, sensor.topic(_ecm))
# Parent Test
self.assertEqual(j.entity(), sensor.parent(_ecm))

def on_udpate_cb(_info, _ecm):
self.iterations += 1

fixture.on_post_update(on_post_udpate_cb)
fixture.on_update(on_udpate_cb)
fixture.on_pre_update(on_pre_udpate_cb)
fixture.finalize()

server = fixture.server()
server.run(True, 2, False)

self.assertEqual(2, self.pre_iterations)
self.assertEqual(2, self.iterations)
self.assertEqual(2, self.post_iterations)


if __name__ == '__main__':
unittest.main()