You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using pyFirmata to control multiple DC motors connected to a single Arduino Nano.
Problem: I am unable to create multiple instances of the pyfirmata.Arduino object connecting to the same communication port. See example code below where I create a general class to control a DC motor. Each motor that needs to be controlled is connected to the same Arduino comm port but only thing that differs is the pin number at which the pwm and direction signals are outputted from. When I create two different instances of the MyMotor() class, I get the following error:
I think I figured out a temporary solution to my issue. However maybe this solution could be incorporated into pyfirmata. In summary, class attribute can be used to keep track of all Arduino comm ports already in use by pyfirmata.
importserialimportpyfirmataclassMyMotor():
open_boards= []
def__init__(self, port, pin_pwm, pin_direction):
# Setup the board try:
self.board=pyfirmata.Arduino(port)
self.open_boards.append(self.board)
print('Success connecting to board.')
exceptserial.serialutil.SerialException:
print('Problem connecting to Arduino. Comm port may already be in use. Checking and trying to fix...')
problem_fixed=Falseforiinself.open_boards:
ifi.sp.port==port:
self.board=iproblem_fixed=Truebreakifnotproblem_fixed:
print('Unable to fix the problem.')
returnelse:
print('Requested port was already in used. Success with connecting to board. ')
# Assign the pins self.pin_pwm=self.board.get_pin('d:{}:p'.format(pin_pwm))
self.pin_direction=self.board.get_pin('d:{}:o'.format(pin_direction))
defrun_motor(self, speed=0.5, direction=True):
self.pin_pwm.write(speed)
self.pin_direction.write(direction)
defstop_motor(self):
self.pin_pwm.write(0.0)
motor_1=MyMotor(port='COM5', pin_pwm=3, pin_direction=4)
motor_1.run_motor()
motor_2=MyMotor(port='COM5', pin_pwm=5, pin_direction=7)
motor_2.run_motor()
I am using pyFirmata to control multiple DC motors connected to a single Arduino Nano.
Problem: I am unable to create multiple instances of the pyfirmata.Arduino object connecting to the same communication port. See example code below where I create a general class to control a DC motor. Each motor that needs to be controlled is connected to the same Arduino comm port but only thing that differs is the pin number at which the pwm and direction signals are outputted from. When I create two different instances of the MyMotor() class, I get the following error:
Error:
Example code
The text was updated successfully, but these errors were encountered: