MrYsLab / telemetrix

A user-extensible replacement for StandardFirmata. All without the complexity of Firmata!
GNU Affero General Public License v3.0
68 stars 26 forks source link

Bug fix where each stepper motor references the same info list #76

Closed AaronYoung5 closed 8 months ago

AaronYoung5 commented 8 months ago

Hi, thanks for making this package, I've found it very useful! (I think) I've run into a bug with the stepper_info_list. TL;DR the stepper_info_list is not copied between motor instances, so when you call a method that requires a callback, it will always overwrite all other instances. As in, all stepper motor instances share the same info list.

This code snippet reproduces the issue that I see:

from telemetrix import telemetrix
import time

board = telemetrix.Telemetrix()

def create_motor(pin1, pin2, enable):
    motor = board.set_pin_mode_stepper(pin1=pin1, pin2=pin2)
    board.stepper_set_enable_pin(motor, enable)
    board.stepper_set_3_pins_inverted(motor, enable=True)
    return motor

motorX = create_motor(2, 3, 8)
motorY = create_motor(4, 5, 8)

def current_position_callback_X(_):
    print('X')

def current_position_callback_Y(_):
    print('Y')

board.stepper_get_current_position(motorX, current_position_callback_X)
board.stepper_get_current_position(motorY, current_position_callback_Y)

time.sleep(1)

Which outputs:

Y
Y

Where the indented output (which is the result you get with the changes in this PR) is:

X
Y

I've created this PR assuming this is a bug, but maybe I'm using the library wrong. Please let me know if that's the case. Thanks again!

MrYsLab commented 8 months ago

Great catch! Thanks for the pull request. I think no one found this before because they use a single callback for all motors and then key off the motor ID.

I am going to release it to PyPi. I will add a comment here when it is available.

MrYsLab commented 8 months ago

Thanks once again. The release is available in PyPi.