为什么 NodeHandle 会挂起?
Why does NodeHandle hang?
我在尝试在 Indigo 中创建订阅者时遇到问题。我在 class 中有一个 shared_ptr 来保存 NodeHandle 对象。我这样做是为了让 NodeHandle 可以在其他 class 成员中使用。问题是当线程启动时,它似乎挂在对 MyClass 构造函数中的 NodeHandle 对象的 make_shared 调用上,因为它永远不会到达下一行之后。
class MyClass
{
private:
std::shared_ptr<ros::NodeHandle> nh;
std::map<std::string, std::string> remap;
// ...
};
MyClass::MyClass()
{
// remap is empty
ros::init(remap, "my_node");
nh = make_shared<ros::NodeHandle>();
cout << "does not reach this line" << endl;
}
int MyClass::run()
{
// ...
}
我开始发帖很喜欢...
{
// ...
myobj = make_shared<MyClass>();
my_thread = thread(&MyClass::run, myobj);
// ...
}
想法?
好吧,这里有一个使用 Boost::make_shared 作为节点句柄的例子。
请注意,它使用了 ros::NodeHandlePtr,一个已经存在的 Boost 共享指针,而不是使用“std::make_shared”。
这可能并没有真正回答问题,但我建议另一种使用 boost 库的方法。
#include <ros/ros.h>
#include <std_msgs/Empty.h>
#include <boost/thread/thread.hpp>
void do_stuff(int* publish_rate)
{
ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
ros::Publisher pub_b = node->advertise<std_msgs::Empty>("topic_b", 10);
ros::Rate loop_rate(*publish_rate);
while (ros::ok())
{
std_msgs::Empty msg;
pub_b.publish(msg);
loop_rate.sleep();
}
}
int main(int argc, char** argv)
{
int rate_b = 1; // 1 Hz
ros::init(argc, argv, "mt_node");
// spawn another thread
boost::thread thread_b(do_stuff, &rate_b);
ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
ros::Publisher pub_a = node->advertise<std_msgs::Empty>("topic_a", 10);
ros::Rate loop_rate(10); // 10 Hz
while (ros::ok())
{
std_msgs::Empty msg;
pub_a.publish(msg);
loop_rate.sleep();
// process any incoming messages in this thread
ros::spinOnce();
}
// wait the second thread to finish
thread_b.join();
return 0;
}
如果您在使用 CMakeLists 时遇到问题,请看这里:
cmake_minimum_required(VERSION 2.8.3)
project(test_thread)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
)
find_package(Boost COMPONENTS thread REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
catkin_package(CATKIN_DEPENDS roscpp rospy std_msgs)
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(thread src/thread_test.cpp)
target_link_libraries(thread ${catkin_LIBRARIES} ${BOOST_LIBRARIES})
希望对您有所帮助!
干杯,
问题似乎是由于我有自己的 boost 日志记录系统而不是使用 ROS 记录器(这使得它很难找到,因为它似乎与 ros::NodeHandle 无关,但可能在下面做)。我会注释掉我的整个代码库并开始添加以查看 ros::NodeHandle 何时会 运行 并且在删除我的记录器并将其添加回来时,我会看到它之间的区别 运行宁和挂。
我在尝试在 Indigo 中创建订阅者时遇到问题。我在 class 中有一个 shared_ptr 来保存 NodeHandle 对象。我这样做是为了让 NodeHandle 可以在其他 class 成员中使用。问题是当线程启动时,它似乎挂在对 MyClass 构造函数中的 NodeHandle 对象的 make_shared 调用上,因为它永远不会到达下一行之后。
class MyClass
{
private:
std::shared_ptr<ros::NodeHandle> nh;
std::map<std::string, std::string> remap;
// ...
};
MyClass::MyClass()
{
// remap is empty
ros::init(remap, "my_node");
nh = make_shared<ros::NodeHandle>();
cout << "does not reach this line" << endl;
}
int MyClass::run()
{
// ...
}
我开始发帖很喜欢...
{
// ...
myobj = make_shared<MyClass>();
my_thread = thread(&MyClass::run, myobj);
// ...
}
想法?
好吧,这里有一个使用 Boost::make_shared 作为节点句柄的例子。 请注意,它使用了 ros::NodeHandlePtr,一个已经存在的 Boost 共享指针,而不是使用“std::make_shared”。
这可能并没有真正回答问题,但我建议另一种使用 boost 库的方法。
#include <ros/ros.h>
#include <std_msgs/Empty.h>
#include <boost/thread/thread.hpp>
void do_stuff(int* publish_rate)
{
ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
ros::Publisher pub_b = node->advertise<std_msgs::Empty>("topic_b", 10);
ros::Rate loop_rate(*publish_rate);
while (ros::ok())
{
std_msgs::Empty msg;
pub_b.publish(msg);
loop_rate.sleep();
}
}
int main(int argc, char** argv)
{
int rate_b = 1; // 1 Hz
ros::init(argc, argv, "mt_node");
// spawn another thread
boost::thread thread_b(do_stuff, &rate_b);
ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
ros::Publisher pub_a = node->advertise<std_msgs::Empty>("topic_a", 10);
ros::Rate loop_rate(10); // 10 Hz
while (ros::ok())
{
std_msgs::Empty msg;
pub_a.publish(msg);
loop_rate.sleep();
// process any incoming messages in this thread
ros::spinOnce();
}
// wait the second thread to finish
thread_b.join();
return 0;
}
如果您在使用 CMakeLists 时遇到问题,请看这里:
cmake_minimum_required(VERSION 2.8.3)
project(test_thread)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
)
find_package(Boost COMPONENTS thread REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
catkin_package(CATKIN_DEPENDS roscpp rospy std_msgs)
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(thread src/thread_test.cpp)
target_link_libraries(thread ${catkin_LIBRARIES} ${BOOST_LIBRARIES})
希望对您有所帮助!
干杯,
问题似乎是由于我有自己的 boost 日志记录系统而不是使用 ROS 记录器(这使得它很难找到,因为它似乎与 ros::NodeHandle 无关,但可能在下面做)。我会注释掉我的整个代码库并开始添加以查看 ros::NodeHandle 何时会 运行 并且在删除我的记录器并将其添加回来时,我会看到它之间的区别 运行宁和挂。