1、创建目录
mkdir aa
mkdir aa/src
cd aa/src
catkin_init_workspace #src目录下
cd ..
catkin_make #src上一级执行

加入到环境变量
source devel/setup.bash
echo $ROS_PACKAGE_PATH
#/root/catkin_ws/src:/opt/ros/melodic/share

2、创建包
进入src目录
cd aa/src
catkin_create_pkg learning2 std_msgs rospy roscpp
创建成功后会在src目录下生成learning2 的文件夹结构
查找功能包
rospack find turtlesim
/opt/ros/melodic/share/turtlesim
3、消息发送订阅
http://wiki.ros.org/cn/ROS/Tutorials/WritingPublisherSubscriber%28python%29
在功能包的src目录下创建
#!/usr/bin/env python
# license removed for brevity
# talker.py 消息发送
# chmod +x talker.py 注意执行否则无法运行
import rospy
from std_msgs.msg import String
def talker():
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
#!/usr/bin/env python
# listener.py 消息接收
# chmod +x talker.py
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# node are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()
rqt_graph
查看消息

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import rospy
from std_msgs.msg import String
import time
import sys
def _publish():
rospy.init_node("Ppublish",anonymous=True)
rpy=rospy.Publisher("chatter",String,queue_size=10)
rate = rospy.Rate(10)
while not rospy.is_shutdown():
print("loop...")
rpy.publish("aaa")
rate.sleep()
def _listener():
rospy.init_node("Llistener",anonymous=True)
rospy.Subscriber("chatter",String,lambda str:rospy.loginfo(str))
rospy.spin()
if __name__ == "__main__":
try:
arg1=sys.argv[1]
print(arg1)
x = int(arg1)
if x==1:
_publish()
else:
_listener()
except Exception as err:
rospy.loginfo("err") #为何为不显示出来呀
print(err)
print("请输入参数")
turtlesim 乌龟事件的
https://gitee.com/hupinghit/ros_21_tutorials.git
<launch>
<param name="/turtle_number" value="2"/>
<node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
<param name="turtle_name1" value="Tom"/>
<param name="turtle_name2" value="Jerry"/>
<rosparam file="$(find learning_launch)/config/param.yaml" command="load"/>
</node>
<node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" output="screen"/>
</launch>


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
import rospy
from geometry_msgs.msg import Twist
def velocity_publisher():
# ROS节点初始化
rospy.init_node('velocity_publisher', anonymous=True)
# 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
#设置循环的频率
rate = rospy.Rate(10)
while not rospy.is_shutdown():
# 初始化geometry_msgs::Twist类型的消息
vel_msg = Twist()
vel_msg.linear.x = 0.5 #线速度
vel_msg.angular.z = 0.2 #角速度 一直画圈
# 发布消息
turtle_vel_pub.publish(vel_msg)
rospy.loginfo("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]",
vel_msg.linear.x, vel_msg.angular.z)
# 按照循环频率延时
rate.sleep()
if __name__ == '__main__':
try:
velocity_publisher()
except rospy.ROSInterruptException:
pass
接收
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将订阅/turtle1/pose话题,消息类型turtlesim::Pose
import rospy
from turtlesim.msg import Pose
def poseCallback(msg):
rospy.loginfo("Turtle pose: x:%0.6f, y:%0.6f", msg.x, msg.y)
def pose_subscriber():
# ROS节点初始化
rospy.init_node('pose_subscriber', anonymous=True)
# 创建一个Subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
rospy.Subscriber("/turtle1/pose", Pose, poseCallback)
# 循环等待回调函数
rospy.spin()
if __name__ == '__main__':
pose_subscriber()
网友评论