Pages: 1 [2]   Go Down
Author Topic: Java + Firmata interfacing with Arduino  (Read 4311 times)
0 Members and 1 Guest are viewing this topic.
Chile
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino and this forum members rock!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Agib,
your java class is great. And if we can use it to send strings to Arduino, why not? I have no problem with that.
However, the issue now is that Arduino has to be able to read the information coming from serial (sent by our java API). It does well with the Serial.read() but byte per byte (this is my understanding). As an example, I want a variable declared in the firmware uploaded into Arduino as:

unsigned long Time = 0123456789;

Then "Time" would used as a time and date variable (in unix_t or EPOCH time format - see http://www.epochconverter.com ) by receiving its value from USB / and a host computer's java API.  As you see, my variable has 10 bytes... so with Serial.read() in Arduino's firmware it seems to be too complex to get these and the other 30 variables loaded with their initial values from serial sent by a java API if in Arduino I use the "default" Serial library.

Our java classes and rxtx for the java API solved the connection problem. Now the communication is the next step. Using "Serial" library (which includes Serial.read) seems to be very limited.  However, with Firmata it would be great. Your class is not competing with Firmata, I never had in my mind it attempted to replace Firmata since the problem now resides in Arduino's serial reading capability. Let me explain it; to use Firmata as a standard for communication we have to use it in both sides, Arduino board and the java API on the host computer. That is what France is asking too get, but building the host computer's API in Java. We can upload Firmata to Arduino but we do not have yet a Firmata version on the Java API developing side.
 :'(
If moving our API form Java to C++, there are Firmata versions for the host computer (GPL for linux and LGPL for Windows). But we want to stand with Java. A solution is using "Processing": in one side uploading Firmata to Arduino and on the host computer including the "Firmata version for Processing" in Processing ( http://processing.org ). But Processing is not like using Eclipse or Netbeans to develop a Java API... We need a Firmata Java version or to improve the Serial.read function of Serial library for Arduino board... as in my case it would be enough something like Serial.readString() function. Then what we have in our java classes would be enough.
Thanks for sharing your thoughts!
Maybe France found a workaround for that library...  smiley-wink
kind regards,
gps
Logged

Chile
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino and this forum members rock!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Agib,
please excuse me. I sent my last post just when receiving yours.

what are you using on the Arduino board side to read the information from the serial port? Firmata or Serial.read()?

You may well have got it!
« Last Edit: June 14, 2009, 05:22:35 pm by GPSanino » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Firmata Arduino library has an example called SimpleDigitalFirmata which implements the callbacks to listen for commands from the computer, then sets the digital pins correspondingly.

It also appears to send the input state of the digital pins back to the computer, but I've tried fiddling with this and it doesn't behave as I'd expect, so this will need more looking into.

I think we certainly have the beginnings of what could become a Firmata API in Java. The existing code could do with a clean up though! smiley-grin

It's worth noting, the Firmata library doesnt provide any methods for communicating values or strings, so these would have to be implemented using the sysex commands. If all you need is the ability to pass strings back and forth, then Firmata might be overkill.
« Last Edit: June 14, 2009, 05:57:59 pm by agib » Logged

Chile
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino and this forum members rock!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Agib,
just as you said...  your version of arduino.java is a beginning of a Firmata version for Java APIs. I did the same test you did, under linux and it behaved just as you mentioned.
I just changed the comm port in Example.java  and deleted the lib folder. For RXTX, now I am using the plug-in for Eclipse, proposed on
 http://rxtx.qbang.org/wiki/index.php/Wrapping_RXTX_in_an_Eclipse_Plugin
It was tested  for Mac and Windows, but I had no problems to implement it for Linux. It helps to adapt your java APIs to several platforms or environments - not just the one of your computer.
 
I am too much a newbie on coding to be able to help on the clean up... Maybe David A. Mellis can be contacted and can help since he did the Firmata for Processing. He is one of the Administrators of the forum and maybe he can help in finding what is still needed.

Overkilling way to solve my problem?  I feel the same... but I do not know how to build a string from Serial.read(), maybe there is something for concatenating that I have not find, and I should put more effort into that path rather than Firmata for Java. But France's question is one that I guess many are also sharing and I did not mind to try to help on it.
Maybe she can help too on the cleaning up process. smiley-wink

Great work!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sending strings backwards and forwards isn't that difficult, but re-reading your earlier post it sounds like you want to sent the unix_t value from the computer to the arduino. You could send this as a string, but that would be 10 bytes, or you could send it as 4 bytes, then combine them back to the unsigned long on the arduino.

Perhaps you could clarify what it is you're trying to achieve?

I'll take a look at that link when I get a chance tomorrow, it looks interesting.
Logged

Chile
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino and this forum members rock!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Agib, you are right. That is what I want to do. I have a firmware on arduino that does what I want it to do. However it changes into different behaviors depending on the value of some variables as pre-settings. One of them is the "Time" variable. There are about 30 different variables. Most of them use lesser than three bytes but some use 10 as "Time" does.
All this, I can update and upload into and by Arduino IDE, to the Arduino board. It works fine, but I am willing to make a Java API that does this task in a more effective or friendly way, so anybody can use it. It is not a Java API to upload firmwares to Arduino but just send some values through the USB to Arduino and then I use it as a stand alone device loaded with these values.
In Arduino I am trying to make a function that works at the beginning of the code, sending a string or char to the host computer while waiting for connection for a limited time (10 seconds?). If connected to the USB and the java AI receives this string, then acknowledge the connection and sends back the values for those 30 variables. Then I can disconnect Arduino from USB and it is up and running with the new values.
That is the general idea, but in Arduino if I use Serial.read() it receives one byte per byte and do not know how to concatenate them. I was thinking into some character like "|" to separate the values between the variables but again, this does not solve the problem of how to connect the bytes together in order to get the full value of each variable instead of its value on separate bytes. I do not know how to combine those separate bytes, that is why I was thinking about trying the Firmata path.
I still have 18000 bytes of space for coding into Arduino.
thanks!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, the arduino initially needs to send something (just a char will do) to let the computer know it's alive. That we can already do using the Serial.print() on the arduino, and the existing read() in the java app.

As for sending the data, you don't need to send it in its ASCII form of 10 digits, if it is a 4 byte number then you just send those 4 bytes.

On the java side you'd have something like:
Code:
//Important to note, a "Java long" is not the same as a arduino long
//Because javas primitive types are all signed you need to use a type
//larger than you want on the arduino (you could use a int, but it would
//behave oddly when working with it in java)
long timeValue = 1234567890;

//This splits the value into individual bytes
byte b1 = (timeValue>>32)&0xFF;
byte b2 = (timeValue>>16)&0xFF;
byte b3 = (timeValue>>8)&0xFF;
byte b4 = timeValue&0xFF;

serial.write(b1);
serial.write(b2);
serial.write(b3);
serial.write(b4);

And on the arduino:

Code:
unsigned long timeValue = 0;

//Reassemble
timeValue |= Serial.read()<<32;
timeValue |= Serial.read()<<16;
timeValue |= Serial.read()<<8;
timeValue |= Serial.read();

That's the general idea anyway.
Logged

Chile
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino and this forum members rock!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Agib, that looks great. I will try it today.
About Firmata I saw there is a forum on the its website. Maybe to post there your steps so more people can contribute in getting the clean up?
I did not know the << tool. Very interesting.
thanks a lot!
Logged

Chile
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino and this forum members rock!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Agib, I tried your method but as well as with the method rpesented on the DateTime library example, to set actually 53 variables I have to use many other as headers and footers, etc. Ending with Arduino collapsing...
I was looking for another way to receive data from Serial Port, and as an alternative to Firmata, see this solution:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247005964
regards,
gps
Logged

Pages: 1 [2]   Go Up
Jump to: