0

I am fairly new to this and I am trying to get the coordinates of a turtle to calculate Euclidean distances in python. I have a python code which moves around 2 turtles. one named "turtle1" and the other named "turtle2". I am solely confused on how I would only get the coordinates of either turtle. I can set their coordinates. but when they are moving I have no idea how to check their coordinates. I am very new to it and I was wondering, if I could get the coordinates with my subscriber?

2 Answers2

1

You can do this by subscribing to the odometry of the robot. The following code shows how this could look like:

#! /usr/bin/env python

import rospy
from nav_msgs.msg import Odometry

def callback(msg):
    print(msg.pose.pose)

rospy.init_node('get_odometry')
odom_sub = rospy.Subscriber('/odom', Odometry, callback)
rospy.spin()

Make sure the topic you are subscribing is correct.

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • what argument should I send into the method? – randomUser1212421 Apr 03 '22 at 17:43
  • The callback is called by ROS if new odometry is incoming. Please make sure you understood the basic mechanisms of ROS and the API. A good startingpoint is here: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29 – Fruchtzwerg Apr 03 '22 at 17:51
  • honestly still having problems with it – randomUser1212421 Apr 03 '22 at 18:46
  • @randomUser1212421 you'll have to provide more information. What problems are you having? Because the above answer will work. – BTables Apr 03 '22 at 19:44
  • Of course, I have an initialisation at the topof my class for one of the turtles which which is now included on the question. Although I couldnt add the subscriber the person showed me as already have a subscriber so I did this. but my code does move around two turtles by euclidean distance. I noticed when adding the subscriber to some places, the turtles would not move as I have two classes as one turtle chases and the other runs away. so they need to constantly update each other on their position in a while loop in the main program. the rospy.spin caused it to wait I think – randomUser1212421 Apr 05 '22 at 15:31
  • @randomUser1212421 `rospy.spin()` will cause the node to *only* process callbacks/ros events the for remainder of the node's life. If you have a main process loop that is always running you should not be using spin. – BTables Apr 05 '22 at 15:35
  • Yeah, I took it out and I had it in the while loop, but it did not do anything. I will try in the move function (used in main while loop to calculate turtle) – randomUser1212421 Apr 05 '22 at 15:43
0

The documentation of the turtlesim node: http://wiki.ros.org/turtlesim gives information about the node.

For each turtleX the position is published into /turtleX/pose topic, and the message is a turtlesim/pose type (doc: http://docs.ros.org/en/noetic/api/turtlesim/html/msg/Pose.html).

This following code is a suggestion allowing to save the position of each turtle and print the distance.

node_turtle_measure.py:

#!/usr/bin/python3

import math
import rospy
from turtlesim.msg import Pose


class TurtleDistanceMeasure:
    
    @staticmethod
    def DISTANCE_POSE(pose1,pose2): 
        return math.sqrt((pose1.x-pose2.x)**2+(pose1.y-pose2.y)**2)

    def __init__(self):
        self._pose_turtle1 = Pose()
        self._pose_turtle2 = Pose()

    # save turtle1 pose
    def callback_turtle1(self,msg): 
        self._pose_turtle1 = msg

    # save turtle2 pose
    def callback_turtle2(self,msg): 
        self._pose_turtle2 = msg

    def get_distance(self): 
        return TurtleDistanceMeasure.DISTANCE_POSE(self._pose_turtle1,self._pose_turtle2)


if __name__=="__main__":
    rospy.init_node("node_turtle_measure")

    turtle_distance = TurtleDistanceMeasure()
    sub_pose_1 = rospy.Subscriber("pose1",Pose,callback=turtle_distance.callback_turtle1)
    sub_pose_2 = rospy.Subscriber("pose2",Pose,callback=turtle_distance.callback_turtle2)

    rate = rospy.Rate(20)
    while(not rospy.is_shutdown()): 
        distance = turtle_distance.get_distance()
        rospy.loginfo(f"distance: {distance}")
        rate.sleep()

Following this configuration:

  • first turtle name: turtle1
  • second turtle name: turtle2

The execution should be:

$ rosrun {your_pkg} node_turtle_measure.py pose1:=turtle1/pose pose2:=turtle2/pose

PouceHeure
  • 11
  • 1