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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Magnetometer Python interface #972

Merged
merged 8 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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
Expand Down Expand Up @@ -107,6 +108,7 @@ if (BUILD_TESTING)
pyJointAxis_TEST
pyLight_TEST
pyLink_TEST
pyMagnetometer_TEST
pyMaterial_TEST
pyMesh_TEST
pyModel_TEST
Expand Down
2 changes: 2 additions & 0 deletions python/src/sdf/_ignition_sdformat_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
62 changes: 62 additions & 0 deletions python/src/sdf/pyMagnetometer.cc
Original file line number Diff line number Diff line change
@@ -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 <pybind11/operators.h>
#include <pybind11/pybind11.h>

#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_<sdf::Magnetometer> geometryModule(module, "Magnetometer");
geometryModule
.def(pybind11::init<>())
.def(pybind11::init<sdf::Magnetometer>())
.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
41 changes: 41 additions & 0 deletions python/src/sdf/pyMagnetometer.hh
Original file line number Diff line number Diff line change
@@ -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 <pybind11/pybind11.h>

#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_
73 changes: 73 additions & 0 deletions python/test/pyMagnetometer_TEST.py
Original file line number Diff line number Diff line change
@@ -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)
ahcorde marked this conversation as resolved.
Show resolved Hide resolved

mag4 = Magnetometer()
self.assertNotEqual(mag, mag4)


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