Bot not turning : Quiz-1 - Publisher, Subscriber & Messages

The code I have used is posted below. The problem I am facing is that the BOT keeps moving forward and does not turn.
As per my understanding while loops keep repeating until they are broken, when the file is executed the pub.publish(move) should be executed with forward command until the distance is <= 1, then it should go left. But the BOT is not turning left.

Appreciate any help.

#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

def callback(self,LaserScan):
    ls = LaserScan
def front():
    return self.ls.ranges[360]
def left():
    return self.ls.ranges[719]
def right():
    return self.ls.ranges[0]
rospy.init_node('topics_quiz_node', anonymous = True)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
rate = rospy.Rate(2)
move = Twist()
while not rospy.is_shutdown():
        if front > 1 :                                     #forward
            move.linear.x = 0.3
            move.angular.z = 0
        elif front <= 1 and right <=1 :          #left
            move.linear.x = 0
            move.angular.z = 0.5
        elif front <= 1 and left <=1 :           #right
            move.linear.x = 0
            move.angular.z = -0.5 
        pub.publish(move)
rate.sleep()

Hi @maulin95,
There is several issues here:

  1. the ‘self’ variable is only used in classes, remove it completely here.

  2. When you call a function, like the front() function, you have to use

if front() > 1 :

front would be a variable you never set. The whole code could be much simpler if you just do this:

#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

def callback(LaserScan):
    ls = LaserScan
    front = ls.ranges[360]
    left = ls.ranges[719]
    right = ls.ranges[0]

rospy.init_node('topics_quiz_node', anonymous = True)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
rate = rospy.Rate(2)
move = Twist()
while not rospy.is_shutdown():
        if front > 1 :                                     #forward
            move.linear.x = 0.3
            move.angular.z = 0
        elif front <= 1 and right <=1 :          #left
            move.linear.x = 0
            move.angular.z = 0.5
        elif front <= 1 and left <=1 :           #right
            move.linear.x = 0
            move.angular.z = -0.5 
        pub.publish(move)
rate.sleep()

It is also advisable to put your publishing logic into the LaserScan callback. For example my solution lnooks like this. Perhaps try to adapt it, using your own logic.

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
var = Twist()

def callback(msg):
    if sum(msg.ranges[320:400]) < 200:
        var.linear.x = 0
        var.angular.z = 1
    else:
        var.linear.x = 1
        var.angular.z = 0
    pub.publish(var)

rospy.init_node('laser_scan')
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
rospy.spin()
2 Likes

Hi @simon.steinmann91, thanks for your reply.
In my code:
Actually, my code was just like this before, I kept getting error ‘front’ is not defined.

In your code:
Yours is very much better and compact. I tried adapting my code with reference to yours, but ended up almost exactly like yours. And it works.

Thanks a lot for your support.

2 Likes

you got that errror, because you tried to get ‘front’, a variable, which was not defined. You only defined the function ‘front’, which you have to call with ‘front()’

1 Like

Is there supposed to be a node initialized for the publisher in your example? If so, which node is supposed to be named topics_quiz_node? There was so much left unexplained in the quiz instructions.

In simon.steinmann91 code (do not use mine) the laser_scan node is supposed to be topics_quiz_node.
I believe the quiz was vague on purpose to test our understanding.

Hi @JoshKindhart,
In general you initialize one node per script, so you dont have to make another. For the quiz, make sure that you follow the instructions exactly and name and define everything correctly. My code was not meant as a copy paste solution, but as a guide on how to structure it :wink: