How to get started with USB16 controller?

Hi everyone

I’ve recently purchased the USB16 controller and some servo motors, I plan to do some simple position control with them. The thing is I’ve never done this before, and besides hooking up the USB interface and the servo power supply I’m completely at a loss.

Firstly I’m not sure if i have to write my own code to program the position control or are there “easy to use, available softwares” out there. (I’ve downloaded an application called pololu_controller.exe by searching the forum, but it seems that its for 8 controllers and didn’t work).

Secondly, if i do have to code my on commands, what software should I use for coding? (I’ve been coding in Matlab for years but that’s pretty much it)

So yea, I’m pretty much lost as to what to do next after connecting all the powers, so please, any help will be much appreciated.

Best regards

John

Hi John,

What are you hoping to do with your servos?

There are a few programs that let you do some basic stuff with the Pololu servo controllers, but none are particularly stellar. For most applications I think you will want to write your own program.

If you’d like to learn a new programming language, like C for example, you can find example code around the forum and we can give you a few pointers, but if you want to stick with what you’re familiar with, there’s no reason not to use Matlab. Here is a simple example m-file functoin to open a serial port, move a servo, and close the serial port:

function moveServo(s,x)
%moveServo(s,x) for Pololu serial servo controllers, using Pololu mode
%(remember to remove the blue mode-selection jumper!) absolute position
%command #4. You may need to edit the port variable in this file to match
%your com port.
%
%s=servo number
%x=absolute position, 500<=x<=5500

port = 'COM3';%Edit to your com port number

ser1 = serial(port);
set(ser1, 'InputBufferSize', 2048);
set(ser1, 'BaudRate', 9600);
set(ser1, 'DataBits', 8);
set(ser1, 'Parity', 'none');
set(ser1, 'StopBits', 1);

fopen(ser1);
fwrite(ser1, [128, 1, 4, s, binvec2dec(bitget(x,8:13)), binvec2dec(bitget(x,1:7))]);

fclose(ser1);
delete(ser1);

I would also suggest you read through the manual carefully, but don’t feel shy about asking specific questions.

-Adam

Hi, thanks for the quick reply. Basically my function is to do the most basic position contorl, i have to tell 4 servo motors to reach their respective position from the neutural position, and then return to the neutral position when I tell them to.

Thanks for sharing the code, I’ll go through it and try to understand it.

Hi guys

Just ran the code in Matlab and nothing happened. I hooked up the controller to the computer, made sure it’s COM3, hooked up a DC power supply of 5.5V with max current limit of 0.5A. both the green USB active LED and the green signal LED was lit. I connected a servo to one of the ports. But as I run the code I get nothing, and the green signal LED doesn’t flash either.

Any ideas?

How are you running the code?

To be called as a function (as written) you would need to save it in a file called “moveServo.m” in either you active Matlab directory or a directory included in its path. Then you would call it by typing something like “moveServo(0,3000)”.

For serial port troubleshooting, you can modify the code slightly and just paste it into the Matlab command line:

s=0
x=3000
port = 'COM1';%Edit to your com port number
ser1 = serial(port);
set(ser1, 'InputBufferSize', 2048);
set(ser1, 'BaudRate', 9600);
set(ser1, 'DataBits', 8);
set(ser1, 'Parity', 'none');
set(ser1, 'StopBits', 1);
fopen(ser1);
fwrite(ser1, [128, 1, 4, s, binvec2dec(bitget(x,8:13)), binvec2dec(bitget(x,1:7))]);
fclose(ser1);
delete(ser1);

If you can’t see the serial activity LED flickering at all (it’s very fast and hard to see) you might try HDD Free Serial Port Monitor. It’s a free program that taps into communications with your serial ports, so you can see what Matlab is, or isn’t, sending to a particular port (even virtual USB-Serial ports).

-Adam

P.S. That function uses Pololu mode, so be sure you don’t have the jumper over the communication mode pins.

Hi,
First of all thanks for the reply.
I’ve got it working finally! apperently the code was running but the servo power connection wasn’t connected properly (loose). Are we meant to solder the blue power connector to the circuit board?

Hello.

If you want to use that terminal block, you’ll need to solder it in. Many battery packs can also plug into the servo header pins.

- Jan

Hi guys

Just wondering what some of the numbers mean in this line of code -
fwrite(ser1, [128, 1, 4, s, binvec2dec(bitget(x,8:13)), binvec2dec(bitget(x,1:7))]);

I know that ser1 is for the com port, and s specifies the servo port number, but can some one please explain the rest?

Thanks in advance.

The command “fwrite” sends a string of bytes (8-bit numbers, 0-255) to the serial port specified in the first argument. The second argument is an array of bytes that make up a Pololu mode command, as described in the servo controller user’s guide.

The first byte ‘128’ is a “start of command” byte, always equal to 128 (hex 0x80). Each subsequent byte MUST be less than the start byte.

The second byte ‘1’ is the device ID, which is always 1 for Pololu servo controllers (DC motor controllers have device ID 2, etc…).

The third byte ‘4’ is the command number, in this case command 4: Set Position, Absolute, as described in the user’s guide.

The fourth byte ‘s’ is the servo number that the command applies to.

The fifth and sixth bytes are the servo position, in a range from 500 to 5500, split up into two separate bytes. To keep each byte’s value under 128 as specified by the Pololu mode protocol, they each store 7-bit numbers each with a value of less than 128. This splitting in hex/binary is spelled out a little more clearly in this thread. The binvec2dec(bitget()) functions are just a way in Matlab to separate bits 1-7 and bits 8-13 of servo number into two separate bytes.

I hope that clears things up a little bit (haha, sorry), and feel free to keep asking questions!

-Adam