1
+ # Simplistic data recording
2
+ import time
3
+ import multiprocessing
4
+ import numpy as np
5
+ import pandas as pd
6
+
7
+ from pyomyo import Myo , emg_mode
8
+
9
+ def data_worker (mode = emg_mode .FILTERED , seconds = 15 , filepath = "data_gather.csv" ):
10
+ collect = True
11
+
12
+ # ------------ Myo Setup ---------------
13
+ m = Myo (mode = mode )
14
+ m .connect ()
15
+
16
+ myo_data = []
17
+
18
+ def add_to_queue (emg , movement ):
19
+ myo_data .append (emg )
20
+
21
+ m .add_emg_handler (add_to_queue )
22
+
23
+ def print_battery (bat ):
24
+ print ("Battery level:" , bat )
25
+
26
+ m .add_battery_handler (print_battery )
27
+
28
+ # Its go time
29
+ m .set_leds ([0 , 128 , 0 ], [0 , 128 , 0 ])
30
+ # Vibrate to know we connected okay
31
+ m .vibrate (1 )
32
+
33
+ print ("Data Worker started to collect" )
34
+ # Start collecing data.
35
+ start_time = time .time ()
36
+
37
+ while collect :
38
+ if (time .time () - start_time < seconds ):
39
+ m .run ()
40
+ else :
41
+ collect = False
42
+ collection_time = time .time () - start_time
43
+ print ("Finished collecting." )
44
+ print (f"Collection time: { collection_time } " )
45
+ print (len (myo_data ), "frames collected" )
46
+
47
+ # Add columns and save to df
48
+ myo_cols = ["Channel_1" , "Channel_2" , "Channel_3" , "Channel_4" , "Channel_5" , "Channel_6" , "Channel_7" , "Channel_8" ]
49
+ myo_df = pd .DataFrame (myo_data , columns = myo_cols )
50
+ myo_df .to_csv (filepath , index = False )
51
+ print ("CSV Saved at: " , filepath )
52
+
53
+ # -------- Main Program Loop -----------
54
+ if __name__ == '__main__' :
55
+ seconds = 10
56
+ file_name = str (seconds )+ "_test_emg.csv"
57
+ p = multiprocessing .Process (target = data_worker , args = (seconds , file_name ))
58
+ p .start ()
0 commit comments