Chinese 32 servo controller data format

Hi.

I have a Chinese 32 servo controller and I am looking for a simple way to send Data to the controller from a Nano. Normally this would be sent to the Controller board with Serial.

/#1P1500#2P1500#3P1500T1000D800\r\n

Servo movement #P1 is servo 1 move to 1500, #2 is servo 2 move to 1500, T1000 is speed to move D800 is Time delay.

I need to send calculated variables to the controller. This is the best way I have found of doing it.

 //int sensorValue = analogRead(A0);
int servo_Hip = 45;
int servo_Leg = 55;
int servo_Foot = 20;
//int speed = 500;
//int duration = 300;

void setup() {
  Serial.begin(115200);
}

void loop() {
  char buffer[50];
  sprintf(buffer, "#F%d#F%d#F%d\r\n", servo_Hip, servo_Leg, servo_Foot);
  Serial.println(buffer);
  delay(20);
}

This prints in the Serial monitor “#F45#F55#F20” with 2 spaces between prints.

My question is:- Is there a better way of doing this?

Regards Antony.

Please explain. What I see when I run your code is a single empty line (are those the two spaces that you are referring to?).

13:58:25.522 -> #F45#F55#F20
13:58:25.522 -> 
13:58:25.522 -> #F45#F55#F20
13:58:25.522 -> 
13:58:25.522 -> #F45#F55#F20
13:58:25.522 -> 
...
...

The empty line is because your command contains a \r\n and the Serial.println() also adds a \r\n.
If you want to prevent the empty line and keep the command as is, use Serial.print() instead of Serial.println().

Not necessarily any better, but you could avoid the need for sprintf() and the buffer by using individual print statements:

void loop() {
  Serial.print("#F");
  Serial.print(servo_Hip);
  Serial.print("#F");
  Serial.print(servo_Leg);
  Serial.print("#F");
  Serial.println(servo_Foot);
  delay(20);
}

Hi.

Sterretje, I was asking if there was a better way of doing the serial print. As the formatting is already defined for me in the manual. But no Arduino code.

David, Yes But I don’t see that it any better. Yes it works.

Thanks for looking, Antony.

I see little resemblance between:

and:

So was the latter your attempt, and you want to know how to replicate the first line better, or is there some other purpose to your posting? Certainly,
/#1P1500#2P1500#3P1500T1000D800\r\n
is relatively trivial to replicate, with a little effort.

Good Morning.

Sorry it might be the way I am trying to explain what it is that I want.
The instruction manual only gives this as a Example in Arduino.


void setup() 
{
  Serial.begin(9600);
}
void loop() 
{
  while(1)
  {
    //       #1P1500#2P1500T1000D800\r\n 
    Serial.print("#1P800T10000T1000D800\r\n");  // Servo S1 rotates to a pulse width of 800, using a time of 1000ms.
    delay(1000);  // Delay 1000ms, the servo just finished executing the previous command.
    Serial.print("#1P1800T10000T1000D800\r\n");  // Servo S1 rotates to the position with a pulse width of 1800, using a time of 1000ms.
    delay(1000);  // Delay 1000ms, the servo just finished executing the previous command.
  }
}

And this is the output in serial monitor.

output

Is there another command or way other than using the sprintf command ? As david2018 points out I could just do it line by line statements.

Regards Antony.

first question that comes in my mind is if there are always the same number of fields?

If so you can encapsulate it in a function

void myServo(int p1, int p2, int p3, int time, int del)
{
  .. Serial print stuf here
}

This allows you to do write the code as

myServo(1500,1500,1500,1000,800);
myServo(1800,1000,1500,1000,800);
etc

@AntonyG
In addition to @camsysca question:

Please explain, what your printing of "#F45#F55#F20" has to do with required format of #1P1500#2P1500 ?

In another words - if your need a '#' and a number - why did you print '#' and 'F' ? Where this 'F' coming from?

Hi.

Yes I could and might well use that method , But its the method of printing to the serial that I have not done before.

The truth is I have no idea. I think it was just to test that it would print in the serial monitor OK. My bad.. Sorry

Its the p that is required by the controller to indicate witch servo is required to move.

So the format should be “#” is the start point, “P” is the servo number, and the next one is a number between 500 and 2500 for Servo position.

Then are the optional “T” is the refers to the time of execution and represents the speed, in the range 0 9999,

Also optional “D” refers to the Instruction interval of delay time, in the range 0-9999,

Theirs also a “ \r” which is a carriage return, and a “\n” which is a line feed.

The serial message can contain more than one servo position

Right. So the format is 'sorta fixed'. What determines what gets sent? do you only send changes? Do time and delay always get sent? How many servos are there? Is it possible that you'll want to send 32 positions plus T and D, or are there other limits? One of the things that could bite you is the 64 character buffer limit on outbound serial - you won't lose characters, but your code will slow to the transmission speed, which can cause you no end of grief. If there's a lot of servos, you might be better to, for example, send 4, 6,or 8 servos at a time, depending upon what will fit in the 64 character buffer.

edit - also, 9600 baud is horridly limiting. What are the limits for the controller - will it accept 115200, for example?

I think we need a few more details, please.

Hi, could you share a link to the controller you're using please.

Hi. I plan to be controlling 24 servos. Which I have done in the testing. Works very well. I send information for 3 servos at a time. And the Baud rate is fixed @96009600.

Yes NP 32 channel controller

The instruction for Arduino programming are in screen shot below.

A Link to the Manual for the board Cloud manual

Regards Antony,

This seems to have died. Have you decided on an approach? I still have questions.
Is the update order random? I.e. at one point in time, you might want to only update 2 servos, #2 and #17? Or do you intend to update all 25 in a round-robin fashion? How you structure your sending code is dependent on that. If the former, then a list of channel, value to be sent is likely appropriate. If the latter, a pointer to an array of values, one per channel, is sufficient.

Depending on the method chosen, have you done a throughput calculation? I.e. how many updates do you need to do, vs how many updates you can send via serial at 9600? Round-robin might be easier to code, but it might make your movements slow as molasses. Doesn't matter, if each servo only moves once in 20 seconds, but you're not telling us anything about the application.

To send #NPVVVV *24 + TNNNNDNNNNcrlf is 156 chars(after the 64th, approximately, your Arduino will seem to go to sleep, while the buffer slowly absorbs the next 92 - at 1 char per ms). you'll only get six of those done per second. Seems totally a waste, if only one or two servos are moving. So, a dynamic list, sent in triplets, would make sense.

This one line approach suggests that it specifies the position of all servos.

Wouldn't it make sense to have separate lines for each servo and only those servos that change positions?

Presumably these movements occur at specific times involving 1 or more servo, each at different speeds. Any delays would be managed by when the commands are sent

Strangely, that's what I just asked our OP. Let's wait for a reply, shall we?

If you'd gone for a lynxmotion controller https://uk.robotshop.com/products/lynxmotion-ssc-32u-usb-servo-controller or possibly (untested) a clone https://www.ebay.co.uk/itm/336173051648 then there's a binary format that I use for my 29 rapid servos (I use them like windscreen wipers). The format is explained here SSC-32 Binary Commands and is useful if you need all your servos moving constantly to the point that the UART speed is exceeded when using text/non-binary format.

Hi. just a update on the code.

char buffer[90];
  //               0     1    2    3    4    5    6    7    8    9     10    11    12    13    14    15    16    17 To do add execution time and delay time between instructions
  sprintf(buffer, "#1P%d#2P%d#3P%d#4P%d#5P%d#6P%d#7P%d#8P%d#9P%d#10P%d#11P%d#12P%d#13P%d#14P%d#15P%d#16P%d#17P%d#18P%d\r\n", LF_servo_Hip, LF_servo_Leg, LF_servo_Foot, LC_servo_Hip, LC_servo_Leg, LC_servo_Foot, LB_servo_Hip, LB_servo_Leg, LB_servo_Foot, RF_servo_Hip, RF_servo_Leg, RF_servo_Foot, RC_servo_Hip, RC_servo_Leg, RC_servo_Foot, RB_servo_Hip, RB_servo_Leg, RB_servo_Foot);  //   #1P1500#2P1500T1000D800\r\n
 

Very long line of code Sorry.
but it works.
Regards Antony.

That is much too small, if you want to allow up to 4 digits for each number you would need 138 characters. Just a single digit for each number results in a line with 84 characters (including the terminating null).

Why would you write something so humanly illegible? Make a function receiving three arguments (PWM, timing and delay) and call it 32 times to build the array of characters.

In your mind, perhaps, but that buffer overrun will soon bite you, if it isn’t already.