Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Myo data in the time #83

Open
AdrianIbarra opened this issue Aug 14, 2020 · 3 comments
Open

Get Myo data in the time #83

AdrianIbarra opened this issue Aug 14, 2020 · 3 comments
Labels

Comments

@AdrianIbarra
Copy link

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
Copy link
Owner

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:

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))

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
Copy link
Author

AdrianIbarra commented Aug 14, 2020

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants