,

Turtlesim with Two Robots | ROS Noetic Tutorial 6

Design two turtles namely "Dogobot" and "Catobot" which can be included within the Turtlesim and both the turtles should run concurrently in a random motion.


You can use the Spawn module to create another turtle.


SOURCE CODE:

#!/usr/bin/env python3

import rospy

from geometry_msgs.msg import Twist

from turtlesim.srv import Spawn

import random

def create_turtle(turtle_x, turtle_y, turtle_theta, turtle_name):

    rospy.wait_for_service('spawn')

    spawner = rospy.ServiceProxy('spawn', Spawn)

    spawner(turtle_x, turtle_y, turtle_theta, turtle_name)


def move_turtles_random():

    v_pub1 = rospy.Publisher('/turtle2/cmd_vel', Twist, queue_size=10)

    v_pub2 = rospy.Publisher('/turtle3/cmd_vel', Twist, queue_size=10)

        rate = rospy.Rate(10)

    vel1 = Twist()

    vel2 = Twist()

    del_t = 1

    while not rospy.is_shutdown():

        if del_t % 5 == 0:

            vel1.linear.x  = random.uniform(0, 3)

            vel1.angular.z = random.uniform(-2, 2)

            vel2.linear.x  = random.uniform(0, 3)

            vel2.angular.z = random.uniform(-2, 2)

        v_pub1.publish(vel1)

        v_pub2.publish(vel2)

        del_t += 1

        rate.sleep()

if __name__ == '__main__':

    try:

        rospy.init_node('turtle_spawner')

        create_turtle(1, 1, 0, 'turtle2')

        create_turtle(9, 9, 0, 'turtle3')

        # turtle1 is preexisting turtle in sim

        # move_turtle('turtle2', 2, 2)

        # move_turtle('turtle3', 2, 2)

        move_turtles_random()

    except rospy.ROSInterruptException:

        pass


OUTPUT SCREENSHOTS (turtlesim):

TurtleSim with two robots
TurtleSim with two robots

TurtleSim with two robots
TurtleSim with two robots



 

 

 






















0 comments:

Post a Comment