turtlebot 2 in a Zigzag motion with avoiding obstacle using hokuyo urg-04lx laser scanner

asked 2018-02-10 16:23:29 -0500

karmel gravatar image

updated 2018-02-10 16:29:33 -0500

hi all , i'am trying to move the turtlebot 2 in a Zigzag motion with avoiding obstacle using hokuyo urg-04lx laser scanner, iam using python to write the code , the code make the robot going straight then turning at angle 35 every time it reflect it direction , the problem is after the robot turn at the dedicated angle it stop then turn , the motion dose not look continue , this my code , pleas help me.

#!/usr/bin/env python

import rospy

from std_msgs.msg import Int32

from geometry_msgs.msg import Twist

from math import radians

from sensor_msgs.msg import LaserScan

import tf

from std_msgs.msg import Empty

from time import sleep

def scan_callback(msg): global g_range_ahead g_range_ahead = msg.ranges[len(msg.ranges)/2] scan_sub = rospy.Subscriber('scan', LaserScan, scan_callback) cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)

class DrawZgzag(): def __init__(self): # initiliaze rospy.init_node('drawZgzag', anonymous=False)

    # What to do you ctrl + c    
    rospy.on_shutdown(self.shutdown)

    self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)


    r = rospy.Rate(5);
    d = 35


    while not rospy.is_shutdown():

        move_cmd = Twist()
        move_cmd.linear.x = 0.2

        d = d * -1

        turn_cmd = Twist()
        turn_cmd.linear.x = 0
        turn_cmd.angular.z = radians(d); 

        turn_cmd2 = Twist()
        turn_cmd2.linear.x = 0
        turn_cmd2.angular.z = radians(45);

        if(g_range_ahead < 0.5):
            rospy.loginfo("obisticl")

            for x in range(0,10): 
                self.cmd_vel.publish(turn_cmd2) 
                r.sleep()

        else: 
            rospy.loginfo("Going Straight")
            for x in range(0,10):
                self.cmd_vel.publish(move_cmd)
                r.sleep()
    # turn 90 degrees


        rospy.loginfo("Turning")
            for x in range(0,10): 
                self.cmd_vel.publish(turn_cmd) 
                r.sleep()



def shutdown(self):
    # stop turtlebot
    rospy.loginfo("Stop Zgzag")
    self.cmd_vel.publish(Twist())
    rospy.sleep(1)

if __name__ == '__main__': try: DrawZgzag() except: rospy.loginfo("node terminated.")

edit retag flag offensive close merge delete