I am using a MPU6050 Accelerometer/Gyro breakout (GY-521) to retrieve data and send it to a java program. I have tried several ways, but I couldn't find a way to send the float values calculated from the Arduino over Serial. Could you please let me know a good way of serial communication with Arduino and a Java Program? Thanks in advance.
UPDATE:
Thank you for your comments and replies. What I have tried is the I2Cdev library code. I could work with it without any issue. Code is in this link. I also could run the processing code to get a visual output. But the problem I have i that I'm not able to send the accelerometer and gyro data to a Java/C# program and process it from there. I tried sending an array of FIFO bytes from the Arduino and convert it from the C# code and processing code, but it didn't give me correct results. Now what i did was, just to send a string of values and receive it from the C# program. But I don't know whether it's good method. I would be glad if you could help me in this. Thanks again.
Arduino
#ifdef OUTPUT_TEAPOT
// display quaternion values in InvenSense Teapot demo format:
teapotPacket[2] = fifoBuffer[0];
teapotPacket[3] = fifoBuffer[1];
teapotPacket[4] = fifoBuffer[4];
teapotPacket[5] = fifoBuffer[5];
teapotPacket[6] = fifoBuffer[8];
teapotPacket[7] = fifoBuffer[9];
teapotPacket[8] = fifoBuffer[12];
teapotPacket[9] = fifoBuffer[13];
Serial.write(teapotPacket, 14); //Serial.write(buf, len)
teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
#endif
#ifdef OUTPUT_ACCEL_STR_RAW
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
String accelGyroRaw = "STX,";
accelGyroRaw += ax;
accelGyroRaw += ',';
accelGyroRaw += ay;
accelGyroRaw += ',';
accelGyroRaw += az;
accelGyroRaw += ',';
accelGyroRaw += gx;
accelGyroRaw += ',';
accelGyroRaw += gy;
accelGyroRaw += ',';
accelGyroRaw += gz;
accelGyroRaw += ',';
accelGyroRaw += "ETX";
Serial.println(accelGyroRaw);
#endif
C# Code
class Program
{
static SerialPort port = new SerialPort("COM7", 115200);
//static int serialCount = 0;
//static int aligned = 0;
//static char[] teapotPacket = new char[14];
//static float[] q = new float[4];
static void Main(string[] args)
{
Console.WriteLine("****** Serial Console Read ********");
port.Open();
port.DataReceived += port_DataReceived;
Console.Read();
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string s = port.ReadLine();
string[] accelGyroVals = s.Split(',');
string[] accl = new string[3];
string[] gyro = new string[3];
if (accelGyroVals[0] == "STX" && accelGyroVals[accelGyroVals.Length - 1] == "ETX\r")
{
accl[0] = accelGyroVals[1];
accl[1] = accelGyroVals[2];
accl[2] = accelGyroVals[3];
gyro[0] = accelGyroVals[4];
gyro[1] = accelGyroVals[5];
gyro[2] = accelGyroVals[6];
Console.WriteLine("a : " + accl[0] + " , " + accl[1] + " , " + accl[2] + "\t g : " + gyro[0] + " , " + gyro[1] + " , " + gyro[2]);
}
}
static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
}
}
}
Sorry for this for lengthy code and if my question is unclear. It's my first time to work with an IMU. Thank you.
==
to compare String contents, but in Java that operator will only compare object instances and you must use the .equals() method to compare contents of Strings. Given the non-capitalization of "string" it appears your posted code is actually C#, but the "==" will subtly fail if you try to make trivial changes to build and run it as java. – Chris Stratton Jul 28 '14 at 14:23