Serial writer

Ask for a new OpenViBE feature.
Post Reply
BrainStorm
Posts: 2
Joined: Sat Feb 26, 2011 7:02 pm

Serial writer

Post by BrainStorm »

Hello every1!

Firstly, i would like to thanks all the developers of this great and innovative project, that i just discovered and... yes, i felt in love.
I was thinking... OpenViBE is great, but it lacks of a serial writer? Or it has one? I mean... a layer that can write conditional datas on a serial port, so prototyping would just be flawless...
When i say conditional data, i'm talking about some kind of "filtering"... something like "go to right" and "go to left" if a certain signal is acquired, then write it on a serial port, so there can be another software on a lower level that can command and drive any peripheric which support serial communication... is it possible?

Many thanks again for this great job!!!

See you,
Dino

ddvlamin
Posts: 160
Joined: Thu Aug 13, 2009 8:39 am
Location: Ghent University
Contact:

Re: Serial writer

Post by ddvlamin »

BrainStorm wrote:Firstly, i would like to thanks all the developers of this great and innovative project, that i just discovered and... yes, i felt in love.
I had the same feeling when I first saw the project. You can immediately see that it is made by experienced software developers. With not too much effort you can easily start to write your own plugins (http://openvibe.inria.fr/documentation/ ... lugin.html this is a great place to start )
BrainStorm wrote:I was thinking... OpenViBE is great, but it lacks of a serial writer? Or it has one? I mean... a layer that can write conditional datas on a serial port, so prototyping would just be flawless...
When i say conditional data, i'm talking about some kind of "filtering"... something like "go to right" and "go to left" if a certain signal is acquired, then write it on a serial port, so there can be another software on a lower level that can command and drive any peripheric which support serial communication... is it possible?
To my knowledge there is no box to write commands to the serial port (for example a command based on the output of a classifier). However there are VRPN boxes ( http://openvibe.inria.fr/documentation/ ... erver.html ) which you can use for this purpose. The only thing you have to do is to write your own client program which connects to the VRPN server box that is initialized in OpenViBE. This way you can easily balance the load across different cores or even computers (because you have two separate applications).

In my experience, the problem with some communication protocols (to control some external device) is that you have to wait for an answer. In a real-time application such as OpenViBE you have to keep processing the signals and thus there is no time to wait for responses. So you can and are off course welcome to write a box for serial port communication, but it will probably need to be executed in a separate thread within OpenViBE then.

For the VRPN (the analog case) you will need code like that (was given to me by my student):

Code: Select all

#include <iostream>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "vrpn_Analog.h"

using namespace std;

//Callback handler
void   VRPN_CALLBACK handle_analog(void *userdata, vrpn_ANALOGCB b);

int main(int argc, const char* argv[]){
	
	vrpn_Analog_Remote *ana;

	// initialize the analog
	ana = new vrpn_Analog_Remote("openvibe-vrpn@localhost");

	// Set up the analog callback handler
	ana->register_change_handler(NULL, &handle_analog);

	// main interactive loop  
	while ( 1 ){
		// Let the  analog device do its thing
		ana->mainloop();
	}
	return 0;
}

void   VRPN_CALLBACK handle_analog(void *userdata, vrpn_ANALOGCB b){
	for (int i=0; i< b.num_channel; i++)
	printf("Chan[%d] = %lf\n", i, b.channel[i]);
}
and for serial port communication

Code: Select all

boolean initTty(FD_TYPE * m_i32FileDescriptor)
{
	char * l_ttyname = "\\\\.\\COM1";

	DCB dcb = {0};
	*m_i32FileDescriptor = CreateFile((LPCSTR) l_ttyname, GENERIC_WRITE | GENERIC_READ, 
                  0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

	if (*m_i32FileDescriptor == INVALID_HANDLE_VALUE) 
	{
		printf("Could not open Communication Port %s.\n",(char *) l_ttyname);
		return false;
	}
    
	if (!GetCommState(*m_i32FileDescriptor, &dcb))      // get current DCB settings
	{ 
		return false; 
	}

    // update DCB rate, byte size, parity, and stop bits size
	dcb.DCBlength = sizeof(dcb);
	dcb.BaudRate = CBR_115200; //TERM_SPEED;
	dcb.ByteSize = 8;
	dcb.Parity   = NOPARITY;
 	dcb.StopBits = ONESTOPBIT;
	dcb.EvtChar = '\0';
   
    // update flow control settings
 	dcb.fDtrControl     =  DTR_CONTROL_ENABLE;
 	dcb.fRtsControl     =  RTS_CONTROL_ENABLE;
	dcb.fOutxCtsFlow    = FALSE; //must be FALSE, robot arm does not send back a clear-to-send signal
	dcb.fOutxDsrFlow    = FALSE;
	dcb.fDsrSensitivity = FALSE;;
	dcb.fOutX           = FALSE;
	dcb.fInX            = FALSE;
	dcb.fTXContinueOnXoff = FALSE;
	dcb.XonChar         = 0;
	dcb.XoffChar        = 0;
	dcb.XonLim          = 0;
	dcb.XoffLim         = 0;
	dcb.fParity = FALSE;
	dcb.EvtChar = '.';

	SetCommState(*m_i32FileDescriptor, &dcb);
	SetupComm(*m_i32FileDescriptor, 1024, 1024);
	EscapeCommFunction(*m_i32FileDescriptor, SETDTR);
	SetCommMask (*m_i32FileDescriptor, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
    
	return true;
 }

void closeTty(FD_TYPE m_i32FileDescriptor)
{
	CloseHandle(m_i32FileDescriptor);
}
Best regards,
Dieter Devlaminck

BrainStorm
Posts: 2
Joined: Sat Feb 26, 2011 7:02 pm

Re: Serial writer

Post by BrainStorm »

Many thanks for the reply ddvlamin!

I'm not a good c++ programmer, but in the past i developed a multiagent application with Java, so i think i can manage the communication between OpenViBE and my app by developing a "wrapper" between the acquisition server (or the VRPN) and the output data. The fact that you need to wait the answer, is good manageable (i think) by making different agents dedicated works.
An infrastructure like this:

Brain pulses -> OpenViBE -> an agent evaluating the input -> an agent for serial writing -> hardware controllers -> an agent monitoring the state and waiting for the reply (answer) -> an agent sending back the output -> an agent evaluating the output (checking for errors or anything else, even the fine going of the thing) -> OpenViBE -> loop it again and again.

Now, since the agents work in infinite loops, every agent could be dedicated on a thread or on a core of the processor, there's no problem with the evaluation on separate threads. It just depends on how you manage the background works, but the real problem is that a thread could afflict (in case of trouble) the running of another thread, propagating error or time-consuming cpu, making it starving or deadlocking... but if you write a good program, by evaluating ALL the "flaws" possibilities, i think it can do the job.

What do you think?
I'm not a good C++ programmer, so i bet i have to study a lot to make my application, and i don't have time, as i'm not a researcher and i work in a computer shop that consume all my day. I'll try to make my best, by the way.
Thanks and thanks again for your code, i will check it out and let you know!
Again thanks!

Dino

________
edit: i think that network sockets can be used for communications and interoperability between different hw/sw architectures...

ddvlamin
Posts: 160
Joined: Thu Aug 13, 2009 8:39 am
Location: Ghent University
Contact:

Re: Serial writer

Post by ddvlamin »

It seems you have more experience with threading than me :) I don't really have a solid experience in this area, but it sounds logical.

I learned to program in Java too. And although C/C++ is a bit more difficult, if you know Java you should be able to program this in C/C++ too.
edit: i think that network sockets can be used for communications and interoperability between different hw/sw architectures...
I think the VRPN connection runs across a TCP/IP connection and the connection between the acquisition server and client too.

I hope you find some time,
Best regards,
Dieter Devlaminck

yrenard
Site Admin
Posts: 645
Joined: Fri Sep 01, 2006 3:39 pm
Contact:

Re: Serial writer

Post by yrenard »

Dear BrainStorm,

if you are working on specific events (in OpenViBE, this is equivalent to reacting to stimulations), then you should also consider scripting this with Lua. Further information can be found from the Lua Stimulator box documentation, the official Lua documentation website, the serial port dedicated page on the Lua wiki and probably this post from my blog explaining how to use an external Lua module in OpenViBE.

Have fun and send us a video of whatever you control with OpenViBE !
Yann

Post Reply