Controlling Servo Motors
Using the Pololu Servo Controller in .NET
Tutorial and Example Program
Step 1: Buy a servo Controller.
In this guide I’ll be providing code for the Pololu 8 Servo Serial Controller, which I highly recommend. It has a protocol which provides around 5,000 unique positions, as compared to the 255 provided by most other controllers.
You can buy the 8 servo controller for $23.95, its a good deal.
Pololu 8 Servo Serial Controller
Step 2: Find a Power Source.
The Pololu 8 Servo Controller takes a 5-16V input. You could use any power supply in that range, as long as it can source enough current for your purposes.
I used an old AT power supply. I bet you can find one on ebay for 10$ or less. Chances are you can find a really old computer and steal its power supply. The 5V lines work perfect. Some of the newer power supplies (ATX) have something called current sensing, which will not output a voltage unless there is some continuity between the supply wires and ground. I’m not quite sure these work, but you can read up on current sensing here:
You could also use 4xAA or 4xAAA batteries, or you could use a 5V wall wart (one of those power adapters w/ the bulky plug) from some old device. Get creative. You need to provide power to the servos (anywhere from 4-6 Volts) and to the controller (anywhere from 5-16 V). If the servo battery pack is above 5V, it can also power the controller by placing a jumper on the board. (Vcc=Vs).
Step 3: Find a serial cable
A standard DB9 (9 pin) Female to Male serial cable will work. Do not use a NULL Modem Cable, as it will not work.
Here is one for $1.68 if you can’t find any: http://www.cablewholesale.com/specs/10d1-03203.htm
Step 4: Connect up the power, serial port and your servo!
As stated before, you need to provide power to the servos (anywhere from 4-6 Volts) and to the controller (anywhere from 5-16 V). If the servo battery pack is above 5V, it can also power the controller by placing a jumper on the board. (Vcc=Vs). Plug in your power to the pins on the servo controller. Connect your servo to pin 7 (furthest from the servo power pins). You could use another pin, just remember to change it in the software later.
Diagram taken from the Users Guide PDF
Step 5: Determine which serial (COM) port you will use.
Go to Control Panel -> System -> Device Manager, and browse your Ports. If you have a serial port built in, it will most likely say Communications Port. If its a USB or PCI adapter, it may say something different. Try to determine which one you are using. Don’t worry if you pick the wrong one, your servos just won’t move, and you can try another port.
Step 6: Download and install MS Visual C#
Its a free download, and an extremely easy environment to program in. The example program provided is programmed in C#.
Visual Studio Download Page
Extract it and open PololuServoExample.sln
Step 8: Set your COM Port, and Test it out!
First, set the COM Port to what you determined it was from the control panel. Then either put a number in one of the coordinate text boxes, and hit the Go button, or move the trackbar. The servo connected to pin 7 will move!
Take note that the Top Box and trackbar use pololu protocol, and the bottom ones use MiniSSC. You cannot switch between them without resetting your controller. You can do this buy turning off the power to the controller, and turning it on again.
Pololu mode has a MUCH higher resolution than miniSSC, and therefore you should use Pololu mode. But both examples are given just in case.
Take note that once you have added the serialport class to your project, turning the servos takes only 3 lines of code for both protocols:
SerialPortClass serialPort1 = new SerialPortClass(); //create an instance of our class so we can access its functions
serialPort1.Pololu(comPort, baudRate, 7, servo, servoSpeed); //this function will move servo 7, at the quickest speed,
//via a certain COMport and baud rate
All your code is shown there. Notice that in the example program the declaration of serialPort1 occurs at the top of the form. If you need to access it from another form you could either declare a new instance, or make serialPort1 public.
If you have problems, try changing the COM port around. Also, you can increase the baud rate for quicker performance. I haven’t had any problems at 38400.