Mujoco KDL Wrapper  0.3.18
MuJoCo + KDL bridge for robot kinematics and dynamics
Loading...
Searching...
No Matches
test_camera_ros.cpp
Go to the documentation of this file.
1/* test_camera_ros.cpp
2 * The CameraInfo a sim camera publishes, and the gate that keeps an unwatched topic free.
3 * Built only when the camera_ros target is (MJ_KDL_WITH_ROS). */
4
6
7#include <gtest/gtest.h>
8
9#include <cstdio>
10#include <filesystem>
11#include <fstream>
12#include <string>
13
14namespace fs = std::filesystem;
15
16// 640x480 at fovy 45 deg: f = 480 / (2 * tan(22.5 deg)) = 579.4112549695428.
17static constexpr const char *kMjcf = R"(<mujoco>
18 <worldbody>
19 <camera name="wrist" fovy="45" pos="0 0 1"/>
20 </worldbody>
21</mujoco>)";
22
23class CameraRosTest : public testing::Test
24{
25 protected:
26 mjModel *model_ = nullptr;
27 rclcpp::Node::SharedPtr node_;
29
30 void SetUp() override
31 {
32 const auto path = fs::temp_directory_path() / "mj_kdl_camera_ros_test.xml";
33 std::ofstream(path) << kMjcf;
34
35 char error[1024] = { 0 };
36 model_ = mj_loadXML(path.c_str(), nullptr, error, sizeof(error));
37 fs::remove(path);
38 ASSERT_NE(model_, nullptr) << error;
39
40 node_ = std::make_shared<rclcpp::Node>("mj_kdl_camera_ros_test");
41
42 conf_.camera = "wrist";
43 conf_.frame_id = "wrist_optical";
44 conf_.topic_ns = "wrist";
45 conf_.width = 640;
46 conf_.height = 480;
47 conf_.rate_hz = 30.0;
48 }
49
50 void TearDown() override
51 {
52 node_.reset();
53 if (model_) mj_deleteModel(model_);
54 }
55};
56
57TEST_F(CameraRosTest, IntrinsicsComeFromTheModelFovy)
58{
59 mj_kdl::CameraRosPublisher pub(*node_, model_, conf_);
60 const auto &k = pub.camera_info().k;
61
62 EXPECT_NEAR(k[0], 579.4112549695428, 1e-9);
63 EXPECT_NEAR(k[4], 579.4112549695428, 1e-9);
64 EXPECT_DOUBLE_EQ(k[2], 320.0);
65 EXPECT_DOUBLE_EQ(k[5], 240.0);
66 EXPECT_DOUBLE_EQ(k[8], 1.0);
67 EXPECT_EQ(pub.camera_info().width, 640u);
68 EXPECT_EQ(pub.camera_info().height, 480u);
69 EXPECT_EQ(pub.camera_info().header.frame_id, "wrist_optical");
70}
71
72TEST_F(CameraRosTest, NobodyWatchingCostsNothing)
73{
74 mj_kdl::CameraRosPublisher pub(*node_, model_, conf_);
75
76 // Due by rate at both times; with no subscriber neither asks for a frame.
77 EXPECT_FALSE(pub.wants_frame(0.0));
78 EXPECT_FALSE(pub.wants_frame(10.0));
79}
80
81int main(int argc, char *argv[])
82{
83 testing::InitGoogleTest(&argc, argv);
84 rclcpp::init(argc, argv);
85 const int result = RUN_ALL_TESTS();
86 rclcpp::shutdown();
87 return result;
88}
void SetUp() override
mj_kdl::CameraConf conf_
rclcpp::Node::SharedPtr node_
void TearDown() override
bool wants_frame(double sim_t) const
const sensor_msgs::msg::CameraInfo & camera_info() const
int main(int argc, char *argv[])
TEST_F(CameraRosTest, IntrinsicsComeFromTheModelFovy)