Programming SSC-32 in C#

I am working to learn how to interface with the SSC-32 using C#.
The serial port routine I developed seems to run slowly or jerky
when I try and control a single channel using a slider control.

Can anyone give me some advice on how to make it work faster
or smoother?

public Form1()
{
InitializeComponent();
SerialPort serialPort1 = new SerialPort();

        serialPort1.BaudRate = 115200;
        serialPort1.PortName = "COM4";
        serialPort1.Parity = Parity.None;
        serialPort1.DataBits = 8;
        serialPort1.StopBits = StopBits.One;

        SendSampleData01(Convert.ToString(2000));
    }


    public void elevation_Scroll(object sender, EventArgs e)
    {
        int PWM01 = elevation.Value;
        textBox1.Text = Convert.ToString(PWM01);
        SendSampleData01(textBox1.Text);
    }
    void SendSampleData01(String PWM01)
    {
        Char CR = Convert.ToChar(13);

        // Open the port for communications

        serialPort1.Open();

        // Write a set of bytes

        serialPort1.Write("#1 P" + PWM01 + CR);

        
        // Close the port

        serialPort1.Close();

There are one or two things you need to do differently…

First, dont open and close the port in the scroll event. Either open the port at form load(init) and close at form dispose or have a small seperate routine to open and close the port. This is the main reason you are seeing jerking. The time it takes to open the port.

Secondly, the scroll bar values will change more rapidly than the comms can send. You need to calculate how long the previous move will take before sending a new move as it may very well overwrite or corrupt the first command. For example, if it takes 200ms for the previous move to complete then use a timer set to 200 and wait for it to time out before sending the next adjustment.

Thirdly… Microsoft scroll bars and Trakbars are dreadful. They cause all sorts of issues. I know laurentius has used some in his SEQ app but I think you will find they are a Borland component not Microsoft.

What is the value span of your slider (perhaps 2000?)? If you are using 2000 us, then you may need to adjust your program so that it doesn’t send a new position command for each 1 us change (which would possibly overlosd the controller). I made a joystick program for my servo controller (a different type) and used some simple math so that the large joystick range was divided into ~250 descrete steps for the servo positions. I included a 10 ms timer delay for reading the joystick position, which you could do for reading the slider position. The servo response was quick and smooth. Try making a slider with a range of 0 to 250, with each of the slider positions being “500 + 8(slider value)” to generate the position value to send to the controller. This will set slider position “0” at 500 us and slider position “250” at 2500 us for the full 180 deg operation. If this works, then you could try making the slider 0 to 500, and change the multiplier fron 8 to 4. As previously suggested, open the com port when the program opens, and close it when the program is closed.

I’m doing a similar thing using a trakbar.

The best way to get round the resolution problem is to set

.Maximum=2500 (or whatever your max value is)
.Minumum=500 (or whatever your min value is)
.SmallChange=10
.LargeChange=50 (or 100 for bigger steps)

This achieves what zoomcat is saying to provide 200 small steps rather that 2000

Make sure you get the max and min right as you can damage the servo driving it too far, on mine its 2300 max and 750 min which gives about 170 degrees of movement.

Also, when I send a move, I know how long the move will be so I set the timer to that value and then disable the trakbar. This prevents a second command being sent. The timer re-enables the trakbar.

The advice to; “dont open and close the port in the scroll event” had the biggest effect. Now it works much smoother. I also opened up the change (small change) to 10 and it’s good. Thanks again for the help - Onward!

When I converted The Hex code from the Atom to the Atom Pro, I tried updating the timing to make sure commands executed at the appropriate time. I later changed it to instead query the SSC-32 to see if it is busy (Q command). It made my walking code work smoother. You might try this with your trakbar.

Kurt

Thats definitely on the agenda… At the moment I am battling with Windows events. It seems the scroll event and the change event can be triggered by other events I was not expecting so I’m considering a change of approach…

God I hate windows… :laughing: