I am having a problem. I am building a kit for others to use, and want to avoid having them solder. I have a two wheeled vehicle powered by two dc motors, so I have two 18v7 dc motor controllers all wired up and ready to go. I am controlling them via a BeagleBone black using python. Here is the code:
#!/usr/bin/python
import serial
import time
class MotorControllerOne(object):
def __init__(self, port="/dev/ttyACM0"):
self.ser=serial.Serial(port = port)
def exitSafeStart(self):
command = chr(0x83)
self.ser.open()
self.ser.write(command)
self.ser.flush()
self.ser.close()
def setSpeed(self, speed):
if speed > 0:
channelByte = chr(0x85)
else:
channelByte = chr(0x86)
lowTargetByte = chr(speed & 0x1F)
highTargetByte = chr((speed >> 5) & 0x7F)
command = channelByte + lowTargetByte + highTargetByte
self.ser.open()
self.ser.write(command)
self.ser.flush()
self.ser.close()
def reset(self):
self.ser.reset()
def close(self):
self.ser.close()
class MotorControllerTwo(object):
def __init__(self, port="/dev/ttyACM1"):
self.ser=serial.Serial(port = port)
def exitSafeStart(self):
command = chr(0x83)
self.ser.open()
self.ser.write(command)
self.ser.flush()
self.ser.close()
def setSpeed(self, speed):
if speed > 0:
channelByte = chr(0x85)
else:
channelByte = chr(0x86)
lowTargetByte = chr(speed & 0x1F)
highTargetByte = chr((speed >> 5) & 0x7F)
command = channelByte + lowTargetByte + highTargetByte
self.ser.open()
self.ser.write(command)
self.ser.flush()
self.ser.close()
def reset(self):
self.ser.reset()
def close(self):
self.ser.close()
if __name__=="__main__":
motor1 = MotorControllerOne()
motor2 = MotorControllerTwo()
print 'Ports created'
motor1.exitSafeStart()
motor2.exitSafeStart()
print 'SafeStart'
motor1.setSpeed(int(2000))
motor2.setSpeed(int(-2000))
print 'setSpeed'
time.sleep(.5)
motor1.setSpeed(int(0))
motor2.setSpeed(int(0))
time.sleep(.5)
print 'at the end'
motor1.close()
motor2.close()
print 'done'
So here is the problem. This runs once fine, but when I try to run it again, it hangs. When I halt the program I get the following error:
Traceback (most recent call last):
File "./dcmotor.py", line 59, in <module>
motor1.exitSafeStart()
File "./dcmotor.py", line 11, in exitSafeStart
self.ser.flush()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 491, in flush
self.drainOutput()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 565, in drainOutput
termios.tcdrain(self.fd)
When I use the SmcCmd command to try and interrogate the unit, it give me a time out. If I change the code and only talk to one unit, then it runs multiple times just fine.
Any suggestions?