Robotics StackExchange | Archived questions

Turtlesim ROS (Moving around the wall in Squarish Shape)

TASK: Let the turtlebot go along a straight line such that it doesn't hit the border (wall) of the simulated screen. When it faces a border it always takes a 90 degree clockwise turn. As a result of the above constraints the robot path must look squarish. Use ROS Publisher and Subscriber Every time it turns the path of the robot from there on must change color indicating that the direction has changed. Hint: Use ROS service.

Hi everyone! I'm just now learning ROS, I got an task that I have to make the turtle to take a 90 degree clockwise turn when it faces a border. At the time, the color must be changed while it turning the path that indicates that the turtle direction has changed.

I have tried the code in Visual Studio Code, But it's not coming like the exact one that I am looking for.

#!usr/bin/env python3
import rospy
from turtlesim.msg import Pose
from geometry_msgs.msg import Twist
from turtlesim.srv import SetPen

previous_x = 0
def call_set_pen_service(r, g, b, width, off):
    try:
        set_pen = rospy.ServiceProxy("/turtle1/set_pen", SetPen)
        set_pen(r, g, b, width, off)
        #rospy.logwarn(e)

def pose_callback(pose: Pose):
    cmd = Twist()
    if pose.x > 9.0 or pose.x < 2.0 or pose.y > 9.0 or pose.y < 2.0:
        cmd.linear.x = 1.0
        cmd.angular.z = 0.7
    else:
        cmd.linear.x = 5.0
        cmd.angular.z = 0.0
    pub.publish(cmd)

    global previous_x
    if pose.x >= 5.5 and previous_x < 5.5:
        rospy.loginfo("Set color to red!")
        call_set_pen_service(255, 0, 0, 3, 0)
    elif pose.x < 5.5 and previous_x >= 5.5:
        rospy.loginfo("Set color to green!")
        call_set_pen_service(0, 255, 0, 3, 0)
    previous_x = pose.x

if __name__ = '__main__':
        rospy.init_node("turtle_controller")
        rospy.wait_for_service("/turtle1/set_pen")
        pub = rospy.Publisher("/turtle1/cmd_vel", Twist, queue_size = 10)
        sub = rospy.Subscriber("/turtle1/pose", Pose, callback=pose_callback)
        rospy.loginfo("Node has been started")
        rospy.spin()

image description image description image description image description

It would be great and appreciate if I get help from anybody. Thanks!

Asked by vibito on 2022-10-22 07:10:27 UTC

Comments

Answers