ROS Turtlesim Mobility | ROS Noetic Tutorial 4

Mobility of Turtlesim in ROS1

Perform the TurtleSim simulation is ROS and make the circular movement and square movement of the turtle.


SOURCE CODE: rectangle_movement.py


#!/usr/bin/env python3

import rospy

from geometry_msgs.msg import Twist


# This is one logic, where square and rectangle are drawn without changing the orientation of the turtle


def square_movement():

    pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)

    rospy.init_node('tsim_sqmove_driver', anonymous=True)

    rate = rospy.Rate(1)

    del_t = 0

    direction = 0

    while not rospy.is_shutdown():

        robo_vel = Twist()

        if del_t % 20:

            if direction == 0:

                robo_vel.linear.y = 0

                robo_vel.linear.x = 1

                direction = 1

            elif direction == 1:

                robo_vel.linear.y = 1

                robo_vel.linear.x = 0

                direction = 2

            elif direction == 2:

                robo_vel.linear.y = 0

                robo_vel.linear.x = -1

                direction = 3

            elif direction == 3:

                robo_vel.linear.y = -1

                robo_vel.linear.x = 0

                direction = 0

        del_t += 1

        pub.publish(robo_vel)

        rate.sleep()


if __name__ == '__main__':

    try:

        square_movement()

    except rospy.ROSInterruptException:

        pass



Turtlesim
Turtlesim



triangle_movement.py


#!usr/bin/env python3


import rospy

from geometry_msgs.msg import Twist

import math


def rotate(pub, robo_vel, angle=2*math.pi/3):

    angular_speed = 2

    robo_vel.angular.z = angular_speed

    start_t = rospy.Time.now().to_sec()

    d_angle = 0

    while d_angle < angle:

        pub.publish(robo_vel)

        cur_t = rospy.Time.now().to_sec()

        d_angle = angular_speed * (cur_t - start_t)

    robo_vel.angular.z = 0

    pub.publish(robo_vel)


def triangle_movement():

    pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)

    rospy.init_node('tsim_trimove_driver', anonymous=True)

    rate = rospy.Rate(1)

    del_t = 0

    while not rospy.is_shutdown():

        robo_vel = Twist()

        robo_vel.linear.x = 2

        if del_t % 20 == 0 and del_t != 0:

            robo_vel.linear.x = 0

            rotate(pub, robo_vel, 2*math.pi/3)

        del_t += 1

        pub.publish(robo_vel)

        rate.sleep()


if __name__ == '__main__':

    try:

        triangle_movement()

    except rospy.ROSInterruptException:

        pass




Comments

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide]

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2