예제 #1
0
    def find_bluetooth_adapter(self, tty_port=None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError('Bluetooth adapter not found!')

        self.ble = BLE(tty_port)
예제 #2
0
    def find_bluetooth_adapter(self, tty_port=None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError('# Bluetooth adaptoru bulunamadi!\n#')

        self.ble = BLE(tty_port)
예제 #3
0
    def find_bluetooth_adapter(self, tty_port = None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError('Bluetooth adapter not found!')

        self.ble = BLE(tty_port)
예제 #4
0
def run():
    import signal

    QtCore.QCoreApplication.setOrganizationName("productize")
    QtCore.QCoreApplication.setOrganizationDomain("productize.be")
    QtCore.QCoreApplication.setApplicationName("BTLE tool")
    app = QtGui.QApplication(["BTLE tool"])

    signal.signal(signal.SIGINT, signal.SIG_DFL)

    baud_rate = 115200
    ble = BLE(baud_rate)
    ble.start()
    ct = ActivityThread(ble)
    ble.scan_response.connect(lambda x: print_scan_response(ble, x))
    ct.start()
    return app.exec_()
예제 #5
0
def run():
  import signal

  QtCore.QCoreApplication.setOrganizationName("productize")
  QtCore.QCoreApplication.setOrganizationDomain("productize.be")
  QtCore.QCoreApplication.setApplicationName("BTLE tool")
  app = QtGui.QApplication(["BTLE tool"])

  signal.signal(signal.SIGINT, signal.SIG_DFL)

  baud_rate = 115200
  ble = BLE(baud_rate)
  ble.start()
  ct = ActivityThread(ble)
  ble.scan_response.connect(lambda x: print_scan_response(ble, x))
  ct.start()
  return app.exec_()
예제 #6
0
    def __init__(self, app):
        super(MyWindow, self).__init__()
        self.setupUi(self)      #初始化GUI

        self.loop = quamash.QEventLoop(app)             # BLE协程循环
        self.ble = BLE(self)                            # 实例化BLE

        self.emit_thread = EmitThread(self)             # 实例化Emit对象
        self.emit_thread.signal.connect(self.image)     # 实时成像发射信号与槽函数连接

        self.save_thread = saveThread(self)             # 实例化save对象(连续测量10组实时数据)

        self.signal_slot()                              # 菜单栏的槽函数设置

        self.alg = 'svd'        # 默认调用灵敏度矩阵法
        self.disp = 'interp'    # 默认使用矩阵插值方式显示图像
        self.refedata = np.zeros(28)    # 空场参考值
        self.projdata = np.zeros(28)    # 历史测量值
        self.proj_d = np.zeros(28)      # 边界值差分(测量值-空场值)
        self.elem_data = np.zeros(576)  # 图像重建数值
        self.statusBar().showMessage('Ready')

        # 实例化MyFigure
        self.F = MyFigure(width=16, height=9, dpi=100)

        # ax1初始显示0
        self.cache1 = self.F.ax1.bar(range(28), np.zeros(28), color='deepskyblue')

        # 鼠标移动显示当前位置的图像重建数值大小
        plt.gcf().canvas.mpl_connect('motion_notify_event', self.motion_notify)

        # ax3初始显示为灰色
        self.cache3 = []
        for i in np.arange(8):
            for j in np.arange(i + 1, 8):
                cache, = self.F.ax3.plot([self.F.theta[i], self.F.theta[j]], [1, 1], color='dimgray')
                self.cache3.append(cache)

        plt.tight_layout()
        plt.ion()       #进入交互成像模式
        self.gridLayout.addWidget(self.F, 0, 1)     #将画布置于GUI界面中
예제 #7
0
class Myo(object):

    def __init__(self):
        self.ble = None
        self.connection = None

    def connect(self, tty_port = None):
        self.safely_disconnect()
        self.find_bluetooth_adapter(tty_port)

        address = self.find_myo_device()
        connection_packet = self.ble.connect(address)
        self.connection = multiord(connection_packet.payload)[-1]
        self.ble.wait_event(3, 0)
        print('Connected.')

        is_fw_valid = self.valid_firmware_version()

        if is_fw_valid:
            device_name = self.read_attribute(0x03)
            print('Device name: %s' % device_name.payload[5:])
            self.write_attribute(0x1d, b'\x01\x00')
            self.write_attribute(0x24, b'\x02\x00')
            self.initialize()
        else:
            raise ValueError('The firmware version must be v1.x or greater.')

    def find_bluetooth_adapter(self, tty_port = None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError('Bluetooth adapter not found!')

        self.ble = BLE(tty_port)

    def find_tty(self):
        for port in comports():
            print port
            if re.search(r'PID=2458:0*1', port[2]):
                return port[0]

        return None

    def run(self, timeout=None):
        if self.connection is not None:
            self.ble.receive_packet(timeout)
        else:
            raise ValueError('Myo device not paired.')

    def valid_firmware_version(self):
        firmware = self.read_attribute(0x17)
        _, _, _, _, major, minor, patch, build = unpack('BHBBHHHH', firmware.payload)

        print('Firmware version: %d.%d.%d.%d' % (major, minor, patch, build))

        return major > 0

    def add_listener(self, listener):
        if self.ble is not None:
            self.ble.add_listener(listener)
        else:
            print('Connect function must be called before adding a listener.')

    def vibrate(self, duration):
        cmd = b'\x03\x01'
        if duration == VibrationType.LONG:
            cmd = cmd + b'\x03'
        elif duration == VibrationType.MEDIUM:
            cmd = cmd + b'\x02'
        elif duration == VibrationType.SHORT:
            cmd = cmd + b'\x01'
        else:
            cmd = cmd + b'\x00'
            
        self.write_attribute(0x19, cmd)

    def initialize(self):
        self.write_attribute(0x28, b'\x01\x00')
        self.write_attribute(0x19, b'\x01\x03\x01\x01\x00')
        self.write_attribute(0x19, b'\x01\x03\x01\x01\x01')

    def find_myo_device(self):
        print('Find Myo device...')
        address = None
        self.ble.start_scan()
        while True:
            packet = self.ble.receive_packet()

            if packet.payload.endswith(b'\x06\x42\x48\x12\x4A\x7F\x2C\x48\x47\xB9\xDE\x04\xA9\x01\x00\x06\xD5'):
                address = list(multiord(packet.payload[2:8]))
                break

        self.ble.end_scan()
        return address

    def write_attribute(self, attribute, value):
        if self.connection is not None:
            self.ble.write_attribute(self.connection, attribute, value)

    def read_attribute(self, attribute):
        if self.connection is not None:
            return self.ble.read_attribute(self.connection, attribute)
        return None

    def safely_disconnect(self):
        if self.ble is not None:
            self.ble.end_scan()
            self.ble.disconnect(0)
            self.ble.disconnect(1)
            self.ble.disconnect(2)
            self.disconnect()

    def disconnect(self):
        if self.connection is not None:
            self.ble.disconnect(self.connection)
예제 #8
0
class Myo(object):
    def __init__(self):
        self.ble = None
        self.connection = None

    def connect(self, tty_port=None):
        self.safely_disconnect()
        self.find_bluetooth_adapter(tty_port)

        address = self.find_myo_device()
        try:
            connection_packet = self.ble.connect(address)
            #print(str(connection_packet))
            self.connection = multiord(connection_packet.payload)  #[-1]
            self.ble.wait_event(3, 0)
            print('Connected.')
        except Exception as e:
            self.safely_disconnect()
            raise

        is_fw_valid = self.valid_firmware_version()

        if is_fw_valid:
            device_name = self.read_attribute(0x03)
            print('Device name: %s' % device_name.payload[5:])
            self.write_attribute(0x1d, b'\x01\x00')
            self.write_attribute(0x24, b'\x02\x00')
            self.initialize()
        else:
            raise ValueError('The firmware version must be v1.x or greater.')

    def find_bluetooth_adapter(self, tty_port=None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError('Bluetooth adapter not found!')

        self.ble = BLE(tty_port)

    def find_tty(self):
        for port in comports():
            for p in port:
                print(p)
            if re.search(r'PID=2458:0*1', port[2]):
                return port[0]
            print('No')

        return None

    def run(self, timeout=None):
        if self.connection is not None:
            self.ble.receive_packet(timeout)
            return self.ble.getData()
        else:
            raise ValueError('Myo device not paired.')

    def valid_firmware_version(self):
        firmware = self.read_attribute(0x17)
        _, _, _, _, major, minor, patch, build = unpack(
            'BHBBHHHH', firmware.payload)

        print('Firmware version: %d.%d.%d.%d' % (major, minor, patch, build))

        return major > 0

    def add_listener(self, listener):
        if self.ble is not None:
            self.ble.add_listener(listener)
        else:
            print('Connect function must be called before adding a listener.')

    def vibrate(self, duration):
        cmd = b'\x03\x01'
        if duration == VibrationType.LONG:
            cmd = cmd + b'\x03'
        elif duration == VibrationType.MEDIUM:
            cmd = cmd + b'\x02'
        elif duration == VibrationType.SHORT:
            cmd = cmd + b'\x01'
        else:
            cmd = cmd + b'\x00'

        self.write_attribute(0x19, cmd)

    def initialize(self):
        self.write_attribute(0x28, b'\x01\x00')
        self.write_attribute(0x19, b'\x01\x03\x01\x01\x00')
        self.write_attribute(0x19, b'\x01\x03\x01\x01\x01')

    def find_myo_device(self):
        print('Find Myo device...')
        address = None
        self.ble.start_scan()
        print('Scanning...')
        while True:
            packet = self.ble.receive_packet()
            print(packet.payload.decode('utf-8'))
            packet.payload = packet.payload.decode('utf-8')
            if packet.payload.endswith(
                    '\x06\x42\x48\x12\x4A\x7F\x2C\x48\x47\xB9\xDE\x04\xA9\x01\x00\x06\xD5'
            ):
                #packet.payload = str.encode(packet.payload)
                print(packet.payload)
                address = list(multiord(packet.payload[2:8]))
                break
        print(address)
        self.ble.end_scan()
        return address

    def write_attribute(self, attribute, value):
        if self.connection is not None:
            self.ble.write_attribute(self.connection, attribute, value)

    def read_attribute(self, attribute):
        if self.connection is not None:
            return self.ble.read_attribute(self.connection, attribute)
        return None

    def safely_disconnect(self):
        if self.ble is not None:
            self.ble.end_scan()
            self.ble.disconnect(0)
            self.ble.disconnect(1)
            self.ble.disconnect(2)
            self.disconnect()

    def disconnect(self):
        if self.connection is not None:
            self.ble.disconnect(self.connection)
예제 #9
0
class Myo(object):

    # Initializes ble object and connection
    def __init__(self):
        self.ble = None
        self.connection = None

    # Disconnects and then finds bluetooth usb adapter && myo device
    def connect(self, tty_port=None):
        self.safely_disconnect()
        # Finds bluetooth adapter and myo device address
        self.find_bluetooth_adapter(tty_port)
        address = self.find_myo_device()

        print("received address from function")
        # Connects ble to myo
        connection_packet = self.ble.connect(address)
        self.connection = multiord(connection_packet.payload)[-1]
        self.ble.wait_event(3, 0)
        print("Connected.")

        # Validate myo object
        is_fw_valid = self.valid_firmware_version()

        if is_fw_valid:
            device_name = self.read_attribute(0x03)
            print("Device name: %s" % device_name.payload[5:])
            self.write_attribute(0x1D, b"\x01\x00")
            self.write_attribute(0x24, b"\x02\x00")
            self.initialize()
        else:
            raise ValueError("The firmware version must be v1.x or greater.")

    # Find Bluetooth adapter
    def find_bluetooth_adapter(self, tty_port=None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError("Bluetooth adapter not found!")

        self.ble = BLE(tty_port)

    # Helper method to find tty port
    def find_tty(self):
        for port in comports():
            if re.search(r"PID=2458:0*1", port[2]):
                return port[0]

        return None

    # Runs and recieves packet while condition exists
    def run(self, timeout=None):
        if self.connection is not None:
            self.ble.receive_packet(timeout)
        else:
            raise ValueError("Myo device not paired.")

    # Helper method verify firmware version
    def valid_firmware_version(self):
        # Read firmware attribute
        firmware = self.read_attribute(0x17)
        _, _, _, _, major, minor, patch, build = unpack("BHBBHHHH", firmware.payload)

        print("Firmware version: %d.%d.%d.%d" % (major, minor, patch, build))

        return major > 0

    # Adds device listener
    def add_listener(self, listener):
        if self.ble is not None:
            self.ble.add_listener(listener)
        else:
            print("Connect function must be called before adding a listener.")

    # Vibrates myo
    def vibrate(self, duration):
        # initial vibrate UUID
        cmd = b"\x03\x01"
        # Concatenate vibrate UUID base with Vibration length
        if duration == VibrationType.LONG:
            cmd = cmd + b"\x03"
        elif duration == VibrationType.MEDIUM:
            cmd = cmd + b"\x02"
        elif duration == VibrationType.SHORT:
            cmd = cmd + b"\x01"
        else:
            cmd = cmd + b"\x00"
        # Write vibration attribute
        self.write_attribute(0x19, cmd)

    # Initialize myo object
    def initialize(self):
        self.write_attribute(0x28, b"\x01\x00")
        self.write_attribute(0x19, b"\x01\x03\x01\x01\x00")
        self.write_attribute(0x19, b"\x01\x03\x01\x01\x01")

    # Finds myo by connecting with ble object
    def find_myo_device(self):
        print("Find Myo device...")
        address = None
        print("before ble startscan")
        self.ble.start_scan()
        print("after ble startscan")
        while True:
            packet = self.ble.receive_packet()
            print("inside find myo loop")
            if packet.payload.endswith(b"\x06\x42\x48\x12\x4A\x7F\x2C\x48\x47\xB9\xDE\x04\xA9\x01\x00\x06\xD5"):
                print("package payload found!")
                address = list(multiord(packet.payload[2:8]))
                print("address found")
                break

        self.ble.end_scan()
        print("returning address")
        return address

    # Write attributes to myo with attributes and value
    def write_attribute(self, attribute, value):
        if self.connection is not None:
            self.ble.write_attribute(self.connection, attribute, value)

    # Read attribute from myo
    def read_attribute(self, attribute):
        if self.connection is not None:
            return self.ble.read_attribute(self.connection, attribute)
        return None

    # Graceful disconnect by removing ble object and disconnecting
    def safely_disconnect(self):
        if self.ble is not None:
            self.ble.end_scan()
            self.ble.disconnect(0)
            self.ble.disconnect(1)
            self.ble.disconnect(2)
            self.disconnect()

    # Disconnects current myo object
    def disconnect(self):
        if self.connection is not None:
            self.ble.disconnect(self.connection)
예제 #10
0
class Myo(object):
    def __init__(self):
        self.ble = None
        self.connection = None

    def connect(self, tty_port=None):
        self.safely_disconnect()
        self.find_bluetooth_adapter(tty_port)

        address = self.find_myo_device()
        connection_packet = self.ble.connect(address)
        self.connection = multiord(connection_packet.payload)[-1]
        self.ble.wait_event(3, 0)
        print('# BAGLANTI YAPILDI\n#')

        is_fw_valid = self.valid_firmware_version()

        if is_fw_valid:
            device_name = self.read_attribute(0x03)
            print('# Cihaz adi : %s\n#' % device_name.payload[5:])
            self.write_attribute(0x1d, b'\x01\x00')
            self.write_attribute(0x24, b'\x02\x00')
            self.initialize()
        else:
            raise ValueError(
                '# Myo Armband yazilimi 1.x veya uzeri olmali.\n#')

    def find_bluetooth_adapter(self, tty_port=None):
        if tty_port is None:
            tty_port = self.find_tty()
        if tty_port is None:
            raise ValueError('# Bluetooth adaptoru bulunamadi!\n#')

        self.ble = BLE(tty_port)

    def find_tty(self):
        for port in comports():
            if re.search(r'PID=2458:0*1', port[2]):
                return port[0]

        return None

    def run(self, timeout=None):
        if self.connection is not None:
            self.ble.receive_packet(timeout)
        else:
            raise ValueError('# Myo cihazi eslesemedi.\n')

    def valid_firmware_version(self):
        firmware = self.read_attribute(0x17)
        _, _, _, _, major, minor, patch, build = unpack(
            'BHBBHHHH', firmware.payload)

        print('# Yazilim surumu: %d.%d.%d.%d\n#' %
              (major, minor, patch, build))

        return major > 0

    def add_listener(self, listener):
        if self.ble is not None:
            self.ble.add_listener(listener)
        else:
            print(
                '# Connect function must be called before adding a listener.\n'
            )

    def vibrate(self, duration):
        cmd = b'\x03\x01'
        if duration == VibrationType.LONG:
            cmd = cmd + b'\x03'
        elif duration == VibrationType.MEDIUM:
            cmd = cmd + b'\x02'
        elif duration == VibrationType.SHORT:
            cmd = cmd + b'\x01'
        else:
            cmd = cmd + b'\x00'

        self.write_attribute(0x19, cmd)

    def initialize(self):
        self.write_attribute(0x28, b'\x01\x00')
        self.write_attribute(0x19, b'\x01\x03\x01\x01\x00')
        self.write_attribute(0x19, b'\x01\x03\x01\x01\x01')

    def find_myo_device(self):
        print('# Myo cihazi araniyor...\n#')
        address = None
        self.ble.start_scan()
        while True:
            packet = self.ble.receive_packet()

            if packet.payload.endswith(
                    b'\x06\x42\x48\x12\x4A\x7F\x2C\x48\x47\xB9\xDE\x04\xA9\x01\x00\x06\xD5'
            ):
                address = list(multiord(packet.payload[2:8]))
                break

        self.ble.end_scan()
        return address

    def write_attribute(self, attribute, value):
        if self.connection is not None:
            self.ble.write_attribute(self.connection, attribute, value)

    def read_attribute(self, attribute):
        if self.connection is not None:
            return self.ble.read_attribute(self.connection, attribute)
        return None

    def safely_disconnect(self):
        if self.ble is not None:
            self.ble.end_scan()
            self.ble.disconnect(0)
            self.ble.disconnect(1)
            self.ble.disconnect(2)
            self.disconnect()

    def disconnect(self):
        if self.connection is not None:
            self.ble.disconnect(self.connection)
예제 #11
0
env = Environment((grid_min_coord_x, grid_min_coord_y), (grid_max_coord_x,grid_max_coord_y), (agent_coord_x,agent_coord_y), (goal_coord_x,goal_coord_y))

flag = input("do you want to enter any obstacles (Y/N)? ")

while flag == "Y":
	
	bottom_left_coord_x = int(input("\t enter bottom left x coordinate: "))
	bottom_left_coord_y = int(input("\t enter bottom left y coordinate: "))
	top_right_coord_x = int(input("\t enter top right x coordinate: "))
	top_right_coord_y = int(input("\t enter top right y coordinate: "))
	
	env.add_obstacle((bottom_left_coord_x,bottom_left_coord_y), (top_right_coord_x,top_right_coord_y))

	flag = input("do you want to enter more obstacles (Y/N)? ")

agent = Agent()

ble1 = BLE(4, -73, (0,0))
ble2 = BLE(4, -73, (100,0))
ble3 = BLE(4, -73, (50,50))

wp = WaypointPlanner(env, agent)
plan = wp.plan()

print(plan)

wp.visualise(plan)