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.
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().
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.
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.
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.
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.
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?
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.
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
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.