If you have understood how the requirement, distance-sensor.py, publisher script works, then the following subscriber script should be pretty straightforward to follow:
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Range
def callback(msg):
print msg.data
rospy.loginfo(rospy.get_caller_id() + 'GoPiGo3 measures distance %s mm', msg.data*1000)
rospy.init_node('distance_sensor_subscriber')
sub = rospy.Subscriber('distance_sensor/distance', Range, callback)
rospy.spin()
This snippet corresponds to the distance-sensor_subscriber.py file in the ./pkg_mygopigo/src folder of the code for this chapter. The main difference in the subscriber script is that, since we are listening to a topic, we do not need to specify a rate of execution. We simply loop forever with the following line:
rospy...