Tutlesim GO2GOAL python code error : TypeError: unsupported operand type(s) for -: 'str' and 'float' [closed]
I tried the following code from Turtlesim tutorial (GO TO GOAL):
import rospy
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose
from math import pow, atan2, sqrt
class TurtleBot:
def __init__(self):
rospy.init_node('turtlebot_controller', anonymous=True)
self.velocity_publisher = rospy.Publisher('/turtle1/cmd_vel',Twist, queue_size=10)
self.pose_subscriber = rospy.Subscriber('/turtle1/pose',Pose, self.update_pose)
self.pose = Pose()
self.rate = rospy.Rate(10)
def update_pose(self,data):
self.pose = data
self.pose.x = round(self.pose.x, 4)
self.pose.y = round(self.pose.y, 4)
def euclidean_distance(self, goal_pose):
return sqrt(pow((goal_pose.x - self.pose.x), 2) + pow((goal_pose.y - self.pose.y), 2))
def linear_vel(self, goal_pose, constant = 1.5):
return constant * self.euclidean_distance(goal_pose)
def steering_angle(self, goal_pose):
return atan2(goal_pose.y - self.pose.y, goal_pose.x - self.pose.x)
def angular_vel(self, goal_pose, constant = 6):
return constant * (self.steering_angle(goal_pose) - self.pose.theta)
def move2goal(self):
goal_pose = Pose()
goal_pose.x = input("Set your x goal: ")
goal_pose.y = input("Set your y goal: ")
distance_tolerance = input("Set your tolerance: ")
vel_msg = Twist()
while self.euclidean_distance(goal_pose) >= distance_tolerance:
vel_msg.linear.x = self.linear_vel(goal_pose)
vel_msg.linear.y = 0
vel_msg.linear.z = 0
vel_msg.angular.x = 0
vel_msg.angular.y = 0
vel_msg.angular.z = self.angular_vel(goal_pose)
self.velocity_publisher.publish(vel_msg)
self.rate.sleep()
vel_msg.linear.x = 0
vel_msg.linear.z = 0
self.velocity_publisher.publish(vel_msg)
rospy.spin()
if __name__ == '__main__':
try:
x = TurtleBot()
x.move2goal()
except rospy.ROSInterruptException:
pass
When i run it, it asks for value of x, value of y and then value of tolerance. But after entering tolerance it shows this error:
Traceback (most recent call last):
File "/home/paul_pavish/draw_arm/devel/lib/turtle_control/turtle_control_node.py", line 15, in <module>
exec(compile(fh.read(), python_script, 'exec'), context)
File "/home/paul_pavish/draw_arm/src/turtle_control/turtle_control_node.py", line 82, in <module>
x.move2goal()
File "/home/paul_pavish/draw_arm/src/turtle_control/turtle_control_node.py", line 57, in move2goal
while self.euclidean_distance(goal_pose) >= distance_tolerance:
File "/home/paul_pavish/draw_arm/src/turtle_control/turtle_control_node.py", line 29, in euclidean_distance
return sqrt(pow((goal_pose.x - self.pose.x), 2) + pow((goal_pose.y - self.pose.y), 2))
TypeError: unsupported operand type(s) for -: 'str' and 'float'
I assume that the goal_pose = Pose()
didn't do it's part. If yes, then that means that import from a different module from turtlesim.msg import Pose
didn't work. I tried simple Publisher & Subscriber (it worked) and Turtlesim_teleop_key(it also worked) along with some other customisations on Publisher & Subscriber (worked). Then whats happening here ???
Any suggestions on how to fix this TypeError ?
I tried converting ever pose related object/variable to float
, and it seems working fine then, but that's not the answer for long run, . Need Help ASAP.
Thanks in advance
Paul Pavish