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

Python Class for simple subscriber

asked 2018-12-11 04:34:38 -0600

kk2105 gravatar image

Hi Guys,

Can anybody help me in getting the python class program for simple subscriber using Python.

Thank you, KK

edit retag flag offensive close merge delete

Comments

3

The tutorial page is here. Can you tell us exactly which part you need help with?

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-11 05:40:33 -0600 )edit

1 Answer

Sort by » oldest newest most voted
4

answered 2018-12-11 21:46:53 -0600

Hamid Didari gravatar image

hi
this is an example code of simple python class that subscribe one topic:

#!/usr/bin/env python
import rospy
import sys
from std_msgs.msg import String

class simple_class:

  def __init__(self):
    self.sub = rospy.Subscriber("chatter",String,self.callback)

  def callback(self,data):
    print(data.data)



def main(args):
  obc = simple_class()
  rospy.init_node('simple_class', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")

if __name__ == '__main__':
    main(sys.argv)
edit flag offensive delete link more

Comments

@hamid Thank you for the solution. Will try the same and post the updates.

kk2105 gravatar image kk2105  ( 2018-12-16 22:16:00 -0600 )edit

Hey I tried something similar, with a callback function defined within the class for the subscriber. But I receive an error for positional arguments. It asks me to have 'self' first and not 'data'. When I make the changes it now says 'data' first and 'self' second. Do you know what this could be?

Mojizs gravatar image Mojizs  ( 2023-03-05 19:37:14 -0600 )edit

I need to see your code and error. Please ask a new question and put your code in it.

Hamid Didari gravatar image Hamid Didari  ( 2023-03-06 09:33:40 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2018-12-11 04:34:38 -0600

Seen: 6,935 times

Last updated: Dec 11 '18