Quiz for topics ROS in 5 days - Subscribers and Messages

Hi ,
Can an experienced hand provide what went wrong?
I give permissions to execute files
I compiled packages with catkin_make
I roslaunch topics_quiz topics_quiz.launch
I was unable to start robot moving despite using the code below:

#! /usr/bin/env python

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

def callback(msg): 
  print msg.ranges[360] 

#If the distance to an obstacle in front of the robot is bigger than 1 meter, the robot will move forward
  if msg.ranges[360] > 1:
      move.linear.x = 0.1
      move.angular.z = 0.0

#If the distance to an obstacle in front of the robot is smaller than 1 meter, the robot will turn left
  if msg.ranges[360] < 1: 
      move.linear.x = 0.0
      move.angular.z = 0.2
        
turn right
  if msg.ranges[719] < 1:
      move.linear.x = 0.0
      move.angular.z = -0.2
        
turn left
  if msg.ranges[0] < 1:
      move.linear.x = 0.0
      move.angular.z = 0.2
      
  pub.publish(move)

rospy.init_node('topics_quiz_node')
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback) #We subscribe to the laser's topic
pub = rospy.Publisher('/cmd_vel', Twist)
move = Twist()

rospy.spin()

Hi ROS seniors and mentor,
I am unfamiliar with writing publisher and subscriber into one python file. I got my Tutlebot moving with 4 files;
May I know if I am on the right track?
1a)launch file for publisher(topics_quiz_launch.launch)
1b)publisher python file(topics_quis.py)
2a)launch file for subscriber(topics_quiz_sublaunch.launch)
2b)subscriber python file(topics_quis_sub.py)

In my catkin workspace

$ cd ~/catkin_ws
$ source ./devel/setup.bash
#START Launch File: topics_quiz_launch.launch#



#END Launch File: topics_quiz_launch.launch#

#Python File : topics_quis.py#
#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

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

move.angular.z = 0.5

while not rospy.is_shutdown():

pub.publish(move)

rate.sleep()

#Compile package#
catkin_make --only-pkg-with-deps topics_quiz
#launch publisher file titled topics_quiz_launch.launch#
user: ~$ rostopic echo /cmd_vel
#The robot responded to publisher to move#

#START Launch File: topics_quiz_sublaunch.launch#



#END Launch File: topics_quiz_sublaunch.launch#

#Python File : topics_quis_sub.py#

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

def callback(msg):
print msg.ranges[360] #print distance to an obstacle in front of robot#

if msg.ranges[360] > 1.0:
    move.linear.x = 0.1
    move.angular.z = 0.0
    print msg.ranges[360]

if msg.ranges[360] < 1.0:
    move.linear.x = 0.0
    move.angular.z =0.2
    print msg.ranges[360]

if msg.ranges[719] < 1.0:
    move.linear.x = 0.0
    move.angular.z = 0.2
    print msg.ranges[719]

if msg.ranges[0] < 1.0:
    move.linear.x = 0.0
    move.angular.z = 0.2

    print msg.ranges[0]

rospy.init_node(‘topics_quiz_node’)
sub = rospy.Subscriber(’/kobuki/laser/scan’, LaserScan,callback)
move = Twist()
rospy.spin()
#Compile package#
catkin_make --only-pkg-with-deps topics_quiz
#launch publisher file titled topics_quiz_sublaunch.launch#
user: ~$ rostopic echo /kobuki/laser/scan
#The robot responded to subscriber to move#

Hi Hi ROS seniors and mentor, can you advise on the laser readings by sensor showing more than 3.17 m instead of 1 m
1)print msg.ranges[360]
I managed to compress the code into 2 files and nove TurtleBot
2a)launch file for subscriber(topics_quiz_sublaunch.launch)
2b)subscriber python file(topics_quis_sub.py)
$ cd ~/catkin_ws
$ source ./devel/setup.bash

START Launch File: topics_quiz_sublaunch.launch

END Launch File: topics_quiz_sublaunch.launch

Python File : topics_quis_sub.py

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

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
move.linear.y = 0.0
move.angular.z = 0.5

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

def callback(msg):

print msg.ranges[360] #print distance to an obstacle in front of robot#

if msg.ranges[360] > 1.0:
    move.linear.x = 0.1
    move.angular.z = 0.0
    print msg.ranges[360]

if msg.ranges[360] < 1.0:
    move.linear.x = 0.0
    move.angular.z =0.2
    print msg.ranges[360]

if msg.ranges[719] < 1.0:
    move.linear.x = 0.0
    move.angular.z = 0.2
    print msg.ranges[719]

if msg.ranges[0] < 1.0:
    move.linear.x = 0.0
    move.angular.z = 0.2

    print msg.ranges[0]

rospy.init_node(‘topics_quiz_node’)
sub = rospy.Subscriber(’/kobuki/laser/scan’, LaserScan,callback)
move = Twist()
rospy.spin()
user: ~$ rostopic echo /kobuki/laser/scan
#The robot responded to subscriber to move#

Hi @ohgheeling80,

It’s better to use one file for both publisher and subscriber.

The first code you shared should work if you remove the following lines from the code:

turn right
  if msg.ranges[719] < 1:
      move.linear.x = 0.0
      move.angular.z = -0.2
        
turn left
  if msg.ranges[0] < 1:
      move.linear.x = 0.0
      move.angular.z = 0.2

Hi Bayodesegun,
Thank you so much for advice.

1 Like