Page 1 of 1

Problem with serial communication through USB

Posted: Sat Apr 25, 2020 10:46 am
by Neid
I'm trying to send commands from raspberry pi 3b+ to 32 CH servo control board with serial communication through USB.

Code: Select all

import serial
import time

ser = serial.Serial('/dev/ttyACM0', baudrate=115200,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS
                    )
while True:
    ser.write(b"#3P1100T500D0\n")
    print("1100")
    time.sleep(0.5)
    ser.write(b"#3P1800T500D0\n")
    print("1800")
    time.sleep(0.5)
    
After first command (3P1100T500D0) is executed nothing happens and i have to unplug and plug in again board to even use first command in the code.

Re: Problem with serial connection through USB

Posted: Sat Apr 25, 2020 11:04 am
by B.Goode
Neid wrote:
Sat Apr 25, 2020 10:46 am
I'm trying to send commands from raspberry pi 3b+ to 32 CH servo control board with serial communication through USB.

Code: Select all

import serial
import time

ser = serial.Serial('/dev/ttyACM0', baudrate=115200,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS
                    )
while True:
    ser.write(b"#3P1100T500D0\n")
    print("1100")
    time.sleep(0.5)
    ser.write(b"#3P1800T500D0\n")
    print("1800")
    time.sleep(0.5)
    
After first command (3P1100T500D0) is executed nothing happens and i have to unplug and plug in again board to even use first command in the code.

Raspberry Pi documentation for USB troubleshooting here - https://www.raspberrypi.org/documentati ... /README.md

And information about UART/serial comms here - https://www.raspberrypi.org/documentati ... on/uart.md


I wonder if you have correctly identified the serial device as ttyACM0?

Have you tried a simple loopback data test on the serial device prior to the complexity of attempting to communicate with the servo controller?

Re: Problem with serial connection through USB

Posted: Sat Apr 25, 2020 12:02 pm
by Neid
B.Goode wrote:
Sat Apr 25, 2020 11:04 am
Neid wrote:
Sat Apr 25, 2020 10:46 am
I'm trying to send commands from raspberry pi 3b+ to 32 CH servo control board with serial communication through USB.

Code: Select all

import serial
import time

ser = serial.Serial('/dev/ttyACM0', baudrate=115200,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS
                    )
while True:
    ser.write(b"#3P1100T500D0\n")
    print("1100")
    time.sleep(0.5)
    ser.write(b"#3P1800T500D0\n")
    print("1800")
    time.sleep(0.5)
    
After first command (3P1100T500D0) is executed nothing happens and i have to unplug and plug in again board to even use first command in the code.

Raspberry Pi documentation for USB troubleshooting here - https://www.raspberrypi.org/documentati ... /README.md

And information about UART/serial comms here - https://www.raspberrypi.org/documentati ... on/uart.md


I wonder if you have correctly identified the serial device as ttyACM0?

Have you tried a simple loopback data test on the serial device prior to the complexity of attempting to communicate with the servo controller?
Thanks for the answer. I'll look into all of troubleshooting documentations soon. As I said first command is executed so serial device is probably fine.
Image
Before and after connecting

I have tried communicating with the board through arduino IDE serial monitor and through ServoController program. Both of them worked fine.

Re: Problem with serial communication through USB

Posted: Sat Apr 25, 2020 3:10 pm
by scruss
There's this very old thread about SSC-32 control where it turns out that the board needs \r instead of \n to accept a command: Controlling SSC-32 servo controler board with RPi - Raspberry Pi Forums. Have you tried that?

Although you're writing, not reading, serial.Serial should set a timeout to allow commands to fail. Without it, your program may wait forever for serial commands to finish. Adding something like serial.Serial(… , timeout=1) at least should fail and tell you why.

If this is one of those Maestro/SSC-32 clones (a link to what you have would really help!), you seem to be sending it a move command, wait half a second, another move command, wait another half second, reissue the first move command … repeat forever. Can you query the board to see if the servos have moved (close) to the positions you asked them to before issuing another command? I haven't used this board, and servos are something I only use now and again, but it always surprises me how long it takes for a servo to move.

There may be a higher-level Python module for controlling your board: GitHub - FRC4564/Maestro: Python class to support Pololu's Maestro servo controller over USB serial. Great with Raspberry Pi. - but this may only work with boards that provide the two serial ports, and your screenshot only shows one, ttyACM0.

Re: Problem with serial communication through USB

Posted: Sat Apr 25, 2020 4:10 pm
by Neid
scruss wrote:
Sat Apr 25, 2020 3:10 pm
There's this very old thread about SSC-32 control where it turns out that the board needs \r instead of \n to accept a command: Controlling SSC-32 servo controler board with RPi - Raspberry Pi Forums. Have you tried that?

Although you're writing, not reading, serial.Serial should set a timeout to allow commands to fail. Without it, your program may wait forever for serial commands to finish. Adding something like serial.Serial(… , timeout=1) at least should fail and tell you why.

If this is one of those Maestro/SSC-32 clones (a link to what you have would really help!), you seem to be sending it a move command, wait half a second, another move command, wait another half second, reissue the first move command … repeat forever. Can you query the board to see if the servos have moved (close) to the positions you asked them to before issuing another command? I haven't used this board, and servos are something I only use now and again, but it always surprises me how long it takes for a servo to move.

There may be a higher-level Python module for controlling your board: GitHub - FRC4564/Maestro: Python class to support Pololu's Maestro servo controller over USB serial. Great with Raspberry Pi. - but this may only work with boards that provide the two serial ports, and your screenshot only shows one, ttyACM0.
Thanks for the answer.
I tried changing /n to /r but after that even first command was not executed. Adding timeout didn't change anything, first movement was executed and then it was just printing 1800 and 1100 without any errors but servo didn't move. No matter which command i put first it works perfectly and after that it's just printing. Servos are capable of moving that fast but i tried adding more time between commands, no effect here. It's like after that first command connection between raspberry pi and board is crashing because even if i switch first position in the code after running it once board refuses to do anything until i unplug it and plug in again.

Link to the board
https://pl.aliexpress.com/item/32272674 ... 5c0f7dISrt

Also if i want to move 2 servos at once it's not problem at all if it's in one command (ser.write(b"#3P1800T500D0#2P1800T500D0\n"))

Re: Problem with serial communication through USB

Posted: Sat Apr 25, 2020 5:00 pm
by hippy
Neid wrote:
Sat Apr 25, 2020 4:10 pm
Link to the board
https://pl.aliexpress.com/item/32272674 ... 5c0f7dISrt
Do you have a link to the datasheet for the board ?

Re: Problem with serial communication through USB

Posted: Sat Apr 25, 2020 5:22 pm
by Neid
hippy wrote:
Sat Apr 25, 2020 5:00 pm
Neid wrote:
Sat Apr 25, 2020 4:10 pm
Link to the board
https://pl.aliexpress.com/item/32272674 ... 5c0f7dISrt
Do you have a link to the datasheet for the board ?
Sadly no. The closest thing to that board is video that i found https://www.youtube.com/watch?v=4YMroxH ... =11&t=270s
It shows 24 ch servo control board that is using commands in the same format but link to the shop is down so I'm kinda stuck.

Also there's one hexapod build with that board but it's controlled from wemos d1 mini and the program is writen in C.
https://www.instructables.com/id/Simple ... F-Hexapod/

Re: Problem with serial communication through USB

Posted: Sat Apr 25, 2020 6:10 pm
by scruss
Looking at the logo, it's from RTRobot.
Current Linux software - including manual and Raspberry Pi code - seems to live here: http://www.rtrobot.org/software/
You want Servo Controller X64 for Linux. It's a big/slow download, and there are some precompiled things in there that might not be the best things to run.

Slightly more easy to get manual: https://www.scribd.com/document/4421260 ... h-V2-6-pdf

There's a tiny C example for Raspberry Pi:

Code: Select all

/*
 * test.c:
 * Copyright (c) 2014-2020 Rtrobot. <admin@rtrobot.org>
 *  <http://rtrobot.org>
 ***********************************************************************
*  1.Inatall wiringPi at <http://wiringpi.com/>
*  2. Connect to board via usb
*  3.build:
*       gcc -o test test.c -lwiringPi
*  4.run to test:
*       ./test
 ***********************************************************************
 */


#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>

void wait_serial_return_ok(int fd)
{
	int num=0;
	while(1)
    {
		char c[16];
		c[num]=serialGetchar(fd);
		if(c[num-1]=='O'&&c[num]=='K')
			break;
		num++;
		if(num>=15)
			num=0;
    }
}

int main(int argc, char *argv[])
{
	int fd;
	if ((fd = serialOpen("/dev/ttyACM0", 115200)) < 0)
    {
		fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
		return 1 ;
    }

	serialPuts(fd, "#1P2000T1000D100\r\n");
	//Waiting to return OK
	wait_serial_return_ok(fd);
	serialPuts(fd, "#1P1000#2P666T1000D2333\r\n");
	wait_serial_return_ok(fd);
	
	//Test for send wrong instruction
	serialPuts(fd, "#3P2500T9000D9000\r\n");
		
	//Stop wrong instruction
	serialPuts(fd, "~ST");
		
	//Change right instruction
	serialPuts(fd, "#2P2500T9000D9000\r\n");
	wait_serial_return_ok(fd);
		
	//Test for action groups,Group 1 run 3 times
	serialPuts(fd, "G1F3\r\n");
	wait_serial_return_ok(fd);
	
	//Close serial
	serialClose(fd);
	return 0;
}
What leaps out at me from that code is:
  1. All commands end with "\r\n".
  2. The test program waits for OK to be returned from the serial port
Possibly older user manual here: https://web.archive.org/web/20161213082 ... Manual.pdf

Re: Problem with serial communication through USB

Posted: Sat Apr 25, 2020 6:20 pm
by Neid
scruss wrote:
Sat Apr 25, 2020 6:10 pm
Looking at the logo, it's from RTRobot.
Current Linux software - including manual and Raspberry Pi code - seems to live here: http://www.rtrobot.org/software/
You want Servo Controller X64 for Linux. It's a big/slow download, and there are some precompiled things in there that might not be the best things to run.

Slightly more easy to get manual: https://www.scribd.com/document/4421260 ... h-V2-6-pdf

There's a tiny C example for Raspberry Pi:

Code: Select all

/*
 * test.c:
 * Copyright (c) 2014-2020 Rtrobot. <admin@rtrobot.org>
 *  <http://rtrobot.org>
 ***********************************************************************
*  1.Inatall wiringPi at <http://wiringpi.com/>
*  2. Connect to board via usb
*  3.build:
*       gcc -o test test.c -lwiringPi
*  4.run to test:
*       ./test
 ***********************************************************************
 */


#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>

void wait_serial_return_ok(int fd)
{
	int num=0;
	while(1)
    {
		char c[16];
		c[num]=serialGetchar(fd);
		if(c[num-1]=='O'&&c[num]=='K')
			break;
		num++;
		if(num>=15)
			num=0;
    }
}

int main(int argc, char *argv[])
{
	int fd;
	if ((fd = serialOpen("/dev/ttyACM0", 115200)) < 0)
    {
		fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
		return 1 ;
    }

	serialPuts(fd, "#1P2000T1000D100\r\n");
	//Waiting to return OK
	wait_serial_return_ok(fd);
	serialPuts(fd, "#1P1000#2P666T1000D2333\r\n");
	wait_serial_return_ok(fd);
	
	//Test for send wrong instruction
	serialPuts(fd, "#3P2500T9000D9000\r\n");
		
	//Stop wrong instruction
	serialPuts(fd, "~ST");
		
	//Change right instruction
	serialPuts(fd, "#2P2500T9000D9000\r\n");
	wait_serial_return_ok(fd);
		
	//Test for action groups,Group 1 run 3 times
	serialPuts(fd, "G1F3\r\n");
	wait_serial_return_ok(fd);
	
	//Close serial
	serialClose(fd);
	return 0;
}
What leaps out at me from that code is:
  1. All commands end with "\r\n".
  2. The test program waits for OK to be returned from the serial port
Possibly older user manual here: https://web.archive.org/web/20161213082 ... Manual.pdf
Thank you so much. It works when i end commands with \r\n and also when i stop program and start it again it works perfectly fine.