Reading GPS

Hello,

I wrote a program with TinyGPS for GPS reading ang MPU6050 for other sensor. But I could not read GPS data continuously. I read GPS data sometimes 1 second periodically but sometimes no gps data for 5 minutes. The other sensor data works correctly. The sample program is on the following lines.

What caused this situation ?

#include “TinyGPS++.h”
#include “SoftwareSerial.h”
#include “Wire.h”
#include “I2Cdev.h” // kütüphaneler eklendi
#include “MPU6050.h”
#include “LiquidCrystal.h”
#include “math.h”
#include “string.h”
#include “ctype.h”

TinyGPSPlus gps;
SoftwareSerial ss(7,3); //7-Rx 3-Tx
MPU6050 accelgyro;
LiquidCrystal lcd(12,11,6,5,4,8);

#define ToRad 0.01745329252 //radyana dönüştürme (pi/180)
#define ToDeg 57.2957795131 //dereceye dönüştürme (180/pi)
#define frek 100

const int MPU=0x68;

/* MPU-6050’nin I2C haberleşme adresi
SCL - A5
SDA - A4
INT - 5
*/

long enlem;
long boylam;

float AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int16_t ax,ay,az,gx,gy,gz;
double dT;

int LEDPIN=8;
float pitch, yaw, roll;
float roll_1, pitch_1;
float roll_1max = -1000.00;
float roll_1min = 1000.00;
float pitch_1max = -1000.00;
float pitch_1min = 1000.00;
float accTop = 0.0;
float accPitch = 0.0;
float accRoll = 0.0;
float gyroRate = 0.9;
float accRate = 0.1;
float zeroPitch = 1.0;
float zeroRoll = 1.0;
int baslama = 0;
float ofset_accx,ofset_accy,ofset_accz,ofset_gyx,ofset_gyy,ofset_gyz;
float ACX,ACY,ACZ,GYX,GYY,GYZ;
unsigned long zaman=0;
unsigned long zaman1=0;
unsigned long zaman2=0;
unsigned long OkumaSuresi=0;

void setup(){

lcd.begin(16,2);
lcd.print(" GPS “);
lcd.setCursor(1,1);
lcd.print(“COORDINATES”);
delay(1000);
lcd.setCursor(0,0);
lcd.print(” AND ACCEL. “);
lcd.setCursor(0,1);
lcd.print(” GYROS PARAM. ");
delay(1000);
lcd.clear();

Wire.begin();
accelgyro.initialize();
Wire.beginTransmission(MPU);
accelgyro.setFullScaleGyroRange(0); //0 = +/- 250 degrees/sec | 1 = +/- 500 degrees/sec | 2 = +/- 1000 degrees/sec | 3 = +/- 2000 degrees/sec
accelgyro.setFullScaleAccelRange(0);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
pinMode(LEDPIN,OUTPUT);

Serial.begin(9600);
ss.begin(9600);

}

void loop()
{
gprmcOku();
verileriOku();
RP_calculate();
goster();

Serial.print(“Enlem =”);Serial.print(enlem);
Serial.print(" | Boylam =");Serial.print(boylam);
Serial.print(" | AX = “); Serial.print(ACX);
Serial.print(” | AY = “); Serial.print(ACY);
Serial.print(” | AZ = “); Serial.print(ACZ);
Serial.print(” | Temp = "); Serial.print(dT);

Serial.print(" | GX = “); Serial.print(GYX);
Serial.print(” | GY = “); Serial.print(GYY);
Serial.print(” | GZ = "); Serial.print(GYZ);

Serial.print(" | AXO = “);Serial.print(ofset_accx);
Serial.print(” | AYO = “);Serial.print(ofset_accy);
Serial.print(” | AZO = “);Serial.print(ofset_accz);
Serial.print(” | GXO = “);Serial.print(ofset_gyx);
Serial.print(” | GYO = “);Serial.print(ofset_gyy);
Serial.print(” | GZO = ");Serial.print(ofset_gyz);

Serial.print(" |Pitch = “); Serial.print(pitch);
Serial.print(” |Roll = “); Serial.print(roll);
Serial.print(” |Pitch_1 = “); Serial.print(pitch_1);
Serial.print(” |Pitch_1 Max = “); Serial.print(pitch_1max);
Serial.print(” |Pitch_1 Min = “); Serial.print(pitch_1min);
Serial.print(” |Roll_1 Max = “); Serial.println(roll_1max);
Serial.print(” |Roll_1 Min = "); Serial.println(roll_1min);

}

void verileriOku()
{
zaman1 = micros();
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
dT = ( (double) accelgyro.getTemperature() + 11450.70) / 340.0;
AcX=ax; AcY=ay; AcZ=az;
GyX=gx; GyY=gy; GyZ=gz;
zaman2=micros();

ofset_accx=(accelgyro.getXAccelOffset());
ofset_accy=(accelgyro.getYAccelOffset()); // -2359
ofset_accz=(accelgyro.getZAccelOffset()); // 1688
ofset_gyx=(accelgyro.getXGyroOffset()); // 0
ofset_gyy=(accelgyro.getYGyroOffset()); // 0
ofset_gyz=(accelgyro.getZGyroOffset()); ;

ACX=(AcX-ofset_accx)/8192;
ACY=(AcY-ofset_accy)/8192;
ACZ=(AcZ-ofset_accz)/8192;
GYX=(GyX-ofset_gyx)/131;
GYY=(GyY-ofset_gyy)/131;
GYX=(GyZ-ofset_gyz)/131;

}

void hesapla()
{

float gX, gY, gZ, aX, aY,aZ;

accTop =(AcX * AcX);
accTop +=(AcY * AcY);
accTop +=(AcZ * AcZ);
accTop =sqrt(accTop);
if (fabs(AcY) < accTop)
{
accPitch = asin(AcY/accTop)/ToRad;
accPitch += zeroPitch;
}
if (fabs(AcX) < accTop)
{
accRoll = asin(AcX/accTop)/ -ToRad;
accRoll -= -zeroPitch;
}
if(baslama)
{

//pitch = GyX;
// roll = GyY;
// yaw = GyZ;

pitch += (GyX)/frek;
roll += (GyY)/frek;
yaw = (GyZ)/frek;

pitch += roll * sin(yaw) * ToRad;
roll -= pitch * sin(yaw) * ToRad;

pitch = (pitch * gyroRate) + (accPitch * accRate);
roll = (roll * gyroRate) + (accRoll * accRate);
}
else
{
pitch = accPitch;
roll = accRoll;
baslama=1;
}

}

void goster()
{
lcd.setCursor(0,0);
lcd.print(“AX:”);
lcd.print(ACX);
lcd.setCursor(8,0);
lcd.print(“AY:”);
lcd.print(ACY);
lcd.setCursor(0,1);
lcd.print(“AZ:”);
lcd.print(ACZ);
lcd.setCursor(8,1);
lcd.print(“GX:”);
lcd.print(GYX);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“GY:”);
lcd.print(GYY);
lcd.setCursor(8,0);
lcd.print(“GZ:”);
lcd.print(GYZ);
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.setCursor(6,1);
lcd.print(dT);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Enlem :”);
lcd.print(enlem);
lcd.setCursor(0,1);
lcd.print(“Boylam:”);
lcd.print(boylam);
delay(500);
lcd.clear();

}

void RP_calculate()
{
double x_Buff = float(AcX);
double y_Buff = float(AcY);
double z_Buff = float(AcZ);
roll_1 = atan2(y_Buff, z_Buff) 57.3;
pitch_1 = atan2((-x_Buff), sqrt(y_Buff * y_Buff) +(z_Buff
z_Buff))*57.3;
if(roll_1>=roll_1max)
roll_1max = roll_1;
if(roll_1<=roll_1min)
roll_1min = roll_1;
if(pitch_1>=pitch_1max)
pitch_1max = pitch_1;
if(pitch_1<=pitch_1min)
pitch_1min = pitch_1;
}

void gprmcOku()
{
while(ss.available() > 0)
{
gps.encode(ss.read());
}
if(gps.location.isUpdated())
{
Serial.println(gps.location.lng(),6);
Serial.println(gps.location.lat(),6);
Serial.println(gps.time.value());

boylam = (gps.location.lng()) * 1000000;
enlem = (gps.location.lat()) * 1000000 ;

}
}

Hello,

I wrote a program with TinyGPS for GPS reading ang MPU6050 for other sensor. But I could not read GPS data continuously. I read GPS data sometimes 1 second periodically but sometimes no gps data for 5 minutes. The other sensor data works correctly. The sample program is on the following lines.

What caused this situation ?

#include "TinyGPS++.h"
#include "SoftwareSerial.h"
#include "Wire.h"
#include "I2Cdev.h" // kütüphaneler eklendi
#include "MPU6050.h"
#include "LiquidCrystal.h"
#include "math.h"
#include "string.h"
#include "ctype.h"

TinyGPSPlus gps;
SoftwareSerial ss(7,3);   //7-Rx  3-Tx
MPU6050 accelgyro;
LiquidCrystal lcd(12,11,6,5,4,8);

#define ToRad 0.01745329252   //radyana dönüştürme  (pi/180)
#define ToDeg 57.2957795131   //dereceye dönüştürme  (180/pi)
#define frek 100

const int MPU=0x68;

/* MPU-6050'nin I2C haberleşme adresi 
SCL - A5
SDA - A4
INT - 5
*/

long enlem;
long boylam;
 


float AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int16_t ax,ay,az,gx,gy,gz;
double dT;

int LEDPIN=8;
float pitch, yaw, roll;
float roll_1, pitch_1;
float roll_1max = -1000.00;
float roll_1min = 1000.00;
float pitch_1max = -1000.00;
float pitch_1min = 1000.00;
float accTop = 0.0;
float accPitch = 0.0;
float accRoll = 0.0;
float gyroRate = 0.9;
float accRate = 0.1;
float zeroPitch = 1.0;
float zeroRoll = 1.0;
int baslama = 0;
float ofset_accx,ofset_accy,ofset_accz,ofset_gyx,ofset_gyy,ofset_gyz;
float ACX,ACY,ACZ,GYX,GYY,GYZ;
unsigned long zaman=0;
unsigned long zaman1=0;
unsigned long zaman2=0;
unsigned long OkumaSuresi=0;

void setup(){
   
  lcd.begin(16,2);
  lcd.print("      GPS     ");
  lcd.setCursor(1,1);
  lcd.print("COORDINATES");
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("    AND ACCEL.   ");
  lcd.setCursor(0,1);
  lcd.print("  GYROS PARAM.  ");
  delay(1000);
  lcd.clear();
  
  Wire.begin();
  accelgyro.initialize();
  Wire.beginTransmission(MPU);
  accelgyro.setFullScaleGyroRange(0); //0 = +/- 250 degrees/sec | 1 = +/- 500 degrees/sec | 2 = +/- 1000 degrees/sec | 3 =  +/- 2000 degrees/sec
  accelgyro.setFullScaleAccelRange(0);
  Wire.write(0x6B);
  Wire.write(0); 
  Wire.endTransmission(true);
  pinMode(LEDPIN,OUTPUT);
  
  Serial.begin(9600);
  ss.begin(9600);
 
}


void loop()
{
      gprmcOku();
      verileriOku();
      RP_calculate(); 
      goster();  
      
        
      Serial.print("Enlem =");Serial.print(enlem);
      Serial.print(" | Boylam =");Serial.print(boylam);
      Serial.print(" | AX = "); Serial.print(ACX);
      Serial.print(" | AY = "); Serial.print(ACY);
      Serial.print(" | AZ = "); Serial.print(ACZ);
      Serial.print(" | Temp = "); Serial.print(dT);  
     
      Serial.print(" | GX = "); Serial.print(GYX);
      Serial.print(" | GY = "); Serial.print(GYY);
      Serial.print(" | GZ = "); Serial.print(GYZ);
      
      Serial.print(" | AXO = ");Serial.print(ofset_accx);
      Serial.print(" | AYO = ");Serial.print(ofset_accy);
      Serial.print(" | AZO = ");Serial.print(ofset_accz);
      Serial.print(" | GXO = ");Serial.print(ofset_gyx);
      Serial.print(" | GYO = ");Serial.print(ofset_gyy);
      Serial.print(" | GZO = ");Serial.print(ofset_gyz);
   
      Serial.print(" |Pitch = "); Serial.print(pitch);
      Serial.print(" |Roll  = "); Serial.print(roll);
      Serial.print(" |Pitch_1 = "); Serial.print(pitch_1);
      Serial.print(" |Pitch_1 Max = "); Serial.print(pitch_1max);
      Serial.print(" |Pitch_1 Min = "); Serial.print(pitch_1min);
      Serial.print(" |Roll_1 Max  = "); Serial.println(roll_1max);
      Serial.print(" |Roll_1 Min  = "); Serial.println(roll_1min);
            
}

void verileriOku()
{
   zaman1 = micros();
   accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
   dT = ( (double) accelgyro.getTemperature() + 11450.70) / 340.0;
   AcX=ax;  AcY=ay;   AcZ=az; 
   GyX=gx;   GyY=gy;   GyZ=gz;
   zaman2=micros();
  
   ofset_accx=(accelgyro.getXAccelOffset()); 
   ofset_accy=(accelgyro.getYAccelOffset());  // -2359
   ofset_accz=(accelgyro.getZAccelOffset());  // 1688
   ofset_gyx=(accelgyro.getXGyroOffset());  // 0
   ofset_gyy=(accelgyro.getYGyroOffset());  // 0
   ofset_gyz=(accelgyro.getZGyroOffset()); ;
  
   ACX=(AcX-ofset_accx)/8192;
   ACY=(AcY-ofset_accy)/8192;
   ACZ=(AcZ-ofset_accz)/8192;
   GYX=(GyX-ofset_gyx)/131;
   GYY=(GyY-ofset_gyy)/131;
   GYX=(GyZ-ofset_gyz)/131;
  
}

void hesapla()
{

  float gX, gY, gZ, aX, aY,aZ;
  
  accTop =(AcX * AcX);
  accTop +=(AcY * AcY);
  accTop +=(AcZ * AcZ);
  accTop =sqrt(accTop);
  if (fabs(AcY) < accTop)
  {
    accPitch = asin(AcY/accTop)/ToRad;  
    accPitch += zeroPitch;
  } 
  if (fabs(AcX) < accTop)
  {
    accRoll = asin(AcX/accTop)/ -ToRad;  
    accRoll -= -zeroPitch;
  }
  if(baslama)
  { 
  
   //pitch = GyX;
   // roll =  GyY;
   //   yaw = GyZ;
   
   pitch += (GyX)/frek;
   roll += (GyY)/frek;
   yaw = (GyZ)/frek;
  
   pitch += roll * sin(yaw) * ToRad;
   roll -= pitch * sin(yaw) * ToRad;
  
  
   pitch = (pitch * gyroRate) + (accPitch * accRate);
   roll = (roll * gyroRate) + (accRoll * accRate);
  }
  else 
  {
      pitch = accPitch;
      roll = accRoll;
      baslama=1;
  }
  
} 

void goster()
{
      lcd.setCursor(0,0);
      lcd.print("AX:");
      lcd.print(ACX);
      lcd.setCursor(8,0); 
      lcd.print("AY:");
      lcd.print(ACY);
      lcd.setCursor(0,1);
      lcd.print("AZ:");
      lcd.print(ACZ);
      lcd.setCursor(8,1); 
      lcd.print("GX:");
      lcd.print(GYX);
      delay(500);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("GY:");
      lcd.print(GYY);
      lcd.setCursor(8,0); 
      lcd.print("GZ:");
      lcd.print(GYZ);
      lcd.setCursor(0,1);
      lcd.print("Temp: ");
      lcd.setCursor(6,1);
      lcd.print(dT);
      delay(500);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Enlem :");
      lcd.print(enlem);
      lcd.setCursor(0,1); 
      lcd.print("Boylam:");
      lcd.print(boylam);
      delay(500);
      lcd.clear();
      
}

void RP_calculate()
{
  double x_Buff = float(AcX);
  double y_Buff = float(AcY);
  double z_Buff = float(AcZ);
  roll_1 = atan2(y_Buff, z_Buff) *57.3;
  pitch_1 = atan2((-x_Buff), sqrt(y_Buff * y_Buff) +(z_Buff* z_Buff))*57.3;
  if(roll_1>=roll_1max)
  roll_1max = roll_1;
  if(roll_1<=roll_1min)
  roll_1min = roll_1;
  if(pitch_1>=pitch_1max)
  pitch_1max = pitch_1;
  if(pitch_1<=pitch_1min)
  pitch_1min = pitch_1;
} 

void gprmcOku()
{   
  while(ss.available() > 0) 
  {
     gps.encode(ss.read());
  }
  if(gps.location.isUpdated())
  {
     Serial.println(gps.location.lng(),6);
     Serial.println(gps.location.lat(),6);
     Serial.println(gps.time.value());
    
     boylam = (gps.location.lng()) * 1000000;
     enlem = (gps.location.lat()) * 1000000 ;
  
  }
}

Please modify your post to put all your code in between [code] tags [/code],

...so it looks like this.

This was described in How to Use the Forum. Select the "More..." pulldown in the lower right-hand corner of your post, then "Modify". Insert the bracketed code tags before and after your code, and press Preview and/or Save.

-_- Adding another post was not the right thing to do... Modify your first post and delete all the code, leaving your second post with the correct code block.

To answer your question, you are printing too much on every loop iteration. These prints (including to the LCD) make your Arduino "block" or wait at a print statement. During that time, GPS characters are still being received and, eventually, lost.

The NeoGPS example programs show the correct program structure for printing [u]a little[/u] information during the GPS quiet time. It is also faster, smaller and more accurate than all other libraries. It can be configured to parse only the fields that you use, which saves even more program space and RAM. It is available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

You need to think about how much you are trying to display, and how fast the user can even see it.

A secondary problem is that you are using SoftwareSerial. It is very inefficient, because it disables interrupts for long periods of time. Every time a character is received from the GPS device, interrupts are disabled for 1ms, and the processor does nothing else but wait for all bits of the character to be received. This can interfere with other parts of your sketch, or with other libraries.

AltSoftSerial is the best choice, but it only works on pins 8 & 9 (on an UNO). Strongly recommended.

My NeoSWSerial is second best, and it works on any two pins.

Cheers, /dev

Thank you , when i used AltSoftSerial instead of SoftSerial, my problem was solved.