Ask Your Question
0

Cannot publish a list to a rostopic using a class in Python

asked 2019-11-14 16:01:17 -0600

mkb_10062949 gravatar image

updated 2019-11-15 02:59:46 -0600

Hello, I have created a class and defined a publisher in the __init__. i have defined a function in the class and would like to publish a list into the topic but it seems that the data doesn't get published at all. It works fine when I remove classes and just define a single function but I want to work this out using a class. Here is the code,

#!/usr/bin/python

#from std_msgs.msg import String
import rospy
from trajectory_msgs.msg import JointTrajectoryPoint
import random

class TrialPublisher:

    def __init__(self):
        self.pub = rospy.Publisher('trialtopic', JointTrajectoryPoint, queue_size=10)
        self.node = rospy.init_node('talker', anonymous=True)

    def talker(self):
        #rate = rospy.Rate(10) # 10hz
        action1 = [0.10972262,  0.46574646,  0.74584746, -0.79732597, -0.17833593, -0.74599963]
        action2 = [0.1372262,  0.40574646,  0.64584746, -0.69732597, -0.1633593, -0.7299963]
        action3 = [0.11972262,  0.46574646,  0.70584746, -0.73732597, -0.15833593, -0.73599963]
        action4 = [0.12972262,  0.46574646,  0.72584746, -0.72732597, -0.14833593, -0.74999963]
        action5 = [0.12972262,  0.46574646,  0.73584746, -0.71732597, -0.13833593, -0.8599963]
        action6 = [0.14972262,  0.46574646,  0.6584746, -0.70732597, -0.12833593, -0.44599963]
        action = random.choice([action1, action2, action3, action4, action5, action6])
        x  = JointTrajectoryPoint()
        x.positions = action    #rospy.loginfo(hello_str)
        self.pub.publish(x)

s = TrialPublisher()
s.talker

I get the error as, WARNING: no messages received and simulated time is active. Is /clock being published?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-11-14 16:32:37 -0600

ahendrix gravatar image

rospy.spin() blocks forever and does not return. You probably want rospy.sleep(1) instead.

edit flag offensive delete link more

Comments

Thanks for your answer, I tried it using a class but it doesn't work

mkb_10062949 gravatar imagemkb_10062949 ( 2019-11-15 02:59:19 -0600 )edit
0

answered 2019-11-15 03:04:08 -0600

mkb_10062949 gravatar image

Okay so I found out that I was missing a parenthesis while calling the function talker()

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-11-14 16:01:17 -0600

Seen: 23 times

Last updated: Nov 15