ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Problem getting data from callback(data):

asked 2022-10-31 15:48:43 -0500

akumar3.1428 gravatar image

updated 2023-06-18 09:46:08 -0500

lucasw gravatar image

Hi, this is associated with https://answers.ros.org/question/4086... , I am still struggling with it, I wrote a class and trying to get the data from the call back function associated with the class, any help would be much appreciated. Do I need to use multi threading or there is an easy way to use it ? When I am calling the class the publisher initializes and then callback keep getting the updated data but I am not sure how can I use this data.

#!/usr/bin/env python3
"""OpenCV feature detectors with ros CompressedImage Topics in python.

This example subscribes to a ros topic containing sensor_msgs 
CompressedImage. It converts the CompressedImage into a numpy.ndarray, 
then detects and marks features in that image. It finally displays 
and publishes the new image - again as CompressedImage topic.
"""

__version__=  '0.1'

from moveit_commander.conversions import pose_to_list
from rospy_tutorials.msg import Floats
from rospy.numpy_msg import numpy_msg
from tf import TransformListener
from std_msgs.msg import String
import geometry_msgs.msg
import moveit_commander
import moveit_msgs.msg
from math import pi
import sys, time
import rospy
import copy

VERBOSE=False

class move_xarm:

    # global my_data

    def __init__(self):
        '''Initialize ros publisher and subscriber'''
        # publish trajectories for RViz to visualize
        self.display_trajectory_publisher = rospy.Publisher('/move_group/display_planned_path',moveit_msgs.msg.DisplayTrajectory,queue_size=20)

        self.data_callback_publisher = rospy.Publisher('/callback_data',numpy_msg(Floats),queue_size=1)

        self.my_data = None
        self.listener()
        # subscribed Topic 
        # self.subscriber = rospy.Subscriber("marker_wrt_base_pose", numpy_msg(Floats), self.callback, queue_size = 1)
        if VERBOSE :
            print("subscribed to marker_wrt_base_pose")


    def listener(self):

        # In ROS, nodes are uniquely named. If two nodes with the same
        # name are launched, the previous one is kicked off. The
        # anonymous=True flag means that rospy will choose a unique
        # name for our 'listener' node so that multiple listeners can
        # run simultaneously.
        rospy.init_node('move_group_python_interface_tutorial', anonymous=True)


        rospy.Subscriber("marker_wrt_base_pose", numpy_msg(Floats), self.callback,queue_size = 1)

        # spin() simply keeps python from exiting until this node is stopped
        rospy.spin()

    def callback(self, ros_data):
        '''Callback function of subscribed topic. 
        Here position data get converted into float64'''
        if VERBOSE :
            print('received data of type: "%s"' % ros_data.format)

        self.my_data = ros_data.data
        # my_data = my_data.astype('float64')


        self.data_callback_publisher.publish(ros_data)


if __name__ == '__main__':

    mv = move_xarm()
edit retag flag offensive close merge delete

Comments

Take a look at this question, it may be helpful.

0novanta gravatar image 0novanta  ( 2022-11-01 04:59:43 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-11-01 10:50:17 -0500

akumar3.1428 gravatar image

I resolved this issue, 1. Create another function out of the class say name use_data(args) 2. Call use_data in main function and you are good to go.

One of the common problem is use of spin command,they block the main thread from exiting until ROS invokes a shutdown - via a Ctrl + C.

def use_data(args):

    ic = move_xarm()

    rospy.init_node('move_group_python_interface_tutorial',anonymous=True)

    rate = rospy.Rate(5) # ROS Rate at 5Hz

    while not rospy.is_shutdown():
     do stuff using the variable ic.my_data
     rate.sleep()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-10-31 15:48:43 -0500

Seen: 40 times

Last updated: Nov 01 '22