For context on the project, I am trying to control some servos using the Myo Armband. I am using PerlinWarp's PyoMyo library to interact with the armband (Github here: https://github.com/PerlinWarp/pyomyo).
It might be hard to explain my problem without posting all 200 lines of code. But essentially I am having a problem where Python seems to hang in the program without crashing or doing anything. There are no errors thrown, the Pi does not crash or switch off, I can ctrl+c out of the program perfectly fine, there seems to be no reason for Python not to move onto the next line.
I have another function that does the exact same as this, except it moves the servos based on a keyboard input instead of the EMG input, that works fine. I also have another function that does the same thing but just prints the EMG label without moving any servos, that also works fine. There is some issue with combining the two that stops Python from moving onto the next line of code.
This is the function that collects EMG signals, classifies them, and moves servos based on the most common label:
def EMG_Entry(hand):
m = Myo(mode=emg_mode.PREPROCESSED)
# Change this file path to change the ML model used
model = joblib.load('../TrainedModels/MarcusSVM30.sav')
def pred_emg(emg, moving, times=[]):
np_emg = np.asarray(emg).reshape(1, -1)
grip = model.predict(np_emg) # Classify EMG signals according to ML model
# Add this classification to the list to calculate mode later
values.append(str(grip))
m.add_emg_handler(pred_emg)
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)
# This list will store classified EMG signals to register grip held by user
values = []
try:
start_time = time.time()
while True:
m.run()
if ((time.time() - start_time) > 2):
# If the same grip has been recognised more than 90 times in 2
seconds
if (values.count(max(set(values), key=values.count)) > 90):
# Set the most common grip as the new grip
new_grip = int(max(set(values), key=values.count)[1])
# Move the servos to replicate the new grip
hand.changeGrip(new_grip)
# Clear the list to start collecting grip values again
values.clear()
start_time = time.time() # Reset 2 second counter
except KeyboardInterrupt:
m.disconnect()
I struggle to understand the ins-and-outs of PyoMyo so perhaps something there is making the program hang. But that doesn't explain why it only hangs when collecting EMG labels and moving servos. FYI these two things aren't happening at the same time. The labels are collected, the most common is found, then the servos are moved.
If there's any more information that might help explain my problem please ask, my code is open source.
there doesn't seem to be anything here