error reading gps and accelerometer

Hi,

My project is to build a motorbike logger recording accelerometer and GPS data on a SD card.

My accelerometer is a MPU6050, generating data at 100Hz.
I used this code http://www.kauailabs.com/robotics/kauai-labs-releases-port-of-latest-invensense-mpu-60509150-driver-to-arduino-platform/ which generates interrupts each time a new data is available.

My GPS is a UBLOX NEO-6M, configured with 115200 bps serial communication, 5Hz update rate.

Due to communication time from GPS to arduino, new data from the accelerometer are available while the GPS messages are transfered.
So I used a String buffer to store the GPS sentences and output them to the serial monitor only when the message transmission is finished.

Here is my code to buffer GPS data :

  String GPSbuffer = "";


  GPSbuffer.reserve(200);

   ....


// process GPS data
    while (GPSUART.available()) {
        char inChar = GPSUART.read();
        if (inChar == '

I get the following output :

MPU TimeStamp 85050	YPR -0.61 -3.21 -0.02	0
MPU TimeStamp 85060	YPR -0.61 -3.21 -0.02	0
MPU TimeStamp 85070	YPR -0.61 -3.21 -0.02	0
GPS Timestamp 85063	$GPGGA,000828.60,4341.82420,N,00529.39033,E,1,05,3.43,248.6,M,47.7,M,,*5F

GPS Timestamp 85074	$PUBX,00,000828.60,4341.82420,N,00529.39033,E,296.230,G3-0.201,.03,5,0,0*7A

MPU TimeStamp 85083	YPR -0.61 -3.21 -0.02	0

Which is fine....but sometimes my GPS message is corrupted :

PU TimeStamp 165005	YPR -0.80 -3.27 -0.00	0
MPU TimeStamp 165015	YPR -0.80 -3.27 -0.00	0
GPS Timestamp 165018	$PUBX,00,000948.40,4341.81869,N,00529.38609,E,303.817,G2,0.000,$GPGGA,000948.60,4341.81868,N,00529.38609,E,1,04,2.11,256.2,M,47.7,M,,*59

MPU TimeStamp 165033	YPR -0.80 -3.27 -0.00	0

The end of message \n seems not always detected. This gets even worse when I click on the "autoscroll" button of the serial monitor.

When I don't output the accelerometer, every thing works fine.

Any Idea that could help?

Thanks
)
        GPStimestamp = millis();
               
        if (inChar == '\n') {
            Serial.print("GPS Timestamp ");
            Serial.print(GPStimestamp);
            Serial.print("\t");
            Serial.println(GPSbuffer);
            GPSbuffer="";
        }
        else {
            GPSbuffer += (char)inChar;
        }


I get the following output :

§DISCOURSE_HOISTED_CODE_1§


Which is fine....but sometimes my GPS message is corrupted :

§DISCOURSE_HOISTED_CODE_2§


The end of message \n seems not always detected. This gets even worse when I click on the "autoscroll" button of the serial monitor.

When I don't output the accelerometer, every thing works fine.

Any Idea that could help?

Thanks

Any Idea that could help?

Yes.

  1. Since you appear to be using a HardwareSerial device, GPSUART, hook the "buffering or parsing" code directly to the RX char interrupt with NeoHWSerial. As it is now, the Arduino already handles the RX interrupt and puts the character in the input buffer. Then you have to call available and read to get the character out of the input buffer, and then you have to put it in your own buffer. Just put it in the right place the first time, during the RX interrupt, by using NeoSerial1.attachInterrupt (or whatever GPSUART is using).

  2. Do you really want to log all the GPS data, or do you just want time, location and speed? Logging all the GPS data is about 200 bytes @ 5Hz, or about 1kb/sec. (I assume you are only saving GGA and PUBX,00 sentences.) If you only save the time, location and speed, it would only be ~50bytes @ 5Hz, or about 250 bytes/sec. The raw character stream is 4 times bigger than the parsed data fields. If you want to consider saving the parsed data instead of the raw characters, you might take a look at NeoGPS. I wrote this library specifically for resource-limited applications. It can be hooked to the RX interrupt so that no buffering RAM is required.

jboyton has written a sketch that writes the raw GPS data to an SD card. It uses double-buffering (i.e., extra RAM) and his own SoftwareSerial library. I hope he'll chime in shortly.

  1. You are probably trying to do too much. The debug prints will eventually block, which will allow the input buffer to overflow. Writing to the SD card can have the same effect.

By handling each character just once (suggestion 1) in an interrupt-driven style, and reducing the amount of data you handle (suggestion 2), the problem is much more solvable. It you don't use NeoHWSerial, you will have to change your loop structure to take advantage of the GPS quiet time, to avoid input buffer overflow (suggestion 3).

Cheers,
/dev