Loading [MathJax]/extensions/MathMenu.js

Tuesday, 10 November 2015

Robot Arm - Full Control

Last time on the Robot Arm project, I was able to control two of the motors on the arm from a Raspberry Pi using a single Arduino and motor shield.

The arm however has 5 motors so this does limit what I can do with it. I decided to get a new Arduino and Motor shield and add them to the project.

The new motor shield is a cheap build of the old Adafruit motor shield. This requires different code to control from the previous post. A more detailed description can be found here.

The new Arduino is a standard Uno board.

I got lucky here, because when I plugged it into the USB hub it is recognised at /dev/ttyACM0, the old board was recognised at /dev/ttyUSB0. (It also appears as Arduino Uno, instead of Arduino Diecimila or Duemilanove w/ ATmega168 in the Arduino IDE.)

This allowed me to use both easily with the following design:
No matter how I plug the Arduinos into the hub, or which order, I can still reliably address each one independently.

The python script needed two lines changing to allow me to send the same character to both Arduinos. They each ignore characters intended for the other one.

import termios, fcntl, sys, os, serial

arduino1 = serial.Serial('/dev/ttyUSB0')
arduino2 = serial.Serial('/dev/ttyACM0')
fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
    while 1: # This is a tight loop with high CPU
        try:
            c = sys.stdin.read(1)
            # repr() : Return a string containing a printable
            #    representation of an object
            arduino1.write(c)
            arduino2.write(c)
            print "Read character from stdin", repr(c)
        except IOError: pass
finally:
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)





The full code can be found on GitHub: https://github.com/jscott7/Robot-Arm.git



No comments:

Post a Comment