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

add urdf::Rotation unit test #51

Merged
merged 1 commit into from
Oct 4, 2014
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: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ find_package(console_bridge REQUIRED)
include_directories(SYSTEM ${console_bridge_INCLUDE_DIRS})
link_directories(${console_bridge_LIBRARY_DIRS})

find_package(Boost REQUIRED system thread)
find_package(Boost REQUIRED system thread unit_test_framework)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})

Expand Down
2 changes: 2 additions & 0 deletions urdf_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ target_link_libraries(urdf_to_graphiz urdfdom_model)
add_executable(urdf_mem_test test/memtest.cpp)
target_link_libraries(urdf_mem_test urdfdom_model)

add_executable(urdf_unit_test test/urdf_unit_test.cpp)
target_link_libraries(urdf_unit_test urdfdom_model ${Boost_LIBRARIES})

INSTALL(TARGETS urdfdom_model DESTINATION ${CMAKE_INSTALL_LIBDIR})
INSTALL(TARGETS urdfdom_world DESTINATION ${CMAKE_INSTALL_LIBDIR})
Expand Down
65 changes: 65 additions & 0 deletions urdf_parser/test/urdf_unit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "urdf_parser/urdf_parser.h"
#include <iostream>
#include <iomanip>
#include <cmath>

// the name of our test module
#define BOOST_TEST_MODULE URDF_UNIT_TEST
// needed for automatic generation of the main()
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>


bool quat_are_near(urdf::Rotation left, urdf::Rotation right)
{
static const double epsilon = 1e-3; // quite loose epsilon
double l[4], r[4];
left.getQuaternion(l[0], l[1], l[2], l[3]);
right.getQuaternion(r[0], r[1], r[2], r[3]);
return (std::abs(l[0] - r[0]) < epsilon &&
std::abs(l[1] - r[1]) < epsilon &&
std::abs(l[2] - r[2]) < epsilon &&
std::abs(l[3] - r[3]) < epsilon) ||
(std::abs(l[0] + r[0]) < epsilon &&
std::abs(l[1] + r[1]) < epsilon &&
std::abs(l[2] + r[2]) < epsilon &&
std::abs(l[3] + r[3]) < epsilon);
}

std::ostream &operator<<(std::ostream &os, const urdf::Rotation& rot)
{
double roll, pitch, yaw;
double x, y, z, w;
rot.getRPY(roll, pitch, yaw);
rot.getQuaternion(x, y, z, w);
os << std::setprecision(9)
<< "x: " << x << " y: " << y << " z: " << z << " w: " << w
<< " roll: " << roll << " pitch: " << pitch << " yaw: "<< yaw;
return os;
}


void check_get_set_rpy_is_idempotent(double x, double y, double z, double w)
{
urdf::Rotation rot0;
rot0.setFromQuaternion(x, y, z, w);
double roll, pitch, yaw;
rot0.getRPY(roll, pitch, yaw);
urdf::Rotation rot1;
rot1.setFromRPY(roll, pitch, yaw);
if (true) {
std::cout << "\n"
<< "before " << rot0 << "\n"
<< "after " << rot1 << "\n"
<< "ok " << quat_are_near(rot0, rot1) << "\n";
}
BOOST_CHECK(quat_are_near(rot0, rot1));
}

BOOST_AUTO_TEST_CASE(test_rotation_get_set_rpy_idempotent)
{
double x0 = 0.5, y0 = -0.5, z0 = 0.5, w0 = 0.5;
check_get_set_rpy_is_idempotent(x0, y0, z0, w0);
double delta = 2.2e-8;
check_get_set_rpy_is_idempotent(x0, y0, z0+delta, w0-delta);
}