简体   繁体   中英

Why is ROS Publisher not publishing values?

I am currently trying to write a Python ROS program which can be executed as a ROS node (using rosrun) that implements the defs declared in a separate Python file arm.py (available at: https://github.com/nortega1/dvrk-ros/.. .). The program initially examines the current cartesian position of the arm. Subsequently, when provided with a series of points that the arm must pass through, the program calculates a polynomial equation and given a range of x values the program evaluates the equation to find the corresponding y values.

Within the arm.py file there is a publisher set_position_cartesian_pub that sets the Cartesian position of the arm as follows:

self.__set_position_cartesian_pub = rospy.Publisher(self.__full_ros_namespace + '/set_position_cartesian', Pose, latch = True, queue_size = 1)

The issue is that the publisher set_position_cartesian is not publishing the values of the newPose to the robot - can anyone figure out what the issue might be? I can confirm that the def lagrange correctly calculates the values of the x and y coordinates, which are printed out to the terminal via the command rospy.loginfo(newPose). Any help would be greatly appreciated as I've been trying to solve this issue for the last 2 days!

#! /usr/bin/python
import rospy
import sys
from std_msgs.msg import String, Bool, Float32
from geometry_msgs.msg import Pose
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Vector3
from geometry_msgs.msg import Quaternion
from geometry_msgs.msg import Wrench

class example_application:

def callback(self, data):
  self.position_cartesian_current = data.pose
  rospy.loginfo(data.pose)

def configure(self,robot_name):
    self._robot_name = 'PSM1'
    ros_namespace = '/dvrk/PSM1'
    rospy.Subscriber('/dvrk/PSM1/position_cartesian_current', PoseStamped, self.callback)
    self.set_position_cartesian = rospy.Publisher('/dvrk/PSM1/set_position_cartesian', Pose, latch=True, queue_size = 10)
    rospy.sleep(3)
    rospy.init_node('listener', anonymous=True)
    rospy.spin()

def lagrange(self, f, x):
 total = 0
 n = len(f)
 for i in range(n):
  xi, yi = f[i]
  def g(i, n):
   g_tot = 1
   for j in range(n):
    if i == j:
     continue
    xj, yj = f[j]
    g_tot *= (x - xj) / float(xi - xj)

   return g_tot

  total += yi * g(i, n)
 return total

def trajectoryMover(self):
    newPose = Pose()
    points =[(0.0156561,0.123151),(0.00715134,0.0035123151),(0.001515177,0.002123151),(0.0071239751,0.09123150)]
    xlist = [i*0.001 for i in range(10)]
    ylist = [self.lagrange(points, xlist[i])*0.001 for i in range(10)]
    for x, y in zip(xlist, ylist):
        newPose.position.x = x
        newPose.position.y = y
        newPose.position.z = 0.001
        newPose.orientation.x = 0.001
        newPose.orientation.y = 0.001
        newPose.orientation.z = 0.005
        newPose.orientation.w = 0.002
        rospy.sleep(1)
        self.set_position_cartesian.publish(newPose)
        rospy.loginfo(newPose)
        rospy.spin()

def run(self):
    # self.home()
    self.trajectoryMover()

if __name__ == '__main__':
    try:
        if (len(sys.argv) != 2):
            print(sys.argv[0] + ' requires one argument, i.e. name of dVRK arm')
    else:
        application = example_application()
        application.configure(sys.argv[1])
        application.run()

except rospy.ROSInterruptException:
    pass

You are not publishing because the code stops at rospy.spin() when you call application.configure() . For what I understand of what you are trying to do, the code will publish 10 poses to a topic, then you don't need it anymore.

I've moved the location of rospy.spin() , but the code needs more revision than that.

#! /usr/bin/python
import rospy
import sys
from std_msgs.msg import String, Bool, Float32
from geometry_msgs.msg import Pose
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Vector3
from geometry_msgs.msg import Quaternion
from geometry_msgs.msg import Wrench

class example_application(object):
    def callback(self, data):
        self.position_cartesian_current = data.pose
        rospy.loginfo(data.pose)

    def configure(self,robot_name):
        self._robot_name = 'PSM1'
        ros_namespace = '/dvrk/PSM1'
        rospy.Subscriber('/dvrk/PSM1/position_cartesian_current', PoseStamped, self.callback)
        self.set_position_cartesian = rospy.Publisher('/dvrk/PSM1/set_position_cartesian', Pose, latch=True, queue_size = 10)

    def lagrange(self, f, x):
        total = 0
        n = len(f)
        for i in range(n):
            xi, yi = f[i]
            def g(i, n):
                g_tot = 1
                for j in range(n):
                    if i == j:
                        continue
                    xj, yj = f[j]
                    g_tot *= (x - xj) / float(xi - xj)

                return g_tot

            total += yi * g(i, n)
        return total

    def trajectoryMover(self):
        newPose = Pose()
        points =[(0.0156561,0.123151),(0.00715134,0.0035123151),(0.001515177,0.002123151),(0.0071239751,0.09123150)]
        xlist = [i*0.001 for i in range(10)]
        ylist = [self.lagrange(points, xlist[i])*0.001 for i in range(10)]
        for x, y in zip(xlist, ylist):
            newPose.position.x = x
            newPose.position.y = y
            newPose.position.z = 0.001
            newPose.orientation.x = 0.001
            newPose.orientation.y = 0.001
            newPose.orientation.z = 0.005
            newPose.orientation.w = 0.002
            self.set_position_cartesian.publish(newPose)
            rospy.loginfo(newPose)

    def run(self):
        # self.home()
        self.trajectoryMover()

if __name__ == '__main__':
    if (len(sys.argv) != 2):
        print(sys.argv[0] + ' requires one argument, i.e. name of dVRK arm')
    else:
        application = example_application()
        application.configure(sys.argv[1])
        application.run()

    try:
        rospy.spin()
    except KeyboardInterrupt:
        rospy.loginfo("Keyboard Interrupt")

Think of:

  • making the script argument a parameter of the node.
  • moving the configure method to the __init__ method.
  • taking the g() function outside lagrange() .

It's a good practice to use relative topic names, instead of absolute ones (absolute: topic name start with / , eg: '/dvrk/PSM1' ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM