I am getting an attribute error when running this code "print(self.x) AttributeError: Controller instance has no attribute 'x'"
Code:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
import sys
import math
import time
from turtlesim.msg import Pose
class Controller:
def __init__(self):
rospy.init_node('mover',anonymous = True)
self.speed_publisher = rospy.Publisher('/turtle1/cmd_vel',Twist,queue_size =10)
self.pose_subscriber = rospy.Subscriber('/turtle1/pose',Pose,self.callback)
self.rate = rospy.Rate(10)
def callback(self,msg):
self.x = msg.x
self.y = msg.y
def _move(self,speed,distance,direction = True):
self.velocity_message = Twist()
self.speed = speed
self.distance = distance
self.direction = direction
print(self.x)
current_x = 0
current_y = 0
current_theta = 0
if self.direction == 1:
self.velocity_message.linear.x = abs(self.speed)
else:
self.velocity_message.linear.x = -abs(self.speed)
self.distance_moved = 0
while not rospy.is_shutdown():
rospy.loginfo("Turtlesim moves forward")
self.speed_publisher.publish(self.velocity_message)
self.rate.sleep()
self.distance_moved = self.distance_moved + abs(0.5*math.sqrt(((self.x-current_x)**2)+(self.y-current_y)**2))
print (self.distance_moved)
if not (self.distance_moved<self.distance):
rospy.loginfo('Reached')
break
self.velocity_message.linear.x = 0
self.speed_publisher.publish(self.velocity_message)
rospy.spin()
if __name__ == '__main__':
try:
control = Controller()
control._move(0.1,2,1)
except rospy.ROSInterruptException:
pass
Hi,
Define
self.x
andself.y
on__init__
statement.