4.3 Topic Quiz -- Error?

Hi all,

This is a question on 4.3 Topic Quiz of Unit 4 Understanding ROS Topics: Subscribers & Messages.
I am having difficulty trying to solve an error that the auto-checker points out:
"[assess] Not publishing to /cmd_vel. Let’s fix this before we continue.
Things your can check:

  • Did you create a publisher correctly in your source code?
  • Is the logic correct, such that the publisher gets to publish?
  • Your robot should start moving within 5 seconds of launching your package.
  • Run rostopic echo /cmd_vel and confirm that you have some data there."

However, I am publishing in my code, the publisher gets to publish, my robot starts moving in about 5 seconds (it correctly avoids the object), and when I run rostopic echo /cmd_vel I can confirm that data is being transferred.
I have also done catkin_make and source devel/setup.bash

This is my python source code:

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
rospy.init_node('topics_quiz_node')
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
rate = rospy.Rate(10)
move = Twist()


def callback(laser):
  if(laser.ranges[360] < 1):
    move.linear.x = 0.2
    move.angular.z = 0.5
  elif(laser.ranges[0] < 1):
    move.linear.x = 0.2
    move.angular.z = 0.5
  elif(laser.ranges[719] < 1):
    move.linear.x = 0.2
    move.angular.z = -0.5
  else:
    move.linear.x = 0.2
    move.angular.z = 0

while not rospy.is_shutdown(): 
    rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
    pub.publish(move)
    rate.sleep()

and this is my launch file:

<launch>
    <!-- My Package launch file -->
    <node pkg="topics_quiz" type="my_script.py" name="topics_quiz_node" output="screen">
    </node>
</launch>

Can somebody please give me advice on what I am doing wrong or if the auto-checker is bugged?

Thank you in advance!

You need to check that all of your programs in the terminals are killed, and also make sure you follow all of the instructions about naming the files because those two things affect the auto checker.

Nice try, but your logic is not optimal:

  • You should not create the subscriber in the loop, because each time the loop runs it’s being recreated. It should be created only once.
  • You don’t even need the loop.
  • You also don’t need the rospy.Rate object.

Try:

#! /usr/bin/env python

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

def callback(laser):
  if(laser.ranges[360] < 1):
    move.linear.x = 0.2
    move.angular.z = 0.5
  elif(laser.ranges[0] < 1):
    move.linear.x = 0.2
    move.angular.z = 0.5
  elif(laser.ranges[719] < 1):
    move.linear.x = 0.2
    move.angular.z = -0.5
  else:
    move.linear.x = 0.2
    move.angular.z = 0
  
   pub.publish(move)

rospy.init_node('topics_quiz_node')
rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
while pub.get_num_connections() < 1:
     # wait for a connection to publisher
     pass

move = Twist()
rospy.spin()

Hi, thank you very much for your responses.
I was able to pass with a full score thanks to your advice.

1 Like