如果Condtion Ros Ardunio,无法从中获取数据

发布于 2025-02-10 13:40:15 字数 962 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-02-17 13:40:15

您面临的问题是,您正在比较这里定义为char*的两个字符串,而不是使用Arduino Classic String类型。您可以看到消息内部的字符串类型l15“ rel =“ nofollow noreferrer”>,例如在此生成。

比较

const char* str1 = "abc";
const char* str2 = "abc";
bool equal = str1 == str2;

始终是错误的,因为您正在比较内存位置而不是字符串本身。

因此,您有两个选项:

创建两个Arduino String对象,然后比较它们

String arduinoString1 = String(str1);
String arduinoString2 = String(str2);
bool equal = arduinoString1 == arduinoString2;

,甚至更好的是使用标准字符串比较strcmp documentation )函数在此处描述的例如

bool equal = strcmp(str1, str2) == 0;

The problem you are facing is that you are comparing two strings here which are defined as char*, not the Arduino classic String type is used. You can see the type of the string inside the message which was generated for example here.

Comparing

const char* str1 = "abc";
const char* str2 = "abc";
bool equal = str1 == str2;

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

String arduinoString1 = String(str1);
String arduinoString2 = String(str2);
bool equal = arduinoString1 == arduinoString2;

Or even better is to use the standard string comparision strcmp (documentation) function as described here for example:

bool equal = strcmp(str1, str2) == 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文