Motor control with the Raspberry Pi and Pololu Qik

20130915-035623.jpg

The last piece of electronics to add to my project was the motor controller. After doing some digging, I decided to get a Pololu Qik Dual Serial Motor Controller to power a Tamiya Double Gearbox. Pololu is actually recommending a different (older) motor controller to go with the gearbox. So I did some research, and the only advantage the older motor controller has over the Qik, as far as I can tell, is that the older version supports up to 10A of current vs the Qik’s 1A (3A peak). But according to the specs, the FA-130 motor that comes with the gearbox has a stall current of 2.2A, so I think the Qik should be sufficient. Also, the motor controller they’re recommending is slightly more expensive and is in the “old and discontinued” section, so there’s that too.

Hooking up the Qik to the Raspberry Pi is pretty straight forward, except you first have to re-configure the Pi’s serial port so that it’s not being used for console logins (I followed these directions for this step). The only thing that might be a bit confusing (it was for me) is that with the serial port, you hook up TX to RX and RX to TX. This makes sense when you consider that you want the writes to go to reads, and reads to writes, but this is different to i2c where you want SCL to connect with SCL and SDA with SDA.

On the software side of things, you just need the Python serial package (sudo apt-get install python-serial), and you’re good to go. The following code will turn on motor 0 at full speed for 1 second, pause 1 second, then reverse for 1 second.

s = serialport = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.5)
s.write( chr(0xAA) + chr(0x09) + chr(0x08) + chr(127) )  # motor 0 full speed forward
time.sleep(1)
s.write( chr(0xAA) + chr(0x09) + chr(0x08) + chr(0) )    # motor 0 speed to 0
time.sleep(1)
s.write( chr(0xAA) + chr(0x09) + chr(0x0A) + chr(127) )    # motor 0 full speed reverse
time.sleep(1)
s.write( chr(0xAA) + chr(0x09) + chr(0x0A) + chr(0) )      # motor 0 stop

The short version of the command is that the first byte is a starter byte, the next byte is the device ID (you can have multiple), the 3rd byte is the command, and then parameters follow (in this case, 1 byte containing the motor speed). More information on these commands can be found in the docs, and the Qik also has some other nifty features, so it’s definitely worth reading the manual if you get one. For the most part, as has generally been the case so far, I was surprised by how easy it was to get set up. I also like the serial interface because it’s dead simple to use, but once I get to more powerful motor controllers, I’m probably going to need to do PWM without a handy dandy serial interface.

Now that all the electronics are working, the next step is to put it all together, so stay tuned.