def main(): # instantiate the distance object #sensor = DistanceSensor() my_sensor = EasyDistanceSensor() #sensor.start_continuous() rospy.init_node("distance_sensor") pub_distance = rospy.Publisher("~distance", Range, queue_size=10) msg_range = Range() msg_range.header.frame_id = "distance" msg_range.radiation_type = Range.INFRARED msg_range.min_range = 0.02 msg_range.max_range = 3.0 rate = rospy.Rate(rospy.get_param('~hz', 1)) while not rospy.is_shutdown(): # read distance in meters read_distance = my_sensor.read() / 100.0 msg_range.range = read_distance # Add timestamp msg_range.header.stamp = rospy.Time.now() # Print current distance print msg_range.range * 1000, " mm" # Publish distance message pub_distance.publish(msg_range) rate.sleep()
class MrA(gopigo3.GoPiGo3): def __init__(self): super().__init__() self.imu = InertialMeasurementUnit(bus="GPG3_AD1") self.ds = EasyDistanceSensor() def dist(self): num = self.ds.read() print("distance from object: {} mm".format(num)) return num def angle(self): num = self.imu.read_euler()[0] print("currently facing angle: {} deg".format(num)) return num
#!/usr/bin/env python # # https://www.dexterindustries.com # # Copyright (c) 2017 Dexter Industries # Released under the MIT license (http://choosealicense.com/licenses/mit/). # For more information see https://github.com/DexterInd/DI_Sensors/blob/master/LICENSE.md # # Python example program for the Dexter Industries Distance Sensor from __future__ import print_function from __future__ import division # import the modules from di_sensors.easy_distance_sensor import EasyDistanceSensor from time import sleep # instantiate the distance object my_sensor = EasyDistanceSensor() # and read the sensor iteratively while True: read_distance = my_sensor.read() print("distance from object: {} mm".format(read_distance)) sleep(0.1)