Differential drive robot doesn't move as programmed ?
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()
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.
@MikeScheutzow Yes it had been expired. Now I have added the code along with the description.