C# USB 16 servo controller. Help!

Hi all.

I am new to this.
My project is to control servo in C#.
I am still new to C#.

Does anyone here know how to program in C# using Pololu 16 servo controller usb.

Can you give example code in c# to control Pololu usb 16 servo controller.

moving the servo left and right
set servo to nentrual
etc…

Thank in advance. :stuck_out_tongue:

Hello,

Our USB servo controller incorporates a USB-to-serial adapter, so from the programming side, you just need to talk to a serial (COM) port. The 2005 versions of Microsoft’s development tools have built-in support for serial ports. A search for something like “visual studio 2005 serialport” brings up a lot of examples.

Once you can talk to the serial port, you just have to send the right sequence of bytes. You can keep it simple by beginning in Mini SSC II mode (blue shorting block on) and sending the three-byte sequence: 0xFF, , .

- Jan

All the Pololu servo controllers (8, 16, serial, usb, etc) use the same communications protocol, so you could take a look at this thread for ideas. Assuming you’re using a Windows computer.

-Adam

Hi

thank for the advice, my pololu is on order.

This is what I did so far, I can not test it until the servo controller arrive.
Is that correct way for

controlling servo number 0, turning max position.
The first byte is start, second is servo number, third is servo position

Using SSCII Mode, with short J1 jumper

I can get the port open, but not sure sending the correct info.

        string testing = "0xFF\r\n0x00-0x10\r\n0x00-0xFE";
   
        if (port.IsOpen) 
             port.WriteLine(testing);
        else 
            MessageBox.Show("port is closed!", "check port, MessageBoxButtons.OK, MessageBoxIcon.Error);
        testing="";

You are way off. To send one command, you need to send three bytes. A string like “0xFF” represents the four characters (and possibly some kind of terminating character you don’t see), each of which is a byte (or more). Combinations like \r and \n represent special characters, which are not called for in the servo controller protocol.

Finally I’m not sure what you’re trying to achieve with the “0x00-0x10” portion of your string, but I think it reveals several errors in how you’re thinking about this. To send a command, you have to choose one of the bytes to send; it looks like you’re trying to specify a range. Also, if you’re trying to control sixteen servos, their numbers are 0x00 through 0x0F.

- Jan

Using SSCII Mode, with short J1 jumper
c# will this work.

  if (port.IsOpen)
            {
                byte[] buffer = new byte[3];
                buffer[0] = 255; // start
                buffer[1] = 0; // servo # 0
                buffer[2] = 200; // position of servo 
                port.WriteLine(buffer, 0, 3);
            
            }
            else
                MessageBox.Show("Serial port is closed!");
            txtOut.Clear();

As far as I know, the WriteLine member function will send a New Line character as a fourth byte, which is definitely not what you want. You should replace this line with:

port.Write(buffer, 0, 3);

- Ben

Thank for the advice.

if (port.IsOpen)
{
byte[] buffer = new byte[3];
buffer[0] = 255; // start
buffer[1] = 0; // servo # 0
buffer[2] = 200; // position of servo
port.Write(buffer, 0, 3);

}

All I need to wait for the servo controller to arrive to test it out :smiley:

Another quick question.
from the manual. 500 to 5500 is the servo position and 3000 is the neutral.

so I can assume.
for SSC II mode
0-254 is the position and 127 is neutral.

How do I set the speed of the servo in SSC II mode? and what are their range?

And lastly how do I turn off the servo
servoOff()

Yes.

You cannot set the speed in the SSC II mode. If you want to set the speed, you must use the Pololu mode.

You don’t–the servo will always be getting a position pulse train.*

- Ben

Edit:
* To clarify my previous answer slightly, you cannot turn off the servo when using SSC II mode. You can turn off the servo using the Pololu mode by sending command zero with a bit six of the data byte cleared.

public void SendData(string ComPort, int BaudRate, int ServoNumber, int ServoPos) //Mini-SSC protocol
        {
            //create new serial port object with certain comport and baudrate
            SerialPort port = new SerialPort(ComPort, BaudRate, Parity.None, 8, StopBits.One);
            // Open the port for communications
            port.Open();
            //convert position into hex string
            string ServoPosHex = ServoPos.ToString("X");
            //convert position into single byte
            byte PositionByte = byte.Parse(ServoPosHex, System.Globalization.NumberStyles.HexNumber);
            //convert servo number into hex string
            string ServoNumberHex = ServoNumber.ToString("X");
            //convert servo number into single byte
            byte ServoNumberByte = byte.Parse(ServoNumberHex, System.Globalization.NumberStyles.HexNumber);

            //write 3 bytes, start byte, servo number, position
            port.Write(new byte[] { 0xFF, ServoNumberByte, PositionByte }, 0, 3);

            // Close the port
            port.Close();
        }

This should work… this is how I did it in C#. Whenever you want to move the servos, just call SendData.

Ex: SendData(“COM5”,9600,7,127) will move servo 7, connected to COM5, via 9600 baud, to position 127.

Colin

thank for all your help. I am still waiting for my snail mail for my controller. I was told it should arrive tomorrow by the post office.

Anyway, I will try the SSC II mode first.
Using cfinger code.

Ex: SendData(“COM5”,9600,7,127) will move servo 7, connected to COM5, via 9600 baud, to position 127.

I am currently reading the Pololu mode, because it has some function I need, like turning off the servo, and controlling the speed of the servo.
I have tried to read the manual, but too many 0 and HEX. I don:t really understand it. I guess I should have taken computer science instead of Biology. :frowning:

cfinger or anyone here know how to program in C# using Pololu mode.
Do you have example code in c#

Thank

Yeah, I have the code for pololu mode as well. The manual is confusing at first, but take it slowly and you’ll get it.

I’ll post the code in a few days… I just got back from Defcon15. I think I might post a bunch of my code to help people out.

Colin

If you’re interested I wrote up a tutorial on using the servo controller in C#. You can find it here: http://www.colinkarpfinger.com/pololu/

There is an example program you can download here: http://www.colinkarpfinger.com/pololu/PololuServoExample.zip

Hi cfinger

Thank for your work.
I will look it over on the weekend and try it.

Hi cfinger

I have tried using your c# example.
And everything work fine.

I was only one question left.
How do I turn off the servo using your example.

something like…
servoOff()
"cut the signal off"
I have no idea how to do below.
Can you help me cfinger

"You can turn off the servo using the Pololu mode by sending command zero with a bit six of the data byte cleared. "

Hi

This is how I did to turn off the servo.
It seems to work, but I wans wondering if there is any
other way to do it.

  port.Write(new byte[] { 
            0x80,
            0x01,
            0x00,
            ServoNumberByte, 
            SpeedByte, 
        }, 0, 5);

   
            port.Write(new byte[] { 
            0x80, 
            0x01, 
            0x00,
            ServoNumberByte, 
            0x00,
            0x00
         }, 0, 6);

            // Close the port
            port.Close();
        }

Hello.

Command 0 only takes one data byte, however in your sample code you are sending it two. If you want to shut off a servo while using Pololu mode, I believe you should do the following:

port.Write(new byte[] {
0x80,
0x01,
0x00,
servoNumber,
0x00}, 0, 5);

- Ben

Thank
Ben, I have tried with your code for servo off. It works, with less coding.

This is what I have done so far.

16 usb servo.
I can set the max speed baud of 57600.
The servo can move right and left.
The servo can turn off.

I still have two questions
1)
For speed setting.
I set 0 for fastest and 127 for slowest.
To be honest it look like both goes the same speed.
0 and 100,127 speed setting…
To my eyes they all seem to go at the same speed.
I have tried with my brother Hitec servo
475
5475
645
77
805
5990
5980
Did I do the code wrong.

string data1Hex = data1.ToString(“X”);
byte data1Byte=byte.Parse(data1Hex,System.Globalization.NumberStyles.HexNumber);

string ServoSpeedHex = ServoSpeed.ToString(“X”);
byte SpeedByte = byte.Parse(ServoSpeedHex,System.Globalization.NumberStyles.HexNumber);

string ServoNumberHex = ServoNumber.ToString(“X”);
byte ServoNumberByte = byte.Parse(ServoNumberHex, System.Globalization.NumberStyles.HexNumber);

//first send SPEED command
port.Write(new byte[] {
0x80, //synch value (always 0x80)
0x01, //device ID (0x01 for 16 usb servo controller)
0x01, //servo command (setting speed is command 1)
ServoNumberByte, //servo number
SpeedByte, //Data Byte 1: Speed. 0 = instant, 1-127 accel control (0 = quickest, 127 = slowest)
//Data Byte 2: Unnecessary for setting speed.
}, 0, 5);

//second, send ABSOLUTE POSITION command
port.Write(new byte[] {
0x80, //synch value (always 0x80)
0x01, //device ID (0x01 for 16 usb servo controller)
0x04, //servo command (command 4 == Absolute Position (2bytes) )
ServoNumberByte, //servo number
upperBitsByte, //Data Byte 1: This byte is composed of the
lowerBitsByte
}, 0, 6);

port.Close();

  1. My second question.
    Can we store the final motion sequences into pololu controller memory so that we don:t have to use the computer.

You have the interpretation of the speed byte wrong, which most likely explains why you’re not able to see a difference in servo speed.

Speed byte:
0 = instant (servo moves to new position as fast as it can)
1 = slowest speed (servo output pulse width increases by .05 ms per second)
127 = fastest speed (servo output pulse width increases by 6.35 ms per second)

A speed of 100+ would probably look to the eye very similar to instantaneously setting the servo position. Try setting the speed to something low like 1 or 10 and let me know if that makes it noticeable.

As for your second question, Pololu servo controllers cannot store motion sequences; you will always need to have your controller connected to either a computer or a microcontroller. Storing motion sequences is a feature we would like to offer in future products, though.

- Ben

Thank Ben.

Now I feel silly, the speed actually work.

For what ever the reason, I keep testing the value…
0,100,127.
To the naked eyes they all seem to go at the same speed.

But when I tried 10… the servo actually goes slower.

Another questions…

  1. Does the usb 16 servo controller have a feedback.
  2. Is it possible to program to get a position of the servo.
    (I would love this feature, but not sure if it possible to program it.)