ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Differential drive robot doesn't move as programmed ?

asked 2022-08-04 12:06:06 -0500

annamalai gravatar image

updated 2022-08-07 09:09:16 -0500

lucasw gravatar image

Hi,

I was trying to create a Wall following differential drive robot based on the ranges data obtained from 2D Laser scan sensor by following this tutorial given in this Link(Part 7). I had custom modified the Python code(given below) for the node unlike given in the tutorial. It seems fine to me but when I run it using rosrun it moves in a unsual manner as shown in this video (which I have uploaded) even if I set the angular velocities to be zero. I don't know what is the problem ?

I am using ROS noetic in Linux Mint Cinnamon 20.3 and version of Gazebo is 11.10.2.

The Python Code:

#! /usr/bin/env python3

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

dist = [0,0,0]
state_ = 0
def clbk_laser(msg):
    dist[0] = msg.ranges[0] #LEFT
    dist[1] = msg.ranges[359] #STRAIGHT
    dist[2] = msg.ranges[719] #RIGHT
    print(dist)
    move_accord(dist)

def move_accord(dist) :
    d = 0.1
    if dist[0] < d and dist[2] > d and dist[1] < d :
        state_ = 2
    if dist[0] < d and dist[2] > d and dist[1] > d :
        state_ = 0
    if dist[0] > d and dist[2] < d and dist[1] < d :
        state_ = 1
    if dist[0] > d and dist[2] < d and dist[1] > d :
        state_ = 0
    if dist[0] > d and dist[2] > d and dist[1] < d :
        state_ = 2
    if dist[0] > d and dist[2] > d and dist[1] > d :
        state_ = 0
    change_state(state_)    

def change_state(state_):        
    msg = Twist()
    if state_ == 0 :
        print("Straight")
        print(state_)
        msg.linear.x = 100
        msg.angular.z = 0
        pub.publish(msg)
    if state_ == 1 :
        print("Turn Left")
        print(state_)
        msg.linear.x = 0
        msg.angular.z = 0
        pub.publish(msg)
    if state_ == 2 :
        print("Turn Right")
        print(state_)
        msg.linear.x = 0
        msg.angular.z = 0
        pub.publish(msg)

def main():

    global pub,dist,state_
    rospy.init_node('laser_scan_test')
    pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
    sub = rospy.Subscriber('/two_wheele_robo/laser/scan', LaserScan, clbk_laser)
    rate = rospy.Rate(1)
    rate.sleep()
    rospy.spin()

if __name__ == '__main__' :
    main()
edit retag flag offensive close merge delete

Comments

Your pastebin link does not work for me (expired?) On this site, please don't use these temporary file web sites.

Please edit your question, copy/paste the actual code into the description, and format it with the 101010 button. You edit using the "edit" button near the end of your description.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-08-06 07:50:48 -0500 )edit

@MikeScheutzow Yes it had been expired. Now I have added the code along with the description.

annamalai gravatar image annamalai  ( 2022-08-06 12:46:20 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-08-06 15:33:13 -0500

Mike Scheutzow gravatar image

The simulation appears to be working correctly to me. It's doing exactly what you coded: all 3 values in list d are > 0.1, so it selected straight ahead. After the robot runs into an obstacle, it is unable to move any further in the forward direction.

P.S. the units of the Twist message are "meters/second" and "radians/second". Setting linear.x to 100 is a very bad idea.

edit flag offensive delete link more

Comments

Oh but I don't know why it doesn't work for me. In my case other than this node everything is same as of tutorial, same world, same robot of same robot description(only I have changed the name from m2wr to two_wheele_robo).

Yes setting linear.x to 100 is bad but as you have seen in the video, the robot in a disobeying manner turns and moves backwards, so for the testing purpose I had set it to be 100 and similarly angular.z to be 0 and d to be 0.1.

annamalai gravatar image annamalai  ( 2022-08-07 03:17:28 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-08-04 12:06:06 -0500

Seen: 65 times

Last updated: Aug 06 '22