During the grading of my Topics Quiz, I missed points for “not subscribed to /kobuki/laser/scan”.
Here is the breakdown of the marks:
- topics_quiz pkg found (mark: 1.0)
- Correctly publishing on /cmd_vel (mark: 4.0)
- ! Not subscribed to /kobuki/laser/scan (mark: 4.0)
- Avoid the obstacle correctly (mark: 7.0)
My code:
#! /usr/bin/python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
class Avoid_Wall:
def __init__(self):
# init node
rospy.init_node('avoid_wall')
# init subscriber and publisher
self.sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, self.callback)
self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
# optional: init Publisher msgs for easier access
self.move = Twist() # class Twist(genpy.Message)
# no need to init Subscriber msgs as you receive them in your `callback` function
# self.laser_scan = LaserScan() # class LaserScan(genpy.Message)
def callback(self, msg):
self.laser_scan = msg # class LaserScan(genpy.Message)
r = self.laser_scan.ranges # 720 len array; 0 is right side, 719 is left side
if r[180] > 1 and r[360] > 1 and r[540] > 1:
print('Moving forward!')
self.move.linear.x = .2
self.move.angular.z = 0
else:
print('Object in front! Turning Left!')
self.move.linear.x = .1
self.move.angular.z = .5
if r[0] < 1:
print('Object on right!')
if r[719] < 1:
print('Object on left!')
self.pub.publish(self.move)
def start(self):
rospy.spin() # loop: passes msg to callback in subscriber
if __name__=='__main__':
node = Avoid_Wall()
node.start()