Introduction
Topics let different parts of a robot talk without knowing each other directly. This makes the system easier to build and change.
Topics let different parts of a robot talk without knowing each other directly. This makes the system easier to build and change.
ros::Publisher pub = nh.advertise<std_msgs::String>("chatter", 1000); ros::Subscriber sub = nh.subscribe("chatter", 1000, chatterCallback);
Publishers send messages to a named topic.
Subscribers listen to messages from that topic without knowing who sent them.
ros::Publisher pub = nh.advertise<std_msgs::String>("sensor_data", 10);
ros::Subscriber sub = nh.subscribe("sensor_data", 10, sensorCallback);
This program listens to the 'chatter' topic and prints any messages it receives. It does not need to know who sends the messages.
#include "ros/ros.h" #include "std_msgs/String.h" void chatterCallback(const std_msgs::String::ConstPtr& msg) { ROS_INFO("I heard: [%s]", msg->data.c_str()); } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe("chatter", 1000, chatterCallback); ros::spin(); return 0; }
Topics allow many publishers and subscribers to connect without direct links.
This decoupling helps when parts of the robot change or restart independently.
Topics let parts communicate without knowing each other.
This makes robot software flexible and easier to maintain.
import rospy
from std_msgs.msg import String
def callback(msg):
print(f'Received: {msg.data}')
rospy.init_node('listener')
rospy.Subscriber('chatter', String, callback)
rospy.spin()Pass the test and this pattern moves unknown