我目前正在尝试在两个ros节点之间传递一些信息。 作为用python编写的ros节点的发布者在主题上发布字符串,并且作为用cpp编写的ros节点的订阅者具有回调函数,该函数对发送到该主题的消息作出反应。
我的问题在于cpp部分中的回调函数,它不处理最近发布的消息,而只处理刚刚发布的消息之前发送的消息.cpp订阅者基本上是一个消息。
回应主题表明发布到主题的消息是正确的,因此cpp部分必定是错误的。 回调目前只打印了incomming消息,没有别的。
CPP部分的订户初始化为:
status(nodehandle.subscribe("/status",10000,&wrapper::callback, this))
void wrapper::callback(const std_msgs::String& command)
{
ROS_ERROR_STREAM("RECEIVED a callback");
std::cout <<"Received unconverted message: "<< command.data << std::endl;
}
我尝试更改queue_size但没有效果。
我确信python代码是通过回显主题来调试它来发布正确的消息。
发送多条消息,证明cpp部分收到的消息被一条消息延迟。
为什么我无法阅读发表在该主题上的最新消息?
MWE:
ros::NodeHandle nodehandle;
ros::Publisher control_cpp_to_python;
ros::Publisher control_front_to_python;
ros::Subscriber status_manual;
ros::Subscriber status_auto;
ros::Rate loop_rate;
void callback_manual(const std_msgs::String& command)
{
ROS_ERROR_STREAM("RECEIVED a callback");
std::cout <<"Received unconverted message - manuak: "<< command.data << std::endl;
if(checked == true)
{
std::cout << "Message received from correct callback - Manual" << std::endl;
checked = false;
thread_running=false;
}
else
{
checked = true;
}
}
void callback_auto(const std_msgs::String& command)
{
ROS_ERROR_STREAM("RECEIVED a callback");
std::cout <<"Received unconverted message - auto: "<< command.data << std::endl;
if(checked == true)
{
std::cout << "Message received from correct callback - Manual" << std::endl;
checked = false;
thread_running=false;
}
else
{
checked = true;
}
}
while(thread_running)
{
loop_rate.sleep();
if(!thread_running) break;
if(mode == "manual")
{
std_msgs::Int8 msg;
std::bitset<4> msg_bit;
msg_bit.set(3,1);
msg_bit.set(2,1);
msg_bit.set(1,1);
msg_bit.set(0,0);
std::cout << "Sending message in manual mode!" << std::endl;
std::cout << "Message being: "<< msg_bit << std::endl;
msg.data = int(msg_bit.to_ulong());
control_front_to_python.publish(msg);
}
if(mode == "auto")
{
std::cout << "Publishing message in auto" << std::endl;
std_msgs::Int8 msg;
msg.data = 8;
control_cpp_to_python.publish(msg);
}
ros::spinOnce();
}
我想这就像它可以被刮掉一样。我想这个问题必定是因为我在回调中将thread_running设置为false。我目前通过checked
bool修复它作为临时修复。