Esempio n. 1
0
    def myo_main(self):
        atexit.register(set_normal_term)
        set_curses_term()

        m = MyoRaw(None)
        m.add_emg_handler(self.proc_emg)
        m.connect()

        m.add_arm_handler(lambda arm, xdir: print('arm', arm, 'xdir', xdir))
        m.add_pose_handler(lambda p: print('pose', p))

        # [EMG0, EMG1, EMG2, EMG3, EMG4, EMG5, EMG6, EMG7, TIME, STATUS]
        dim_data = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=float)
        data = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=float)

        try:
            t_start = time.time()
            while True:
                m.run(1)
                #stop vibration ever
                m.write_attr(0x19, b'\x03\x01\x00')
                emg, self._time = m.plot_emg(t_start)

                if kbhit():
                    key = getch()
                    if key == 'r':
                        #print("ROCK")
                        dim_data[-1:] = 1
                    elif key == 's':
                        #print("SCISSOR")
                        dim_data[-1:] = 2
                    elif key == 'p':
                        #print("PAPET")
                        dim_data[-1:] = 3
                    print("\r", end='')
                else:
                    print("\r{0}".format(dim_data), end="")
                    dim_data[-1:] = 0
                    continue

                #グラフは1次元
                dim_data[:9] = np.append(emg, self._time)
                if self._time > 1.:
                    if len(dim_data) == 10:
                        dim2_data = np.expand_dims(dim_data, axis=0)
                        data = np.append(data, dim2_data, axis=0)
                self.count += 1

        except KeyboardInterrupt:
            pass
        finally:
            m.disconnect()
            if self.save_csv:
                self.save_data(self.saving_path + ".csv", data[1:])
            if self.byn_np: np.save(self.saving_path, data[1:])
            if self.plt_graph: self.data_plot(data)
            print("")
Esempio n. 2
0
def collect_myo_data(detectedMyos):
    global dual

    if (detectedMyos is not None):

        # Closure function to record EMG data
        def proc_emg(timestamp, emg, moving, id):
            emgs = list([e / 2000. for e in emg])
            print(id, '--', timestamp, '--', emgs)

        # Set up first myo
        m = MyoRaw(detectedMyos[0], '1')
        m.add_emg_handler(proc_emg)
        m.connect()
        m.add_arm_handler(lambda arm, xdir: print('arm', arm, 'xdir', xdir))
        m.add_pose_handler(lambda p: print('pose', p))

        # Stop Myo from sleeping during data collection
        m.set_sleep_mode(1)
        m.vibrate(3)
        if (len(detectedMyos) == 2):
            m2 = MyoRaw(detectedMyos[1], '2')
            m2.add_emg_handler(proc_emg)
            m2.connect()

            # Stop myo from sleeping during data collection
            m2.set_sleep_mode(1)
            m2.vibrate(3)
            m2.add_arm_handler(
                lambda arm, xdir: print('arm', arm, 'xdir', xdir))
            m2.add_pose_handler(lambda p: print('pose', p))
            dual = True

        try:
            while True:
                if (dual == True):
                    m.run(1)
                    m2.run(1)
                else:
                    m.run(1)
        except KeyboardInterrupt:
            pass
        finally:
            m.disconnect()
            m2.disconnect()
            print()
Esempio n. 3
0
def collect_myo_data(detectedMyos): 
    global dual
    
    if(detectedMyos is not None):

        # Closure function to record EMG data
        def proc_emg(timestamp, emg, moving, id):
            emgs = list([e / 2000. for e in emg])
            print (id, '--', timestamp, '--', emgs)
        
        # Set up first myo
        m = MyoRaw(detectedMyos[0], '1')
        m.add_emg_handler(proc_emg)
        m.connect()
        m.add_arm_handler(lambda arm, xdir: print('arm', arm, 'xdir', xdir))
        m.add_pose_handler(lambda p: print('pose', p))

        # Stop Myo from sleeping during data collection
        m.set_sleep_mode(1)
        m.vibrate(3)
        if(len(detectedMyos) == 2):
            m2 = MyoRaw(detectedMyos[1], '2')
            m2.add_emg_handler(proc_emg)
            m2.connect()

            # Stop myo from sleeping during data collection
            m2.set_sleep_mode(1)
            m2.vibrate(3)
            m2.add_arm_handler(lambda arm, xdir: print('arm', arm, 'xdir', xdir))
            m2.add_pose_handler(lambda p: print('pose', p))
            dual = True

        try:
            while True:
                if(dual == True):
                    m.run(1)
                    m2.run(1)
                else:
                    m.run(1)
        except KeyboardInterrupt:
            pass
        finally:
            m.disconnect()
            m2.disconnect()
            print()
Esempio n. 4
0
def proc_battery(battery_level):
    print("Battery level: %d" % battery_level)
    if battery_level < 5:
        m.set_leds([255, 0, 0], [255, 0, 0])
    else:
        m.set_leds([128, 128, 255], [128, 128, 255])


m = MyoRaw(sys.argv[1] if len(sys.argv) >= 2 else None)
m.add_emg_handler(proc_emg)
m.add_battery_handler(proc_battery)
m.connect()

m.add_arm_handler(lambda arm, xdir: print('arm', arm, 'xdir', xdir))
m.add_pose_handler(lambda p: print('pose', p))
# m.add_imu_handler(lambda quat, acc, gyro: print('quaternion', quat))
m.sleep_mode(1)
m.set_leds([128, 128, 255], [128, 128, 255])  # purple logo and bar LEDs
m.vibrate(1)

try:
    while True:
        m.run(1)

        if HAVE_PYGAME:
            for ev in pygame.event.get():
                if ev.type == QUIT or (ev.type == KEYDOWN
                                       and ev.unicode == 'q'):
                    raise KeyboardInterrupt()
                # elif ev.type == KEYDOWN and ev.unicode == 'd':
Esempio n. 5
0
    SURFACE = pygame.display.set_mode((600, 700), 0, 32)
    pygame.display.set_caption("2048")
    myfont = pygame.font.SysFont("monospace", 25)
    scorefont = pygame.font.SysFont("monospace", 50)
    tileMatrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    undoMat = []
    rotations = 0
    mClassType = 0

    placeRandomTile()
    placeRandomTile()
    printMatrix()

    m = MyoRaw(sys.argv[1] if len(sys.argv) >= 2 else None)
    listener = PrintPoseListener(m)
    m.add_pose_handler(listener.proc_pose)

    m.connect()

    try:
        while True:
            m.run()
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if checkIfCanGo() == True:
                    if event.type == listener.eventId:
                        if listener.pose == "WAVE_OUT":
                            print("左")
                            rotations = 1