-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple test for gazebo_ros::Node
- Loading branch information
Kevin Allen
committed
Jul 18, 2018
1 parent
112aec3
commit dcf67e2
Showing
5 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2018 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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 "gazebo_ros_plugin_example.hpp" | ||
|
||
GazeboRosNodeExample::GazeboRosNodeExample() | ||
{ | ||
} | ||
|
||
GazeboRosNodeExample::~GazeboRosNodeExample() | ||
{ | ||
} | ||
|
||
void GazeboRosNodeExample::Load(int argc, char ** argv) | ||
{ | ||
// Initialize ROS with arguments | ||
gazebo_ros::Node::InitROS(argc, argv); | ||
|
||
// Create the ROS node | ||
node_ = gazebo_ros::Node::Create("gazebo_ros_node_example"); | ||
|
||
// Create a publisher | ||
pub_ = node_->create_publisher<std_msgs::msg::String>("test"); | ||
|
||
// Run lambda every 1 second | ||
using namespace std::chrono_literals; | ||
timer_ = node_->create_wall_timer(1s, | ||
[this]() { | ||
// Create string message | ||
auto msg = std_msgs::msg::String(); | ||
msg.data = "Hello world"; | ||
|
||
// Warn with this node's name (to test logging) | ||
RCLCPP_WARN(node_->get_logger(), "Publishing"); | ||
|
||
// Publish message | ||
this->pub_->publish(msg); | ||
}); | ||
} | ||
|
||
GZ_REGISTER_SYSTEM_PLUGIN(GazeboRosNodeExample) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2018 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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 GAZEBO_ROS_PLUGIN_EXAMPLE_HPP_ | ||
#define GAZEBO_ROS_PLUGIN_EXAMPLE_HPP_ | ||
|
||
#include <gazebo/common/common.hh> | ||
|
||
#include <std_msgs/msg/string.hpp> | ||
#include <gazebo_ros/node.hpp> | ||
|
||
#include <memory> | ||
|
||
/// Simple example of a gazebo plugin which uses a ROS2 node with gazebo_ros::Node | ||
class GazeboRosNodeExample : public gazebo::SystemPlugin | ||
{ | ||
public: | ||
GazeboRosNodeExample(); | ||
virtual ~GazeboRosNodeExample(); | ||
/// Called by gazebo to load plugin. Creates #node_, #timer_, and #pub_ | ||
void Load(int argc, char ** argv); | ||
|
||
private: | ||
/// ROS node used for publisher and timer | ||
gazebo_ros::Node::SharedPtr node_; | ||
/// Timer called to publish a message every second | ||
std::shared_ptr<rclcpp::TimerBase> timer_; | ||
/// Example publisher | ||
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::String>> pub_; | ||
}; | ||
|
||
#endif // GAZEBO_ROS_PLUGIN_EXAMPLE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2018 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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 <unistd.h> | ||
#include <stdlib.h> | ||
|
||
#include <gtest/gtest.h> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <std_msgs/msg/string.hpp> | ||
|
||
#include <memory> | ||
|
||
using namespace std::chrono_literals; | ||
|
||
class TestGazeboRosPluginExample : public ::testing::Test | ||
{ | ||
public: | ||
static void SetUpTestCase(); | ||
static void TearDownTestCase(); | ||
static int pid; | ||
}; | ||
|
||
int TestGazeboRosPluginExample::pid = -1; | ||
|
||
void TestGazeboRosPluginExample::SetUpTestCase() | ||
{ | ||
// Fork process so gazebo can be run as child | ||
pid = fork(); | ||
if (pid < 0) { | ||
throw std::runtime_error("fork failed"); | ||
} | ||
|
||
// Child process | ||
if (0 == pid) { | ||
// Run gazebo with the example plugin with publishes a string to /test | ||
if (execlp("gzserver", "/usr/bin/gzserver", "-s", "./libgazebo_ros_plugin_example.so", NULL)) { | ||
exit(1); | ||
} | ||
} | ||
} | ||
|
||
void TestGazeboRosPluginExample::TearDownTestCase() | ||
{ | ||
// If fork failed, don't need to do anything | ||
if (pid < 0) { | ||
return; | ||
} | ||
|
||
// Kill gazebo (simulating ^C command) | ||
if (kill(pid, SIGINT)) { | ||
throw std::runtime_error("could not kill"); | ||
} | ||
|
||
// Wait for gazebo to terminate | ||
wait(nullptr); | ||
} | ||
|
||
TEST_F(TestGazeboRosPluginExample, todo) | ||
{ | ||
// Create node and executor | ||
rclcpp::executors::SingleThreadedExecutor executor; | ||
auto node = std::make_shared<rclcpp::Node>("my_node"); | ||
executor.add_node(node); | ||
|
||
// Subscribe to topic published by plugin | ||
using StringPtr = std_msgs::msg::String::SharedPtr; | ||
StringPtr msg; | ||
auto subscriber = node->create_subscription<std_msgs::msg::String>("test", | ||
[&msg](const StringPtr _msg) { | ||
printf("message received!!\n"); | ||
msg = _msg; | ||
}); | ||
|
||
// Wait until message is received or timeout after 5 seconds | ||
using namespace std::literals::chrono_literals; | ||
auto timeout = node->now() + rclcpp::Duration(5s); | ||
while (nullptr == msg && node->now() < timeout) { | ||
executor.spin_once(50ms); | ||
} | ||
|
||
// Wait a little while so gazebo isn't torn down before created | ||
rclcpp::sleep_for(1s); | ||
|
||
// Assert a message was received | ||
ASSERT_NE(msg, nullptr); | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |