我有一台带有SSC-32伺服控制器的四足机器人。伺服控制器有一个串口,所以我购买了USB转串口电缆给它发送命令。因为我最熟悉Python,所以我决定使用PySerial来打开和关闭串口。我在我的MAC上运行Linux VM。
显示的错误消息包含在下面。还包括我的代码。
正如您将看到的,它正在访问端口时遇到困难。但是,当我拔出并重新插入端口时,错误消息消失,我能够成功发送命令。下次我尝试运行代码时,会出现错误消息。我怀疑端口难以关闭,因为当我拔下电缆时,我会手动关闭它,因此代码可以工作。
要尝试解决问题,我尝试删除超时并更改超时时间。此外,我添加了一个while循环,确保端口关闭。这些初步解决方案都没有奏效。
如果您有任何想法,请告诉我。
提前致谢,
克里斯托弗
错误讯息:
Traceback (most recent call last):
File "BrainBotTestCode_2.py", line 5, in <module>
sp = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)
File "/home/christophercaligiuri/.local/lib/python2.7/site- packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/home/christophercaligiuri/.local/lib/python2.7/site- packages/serial/serialposix.py", line 286, in open
self._update_dtr_state()
File "/home/christophercaligiuri/.local/lib/python2.7/site- packages/serial/serialposix.py", line 634, in _update_dtr_state
fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
IOError: [Errno 5] Input/output error
可以找到here或更低的代码:
import serial
import time
#open a serial port
sp = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)
#sets up the move forward module
def moveForward(speed, distance):
#commands to move forward with the parameters
numberOfTimes = (distance/5)*2
for x in range (0,numberOfTimes):
#lifts up first leg
sp.write(("#25 P1600 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#24 P1200 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#25 P2215 T%s\r" %speed).encode())
time.sleep(.5)
#lifts up second leg
sp.write(("#1 P1535 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#0 P1950 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#1 P2150 T%i\r" %speed).encode())
time.sleep(.5)
#lifts up third leg
sp.write(("#17 P2500 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#16 P1000 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#17 P1600 T%i\r" %speed).encode())
time.sleep(.5)
time.sleep(.5)
#lifts up forth leg
sp.write(("#9 P1600 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#8 P1900 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#9 P2250 T%i\r" %speed).encode())
time.sleep(.5)
time.sleep(.5)
#moves body forward
sp.write(("#0 P1425 #8 P1500 #16 P1500 #24 P1600 #26 P1250 T400\r").encode())
time.sleep(2)
#sets up the move backward module
def moveBackward(speed, distance):
#commands to move backward with the parameters
numberOfTimes = distance
#!!run for x in range command when distance is refined!!
#lifts up first leg
sp.write(("#25 P1600 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#24 P2200 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#25 P2215 T%s\r" %speed).encode())
time.sleep(.5)
#lifts up second leg
sp.write(("#1 P1535 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#0 P1000 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#1 P2150 T%i\r" %speed).encode())
time.sleep(.5)
#lifts up third leg
sp.write(("#17 P2500 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#16 P1900 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#17 P1600 T%i\r" %speed).encode())
time.sleep(.5)
time.sleep(.5)
#lifts up forth leg
sp.write(("#9 P1600 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#8 P900 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#9 P2250 T%i\r" %speed).encode())
time.sleep(.5)
time.sleep(.5)
#moves body
sp.write(("#0 P1425 #8 P1500 #16 P1500 #24 P1600 #26 P1250 T400\r").encode())
time.sleep(2)
#sets up the move backward module
def turn(speed, degrees):
#commands to move backward with the parameters
numberOfTimes = degrees
#!!run for x in range command when angle is refined!!
#lifts up first leg
sp.write(("#25 P1600 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#24 P2200 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#25 P2215 T%s\r" %speed).encode())
time.sleep(.5)
#lifts up second leg
sp.write(("#1 P1535 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#0 P1000 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#1 P2150 T%i\r" %speed).encode())
time.sleep(.5)
#lifts up third leg
sp.write(("#17 P2500 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#16 P1900 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#17 P1600 T%i\r" %speed).encode())
time.sleep(.5)
time.sleep(.5)
#lifts up forth leg
sp.write(("#9 P1600 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#8 P900 T%i\r" %speed).encode())
time.sleep(.5)
sp.write(("#9 P2250 T%i\r" %speed).encode())
time.sleep(.5)
time.sleep(.5)
#moves body
sp.write(("#0 P1425 #8 P1500 #16 P1500 #24 P1600 #26 P1250 T400\r").encode())
time.sleep(2)
#the defult, rest position
def defultPosition():
# set the servos to the inital position
sp.write("#0 P1425 #1 P2150 #2 P1625 #8 P1500 #9 P2300 #10 P1500 #16 P1500 #17 P1600 #18 P1475 #24 P1600 #25 P2215 #26 P1450 T.5\r".encode())
#runs all the modules and gets user input
while True:
defultPosition()
command = raw_input("Enter move forward, move backward, turn or cancel: ")
defultPosition()
if command == "cancel":
break
if command == ("move forward") or (command == "move backward"):
speedInput = input("Enter the desired speed: ")
distanceInput = input("Enter the number of inches you wish the robot to move (must be a factor of 5): ")
if command == "turn":
degrees = input("Enter the number of degrees for the robot to move: ")
print ("\nINPUTED COMMAND: %s \n" % command)
if command == "move forward":
#run the moveForward module
print "Initiating command\n"
moveForward(speedInput, distanceInput)
print "Finsihed command; restarting and waiting for another input \n"
if command == "move backward":
#run the moveBackward module
print "Initiating command\n"
moveBackward(speedInput, distanceInput)
print "Finished command; restarting and waiting for another input \n"
if command == "turn":
#runs the turn module
print "Initiating command\n"
turn(speedInput, degrees)
print "Finished command; restarting and waiting for another input \n"
#close serial port
sp.close()
isClosed = sp.is_open
while isClosed == True:
sp.close
isClosed = sp.is_open
print isClosed
答案 0 :(得分:0)
我确定必须使用以下命令配置端口:sudo chmod 0666 / dev / ttyUSB0。这能够解决我遇到的问题。