Open 01baftb opened 3 years ago
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.
import serial
import pyfirmata
class MyMotor():
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.')
except serial.serialutil.SerialException:
print('Problem connecting to Arduino. Comm port may already be in use. Checking and trying to fix...')
problem_fixed = False
for i in self.open_boards:
if i.sp.port == port:
self.board = i
problem_fixed = True
break
if not problem_fixed:
print('Unable to fix the problem.')
return
else:
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))
def run_motor(self, speed=0.5, direction=True):
self.pin_pwm.write(speed)
self.pin_direction.write(direction)
def stop_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