文章参考在ROS仿真中使用Python程序控制UR机械臂运动
在跟随大佬复现的过程中发现新打开一个终端输入rostopic list查看话题时,并没有出现原文章的话题如下图:而是下图所示:
显然区别在于/eff_joint_traj....,经过查看源代码,发现git clone的源路径中控制ur运动的话题名称发生了修改,因此2024年按照前序步骤进行后,响应话题名称也应该进行修改:
mkdir -p ~/ur_ws/src
cd ~/ur_ws/src
git clone https://github.com/ros-industrial/universal_robot
cd ..
catkin_make
主要修改如下,将
client = actionlib.SimpleActionClient('/eff_joint_traj_controller/follow_joint_trajectory',
修改为:主要是eff改为pos
client = actionlib.SimpleActionClient('/pos_joint_traj_controller/follow_joint_trajectory',
对应的整体python代码为
#! /usr/bin/env python
from trajectory_msgs.msg import *
from control_msgs.msg import *
import rospy
import actionlib
from sensor_msgs.msg import JointState
JOINT_NAMES = ['shoulder_pan_joint', 'shoulder_lift_joint', 'elbow_joint',
'wrist_1_joint', 'wrist_2_joint', 'wrist_3_joint']
def move():
#goal就是我们向发送的关节运动数据,实例化为FollowJointTrajectoryGoal()类
goal = FollowJointTrajectoryGoal()
#goal当中的trajectory就是我们要操作的,其余的Header之类的不用管
goal.trajectory = JointTrajectory()
#goal.trajectory底下一共还有两个成员,分别是joint_names和points,先给joint_names赋值
goal.trajectory.joint_names = JOINT_NAMES
#从joint_state话题上获取当前的关节角度值,因为后续要移动关节时第一个值要为当前的角度值
joint_states = rospy.wait_for_message("joint_states",JointState)
joints_pos = joint_states.position
#给trajectory中的第二个成员points赋值
#points中有四个变量,positions,velocities,accelerations,effort,我们给前三个中的全部或者其中一两个赋值就行了
goal.trajectory.points=[0]*4
goal.trajectory.points[0]=JointTrajectoryPoint(positions=joints_pos, velocities=[0]*6,time_from_start=rospy.Duration(0.0))
goal.trajectory.points[1]=JointTrajectoryPoint(positions=[0.1,0,-0.2,0,0,0], velocities=[0]*6,time_from_start=rospy.Duration(1.0))
goal.trajectory.points[2]=JointTrajectoryPoint(positions=[2,0,-1,0,0,0], velocities=[0]*6,time_from_start=rospy.Duration(2.0))
goal.trajectory.points[3]=JointTrajectoryPoint(positions=[2.57,0,-1.57,0,0,0], velocities=[0]*6,time_from_start=rospy.Duration(3.0))
#发布goal,注意这里的client还没有实例化,ros节点也没有初始化,我们在后面的程序中进行如上操作
client.send_goal(goal)
client.wait_for_result()
def pub_test():
global client
#初始化ros节点
rospy.init_node("pub_action_test")
#实例化一个action的类,命名为client,与上述client对应,话题为/eff_joint_traj_controller/follow_joint_trajectory,消息类型为FollowJointTrajectoryAction
client = actionlib.SimpleActionClient('/pos_joint_traj_controller/follow_joint_trajectory', FollowJointTrajectoryAction)
print("Waiting for server...")
#等待server
client.wait_for_server()
print("Connect to server......")
#执行move函数,发布action
move()
if __name__ == "__main__":
count = 0
while not rospy.is_shutdown():
count += 1
pub_test()
rospy.loginfo("发布次数:%d",count)
修改后运行无误,效果如下图所示:
同时在launch文件启动时还会存在一个问题报错:
经过资料查阅发现,是controller中缺少pid参数导致,具体添加在/universal_robot/ur_gazebo/config/ur5_controllers.yaml文件,可以在下图所示位置添加各关节的pid控制参数,但是需要自己调节,不然会出现抖动情况,并且该问题不影响上述oython代码运行,也可以忽略。