C++版本
??Hello_pub.cpp 如下:
#include "ros/ros.h"
#include "std_msgs/String.h"
int main ( int argc, char *argv[] ) {
setlocale ( LC_ALL, "" );
ros::init ( argc, argv, "talker" );
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::String> ( "chatter", 10 );
std_msgs::String msg;
msg.data = "你好啊!!!";
ros::Rate r ( 1 );
while ( ros::ok() ) {
pub.publish ( msg );
ROS_INFO ( "发送的消息:%s", msg.data.c_str() );
r.sleep();
}
return 0;
}
??Hello_sub.cpp 如下:
#include "ros/ros.h"
#include "std_msgs/String.h"
void doMsg ( const std_msgs::String::ConstPtr& msg_p ) {
ROS_INFO ( "我听见:%s", msg_p->data.c_str() );
}
int main ( int argc, char *argv[] ) {
setlocale ( LC_ALL, "" );
ros::init ( argc, argv, "listener" );
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe<std_msgs::String> ( "chatter", 10, doMsg );
ros::spin();
return 0;
}
??在CMakeLists.txt 中添加编译选项:
add_executable(Hello_pub src/Hello_pub.cpp)
add_executable(Hello_sub src/Hello_sub.cpp)
target_link_libraries(Hello_pub ${catkin_LIBRARIES})
target_link_libraries(Hello_sub ${catkin_LIBRARIES})
python版本
??talker_p.py 如下:
import rospy
from std_msgs.msg import String
if __name__ == "__main__":
rospy.init_node("talker_p")
pub = rospy.Publisher("chatter", String, queue_size=10)
msg = String()
msg_front = "hello 你好"
count = 0
rate = rospy.Rate(1)
while not rospy.is_shutdown():
msg.data = msg_front + str(count)
pub.publish(msg)
rate.sleep()
rospy.loginfo("写出的数据:%s", msg.data)
count += 1
??listener_p.py 如下:
import rospy
from std_msgs.msg import String
def doMsg(msg):
rospy.loginfo("接收到的数据:%s", msg.data)
if __name__ == "__main__":
rospy.init_node("listener_p")
sub = rospy.Subscriber("chatter", String, doMsg, queue_size=10)
rospy.spin()
|