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

Tutlesim GO2GOAL python code error : TypeError: unsupported operand type(s) for -: 'str' and 'float' [closed]

asked 2020-11-03 12:54:11 -0600

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

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Paul Pavish
close date 2020-11-10 11:50:20.483687

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-11-03 17:02:26 -0600

Replace

goal_pose.x = input("Set your x goal: ")
goal_pose.y = input("Set your y goal: ")

with

goal_pose.x = float(input("Set your x goal: "))
goal_pose.y = float(input("Set your y goal: "))

This should resolve the issue.

edit flag offensive delete link more

Comments

Thanks @skpro19, Yes it did work . But then what happened to the goal_pose=Pose() , why it didn't work? or if this is the actual method for float input, then why is it wrong in the ROS Turtlesim Go To Goal tutorial ?

I'm just trying to understand.

Thanks & Regards Paul Pavish

Paul Pavish gravatar image Paul Pavish  ( 2020-11-04 01:35:36 -0600 )edit

I don't think there's an issue with goal_pose = Pose(). It is just that the way you were taking the input earlier was causing Python to think that x and y are Strings.

skpro19 gravatar image skpro19  ( 2020-11-04 15:19:44 -0600 )edit

Question Tools

Stats

Asked: 2020-11-03 12:53:16 -0600

Seen: 1,171 times

Last updated: Nov 03 '20