# ROS Basics in 5 Days (Python) Noetic 4 Quiz

I failed the quiz and I was not aware that I can try only 5 Times, so I didn’t go for the solution for the quiz initially can you please help me in getting the right solution so that I can learn from it.

Sure, no problem. can you be specific on which chapter you are referring to since there are multiple quizzes? Also, share the code that you wrote so that we can find out where you went wrong

I guess it’s chapter 4 in section 4.3 . One more thing I would like to ask does failing in quiz would effect my certificate?

Please share the code you wrote so i can check for errors.
Ideally look for solutions by yourself, plenty of resources online. This way you are not restricting yourself to this course and learn more stuff on the way. If you are stuck, i am happy to help out

1 Like

This was the question

1. Create a Publisher that writes into the /cmd_vel topic in order to move the robot.
2. Create a Subscriber that reads from the /kobuki/laser/scan topic. This is the topic where the laser publishes its data.
3. Depending on the readings you receive from the laser’s topic, you’ll have to change the data you’re sending to the /cmd_vel topic in order to avoid the wall. This means, use the values of the laser to decide.

The logic that your program has to follow is the next one:

1. If the laser reading in front of the robot is higher than 1 meter (there is no obstacle closer than 1 meter in front of the robot), the robot will move forward.
2. If the laser reading in front of the robot is lower than 1 meter (there is an obstacle closer than 1 meter in front of the robot), the robot will turn left.
3. If the laser reading at the right side of the robot is lower than 1 meter (there is an obstacle closer than 1 meter at the right side of the robot), the robot will turn left.
4. If the laser reading at the left side of the robot is lower than 1 meter (there is an obstacle closer than 1 meter at the left side of the robot), the robot will turn right.

Code:

#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

disToObstacle = 1

def callback(msg):

``````rospy.loginfo(rospy.get_caller_id()+ 'The distance to obstacle is - %s',msg.ranges)

if msg.ranges>disToObstacle:

print('in')

move.linear.x = .9

move.angular.z = .5

if msg.ranges < disToObstacle:

print('out')

move.linear.x = 0

move.angular.z = .5

while not rospy.is_shutdown():

pub.publish(move)

rate.sleep()
``````

sub = rospy.Subscriber(’/kobuki/laser/scan’,LaserScan,callback)

rospy.init_node(‘topics_quiz_node’)

pub = rospy.Publisher(’/cmd_vel’,Twist,queue_size = 1)

rete = rospy.Rate(2)

move = Twist()

rospy.spin()

This is the code please guide me through

#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

disToObstacle = 1

def callback(msg):

``````rospy.loginfo(rospy.get_caller_id()+ 'The distance to obstacle is - %s',msg.ranges)

if msg.ranges>disToObstacle:

print('in')

move.linear.x = .9

move.angular.z = .5

if msg.ranges < disToObstacle:

print('out')

move.linear.x = 0

move.angular.z = .5

while not rospy.is_shutdown():

pub.publish(move)

rate.sleep()
``````

sub = rospy.Subscriber(’/kobuki/laser/scan’,LaserScan,callback)

rospy.init_node(‘topics_quiz_node’)

pub = rospy.Publisher(’/cmd_vel’,Twist,queue_size = 1)

rete = rospy.Rate(2)

move = Twist()

rospy.spin()

``````#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

def callback(msg):

#Move straight forward

if msg.ranges>1:#change the laser from 300 to 360 since that is the forward pointing laser

print('Forward',msg.ranges)

move.linear.x = 0.3#keep the speed small giving the robot more reaction time

move.angular.z = 0

pub.publish(move)

if msg.ranges<1:#change the laser from 300 to 360 since that is the forward pointing laser

print('Stop')

move.linear.x = 0

move.angular.z = 0

pub.publish(move)

rospy.init_node('topics_quiz_node')# change ’ to ' in all,the text actually turns brown if done correctly

move = Twist()

#publisher created

pub = rospy.Publisher('/cmd_vel',Twist,queue_size = 1)

sub = rospy.Subscriber('/kobuki/laser/scan',LaserScan,callback)

#wait 2 sec for the nodes to connect

rospy.spin()
``````

i have commented the corrections next to the code. This code makes the robot move in a straight line and stop 1m infront of the wall. Try altering the code to make it move left

We can’t provide the complete solution for the quiz, because it is a quiz as Joseph1001 suggested you need to find a way to make the robot turn when it comes within a certain distance of the wall. So look in the previous chapters and research online how to get the robot to turn. You have a 5 chances to pass the quiz. This means you should have 4 left, but remember do not look at the solution until you pass the quiz. Failing the quiz the first few times has no impact on receiving your certificate. This is why you have 5 chances to pass. Once you pass move on to the next section and everything will be fine.

Good luck

1 Like

4 posts were split to a new topic: Prerequisites exam help

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.