ROS programming: Trying to make my turtlebot wander without running into obstacles





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm new to ROS.
I'm trying to make my turtlebot move forward until it is within a minimal distance from an obstacle, then rotate until the path is clear, then move forward again and so on...



I wrote this code:



import rospy, sys
from stopper import Stopper
if __name__ == "__main__":
rospy.init_node("stopper_node", argv=sys.argv)
forward_speed = 0.5
angular_speed = 45
if rospy.has_param('~forward_speed'):
forward_speed = rospy.get_param('~forward_speed')
if rospy.has_param('~angular_speed'):
angular_speed = rospy.get_param('~angular_speed')
my_stopper = Stopper(forward_speed, angular_speed)
my_stopper.start_moving();


and this:



import rospy
import math
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class Stopper(object):

def __init__(self, forward_speed, angular_speed):
self.forward_speed = forward_speed
self.angular_speed = angular_speed
self.min_scan_angle = -30 /180 * math.pi
self.max_scan_angle = 30 / 180 * math.pi
self.min_dist_from_obstacle = 1
self.keep_moving = True
self.keep_rotating = False
self.command_pub = rospy.Publisher("/cmd_vel_mux/input/teleop", Twist, queue_size=10)
self.laser_subscriber = rospy.Subscriber("scan",LaserScan, self.scan_callback, queue_size=1)

def start_moving(self):
print "started moving1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to move")
while not rospy.is_shutdown() and self.keep_moving:
self.move_forward()
rate.sleep()
self.start_rotating()

def move_forward(self):
move_msg = Twist()
move_msg.linear.x = self.forward_speed
self.command_pub.publish(move_msg)


def start_rotating(self):
print "started rotating1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to rotate")
while not rospy.is_shutdown() and self.keep_rotating:
self.rotate()
rate.sleep()
self.start_moving()

def rotate(self):
move_msg = Twist()
move_msg.angular.z = self.angular_speed * 2 * math.pi / 360
self.command_pub.publish(move_msg)

def scan_callback(self, scan_msg):
for dist in scan_msg.ranges:
print dist
if self.keep_moving and dist < self.min_dist_from_obstacle:
self.keep_moving = False
print "keep moving is now false"
self.keep_rotating = True
print "keep rotating is now true"
break
if self.keep_rotating and math.isnan(dist):
self.keep_rotating = False
print "keep rotating is now false"
self.keep_moving = True
print "keep moving is now true"
break


But even though I can't find any logical mistakes in it, it just does not work and occasionaly bumps into stuff. I am running it with the gazebo simulation
"turtlebot_world". I would love to get some help.
Thanks!










share|improve this question




















  • 1





    My first thought was: "He probably doesn't account for the width of the robot." But then I see that you are using every angle from the laser scanner. Is this a 360° scanner? Sry I'm not too familiar with the turtle. If yes, then you should consider using only a range in front of the bot.

    – Darkproduct
    Nov 27 '18 at 13:25











  • Did you try my answer?

    – Benyamin Jafari
    Dec 21 '18 at 10:42


















0















I'm new to ROS.
I'm trying to make my turtlebot move forward until it is within a minimal distance from an obstacle, then rotate until the path is clear, then move forward again and so on...



I wrote this code:



import rospy, sys
from stopper import Stopper
if __name__ == "__main__":
rospy.init_node("stopper_node", argv=sys.argv)
forward_speed = 0.5
angular_speed = 45
if rospy.has_param('~forward_speed'):
forward_speed = rospy.get_param('~forward_speed')
if rospy.has_param('~angular_speed'):
angular_speed = rospy.get_param('~angular_speed')
my_stopper = Stopper(forward_speed, angular_speed)
my_stopper.start_moving();


and this:



import rospy
import math
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class Stopper(object):

def __init__(self, forward_speed, angular_speed):
self.forward_speed = forward_speed
self.angular_speed = angular_speed
self.min_scan_angle = -30 /180 * math.pi
self.max_scan_angle = 30 / 180 * math.pi
self.min_dist_from_obstacle = 1
self.keep_moving = True
self.keep_rotating = False
self.command_pub = rospy.Publisher("/cmd_vel_mux/input/teleop", Twist, queue_size=10)
self.laser_subscriber = rospy.Subscriber("scan",LaserScan, self.scan_callback, queue_size=1)

def start_moving(self):
print "started moving1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to move")
while not rospy.is_shutdown() and self.keep_moving:
self.move_forward()
rate.sleep()
self.start_rotating()

def move_forward(self):
move_msg = Twist()
move_msg.linear.x = self.forward_speed
self.command_pub.publish(move_msg)


def start_rotating(self):
print "started rotating1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to rotate")
while not rospy.is_shutdown() and self.keep_rotating:
self.rotate()
rate.sleep()
self.start_moving()

def rotate(self):
move_msg = Twist()
move_msg.angular.z = self.angular_speed * 2 * math.pi / 360
self.command_pub.publish(move_msg)

def scan_callback(self, scan_msg):
for dist in scan_msg.ranges:
print dist
if self.keep_moving and dist < self.min_dist_from_obstacle:
self.keep_moving = False
print "keep moving is now false"
self.keep_rotating = True
print "keep rotating is now true"
break
if self.keep_rotating and math.isnan(dist):
self.keep_rotating = False
print "keep rotating is now false"
self.keep_moving = True
print "keep moving is now true"
break


But even though I can't find any logical mistakes in it, it just does not work and occasionaly bumps into stuff. I am running it with the gazebo simulation
"turtlebot_world". I would love to get some help.
Thanks!










share|improve this question




















  • 1





    My first thought was: "He probably doesn't account for the width of the robot." But then I see that you are using every angle from the laser scanner. Is this a 360° scanner? Sry I'm not too familiar with the turtle. If yes, then you should consider using only a range in front of the bot.

    – Darkproduct
    Nov 27 '18 at 13:25











  • Did you try my answer?

    – Benyamin Jafari
    Dec 21 '18 at 10:42














0












0








0


1






I'm new to ROS.
I'm trying to make my turtlebot move forward until it is within a minimal distance from an obstacle, then rotate until the path is clear, then move forward again and so on...



I wrote this code:



import rospy, sys
from stopper import Stopper
if __name__ == "__main__":
rospy.init_node("stopper_node", argv=sys.argv)
forward_speed = 0.5
angular_speed = 45
if rospy.has_param('~forward_speed'):
forward_speed = rospy.get_param('~forward_speed')
if rospy.has_param('~angular_speed'):
angular_speed = rospy.get_param('~angular_speed')
my_stopper = Stopper(forward_speed, angular_speed)
my_stopper.start_moving();


and this:



import rospy
import math
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class Stopper(object):

def __init__(self, forward_speed, angular_speed):
self.forward_speed = forward_speed
self.angular_speed = angular_speed
self.min_scan_angle = -30 /180 * math.pi
self.max_scan_angle = 30 / 180 * math.pi
self.min_dist_from_obstacle = 1
self.keep_moving = True
self.keep_rotating = False
self.command_pub = rospy.Publisher("/cmd_vel_mux/input/teleop", Twist, queue_size=10)
self.laser_subscriber = rospy.Subscriber("scan",LaserScan, self.scan_callback, queue_size=1)

def start_moving(self):
print "started moving1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to move")
while not rospy.is_shutdown() and self.keep_moving:
self.move_forward()
rate.sleep()
self.start_rotating()

def move_forward(self):
move_msg = Twist()
move_msg.linear.x = self.forward_speed
self.command_pub.publish(move_msg)


def start_rotating(self):
print "started rotating1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to rotate")
while not rospy.is_shutdown() and self.keep_rotating:
self.rotate()
rate.sleep()
self.start_moving()

def rotate(self):
move_msg = Twist()
move_msg.angular.z = self.angular_speed * 2 * math.pi / 360
self.command_pub.publish(move_msg)

def scan_callback(self, scan_msg):
for dist in scan_msg.ranges:
print dist
if self.keep_moving and dist < self.min_dist_from_obstacle:
self.keep_moving = False
print "keep moving is now false"
self.keep_rotating = True
print "keep rotating is now true"
break
if self.keep_rotating and math.isnan(dist):
self.keep_rotating = False
print "keep rotating is now false"
self.keep_moving = True
print "keep moving is now true"
break


But even though I can't find any logical mistakes in it, it just does not work and occasionaly bumps into stuff. I am running it with the gazebo simulation
"turtlebot_world". I would love to get some help.
Thanks!










share|improve this question
















I'm new to ROS.
I'm trying to make my turtlebot move forward until it is within a minimal distance from an obstacle, then rotate until the path is clear, then move forward again and so on...



I wrote this code:



import rospy, sys
from stopper import Stopper
if __name__ == "__main__":
rospy.init_node("stopper_node", argv=sys.argv)
forward_speed = 0.5
angular_speed = 45
if rospy.has_param('~forward_speed'):
forward_speed = rospy.get_param('~forward_speed')
if rospy.has_param('~angular_speed'):
angular_speed = rospy.get_param('~angular_speed')
my_stopper = Stopper(forward_speed, angular_speed)
my_stopper.start_moving();


and this:



import rospy
import math
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class Stopper(object):

def __init__(self, forward_speed, angular_speed):
self.forward_speed = forward_speed
self.angular_speed = angular_speed
self.min_scan_angle = -30 /180 * math.pi
self.max_scan_angle = 30 / 180 * math.pi
self.min_dist_from_obstacle = 1
self.keep_moving = True
self.keep_rotating = False
self.command_pub = rospy.Publisher("/cmd_vel_mux/input/teleop", Twist, queue_size=10)
self.laser_subscriber = rospy.Subscriber("scan",LaserScan, self.scan_callback, queue_size=1)

def start_moving(self):
print "started moving1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to move")
while not rospy.is_shutdown() and self.keep_moving:
self.move_forward()
rate.sleep()
self.start_rotating()

def move_forward(self):
move_msg = Twist()
move_msg.linear.x = self.forward_speed
self.command_pub.publish(move_msg)


def start_rotating(self):
print "started rotating1"
rate = rospy.Rate(1)
rospy.loginfo("Starting to rotate")
while not rospy.is_shutdown() and self.keep_rotating:
self.rotate()
rate.sleep()
self.start_moving()

def rotate(self):
move_msg = Twist()
move_msg.angular.z = self.angular_speed * 2 * math.pi / 360
self.command_pub.publish(move_msg)

def scan_callback(self, scan_msg):
for dist in scan_msg.ranges:
print dist
if self.keep_moving and dist < self.min_dist_from_obstacle:
self.keep_moving = False
print "keep moving is now false"
self.keep_rotating = True
print "keep rotating is now true"
break
if self.keep_rotating and math.isnan(dist):
self.keep_rotating = False
print "keep rotating is now false"
self.keep_moving = True
print "keep moving is now true"
break


But even though I can't find any logical mistakes in it, it just does not work and occasionaly bumps into stuff. I am running it with the gazebo simulation
"turtlebot_world". I would love to get some help.
Thanks!







python algorithm navigation ros






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 19:21









Benyamin Jafari

3,80852453




3,80852453










asked Nov 26 '18 at 18:28









eliharelihar

449




449








  • 1





    My first thought was: "He probably doesn't account for the width of the robot." But then I see that you are using every angle from the laser scanner. Is this a 360° scanner? Sry I'm not too familiar with the turtle. If yes, then you should consider using only a range in front of the bot.

    – Darkproduct
    Nov 27 '18 at 13:25











  • Did you try my answer?

    – Benyamin Jafari
    Dec 21 '18 at 10:42














  • 1





    My first thought was: "He probably doesn't account for the width of the robot." But then I see that you are using every angle from the laser scanner. Is this a 360° scanner? Sry I'm not too familiar with the turtle. If yes, then you should consider using only a range in front of the bot.

    – Darkproduct
    Nov 27 '18 at 13:25











  • Did you try my answer?

    – Benyamin Jafari
    Dec 21 '18 at 10:42








1




1





My first thought was: "He probably doesn't account for the width of the robot." But then I see that you are using every angle from the laser scanner. Is this a 360° scanner? Sry I'm not too familiar with the turtle. If yes, then you should consider using only a range in front of the bot.

– Darkproduct
Nov 27 '18 at 13:25





My first thought was: "He probably doesn't account for the width of the robot." But then I see that you are using every angle from the laser scanner. Is this a 360° scanner? Sry I'm not too familiar with the turtle. If yes, then you should consider using only a range in front of the bot.

– Darkproduct
Nov 27 '18 at 13:25













Did you try my answer?

– Benyamin Jafari
Dec 21 '18 at 10:42





Did you try my answer?

– Benyamin Jafari
Dec 21 '18 at 10:42












1 Answer
1






active

oldest

votes


















0














I have a solution with the bugs navigation algorithms, hope help you up:



with these repositories (agn_gazebo, bugs), you could bring up a Gazebo simulation with a bounded map and some obstacle and a mobile wheeled robot platform (Pioneer P3dx) which is equipped with a laser scanner (Hokuyo URG) for environmental perception.





USAGE:





  • After cloning from these repositories in ~/catkin_ws/src using:



    git clone https://github.com/agn-7/agn_gazebo.git
    git clone https://github.com/agn-7/bugs.git


  • Then build them with catkin_make in your catkin workspace.



  • After package building, you need to change the agn_gazebo/worlds/final.world file:



    Open it and change all /home/agn/catkin_ws/src/... with your target path with Ctrl + F or Ctrl + H



    I've opened an issue to make it as a dynamic path instead of static, but currently, I couldn't do it.



  • Then Bring up the simulator: roslaunch agn_gazebo gazebo.launch



  • Running any bugs algorithm with a position target:



    rosrun bugs bug.py bug0 11 0

    or
    rosrun bugs bug.py bug1 11 0

    or
    rosrun bugs bug.py bug2 11 0








share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487008%2fros-programming-trying-to-make-my-turtlebot-wander-without-running-into-obstacl%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have a solution with the bugs navigation algorithms, hope help you up:



    with these repositories (agn_gazebo, bugs), you could bring up a Gazebo simulation with a bounded map and some obstacle and a mobile wheeled robot platform (Pioneer P3dx) which is equipped with a laser scanner (Hokuyo URG) for environmental perception.





    USAGE:





    • After cloning from these repositories in ~/catkin_ws/src using:



      git clone https://github.com/agn-7/agn_gazebo.git
      git clone https://github.com/agn-7/bugs.git


    • Then build them with catkin_make in your catkin workspace.



    • After package building, you need to change the agn_gazebo/worlds/final.world file:



      Open it and change all /home/agn/catkin_ws/src/... with your target path with Ctrl + F or Ctrl + H



      I've opened an issue to make it as a dynamic path instead of static, but currently, I couldn't do it.



    • Then Bring up the simulator: roslaunch agn_gazebo gazebo.launch



    • Running any bugs algorithm with a position target:



      rosrun bugs bug.py bug0 11 0

      or
      rosrun bugs bug.py bug1 11 0

      or
      rosrun bugs bug.py bug2 11 0








    share|improve this answer






























      0














      I have a solution with the bugs navigation algorithms, hope help you up:



      with these repositories (agn_gazebo, bugs), you could bring up a Gazebo simulation with a bounded map and some obstacle and a mobile wheeled robot platform (Pioneer P3dx) which is equipped with a laser scanner (Hokuyo URG) for environmental perception.





      USAGE:





      • After cloning from these repositories in ~/catkin_ws/src using:



        git clone https://github.com/agn-7/agn_gazebo.git
        git clone https://github.com/agn-7/bugs.git


      • Then build them with catkin_make in your catkin workspace.



      • After package building, you need to change the agn_gazebo/worlds/final.world file:



        Open it and change all /home/agn/catkin_ws/src/... with your target path with Ctrl + F or Ctrl + H



        I've opened an issue to make it as a dynamic path instead of static, but currently, I couldn't do it.



      • Then Bring up the simulator: roslaunch agn_gazebo gazebo.launch



      • Running any bugs algorithm with a position target:



        rosrun bugs bug.py bug0 11 0

        or
        rosrun bugs bug.py bug1 11 0

        or
        rosrun bugs bug.py bug2 11 0








      share|improve this answer




























        0












        0








        0







        I have a solution with the bugs navigation algorithms, hope help you up:



        with these repositories (agn_gazebo, bugs), you could bring up a Gazebo simulation with a bounded map and some obstacle and a mobile wheeled robot platform (Pioneer P3dx) which is equipped with a laser scanner (Hokuyo URG) for environmental perception.





        USAGE:





        • After cloning from these repositories in ~/catkin_ws/src using:



          git clone https://github.com/agn-7/agn_gazebo.git
          git clone https://github.com/agn-7/bugs.git


        • Then build them with catkin_make in your catkin workspace.



        • After package building, you need to change the agn_gazebo/worlds/final.world file:



          Open it and change all /home/agn/catkin_ws/src/... with your target path with Ctrl + F or Ctrl + H



          I've opened an issue to make it as a dynamic path instead of static, but currently, I couldn't do it.



        • Then Bring up the simulator: roslaunch agn_gazebo gazebo.launch



        • Running any bugs algorithm with a position target:



          rosrun bugs bug.py bug0 11 0

          or
          rosrun bugs bug.py bug1 11 0

          or
          rosrun bugs bug.py bug2 11 0








        share|improve this answer















        I have a solution with the bugs navigation algorithms, hope help you up:



        with these repositories (agn_gazebo, bugs), you could bring up a Gazebo simulation with a bounded map and some obstacle and a mobile wheeled robot platform (Pioneer P3dx) which is equipped with a laser scanner (Hokuyo URG) for environmental perception.





        USAGE:





        • After cloning from these repositories in ~/catkin_ws/src using:



          git clone https://github.com/agn-7/agn_gazebo.git
          git clone https://github.com/agn-7/bugs.git


        • Then build them with catkin_make in your catkin workspace.



        • After package building, you need to change the agn_gazebo/worlds/final.world file:



          Open it and change all /home/agn/catkin_ws/src/... with your target path with Ctrl + F or Ctrl + H



          I've opened an issue to make it as a dynamic path instead of static, but currently, I couldn't do it.



        • Then Bring up the simulator: roslaunch agn_gazebo gazebo.launch



        • Running any bugs algorithm with a position target:



          rosrun bugs bug.py bug0 11 0

          or
          rosrun bugs bug.py bug1 11 0

          or
          rosrun bugs bug.py bug2 11 0









        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 13 '18 at 8:41

























        answered Dec 13 '18 at 8:36









        Benyamin JafariBenyamin Jafari

        3,80852453




        3,80852453
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487008%2fros-programming-trying-to-make-my-turtlebot-wander-without-running-into-obstacl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft