PDA

View Full Version : Ressources for OpenCM-companioncomputer UART communication?



Maxzor
10-16-2016, 07:13 AM
Hello,

I have an amateur project of an automatic cat-feeding machine.
It does not require real-time performance of course :)
I sketched a system with a Dynamixel XL 320, an Open CM 9.04 as a controller, and a wired RasperryPi0 for internet control and image processing (the RPi camera would answer a screenshot of the food tray on demand).

I have dropped for now the idea of controlling directly the servo from the RaspberryPi, although I might tackle it later.
Is there any ressource on the OpenCM UART protocol, on how to exchange information?
It is a very simple project and I only need to send a spinning command with the duration of spin as parameter. I have never achieved low level communication and it would be my first UART implementation. I am aware of past projects using RaspberryPi+USB2AX for example.

BR

tician
10-16-2016, 12:08 PM
There should be example sketches in the OpenCM IDE for basic control of dynamixel servos, then you just add a bit of code to pass simpler commands from the PC via SerialUSB. Basically, all the servo packet work is handled by the dynamixel library and you just add simple parameters to the function calls.

Maxzor
10-16-2016, 12:39 PM
Yes I have no problem running servo command code in the OpenCM, I am just looking for information on how to initialize UART, make those function calls, basically exchange packets between computer and opencm.

jwatte
10-16-2016, 01:10 PM
I am just looking for information on how to initialize UART

You can copy any Arduino tutorial, and just change "Serial" to "SerialUSB."
The cool thing with the USB serial port is that it doesn't matter what bit rate you set for it; it will run as fast as it can no matter what.

A simple method is to read into a buffer, until you get the 10 ('\n' newline) character.
Then, read the command out of that buffer.



char buffer[128];
int bufptr = 0;

void setup() {
SerialUSB.begin();
}

void startthecommand() {
int id, pos;
if (2 == sscanf(buffer, " pos %d %d", &id, &pos)) {
// set servo id to position pos
...
}
else if ( ... ) {
}
else {
// error
}
}

void loop() {
while (SerialUSB.available()) {
buffer[bufptr++] = SerialUSB.read();
if (bufptr == 127 || buffer[bufptr-1] == 10) {
buffer[bufptr] = 0;
startthecommand();
bufptr = 0;
}
}
}

tician
10-16-2016, 01:13 PM
If you use the USB port, then simply pass simple ASCII messages between the two using SerialUSB ("S" for stop; "F" for feed; etc.). The OpenCM SerialUSB object handles all the USB packet stuff, so you just use "SerialUSB.begin(baudrate)" to match the RPi's baudrate then use "write()" to send and "available()" and "read()" to receive messages. For the RPi, accessing the USB interface would be easiest with python but can be done in just about any language. The OpenCM will appear as a serial port of some name (ttyUSB, ttyACM, etc.) when connected over USB.

Using one of the actual OpenCM UARTs to communicate with the RPi using its serial port would not be particularly pretty or useful unless you absolutely cannot have the OpenCM connected as a USB device. Think you can just operate the additional UARTs of the OpenCM as SerialN (where N is 1~3?) and use the normal Arduino Serial object function calls (begin(), write(), available(), read(), etc.).

Maxzor
12-06-2016, 02:34 PM
Hello again and thanks for your help already.
Still I am quite stuck... I figured a good step would be to install the OpenCM IDE on the RPi0, but how?
Downloads are for x86 architectures only, is there an ARM build available, or even the entire OPENCM source code?

I got in my head to directly download sketches from the Raspberry Pi to the OpenCM 9.04, instead of running a polling loop in the OpenCM waiting for SerialUSB packets. It would be great, although I have no idea on how to download sketches without a human input, directly from software.

Hugh
12-06-2016, 10:34 PM
I think the IDE is a rebranded Arduino IDE which looks like it is Java based.
You could try the rpi version of Arduino IDE and see if you can select the OpenCM board - might need to copy some header/hardware files from the OpenCM distro.
Another possibility would be to program the board over USB from a windows box, and then use OpenCM Serial3 TX/RX = pin D24/D25 to simultaneously talk to your RPi over a non-usb UART.
Or just program it from windows, unplug the usb and plug it into you rpi when it comes time to testing/controlling. You might spend half an hour of your life unplugging/plugging 30 times, but this could still be faster than the other solutions.

tician
12-07-2016, 12:56 AM
Just by being powered, the OpenCM will already be polling the SerialUSB interface for the reset sequence needed prior to uploading a new sketch. There is absolutely no benefit to having the IDE on the RPi instead of just passing very simple text commands over the SerialUSB interface. Rewriting the sketch just to change a servo position is way more effort - with many more potential points of failure - than polling a Serial object and interpreting the message to move the servo.

Maxzor
12-07-2016, 02:48 AM
I get it, but at the moment when I download (from Windows) sketches it looks like wether they are stored in RAM or that the bootloader clears the flash on next reboot. How to durably program the openCM9.04?

jwatte
12-07-2016, 12:57 PM
The sketch is durably stored in flash when you download it. The OpenCM should re-start the last downloaded sketch each time you power it on.

I tend to add a short sequence of blinking the LED three or four times in the init() part of the sketch, so I can tell that it's actually starting the sketch.

Maxzor
12-07-2016, 02:57 PM
I get sketches running indefinitely, but when I unplug USB and repower, either from USB-Windows or an external 7V, the board looks reset and the dynamixel doesnt move anymore.
?

tician
12-07-2016, 05:17 PM
Is the OpenCM USB serial port still being used by a running program when it is unplugged? If so, then the port number of the OpenCM will change when it gets plugged in again. It will always require restarting the program communicating over the USB serial port any time the OpenCM is unplugged as the port completely disappears from the system.

jwatte
12-07-2016, 07:36 PM
Also, writing enough to SerialUSB so the buffer fills up, when the board is not actually plugged in, will block the sketch forever. This is a "feature" of the OpenCM firmware.
The fix is to not write to SerialUSB when you're not plugged in.
How do you know if you're plugged in? I take the presence of some data to read from SerialUSB to mean it's been connected. This means I have to send a newline or something in the monitor app before I actually start getting data back.
It also means all SerialUSB.write() / SerialUSB.print() commands need to be wrapped in an if statement somehow.

Hugh
12-07-2016, 10:24 PM
The OpenCm will "forget" what it was doing if you give it a command and unplug it. ie, the program will restart.
It depends on what you get your program to do when it starts (the blinking LED was a good idea).
Usually you would then drop into a "get next command" serial loop. I usually make the 'h' command print 'hello' so you know its all working.
It actually freaked me out a bit then I plugged in my opencm after not using it a while and it started moving my motors around. Until I remembered I had a test program that oscillated the motors for testing.