之前也发布过DRL与ROS系统通信的流程,重新翻看效果不佳。现在重新梳理一些步骤。
Unitree+DRL Vmware环境配置_isaac gym虚拟机安装-CSDN博客
当前环境:ubuntu 20.04, ros-noetic, anaconda env。
1. 安装
在conda环境中安装ros通信需要的包:
pip install rospkg
安装完成后,在编译器中添加路径。使用pycharm。
PyCharm设置External Libraries_pycharm external library-CSDN博客
/opt/ros/noetic/lib/python3/dist-packages
以下的包可以正常识别 。
import rospy
from std_msgs.msg import Header
from sensor_msgs.msg import JointState
from std_msgs.msg import Float64MultiArray, Float64
from gazebo_msgs.msg import LinkStates, LinkState
from gazebo_msgs.msg import ModelState
from geometry_msgs.msg import Pose, Twist
2. 测试
运行roscore
运行 python ./ros_utils/publisher.py
# -*- coding: utf-8 -*-
import rospy
from std_msgs.msg import String
def publisher():
# Initialize the ROS node
rospy.init_node('simple_publisher', anonymous=True)
# Create a publisher object
pub = rospy.Publisher('chatter', String, queue_size=10)
# Set the loop rate
rate = rospy.Rate(1) # 1 Hz
while not rospy.is_shutdown():
# Create a message to publish
hello_str = "Hello, ROS! %s" % rospy.get_time()
# Log the message
rospy.loginfo(hello_str)
# Publish the message
pub.publish(hello_str)
# Sleep for the loop rate duration
rate.sleep()
if __name__ == '__main__':
try:
publisher()
except rospy.ROSInterruptException:
pass
运行 python ./ros_utils/subscriber.py
# -*- coding: utf-8 -*-
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo("Received data: %s", data.data)
def listener():
rospy.loginfo("start ...")
rospy.init_node('simple_publisher', anonymous=True)
rospy.Subscriber("chatter", String, callback)
rospy.spin()
if __name__ == '__main__':
try:
listener()
except rospy.ROSInterruptException:
pass
检查节点