0

Here is the code and the error that I am getting when I run the program . Can anyone explain what mistake I am doing? Also is it good to call a function of a class in the constructor like I did?

#!/usr/bin/env python

import rospy

class move_turtlebot(message):
    def __init__(self):
          self.pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)
          self.vel=Twist()
          rospy.loginfo("initiated!!")
         self.move(message)
    def move(self,message):
          if message=="STOP":
              rospy.loginfo("inside stop")
              self.vel=Twist()
              self.pub.publish(vel)
          elif message=="LEFT":
        #rest of code continues 
if __name__=="__main__":

   rospy.init_node('move_turtlebot')
   rospy.loginfo("here!")

   while not rospy.is_shutdown():
       ahead=move_turtlebot("FORWARD")

The error is:NameError: name 'message' is not defined at the line of defining class

3
  • In your __init__ method message has not yet been defined. Also elif is not indented correctly Commented Jan 11, 2020 at 17:01
  • Your class move_turtlebot inherits from message but this class is not defined/imported Commented Jan 11, 2020 at 17:02
  • I created an object of class "ahead=move_turtlebot("FORWARD"), the message variable is a string "FORWARD", isn't this sufficient. What does defining/importing class mean. Commented Jan 11, 2020 at 17:12

2 Answers 2

1
class move_turtlebot(message): # this means inheritance from a class message which u dont have or didnt define

For a class to have parameters they can be defined at the constructor like this (although there are variations) :

class move_turtlebot:
    def __init__(self,message):
Sign up to request clarification or add additional context in comments.

Comments

1

In your Class definition you are using message:

class move_turtlebot(message): <--- HERE

you don't have message defined, if you want to use message in your consructor add it to your __init__ function like so:

def __init__(self, message)

also I noted that the last line in your init function uses different indentation, if it is the same at your code, change it as well.

Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.