Friday, June 12, 2015

Teste - módulo GPS

Como mais um sensor a ser adicionado no projeto, temos um módulo GPS. Um GPS (Global Positioning System) é um dispositivo que permite identificar a sua posição em qualquer lugar do globo, por meio do envio de ondas de rádio a satélites que orbitam a Terra. No caso, o módulo a ser utilizado é o u-blox NEO-6M.


A utilização com Arduino é bastante simples, bastando realizar as conexões a seguir:
Módulo GPS
Arduino
VCC
3.3V
GND
GND
TXD
alguma porta RX



O módulo GPS não precisa receber nenhum comando; ele somente transmite dados (mensagens padrão NMEA e proprietários da ublox). Portanto, não é preciso conectar o RXD do módulo ao Arduino. Tal conexão é necessária quando é preciso configurar o módulo (por exemplo, mudar o baudrate e as mensagens fornecidas).


Como foi dito, o módulo envia mensagens padrão NMEA pela comunicação serial. O programa abaixo foi criado apenas para ler Serial1 do Arduino Mega e imprimir as mensagens recebidas (GPGGA,GPRMC,GPVTG,GPGSA,GPGSV,GPGLL):


#define GPGGA "$GPGGA"
#define GPRMC "$GPRMC"
#define GPVTG "$GPVTG"
#define GPGSA "$GPGSA"
#define GPGSV "$GPGSV"
#define GPGLL "$GPGLL"

/*
  It requires the use of SoftwareSerial, and assumes that you have a
  9600-baud serial GPS device hooked up on pins 19(RX).
  Connect GPS Vcc to +3.3V, GPS Gnd to Gnd, and GPS TX to Arduino RX
 
  Communication using SoftwareSerial is not stable
  
  There's no need to connect GPS RX to Arduino TX
*/
static const uint32_t GPSBaud = 9600;

// The serial connection to the GPS device
boolean bPrint;
char c;
String buf="";

void setup()
{
 Serial.begin(115200);
 Serial1.begin(GPSBaud);
}
void loop()
{
 if (Serial1.available() > 0)
 {
   c = Serial1.read();
   if(c==13){
     bPrint = true;
   }
   else if(c=='$'){
     buf="";
   }
   if(c!=10 && c!=13){
     buf+=(char)c;
   }
   if(buf.substring(0,6)==GPGGA && bPrint){Serial.print(buf);Serial.print("\r\n");}
   else if(buf.substring(0,6)==GPRMC && bPrint){Serial.print(buf);Serial.print("\r\n");}
   else if(buf.substring(0,6)==GPVTG && bPrint){Serial.print(buf);Serial.print("\r\n");}
   else if(buf.substring(0,6)==GPGSA && bPrint){Serial.print(buf);Serial.print("\r\n");}
   else if(buf.substring(0,6)==GPGSV && bPrint){Serial.print(buf);Serial.print("\r\n");}
   else if(buf.substring(0,6)==GPGLL && bPrint){Serial.print(buf);Serial.print("\r\n");}
   bPrint = false;
 }
}


Os dados recebidos foram os seguintes:


Felizmente, a biblioteca TinyGPS++ está disponível para interpretar as mensagens e facilitar o acesso pelo usuário a informações como latitude, longitude entre outros.  

Obs.: para incluir no ambiente do Arduino, basta realizar o download e colocar a pasta extraída em sketchbook/libraries.
O programa abaixo possui pequenas alterações em relação ao exemplo DeviceExample disponível com a biblioteca


#include <TinyGPS++.h>
/*
  This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
  It requires the use of SoftwareSerial, and assumes that you have a
  9600-baud serial GPS device hooked up on pin 19 (RX).
*/

static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

void setup()
{
 Serial.begin(115200);
 Serial1.begin(GPSBaud);

 Serial.println(F("DeviceExample.ino"));
 Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
 Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
 Serial.println(F("by Mikal Hart"));
 Serial.println();
}

void loop()
{
 // This sketch displays information every time a new sentence is correctly encoded.
 while (Serial1.available() > 0)
   if (gps.encode(Serial1.read()))
     displayInfo();

 if (millis() > 5000 && gps.charsProcessed() < 10)
 {
   Serial.println(F("No GPS detected: check wiring."));
   while(true);
 }
}

void displayInfo()
{
 Serial.print(F("Location: "));
 if (gps.location.isValid())
 {
   Serial.print(gps.location.lat(), 6);
   Serial.print(F(","));
   Serial.print(gps.location.lng(), 6);
 }
 else
 {
   Serial.print(F("INVALID"));
 }

 Serial.print(F("  Date/Time: "));
 if (gps.date.isValid())
 {
   Serial.print(gps.date.month());
   Serial.print(F("/"));
   Serial.print(gps.date.day());
   Serial.print(F("/"));
   Serial.print(gps.date.year());
 }
 else
 {
   Serial.print(F("INVALID"));
 }

 Serial.print(F(" "));
 if (gps.time.isValid())
 {
   if (gps.time.hour() < 10) Serial.print(F("0"));
   Serial.print(gps.time.hour());
   Serial.print(F(":"));
   if (gps.time.minute() < 10) Serial.print(F("0"));
   Serial.print(gps.time.minute());
   Serial.print(F(":"));
   if (gps.time.second() < 10) Serial.print(F("0"));
   Serial.print(gps.time.second());
   Serial.print(F("."));
   if (gps.time.centisecond() < 10) Serial.print(F("0"));
   Serial.print(gps.time.centisecond());
 }
 else
 {
   Serial.print(F("INVALID"));
 }

 Serial.println();
}

Por fim, os dados fornecidos pelo módulo podem ser visualizados a seguir (onde o horário fornecido está no fuso horário UTC±00:00.



No comments:

Post a Comment