Robotics StackExchange | Archived questions

Python Class for simple subscriber

Hi Guys,

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

Thank you, KK

Asked by kk2105 on 2018-12-11 05:34:38 UTC

Comments

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

Asked by PeteBlackerThe3rd on 2018-12-11 06:40:33 UTC

Answers

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)

Asked by Hamid Didari on 2018-12-11 22:46:53 UTC

Comments

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

Asked by kk2105 on 2018-12-16 23:16:00 UTC

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?

Asked by Mojizs on 2023-03-05 20:37:14 UTC

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

Asked by Hamid Didari on 2023-03-06 10:33:40 UTC