如何使用MultiProcessing Python同时移动两个机器人?

时间:2020-04-26 18:06:19

标签: python-2.7 multiprocessing ros

你好,我创建了一个启动文件,在其中我将两个ur5与ns分组为“ robot1”和“ robot2”。我在“ / robot1 / arm_controller / command /”和“ / robot2 / arm_controller / command /”主题中发布了关节角度,并且两个机器人都运行良好。说我做了两个功能,分别负责发布各个主题中的关节角度。即

script.py

class MoveRobot(object):
    def __init__(self):
          rospy.init_node('move_robot')
          pass
    def publish_angles(self):
          reset_robot = JointTrajectoryPoint()
          reset_joint_values = self.joints
          reset_robot.positions = reset_joint_values
          reset_robot.time_from_start = rospy.Duration(0.5)
          self.traj.points = [reset_robot]
          self.action_pub.publish(self.traj)  
          rospy.sleep(2)

class Robot1(MoveRobot):
    def __init__(self):
         super(Robot1, self).__init__()
         self.action_pub = rospy.Publisher("robot1/arm_controller/command", JointTrajectory, queue_size=1)
         self.joints = [1.5708,-1.5708,0, 3.14159, -1.5708,0]
class Robot2(MoveRobot):
    def __init__(self):
         super(Robot2, self).__init__()
         self.action_pub = rospy.Publisher("robot2/arm_controller/command", JointTrajectory, queue_size=1)
         self.joints = [1.5708,-1.5708,0, 3.14159, -1.5708,0]

multiProc.py

所以这里是我生成两个处理器以同时在机器人中发布联合值并移动它们的地方

import multiprocessing as mp
from script import MoveRobot, Robot1, Robot2

class Workers(mp.Process):
    def __init__(self, agent):
        super(Workers, self).__init__()
        self.agent = agent
    def run(self):
        self.agent.publish_angles()

class make_vectorized_agents():
    def __init__(self, robots):
        workers = []
        for r in (robots):
            w = Workers(r)
            workers.append(r)
            w.daemon = True
            w.start()
        for w in workers:
            w.join()
r1 = Robot1()
r2 = Robot2()
m = make_vectorized_agents([r1, r2])
Unfortunately, when I do it I get error as,

Process Worker-2:
Traceback (most recent call last):
    rospy.sleep(0.01)
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/timer.py", line 165, in sleep
    raise rospy.exceptions.ROSInterruptException("ROS shutdown request")
ROSInterruptException: ROS shutdown request
Process Worker-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 267, in _bootstrap
    self.run()
    raise rospy.exceptions.ROSInterruptException("ROS shutdown request")
ROSInterruptException: ROS shutdown request

似乎rosnode无法处理多进程,因为当两个进程同时工作时,rosnode会关闭。还是我需要配置其他内容?

0 个答案:

没有答案