rviz可视化机械臂(python)

发布时间:2024年01月18日

一、准备的东西

一个机械臂的urdf

规划的路径点

二、launch文件的撰写

1.初始化

<?xml version="1.0" encoding="utf-8"?>
<launch>
  <param name="robot_description" textfile="机械臂.urdf" />

  <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

  <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

  <node name="rviz" pkg="rviz" type="rviz" 
    args="机械臂.rviz"
    required="true" />

</launch>

2.加入自己路径点发布节点后的launch文件

(去除joint_state_publisher即可)

rqt_graph

<?xml version="1.0" encoding="utf-8"?>
<launch>
  <param name="robot_description" textfile="机械臂.urdf" />

  <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

  <node name="rviz" pkg="rviz" type="rviz" 
    args="机械臂.rviz"
    required="true" />

</launch>

3.发布路径点话题

写一个节点,它需要发布类型为sensor_msgs / JointState的关节信息

发布了节点之后再打开rqt_grah

sensor_msgs/JointState Documentation

import rospy
import numpy as np
from geometry_msgs.msg import PoseStamped
from sensor_msgs.msg import JointState


def tra_generate(joints):
    joints.position[0] += 0.1
    joints.velocity = []
    joints.effort = []
    return joints


def Path_Publish():  # 初始化节点
    rospy.init_node("joint_states")
    # 创建一个发布者,发布数据类型为Path
    pub = rospy.Publisher("joint_states", JointState, queue_size=10)
    # 设置发布频率
    rate = rospy.Rate(10)
    joints = JointState()
    joints.position = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
    joints.name = ["joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6"]
    while not rospy.is_shutdown():
        # 消息
        while True:
            msg = tra_generate(joints)
            msg.header.stamp = rospy.Time.now()
            # 发布消息
            rospy.loginfo("Publishing Plan...")
            pub.publish(msg)

            # 按照循环频率延时
            rate.sleep()


if __name__ == "__main__":
    try:
        Path_Publish()
    except rospy.ROSInterruptException:
        pass

4.结果图

文章来源:https://blog.csdn.net/drivenzyw/article/details/135681131
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。