Problem communicating sensors data to PC

Hi!
I have an arduino mega which has connected 2x I2C Inertial Sensors and 1x GPS Unit
The GPS is connected throug the Serial 3 port of the arduino mega at 115200 bps
So i want to read the values of pitch roll and yaw from my inertial sensor and also get the GPS sentence at the same time through serial port.

This is the code:

int raw_values[11];
float ypr[3]; // yaw pitch roll
float ypr2[3]; // yaw pitch roll

String inputString, str = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

String txtCmp ="";
float acc;
boolean paso;

// Set the FreeIMU object
FreeIMU my3IMU = FreeIMU();
FreeIMU my3IMU2 = FreeIMU();


void setup() { 
  
  
  
  Serial.begin(115200);
  Serial3.begin(115200); //Set Mega hardware port baud to match GPS default baud
  delay(20);
  Serial3.println("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");
  delay(20);
  Serial3.println("$PMTK300,100,0,0,0,0*2C"); //configurar a 10hz
 
  Wire.begin();
  pinMode(2, INPUT); 
  my3IMU.init(); // the parameter enable or disable fast mode
  my3IMU2.init(0x69, false); 
  
  inputString.reserve(200);
}

void loop() { 
  my3IMU.getYawPitchRoll(ypr);
  my3IMU2.getYawPitchRoll(ypr2);
  my3IMU.getRawValues(raw_values); // *******************************
  paso=digitalRead(2);
  acc=sqrt((float(raw_values[0])*float(raw_values[0]))+(float(raw_values[1])*float(raw_values[1]))+(float(raw_values[2])*float(raw_values[2])));
  
  Serial.print(ypr[0]);          //Yaw Imu 1
  Serial.print(',');
  Serial.print(ypr[1]);          //Pitch Imu 1
  Serial.print(',');
  Serial.print(paso);            //"Step detection"
  Serial.print(',');
  Serial.print(ypr2[1]);         //Pitch Imu 2
  Serial.print(',');
  Serial.print(acc);             //Aceleracion
  Serial.print(',');
  if (stringComplete) {
    Serial.print(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
    Serial3.flush();
  }else {
  Serial.println("");
  }

}

void serialEvent3() {
  while (Serial3.available()) {
    // get the new byte:
    char inChar = (char)Serial3.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

i dont have problem with all the values but the NMEA sentence has a strange behavior. sometimes, it doesnt show all the info, and sometimes it shows a series of NMEA sentences.
this is an example of what i'm getting:
25.35,42.74,0,-8.22,16993.92,$GPGGA,133906.000,4027.1719,N,00343.5303,W,2,8,1.92,695.6,M,51.7,M,0$GPGGA,133906.100,4027.1719,N,00343.5303,W,2,8,1.92,695.6,M,51.$GPGGA,133906.200,4027.1719,N,00343.5303,W,2,8,1.92,695.6,M,51.$GPGGA,133906.300,4027.1718,N,00343.5303,W,2,8,1.92,695.6,M,51.$GPGGA,133906.400,4027.1718,N,00343.5303,W,2,8,1.92,695.6,M,51.7,M,0000,000045
27.26,45.29,0,-8.97,16956.06,
28.16,46.40,0,-9.32,16964.70,
29.07,47.53,0,-9.68,17021.97,
30.08,48.72,0,-10.08,16964.64,
31.07,49.83,0,-10.47,16934.58,
32.09,50.90,0,-10.60,16960.73,
33.09,51.96,0,-11.00,17000.94,
34.11,53.05,0,-11.40,17018.12,
35.29,54.23,0,-11.85,17037.05,
36.36,55.29,0,-12.26,16991.98,
37.49,56.38,0,-12.70,17023.97,
38.64,57.42,0,-12.86,16969.81,
39.81,58.43,0,-13.31,16989.26,
41.06,59.53,0,-13.82,16956.13,
42.33,60.60,0,-14.32,16986.73,$GPGGA,133906.500,4027.1718,N,00343.5303,W,2,8,1.92,695.7,M,51.$GPGGA,133906.600,4027.1718,N,00343.5303,W,2,8,1.92,695.7,M,51.$GPGGA,133906.700,4027.1718,N,00343.5302,W,2,8,1.92,695.7,M,51.7,M,0000,0000
46
44.41,62.25,0,-14.64,17019.00,
45.66,63.24,0,-15.14,16968.12,
46.93,64.19,0,-15.65,17036.51,
48.33,65.23,0,-16.23,17044.66,
49.62,66.18,0,-16.78,17015.23,
50.56,66.81,0,-17.34,16901.05,
51.87,67.71,0,-17.57,16967.59,
52.59,68.22,0,-17.78,16979.10,
53.91,69.11,0,-18.37,16897.28,
55.24,70.00,0,-18.97,17052.08,
56.51,70.84,0,-19.54,16967.96,
57.29,71.35,0,-19.95,17047.26,
58.52,72.14,0,-20.55,16928.76,
60.02,73.08,0,-21.27,17016.70,$GPGGA,133906.800,4027.1717,N,00343.5302,W,2,8,1.92,695.7,M,51.$GPGGA,133906.900,4027.1717,N,00343.5302,W,2,8,1.92,695.7,M,51.$GPGGA,133907.000,4027.1716,N,00343.5302,W,2,8,1.92,695.7,M,51.,M,0000,0000*4E
62.21,74.42,0,-22.36,16872.66,
63.54,75.23,0,-23.03,17059.12,
64.83,75.99,0,-23.72,17006.17,
66.13,76.76,0,-24.43,16975.14,
67.48,77.55,0,-25.16,17061.47,
68.77,78.30,0,-25.89,17162.62,
69.98,79.00,0,-26.61,17006.36,
71.24,79.71,0,-27.39,16978.00,
72.47,80.41,0,-28.17,16973.06,
73.75,81.13,0,-28.57,17043.46,

and This is an example of what i want:
127.61,72.31,0,18.29,16880.72,$GPGGA,120155.200,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,00004E
127.51,72.36,0,18.21,16855.44,
127.40,72.42,0,18.13,16884.47,
127.31,72.42,0,18.05,16934.32,
127.20,72.49,0,17.97,17038.03,
127.11,72.48,0,17.89,16943.43,$GPGGA,120155.300,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
4F
127.01,72.54,0,17.81,16880.81,
126.91,72.60,0,17.74,16871.42,
126.80,72.66,0,17.67,16890.56,
126.70,72.72,0,17.59,16885.39,
126.61,72.78,0,17.50,16976.71,$GPGGA,120155.400,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,000048
126.50,72.84,0,17.42,16892.15,
126.40,72.90,0,17.35,16916.25,
126.30,72.96,0,17.28,16900.62,
126.19,73.02,0,17.20,16872.01,
126.09,73.09,0,17.13,16861.00,$GPGGA,120155.500,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
49
125.98,73.15,0,17.04,16911.52,
125.88,73.21,0,16.97,16941.49,
125.78,73.27,0,16.90,16972.92,
125.68,73.33,0,16.82,16999.74,
125.59,73.33,0,16.74,16917.99,$GPGGA,120155.600,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,00004A
125.48,73.39,0,16.65,16854.59,
125.39,73.44,0,16.58,16979.94,
125.31,73.43,0,16.51,16873.27,
125.23,73.41,0,16.43,16859.85,
125.14,73.46,0,16.36,16992.66,$GPGGA,120155.700,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
4B
125.02,73.52,0,16.28,16952.17,
124.93,73.57,0,16.21,16951.34,
124.83,73.62,0,16.13,16908.24,
124.73,73.68,0,16.06,16908.26,
124.66,73.66,0,15.98,16914.39,$GPGGA,120155.800,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,000044
124.56,73.72,0,15.90,16942.22,
124.46,73.77,0,15.83,16936.60,
124.37,73.82,0,15.76,16948.23,
124.25,73.89,0,15.69,16940.13,
124.17,73.95,0,15.61,17006.44,$GPGGA,120155.900,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
45
124.06,74.01,0,15.53,17033.61,
123.97,74.06,0,15.46,16960.93,
123.88,74.12,0,15.39,16973.43,
123.78,74.17,0,15.31,16901.84,
123.68,74.22,0,15.24,17000.01,$GPGGA,120156.000,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,00004F
123.57,74.27,0,15.16,16967.65,
123.46,74.33,0,15.09,16939.84,
123.37,74.39,0,15.02,16993.97,
123.27,74.45,0,14.94,16939.63,
123.18,74.50,0,14.87,16885.98,$GPGGA,120156.100,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
4E
123.06,74.55,0,14.79,16848.45,
122.98,74.60,0,14.72,16934.84,
122.90,74.59,0,14.65,16934.07,
122.79,74.63,0,14.58,16920.93,
122.70,74.67,0,14.51,16914.98,$GPGGA,120156.200,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*4D

Try here

Yes , i have read it!
also the arduino reference and playground, but i dont see any similar situation as mine.

thanks.

Try re-reading section 6.

checked!

hope is clear enoguh!

you tried this?
http://arduino.cc/playground/Tutorials/GPS

did you set that baud rate or were you instructed to set it at that?

can you please post your Full code, and possibly a link to where you got it.

yes, i flashed my gps to 115200, i have a 10hz update rate from GPS.
the complete code is posted, .

Thanks!

Pure guess, but at 10Hz, that's a lot of data through a 64 byte input buffer - are you shifting it out fast enough?

i configured the GPS for getting just the $GPGGA sentence,
what i get from the GPS is a sentence like this:
$GPGGA,120155.400,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*48
it has 79 characters, is it to big???

OTOH, what is this Serial3.flush(); doing?

I tried before but didnt notice difference'
But im not sure where in the code should i use it

Which version of the IDE are you using?

You don't need the flush just drop it.

Set the baud rate for the Serial3 to somthing less (half) than that of Serial. It helps if the output (of the arduino) runs faster than the input.

You can just read chars from Serial3 and write then stright to Serial. There is no need for the buffer.

Mark

@AWOL: I'm Using IDE 1.0.1

@holmes4: Thank you very much for your advice, now it works, but aftter about 1 1/2 minute it starts doing again the same thing.

Could you post your upto date code and results?.

I'm worried by use of the class String it has problems which can cause some very strange problems. Take a look in the programming forum for post about String.

Mark

Hi Mark, this is the code i'm using:

#include <ADXL345.h>
#include <HMC58X3.h>
#include <ITG3200.h>
#include <bma180.h>
#include <MS561101BA.h>
//#define DEBUG
#include "DebugUtils.h"
#include "FreeIMU.h"
#include "CommunicationUtils.h"
#include <Wire.h>
#include "I2Cdev.h" 
#include "MPU6050.h"

int raw_values[11];
float ypr[3]; // yaw pitch roll
float ypr2[3]; // yaw pitch roll

String inputString, str = "";        
boolean stringComplete = false;  

float acc;
boolean paso;

FreeIMU my3IMU = FreeIMU();
FreeIMU my3IMU2 = FreeIMU();


void setup() { 
  Serial.begin(115200);
  Serial3.begin(57600); 
  delay(20);
  Serial3.println("$PMTK300,100,0,0,0,0*2C"); //configurar a 10hz
  Wire.begin();
  pinMode(2, INPUT); 
  my3IMU.init(); // the parameter enable or disable fast mode
  my3IMU2.init(0x69, false); 
  inputString.reserve(200);
}

void loop() { 
  my3IMU.getYawPitchRoll(ypr);
  my3IMU2.getYawPitchRoll(ypr2);
  my3IMU.getRawValues(raw_values); // *******************************
  paso=digitalRead(2);
  acc=sqrt((float(raw_values[0])*float(raw_values[0]))+(float(raw_values[1])*float(raw_values[1]))+(float(raw_values[2])*float(raw_values[2])));
  
  Serial.print(ypr[0]);          //Yaw Imu 1
  Serial.print(',');
  Serial.print(ypr[1]);          //Pitch Imu 1
  Serial.print(',');
  Serial.print(paso);            //"Step detection"
  Serial.print(',');
  Serial.print(ypr2[1]);         //Pitch Imu 2
  Serial.print(',');
  Serial.print(acc);             //Aceleracion
  Serial.print(',');
  if (stringComplete) {
    Serial.print(inputString); 
    inputString = "";
    stringComplete = false;
    Serial3.flush();
  }else {
  Serial.println("");
  }

}

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read(); 
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

And this is the result:

-39.35,-1.31,0,-12.44,16339.55,$GPGGA,123007.100,4027.1656,N,00343.5385,W,1,5,3.12,669.5,M,51.$GPGGA,123007.300,4027.1655,N,00343.5385,W,1,5,3.12,669.5,M,51.7,M,$GPGGA,123007.400,4027.1655,N,00343.5384,W,1,5,3.12,669.5,M,51.7,M,,$GPGGA,123007.500,4027.1655,N,00343.5384,W,1,5,3.12,669.5,M,51.7,M,,*4E
-39.34,-1.31,0,-12.45,16377.27,
-39.35,-1.31,0,-12.45,16410.62,
-39.34,-1.30,0,-12.46,16214.73,
-39.33,-1.30,0,-12.46,16358.73,$GPGGA,123007.600,4027.1655,N,00343.5383,W,1,5,3.12,669.5,M,51.7,M,,*4A
-39.34,-1.31,0,-12.48,16427.75,
-39.34,-1.31,0,-12.47,16282.03,
-39.34,-1.31,0,-12.48,16386.95,
-39.34,-1.31,0,-12.47,16314.30,
-39.34,-1.31,0,-12.47,16382.99,$GPGGA,123007.700,4027.1654,N,00343.5383,W,1,5,3.12,669.4,M,51.7,M,,*4B

i dont know if is clear enough, but if you see the first line of the results, it prints the gps sentence $GPGGA continuosly and overlaped with the others.
what i expect is just to see something like this:

127.61,72.31,0,18.29,16880.72,$GPGGA,120155.200,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,00004E
127.51,72.36,0,18.21,16855.44,
127.40,72.42,0,18.13,16884.47,
127.31,72.42,0,18.05,16934.32,
127.20,72.49,0,17.97,17038.03,
127.11,72.48,0,17.89,16943.43,$GPGGA,120155.300,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
4F
127.01,72.54,0,17.81,16880.81,
126.91,72.60,0,17.74,16871.42,
126.80,72.66,0,17.67,16890.56,
126.70,72.72,0,17.59,16885.39,
126.61,72.78,0,17.50,16976.71,$GPGGA,120155.400,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,000048
126.50,72.84,0,17.42,16892.15,
126.40,72.90,0,17.35,16916.25,
126.30,72.96,0,17.28,16900.62,
126.19,73.02,0,17.20,16872.01,
126.09,73.09,0,17.13,16861.00,$GPGGA,120155.500,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
49
125.98,73.15,0,17.04,16911.52,
125.88,73.21,0,16.97,16941.49,
125.78,73.27,0,16.90,16972.92,
125.68,73.33,0,16.82,16999.74,
125.59,73.33,0,16.74,16917.99,$GPGGA,120155.600,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,00004A
125.48,73.39,0,16.65,16854.59,
125.39,73.44,0,16.58,16979.94,
125.31,73.43,0,16.51,16873.27,
125.23,73.41,0,16.43,16859.85,
125.14,73.46,0,16.36,16992.66,$GPGGA,120155.700,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000
4B

I think your problem is in this line

inputString += inChar;

in serialEvent3().

You need to use an array of char and not a String!

String has a bug which makes a mess of the heap and then makes a mess of the ram!

Here is a bit of code which (I think - I can't test it) should fix things. It does build.

volatile char inputString[256]; // is 256 bytes big enough to hold the whole GPS response?
volatile int i = 0;
volatile boolean stringComplete = false;

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read(); 
    inputString[i] = inChar;
    i++;
    if (inChar == '\n') {
      stringComplete = true;
      inputString[i] ='/0';
    } 
  }
}

You need to set i to 0 when you set stingComplete = false in loop().

As far as I could work serialEvent is, if not actually an ISR, it must be called from one. So any thing it changes needs to be volatile.

If this does not work try renaming serialEvent3() and then calling it from loop().

Mark

As far as I could work serialEvent is, if not actually an ISR, it must be called from one.

serialEvent is simply called inside "main" after "loop". (source hardware/arduino/cores/arduino/main)
It is most certainly not an ISR, and if your "loop()" never returns, it will never be called.
Variables do not need to be treated as volatile.

serialEvent is simply called inside "main" after "loop". (source hardware/arduino/cores/arduino/main)
It is most certainly not an ISR, and if your "loop()" never returns, it will never be called.
Variables do not need to be treated as volatile.

That's good to know.

Mark

With 1.0 and on, the Serial.flush() method blocks until all outgoing serial data has been shifted out (at least into the outgoing register). How does that help you? The whole idea behind interrupt driven serial output is to eliminate the blocking.

The ONLY time you need to wait for the outgoing buffer to be empty is before you put the Arduino to sleep. Are you planning to do that? If so, that'll really hurt your performance.

If not, get rid of the flush!