NiklasRosenstein / myo-python

Python bindings for the Myo SDK
Other
259 stars 102 forks source link

Myo device listener doe snot respond #65

Closed ziyadAbouelenin closed 6 years ago

ziyadAbouelenin commented 6 years ago
Traceback (most recent call last):
  File "C:\Myoband\EMG_DATA_FROM_MYO_ARMBAND.py", line 129, in <module>
    hub.run(200, listener)
  File "C:\Users\MCL\AppData\Local\Programs\Python\Python36\lib\site-packages\myo\_ffi.py", line 527, in run
    raise TypeError('expected callable or DeviceListener')
TypeError: expected callable or DeviceListener
import csv
import pandas as pd
import math
import myo
import threading
import keyboard
import collections
print("please enter the (pose) you are performing)  : ")
e=input()
n=e+".csv"
s=True

print("Got u! keep pressing (alt) while you performe this gesture")
print("when you are done press (shift) to write the filtered data  in csv files")

with open(n, 'a',newline='') as f:
    writer=csv.writer(f)
    writer.writerow(['time','emg1','emg2','emg3'
                     ,'emg4','emg5','emg6','emg7','emg8',])
def EMGfiles(emg,t):
    with open(n, 'a',newline='') as f:
        writer=csv.writer(f)
        writer.writerow([t,emg[0],emg[1],emg[2],
                         emg[3],emg[4],emg[5],emg[6],emg[7]])

#applying root mean square to data before writting it to csv:

print("press esc to exit")

class MyListener(myo.DeviceListener):

  def __init__(self,queue_size=8):
    self.lock = threading.Lock()
    self.emg_data_queue = collections.deque(maxlen=queue_size)
    self.gyro_data_queue = collections.deque(maxlen=3)
    self.ori_data_queue = collections.deque(maxlen=4)
    self.acc_data_queue = collections.deque(maxlen=3)

  def on_connect(self, device, timestamp, firmware_version):
    device.set_stream_emg(myo.StreamEmg.enabled)

  def on_emg_data(self, device, timestamp, emg_data):
    with self.lock:
      for x in range(0,8):
        self.emg_data_queue.append((emg_data[x]))

  def get_emg_data(self):
    with self.lock:
      return list(self.emg_data_queue)

  def on_gyroscope_data(self, device, timestamp, gyroscope):
          with self.lock:
              self.gyro_data_queue.append((gyroscope.x))
              self.gyro_data_queue.append((gyroscope.y))
              self.gyro_data_queue.append((gyroscope.z))

  def on_orientation_data(self, myo, timestamp, quat):
      with self.lock:
          self.ori_data_queue.append((quat.x))
          self.ori_data_queue.append((quat.y))
          self.ori_data_queue.append((quat.z))
          self.ori_data_queue.append((quat.w)) 

  def get_gyro_data(self):
      with self.lock:
            return list(self.gyro_data_queue)

  def get_ori_data(self):
      with self.lock:
            return list(self.ori_data_queue)      
  def on_accelerometor_data(self, device, timestamp, acceleration):
      with self.lock:
          self.acc_data_queue.append((acceleration.x))
          self.acc_data_queue.append((acceleration.y))
          self.acc_data_queue.append((acceleration.z))

  def get_acc_data(self):
      with self.lock:
            return list(self.acc_data_queue)

a=[]
t=[]
f=0
myo.init(r'C:\Myoband\myo-sdk-win-0.9.0\bin\myo64.dll')
hub = myo.Hub()
try:
  listener = MyListener()
  hub.run(200, listener)

  while True:                                

      if keyboard.is_pressed('alt'):
          EMGfiles(listener.get_emg_data(),f)
          a.append(listener.get_emg_data())
          t.append(f)
          f+=1

      if keyboard.is_pressed('esc'):
          break
      time.sleep(0.0005 )#50hz EMG(0.016) 200 hz (0.0005)
finally:
        hub.shutdown()

the same code run on my laptop seamlessly but for some reason when i try it on another pc using the same steps it does not work. Please Help

NiklasRosenstein commented 6 years ago

Your pc runs the newer Myo-Python version, you should update the version on your laptop and your code.

The specific error is because the Hub.run() method now expects the arguments in the order listener, duration_ms instead of the other way round.

ziyadAbouelenin commented 6 years ago

So should I replace it with Hub.run(listener.duration_ms)? Sorry but I am confused a little bit. Thats it?

NiklasRosenstein commented 6 years ago

The names of the methods have been updated to reflect the actual event name, too (eg. on_connect is now on_connected and on_emg_data is now on_emg).

Just add on_ to the event name that you want to catch and implement it as a method.

https://github.com/NiklasRosenstein/myo-python/blob/1c548d19d1d28d1e2bd15cb82995a8d140f684e3/myo/_ffi.py#L108-L123

Also the data is no longer passed as parameters but must be read from the event object.

Check out the live_emg example.

On a side note, you probably want to flush the emg data queue in the listener after you wrote the data to a file, and maybe also put the hub.run() call inside the while loop.

You also don't need to call hub.shutdown() in the new version.

ziyadAbouelenin commented 6 years ago

Ok thanks I am really grateful for your work, because of it I finished my bachelor project where I designed an EMG-controlled prosthetic hand. I will read the new update and modify my code. Thank you very much.