From a477ba87bbbc76f127ce0fbe3c5a9027a4277edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Tue, 12 Apr 2022 17:11:21 +0200 Subject: [PATCH 1/4] Added Magnetometer Python interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alejandro Hernández --- python/CMakeLists.txt | 2 + python/src/sdf/_ignition_sdformat_pybind11.cc | 2 + python/src/sdf/pyMagnetometer.cc | 62 ++++++++++++++++ python/src/sdf/pyMagnetometer.hh | 41 +++++++++++ python/test/pyMagnetometer_TEST.py | 70 +++++++++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 python/src/sdf/pyMagnetometer.cc create mode 100644 python/src/sdf/pyMagnetometer.hh create mode 100644 python/test/pyMagnetometer_TEST.py diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 57e28269e..741f6cc9d 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -40,6 +40,7 @@ pybind11_add_module(sdformat SHARED src/sdf/_ignition_sdformat_pybind11.cc src/sdf/pyBox.cc src/sdf/pyError.cc + src/sdf/pyMagnetometercc src/sdf/pyMesh.cc src/sdf/pyParserConfig.cc src/sdf/pySphere.cc @@ -64,6 +65,7 @@ if (BUILD_TESTING) set(python_tests pyBox_TEST pyError_TEST + pyMagnetometer_TEST pyMesh_TEST pyParserConfig_TEST pySphere_TEST diff --git a/python/src/sdf/_ignition_sdformat_pybind11.cc b/python/src/sdf/_ignition_sdformat_pybind11.cc index 98fb1265f..e8b50ca45 100644 --- a/python/src/sdf/_ignition_sdformat_pybind11.cc +++ b/python/src/sdf/_ignition_sdformat_pybind11.cc @@ -18,6 +18,7 @@ #include "pyBox.hh" #include "pyError.hh" +#include "pyMagnetometer.hh" #include "pyMesh.hh" #include "pyParserConfig.hh" #include "pySphere.hh" @@ -27,6 +28,7 @@ PYBIND11_MODULE(sdformat, m) { sdf::python::defineBox(m); sdf::python::defineError(m); + sdf::python::defineMagnetometer(m); sdf::python::defineMesh(m); sdf::python::defineParserConfig(m); sdf::python::defineSphere(m); diff --git a/python/src/sdf/pyMagnetometer.cc b/python/src/sdf/pyMagnetometer.cc new file mode 100644 index 000000000..82d506c78 --- /dev/null +++ b/python/src/sdf/pyMagnetometer.cc @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2022 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 "pyMagnetometer.hh" + +#include +#include + +#include "sdf/Magnetometer.hh" + +using namespace pybind11::literals; + +namespace sdf +{ +// Inline bracket to help doxygen filtering. +inline namespace SDF_VERSION_NAMESPACE { +namespace python +{ +///////////////////////////////////////////////// +void defineMagnetometer(pybind11::object module) +{ + pybind11::class_ geometryModule(module, "Magnetometer"); + geometryModule + .def(pybind11::init<>()) + .def(pybind11::init()) + .def(pybind11::self == pybind11::self) + .def(pybind11::self != pybind11::self) + .def("x_noise", &sdf::Magnetometer::XNoise, + "Get the noise values related to the body-frame x axis.") + .def("set_x_noise", &sdf::Magnetometer::SetXNoise, + "Set the noise values related to the body-frame x axis.") + .def("y_noise", &sdf::Magnetometer::YNoise, + "Get the noise values related to the body-frame y axis.") + .def("set_y_noise", &sdf::Magnetometer::SetYNoise, + "Set the noise values related to the body-frame y axis.") + .def("z_noise", &sdf::Magnetometer::ZNoise, + "Get the noise values related to the body-frame z axis.") + .def("set_z_noise", &sdf::Magnetometer::SetZNoise, + "Set the noise values related to the body-frame z axis.") + .def("__copy__", [](const sdf::Magnetometer &self) { + return sdf::Magnetometer(self); + }) + .def("__deepcopy__", [](const sdf::Magnetometer &self, pybind11::dict) { + return sdf::Magnetometer(self); + }, "memo"_a); +} +} // namespace python +} // namespace SDF_VERSION_NAMESPACE +} // namespace sdf diff --git a/python/src/sdf/pyMagnetometer.hh b/python/src/sdf/pyMagnetometer.hh new file mode 100644 index 000000000..747ad9970 --- /dev/null +++ b/python/src/sdf/pyMagnetometer.hh @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2022 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 SDFORMAT_PYTHON_MAGNETOMETER_HH_ +#define SDFORMAT_PYTHON_MAGNETOMETER_HH_ + +#include + +#include "sdf/Magnetometer.hh" + +#include "sdf/config.hh" + +namespace sdf +{ +// Inline bracket to help doxygen filtering. +inline namespace SDF_VERSION_NAMESPACE { +namespace python +{ +/// Define a pybind11 wrapper for an sdf::Magnetometer +/** + * \param[in] module a pybind11 module to add the definition to + */ +void defineMagnetometer(pybind11::object module); +} // namespace python +} // namespace SDF_VERSION_NAMESPACE +} // namespace sdf + +#endif // SDFORMAT_PYTHON_MAGNETOMETER_HH_ diff --git a/python/test/pyMagnetometer_TEST.py b/python/test/pyMagnetometer_TEST.py new file mode 100644 index 000000000..fa565a66a --- /dev/null +++ b/python/test/pyMagnetometer_TEST.py @@ -0,0 +1,70 @@ +# Copyright (C) 2022 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 copy +from ignition.math import Pose3d +from sdformat import Magnetometer, Noise +import unittest + + +class MagnetometerTEST(unittest.TestCase): + + def test_default_construction(self): + mag = Magnetometer() + defaultNoise = Noise() + self.assertEqual(defaultNoise, mag.x_noise()) + self.assertEqual(defaultNoise, mag.y_noise()) + self.assertEqual(defaultNoise, mag.z_noise()) + + def test_set(self): + mag = Magnetometer() + defaultNoise = Noise() + noise = Noise() + self.assertEqual(defaultNoise, mag.x_noise()) + self.assertEqual(defaultNoise, mag.y_noise()) + self.assertEqual(defaultNoise, mag.z_noise()) + + noise.set_type(Noise.NoiseType.GAUSSIAN) + noise.set_mean(1.2) + noise.set_std_dev(2.3) + noise.set_bias_mean(4.5) + noise.set_bias_std_dev(6.7) + noise.set_precision(8.9) + + mag.set_x_noise(noise) + self.assertEqual(noise, mag.x_noise()) + self.assertEqual(defaultNoise, mag.y_noise()) + self.assertEqual(defaultNoise, mag.z_noise()) + + mag.set_y_noise(noise) + self.assertEqual(noise, mag.x_noise()) + self.assertEqual(noise, mag.y_noise()) + self.assertEqual(defaultNoise, mag.z_noise()) + + mag.set_z_noise(noise) + self.assertEqual(noise, mag.x_noise()) + self.assertEqual(noise, mag.y_noise()) + self.assertEqual(noise, mag.z_noise()) + + # Copy Constructor + mag2 = Magnetometer(mag) + self.assertEqual(mag, mag2) + + # Copy operator + mag3 = mag + self.assertEqual(mag, mag3) + + +if __name__ == '__main__': + unittest.main() From 3d2730a9c31f533f3544a7f822bc6de9ce04061c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Herna=CC=81ndez?= Date: Tue, 12 Apr 2022 17:53:50 +0200 Subject: [PATCH 2/4] Fix build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alejandro Hernández --- python/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 741f6cc9d..3a202afcc 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -40,7 +40,7 @@ pybind11_add_module(sdformat SHARED src/sdf/_ignition_sdformat_pybind11.cc src/sdf/pyBox.cc src/sdf/pyError.cc - src/sdf/pyMagnetometercc + src/sdf/pyMagnetometer.cc src/sdf/pyMesh.cc src/sdf/pyParserConfig.cc src/sdf/pySphere.cc From a1c5ba825b3bbe2ab19c0123e5a5d92e9fbc76a6 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 20 Apr 2022 15:22:19 +0200 Subject: [PATCH 3/4] Added test to Cmake Signed-off-by: ahcorde --- python/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index a26442f54..69bf8fc26 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -76,6 +76,7 @@ if (BUILD_TESTING) pyCylinder_TEST pyEllipsoid_TEST pyError_TEST + pyMagnetometer_TEST pyMaterial_TEST pyMesh_TEST pyNoise_TEST From 891453ccb45795e87246624b13ffab6635b998f2 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Mon, 23 May 2022 12:25:41 +0200 Subject: [PATCH 4/4] Added inequality test Signed-off-by: ahcorde --- python/test/pyMagnetometer_TEST.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/test/pyMagnetometer_TEST.py b/python/test/pyMagnetometer_TEST.py index fa565a66a..addf2f3eb 100644 --- a/python/test/pyMagnetometer_TEST.py +++ b/python/test/pyMagnetometer_TEST.py @@ -65,6 +65,9 @@ def test_set(self): mag3 = mag self.assertEqual(mag, mag3) + mag4 = Magnetometer() + self.assertNotEqual(mag, mag4) + if __name__ == '__main__': unittest.main()