如果Condtion Ros Ardunio,无法从中获取数据
我在PC上运行了一个节点,该节点在字符串中发布了[std_msgs/string],它发表了m,r,s,我有以下Arduino代码。
#include <ros.h>
#include <std_msgs/Empty.h>
#include <std_msgs/String.h>
ros::NodeHandle nh;
void messageCb( const std_msgs::String & toggle_msg)
{
nh.loginfo("recived new message ");
nh.loginfo(toggle_msg.data);
if(toggle_msg.data == "M")
{
nh.loginfo("Recived M if-statment ");
}
else if(toggle_msg.data == "R")
{
nh.loginfo("Recived R if-statment ");
}
else if(toggle_msg.data == "S")
{
nh.loginfo("Recived S if-statment ");
}
}
ros::Subscriber<std_msgs::String> sub("talker_vision", &messageCb );
void setup()
{
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
delay(1);
}
我从nh.loginfo(toggle_msg.data);
收到了m,r,s,但是如果条件
if(toggle_msg.data == "M")
{
nh.loginfo("Recived M if-statment ");
}
没有输出,我应用了。
I have a node running on pc which published [std_msgs/String] in string It published M, R, S, I have the following Arduino code.
#include <ros.h>
#include <std_msgs/Empty.h>
#include <std_msgs/String.h>
ros::NodeHandle nh;
void messageCb( const std_msgs::String & toggle_msg)
{
nh.loginfo("recived new message ");
nh.loginfo(toggle_msg.data);
if(toggle_msg.data == "M")
{
nh.loginfo("Recived M if-statment ");
}
else if(toggle_msg.data == "R")
{
nh.loginfo("Recived R if-statment ");
}
else if(toggle_msg.data == "S")
{
nh.loginfo("Recived S if-statment ");
}
}
ros::Subscriber<std_msgs::String> sub("talker_vision", &messageCb );
void setup()
{
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
delay(1);
}
I received M , R , S from nh.loginfo(toggle_msg.data);
but when I applied If condition
if(toggle_msg.data == "M")
{
nh.loginfo("Recived M if-statment ");
}
there is no output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您面临的问题是,您正在比较这里定义为
char*
的两个字符串,而不是使用Arduino ClassicString
类型。您可以看到消息内部的字符串类型l15“ rel =“ nofollow noreferrer”>,例如在此生成。比较
始终是错误的,因为您正在比较内存位置而不是字符串本身。
因此,您有两个选项:
创建两个Arduino String对象,然后比较它们
,甚至更好的是使用标准字符串比较
strcmp
( documentation )函数在此处描述的例如:The problem you are facing is that you are comparing two strings here which are defined as
char*
, not the Arduino classicString
type is used. You can see the type of the string inside the message which was generated for example here.Comparing
will be always false since you are comparing memory positions and not the string itself.
So you have two options:
Create two Arduino string objects and compare them
Or even better is to use the standard string comparision
strcmp
(documentation) function as described here for example: