def get_distance(self, uid): try: dus = BrickletDistanceUS(uid, self.ipcon) return dus.get_distance_value() except Exception: log.warn(uid + " not connected") return -1
def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # Enumeration for Distance US if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER: self.dus = BrickletDistanceUS(uid, self.ipcon) self.dus.register_callback(self.dus.CALLBACK_DISTANCE, self.cb_distance) self.dus.set_distance_callback_period(10000)
class dist_us: def __init__(self): self.dus = None # Create IP Connection self.ipcon = IPConnection() # Register IP Connection callbacks self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) # Connect to brickd, will trigger cb_connected self.ipcon.connect(constants.ownIP, PORT) #self.ipcon.enumerate() def cb_distance(self, distance): dicti = {} dicti['value'] = str(distance) dicti['name'] = str(self.dus.get_identity()[0]) + "_" + str(self.dus.get_identity()[5]) mySocket.sendto(str(dicti),(constants.server1,constants.broadPort)) mySocket.sendto(str(dicti),(constants.server1,constants.broadPort)) # Callback handles device connections and configures possibly lost # configuration of lcd and temperature callbacks, backlight etc. def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # Enumeration for Distance US if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER: self.dus = BrickletDistanceUS(uid, self.ipcon) self.dus.register_callback(self.dus.CALLBACK_DISTANCE, self.cb_distance) self.dus.set_distance_callback_period(10000) def cb_connected(self, connected_reason): # Enumerate devices again. If we reconnected, the Bricks/Bricklets # may have been offline and the configuration may be lost. # In this case we don't care for the reason of the connection self.ipcon.enumerate()
class dist_us: def __init__(self): self.dus = None # Create IP Connection self.ipcon = IPConnection() # Register IP Connection callbacks self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) # Connect to brickd, will trigger cb_connected self.ipcon.connect(constants.ownIP, PORT) #self.ipcon.enumerate() def cb_distance(self, distance): dicti = {} dicti['value'] = str(distance) dicti['name'] = str(self.dus.get_identity()[0]) + "_" + str( self.dus.get_identity()[5]) mySocket.sendto(str(dicti), (constants.server1, constants.broadPort)) mySocket.sendto(str(dicti), (constants.server1, constants.broadPort)) # Callback handles device connections and configures possibly lost # configuration of lcd and temperature callbacks, backlight etc. def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # Enumeration for Distance US if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER: self.dus = BrickletDistanceUS(uid, self.ipcon) self.dus.register_callback(self.dus.CALLBACK_DISTANCE, self.cb_distance) self.dus.set_distance_callback_period(10000) def cb_connected(self, connected_reason): # Enumerate devices again. If we reconnected, the Bricks/Bricklets # may have been offline and the configuration may be lost. # In this case we don't care for the reason of the connection self.ipcon.enumerate()
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get current distance value distance = dus.get_distance_value() print("Distance Value: " + str(distance)) input("Press key to exit\n") # Use raw_input() in Python 2 ipcon.disconnect()
HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS # Callback function for distance value callback def cb_distance(distance): print("Distance Value: " + str(distance)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Register distance value callback to function cb_distance dus.register_callback(dus.CALLBACK_DISTANCE, cb_distance) # Set period for distance value callback to 0.2s (200ms) # Note: The distance value callback is only called every 0.2 seconds # if the distance value has changed since the last call! dus.set_distance_callback_period(200) input("Press key to exit\n") # Use raw_input() in Python 2 ipcon.disconnect()
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get current distance value distance = dus.get_distance_value() print("Distance Value: " + str(distance)) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS # Callback function for distance value reached callback def cb_distance_reached(distance): print("Distance Value: " + str(distance)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get threshold callbacks with a debounce time of 10 seconds (10000ms) dus.set_debounce_period(10000) # Register distance value reached callback to function cb_distance_reached dus.register_callback(dus.CALLBACK_DISTANCE_REACHED, cb_distance_reached) # Configure threshold for distance value "smaller than 200" dus.set_distance_callback_threshold("<", 200, 0) input("Press key to exit\n") # Use raw_input() in Python 2 ipcon.disconnect()
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS # Callback function for distance value callback def cb_distance(distance): print("Distance Value: " + str(distance)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Register distance value callback to function cb_distance dus.register_callback(dus.CALLBACK_DISTANCE, cb_distance) # Set period for distance value callback to 0.2s (200ms) # Note: The distance value callback is only called every 0.2 seconds # if the distance value has changed since the last call! dus.set_distance_callback_period(200) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
return dist # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (HOST_S, PORT_S) print >> sys.stderr, 'connecting to %s port %s' % server_address sock.connect(server_address) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection master = BrickMaster(UID, ipcon) # Create device object a = BrickletAccelerometer(UIDa, ipcon) #dir = BrickletDistanceIR(UIDd, ipcon) dir = BrickletDistanceUS(UIDu, ipcon) ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Display master status and LED blink master.enable_status_led() print(master.get_identity()) cc = 0 # Get current stack voltage (unit is mV) try: while True: cc = cc + 1 stack_voltage = master.get_stack_voltage() #print("Stack Voltage: " + str(stack_voltage/1000.0) + " V")
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS # Callback function for distance value reached callback def cb_distance_reached(distance): print("Distance Value: " + str(distance)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get threshold callbacks with a debounce time of 10 seconds (10000ms) dus.set_debounce_period(10000) # Register distance value reached callback to function cb_distance_reached dus.register_callback(dus.CALLBACK_DISTANCE_REACHED, cb_distance_reached) # Configure threshold for distance value "smaller than 200" dus.set_distance_callback_threshold("<", 200, 0) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
def create_instance(self, uid, ipcon): return BrickletDistanceUS(uid, ipcon)