「バンパー」トピックにサブスクライブすることで、そのノードでデータを利用できるようになります。(言及しなかったように、C++ を使用していると仮定すると、理論は同じですが、明らかに構文が異なります)。ROS Web サイト (リンク) のパブリッシュ/サブスクライブ コードの例を使用します。
#include "ros/ros.h"
#include "std_msgs/String.h"
//Callback for bumper data
// this callback happens
void bumperCallback(const std_msgs::String::ConstPtr& msg)
{
//print received data for debugging
ROS_INFO("I heard: [%s]", msg->data.c_str());
//If the callback data contains "hit" - change to integer, float or whatever type your message is
if(msg->data.c_str() == "hit")
move_backwards(); // Call function to move the robot backwards
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("bumper", 1000, bumperCallback);
ros::spin();
return 0;
}