Having bought a new DC motor, I wanted to set it running via the Raspberry Pi and Arduino motor shield in a similar way to previous Robot arm efforts.
I set up the Raspberry Pi to connect to the Arduino via USB and a Hub. Power for the motor was taken via an external battery pack, as shown below:
(I've recently discovered Fritzing which is a great tool for generating schematics, I downloaded the Linux version from here http://fritzing.org/download/ )
The driver code is based on earlier Robot arm code. It reads a character from a terminal on the Raspberry Pi and sends that to the Ardunio:
# Read characters from stdin and send to Arduino via serial
import termios, fcntl, sys, os, serial
arduino = 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
arduino.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)
In this case on the Arduino, 'a' is interpreted as motor forward, 's' motor backward and space ' ' stop.
This all worked to begin with, I could start and stop the motor. However, after a period, normally less than a minute, I would lose response to any character input and had to reset the Arduino.
Investigating further it seemed like the USB connection had changed to a different tty, e.g. to /dev/ttyUSB0, and back.
I then looked in dmesg and saw the following:
usb disabled by hub (EMI?),
re-enabling…
EMI (ElectroMagnetic Interference)
The solution in this case was to remove the hub and run the USB direct to the Arduino. I'm speculating that the EMI from the motor starting and stopping was the cause of this, and reducing the cable length (and hub) was enough to lower the sensitivity beneath the threshold.