Quiz UNIT 2 - move.linear.x = 0.5 doesn't work

Hi!
I’m working on the quiz since 2 hours an I try to understand why the robot doesn’t move even with the following code :

topic_quiz.py :

#! /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(2)
move = Twist()

move.linear.x = 0.5

def callback(msg):
print msg
sub = rospy.Subscriber(‘kobuki/laser/scan’, LaserScan, callback)
scan = LaserScan()

while not rospy.is_shutdown():
pub.publish(move)
rospy.spin()
move.linear.x = 0.3
if scan.ranges[360] < 1:
move.angular.z = 0.5
rate.sleep()
if scan.ranges[0] < 1:
move.angular.z = 0.5
if scan.ranges[720] < 1:
move.angular.z = -0.5
rate.sleep()

topic_quiz.launch :


And it’s really strange because it worked before. I don’t understand which kind of modification in my code can do something like that…

So if someone can help me or give me some hints :sweat_smile:

Thank you

Hi @quentin.roudier,

while not rospy.is_shutdown():
pub.publish(move)
rospy.spin()

this should be the problem. Remove the red line and see if it works

1 Like

Hi simon.steinmann91

Thank you for your reply. I tried the same thing without the rospy.spin() line but I have the following error :

roslaunch topics_quiz topics_quiz.launch
… logging to /home/user/.ros/log/a69e5cb0-323f-11ea-9872-0aeecc1a3ff6/roslaunch-rosdscomputer-11900.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://rosdscomputer:37111/

SUMMARY

PARAMETERS

  • /rosdistro: kinetic
  • /rosversion: 1.12.14

NODES
/
topics_quiz_node (topics_quiz/topics_quiz.py)

ROS_MASTER_URI=http://master:11311

process[topics_quiz_node-1]: started with pid [12007]
Traceback (most recent call last):
File “/home/user/catkin_ws/src/topics_quiz/src/topics_quiz.py”, line 26, in
if scan.ranges[360] < 1:
IndexError: list index out of range
[topics_quiz_node-1] process has died [pid 12007, exit code 1, cmd /home/user/catkin_ws/src/topics_quiz/src/topics_quiz.py __name:=topics_quiz_node __log:=/home/user/.ros/log/a69e5cb0-323f-11ea-9872-0aeecc1a3ff6/topics_quiz_node-1.log].
log file: /home/user/.ros/log/a69e5cb0-323f-11ea-9872-0aeecc1a3ff6/topics_quiz_node-1*.log
all processes on machine have died, roslaunch will exit
shutting down processing monitor…
… shutting down processing monitor complete
done

I tried to understand what does that mean but I think I don’t have enough knowledges for the moment.

Just the code below, so you can see what I modified (just removed the line as you said) : #! /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(2)
move = Twist()

move.linear.x = 0.5

def callback(msg):
print msg
sub = rospy.Subscriber(‘kobuki/laser/scan’, LaserScan, callback)
scan = LaserScan()

while not rospy.is_shutdown():
pub.publish(move)

move.linear.x = 0.3
if scan.ranges[360] < 1:
    move.angular.z = 0.5
    rate.sleep()
if scan.ranges[0] < 1:
    move.angular.z = 0.5
if scan.ranges[720] < 1:
    move.angular.z = -0.5
    rate.sleep()

This part in the error message tells you what is wrong:

File “/home/user/catkin_ws/src/topics_quiz/src/topics_quiz.py”, line 26, in
if scan.ranges[360] < 1:
IndexError: list index out of range

That is most likely because never received a laserscan. Your whole subscriber callback is wrong. You might wanna go back and look at the examples again. You only define scan = LaserScan() which is just an empty structure. I recommend starting from a simple subscriber callback loop, and then adding the functionality of you publisher.

1 Like

Thank you for your help! Indeed, there was a lot of things I misunderstood. After taking a break, I was finally able to understand it better and I modified my code. It worked and I passed the quiz (I guess, 10 is the best mark or it’s the average?)

1 Like

Yes, 10 is the full score :slight_smile:
Congratulations!

1 Like