1

I have a quadruped robot running with an SSC-32 servo controller. The servo controller has a serial port, so I purchased a USB-to-Serial cable to send commands to it. Because I am most proficient with Python, I decided to use PySerial to open and close the Serial port. I am running a Linux VM on my MAC.

The error message that displays is included below. Also included is my code.

As you will see, it is having difficulty accessing the port. However, when I unplug and replug the port in, the error message disappears and I am able to successfully send commands. The next time I try to run the code, the error message appears. I suspect that the port is having difficulty closing because when I unplug the cable, I am manually closing it and the code works as a result.

To try and fix the problem, I have tried removing the timeout and changing the timeout time. Moreover, I have added a while loop that ensures the port is closing. None of the preliminary solutions have worked.

Let me know if you have any ideas.

Thanks in advance,

Christopher

Error Message:

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

The code can be found here or below:

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
4
  • Are you the virtualization sharing of the port is correct? Can you test the port with some other software? Commented Mar 27, 2018 at 1:41
  • Yes, I am fairly sure that the sharing of the port is correct because the connection is independent of the MAC OS- or at least that is what I believe. Do you know how I would test the sharing of the port? I have tested the port on a windows computer and it worked. Commented Mar 27, 2018 at 4:53
  • Would suggest a terminal emulator connected to the serial port in question to see if it behaves as expected. Commented Mar 27, 2018 at 5:01
  • I was able to test the program on a different VM program and it worked. Thank you for your help! Commented Apr 1, 2018 at 1:20

1 Answer 1

2

I determined that I had to configure the port using the following command: sudo chmod 0666 /dev/ttyUSB0. This was able to solve the problem I experienced.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.