diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index ebe2f79a1..5e8cb807b 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -56,6 +56,7 @@ pybind11_add_module(sdformat SHARED src/sdf/pyJointAxis.cc src/sdf/pyLight.cc src/sdf/pyLink.cc + src/sdf/pyMagnetometer.cc src/sdf/pyMaterial.cc src/sdf/pyMesh.cc src/sdf/pyModel.cc @@ -107,6 +108,7 @@ if (BUILD_TESTING) pyJointAxis_TEST pyLight_TEST pyLink_TEST + pyMagnetometer_TEST pyMaterial_TEST pyMesh_TEST pyModel_TEST diff --git a/python/src/sdf/_ignition_sdformat_pybind11.cc b/python/src/sdf/_ignition_sdformat_pybind11.cc index 73ac7e4e9..d88281d30 100644 --- a/python/src/sdf/_ignition_sdformat_pybind11.cc +++ b/python/src/sdf/_ignition_sdformat_pybind11.cc @@ -34,6 +34,7 @@ #include "pyJointAxis.hh" #include "pyLight.hh" #include "pyLink.hh" +#include "pyMagnetometer.hh" #include "pyMaterial.hh" #include "pyMesh.hh" #include "pyModel.hh" @@ -71,6 +72,7 @@ PYBIND11_MODULE(sdformat, m) { sdf::python::defineJointAxis(m); sdf::python::defineLight(m); sdf::python::defineLink(m); + sdf::python::defineMagnetometer(m); sdf::python::defineMaterial(m); sdf::python::defineMesh(m); sdf::python::defineModel(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..addf2f3eb --- /dev/null +++ b/python/test/pyMagnetometer_TEST.py @@ -0,0 +1,73 @@ +# 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) + + mag4 = Magnetometer() + self.assertNotEqual(mag, mag4) + + +if __name__ == '__main__': + unittest.main()