NiklasRosenstein / myo-python

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

Get Myo data in the time #83

Open AdrianIbarra opened 4 years ago

AdrianIbarra commented 4 years ago

Hi, my name is Adrian.

I have a question about Myo.

In the Python program I can change the number of data with this instruction: listener = EmgCollector (512), but I want to know the time in which the data is acquired, how can i change that?

Thanks for you time.

NiklasRosenstein commented 4 years ago

Hi @AdrianIbarra ,

Not sure I can follow. EmgCollector is not part of the myo-python library. I'm assuming you're using the EmgCollector class form the EMG example script:

https://github.com/NiklasRosenstein/myo-python/blob/3b72991505d8b76fac4ab24b609b6334fe76bde1/examples/03_live_emg.py#L31-L52

As you can see it stores the event.timestamp along with the event.msg in a tuple every time EMG data is received, and you can get that data with EmgCollector.get_emg_data().

AdrianIbarra commented 4 years ago

Yes I am using this program:

from matplotlib import pyplot as plt
from collections import deque
from threading import Lock, Thread

import myo
import numpy as np

class EmgCollector(myo.DeviceListener):
  """
  Collects EMG data in a queue with *n* maximum number of elements.
  """

  def __init__(self, n):
    self.n = n
    self.lock = Lock()
    self.emg_data_queue = deque(maxlen=n)

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

  # myo.DeviceListener

  def on_connected(self, event):
    event.device.stream_emg(True)

  def on_emg(self, event):
    with self.lock:
      self.emg_data_queue.append((event.timestamp, event.emg))

class Plot(object):

  def __init__(self, listener):
    self.n = listener.n
    self.listener = listener
    self.fig = plt.figure()

    self.axes = [self.fig.add_subplot('81' + str(i)) for i in range(1, 9)]
    [(ax.set_ylim([-100, 100])) for ax in self.axes]
    self.graphs = [ax.plot(np.arange(self.n), np.zeros(self.n))[0] for ax in self.axes]
    plt.ion()

  def update_plot(self):
    emg_data = self.listener.get_emg_data()
    emg_data = np.array([x[1] for x in emg_data]).T
    for g, data in zip(self.graphs, emg_data):
      if len(data) < self.n:
        # Fill the left side with zeroes.
        data = np.concatenate([np.zeros(self.n - len(data)), data])
      g.set_ydata(data)
    plt.draw()

  def main(self):
    while True:
      self.update_plot()
      plt.pause(1.0 / 30)

def main():
  myo.init()
  hub = myo.Hub()
  listener = EmgCollector(1000)
  with hub.run_in_background(listener.on_event):
    Plot(listener).main()

if __name__ == '__main__':
  main()

But, I know that the frecuncy is of 200 Hz and i can change te number of adquired data, but i don't know abut time, how many data i im getting in a certain period of time (ms), how can i know that?

thanks again, i like you work.

NiklasRosenstein commented 4 years ago

Not sure I understand the question. 200Hz means you're getting 200 measurements per seconds.