2 GPS Devices Connected to Mega 2560
i working try read 2 gps units (adafuit gps breakout) using single mega 2560 board. able parse gps data single unit code:
then "attempted" extend parse 2 units, giving me weird issues random numbers being inserted serial monitor output.
i'm not sure if should using software serial option or uart (hardware serial). i'm not sure if should try tinygps+ library on adafruit gps library.
any advice nice.
code: [select]
// test code adafruit gps modules using mtk3329/mtk3339 driver
//
// code shows how listen gps module in interrupt
// allows program have more 'freedom' - parse
// when new nmea sentence available! access data when
// desired.
//
// tested , works great adafruit ultimate gps module
// using mtk33x9 chipset
// ------> http://www.adafruit.com/products/746
// pick 1 today @ adafruit electronics shop
// , support open source hardware & software! -ada
#include <adafruit_gps.h>
#include <softwareserial.h>
// if you're using gps module:
// connect gps power pin 5v
// connect gps ground pin ground
// if using software serial (sketch example default):
// connect gps tx (transmit) pin digital 3
// connect gps rx (receive) pin digital 2
// if using hardware serial (e.g. arduino mega):
// connect gps tx (transmit) pin arduino rx1, rx2 or rx3
// connect gps rx (receive) pin matching tx1, tx2 or tx3
// if you're using adafruit gps shield, change
// softwareserial myserial(3, 2); -> softwareserial myserial(8, 7);
// , make sure switch set softserial
// if using software serial, keep line enabled
// (you can change pin numbers match wiring):
// softwareserial myserial(3, 2);
// if using hardware serial (e.g. arduino mega), comment out the
// above softwareserial line, , enable line instead
// (you can change serial number match wiring):
hardwareserial myserial = serial1;
adafruit_gps gps(&myserial);
// set gpsecho 'false' turn off echoing gps data serial console
// set 'true' if want debug , listen raw gps sentences.
#define gpsecho true
// keeps track of whether we're using interrupt
// off default!
boolean usinginterrupt = false;
void useinterrupt(boolean); // func prototype keeps arduino 0023 happy
void setup()
{
// connect @ 115200 can read gps fast enough , echo without dropping chars
// spit out
serial.begin(115200);
serial.println("adafruit gps library basic test!");
// 9600 nmea default baud rate adafruit mtk gps's- use 4800
gps.begin(9600);
// uncomment line turn on rmc (recommended minimum) , gga (fix data) including altitude
gps.sendcommand(pmtk_set_nmea_output_rmcgga);
// uncomment line turn on "minimum recommended" data
//gps.sendcommand(pmtk_set_nmea_output_rmconly);
// parsing data, don't suggest using either rmc or rmc+gga since
// parser doesn't care other sentences @ time
// set update rate
gps.sendcommand(pmtk_set_nmea_update_1hz); // 1 hz update rate
// parsing code work nicely , have time sort thru data, and
// print out don't suggest using higher 1 hz
// request updates on antenna status, comment out keep quiet
gps.sendcommand(pgcmd_antenna);
// nice thing code can have timer0 interrupt go off
// every 1 millisecond, , read data gps you. makes the
// loop code heck of lot easier!
useinterrupt(true);
delay(1000);
// ask firmware version
myserial.println(pmtk_q_release);
}
// interrupt called once millisecond, looks new gps data, , stores it
signal(timer0_compa_vect) {
char c = gps.read();
// if want debug, time it!
#ifdef udr0
if (gpsecho)
if (c) udr0 = c;
// writing direct udr0 much faster serial.print
// 1 character can written @ time.
#endif
}
void useinterrupt(boolean v) {
if (v) {
// timer0 used millis() - we'll interrupt somewhere
// in middle , call "compare a" function above
ocr0a = 0xaf;
timsk0 |= _bv(ocie0a);
usinginterrupt = true;
} else {
// not call interrupt function compa anymore
timsk0 &= ~_bv(ocie0a);
usinginterrupt = false;
}
}
uint32_t timer = millis();
void loop() // run on , on again
{
// in case not using interrupt above, you'll
// need 'hand query' gps, not suggested :(
if (! usinginterrupt) {
// read data gps in 'main loop'
char c = gps.read();
// if want debug, time it!
if (gpsecho)
if (c) serial.print(c);
}
// if sentence received, can check checksum, parse it...
if (gps.newnmeareceived()) {
// tricky thing here if print nmea sentence, or data
// end not listening , catching other sentences!
// wary if using output_alldata , trytng print out data
//serial.println(gps.lastnmea()); // sets newnmeareceived() flag false
if (!gps.parse(gps.lastnmea())) // sets newnmeareceived() flag false
return; // can fail parse sentence in case should wait another
}
// if millis() or timer wraps around, we'll reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out current stats
if (millis() - timer > 2000) {
timer = millis(); // reset timer
serial.print("\ntime: ");
serial.print(gps.hour, dec); serial.print(':');
serial.print(gps.minute, dec); serial.print(':');
serial.print(gps.seconds, dec); serial.print('.');
serial.println(gps.milliseconds);
serial.print("date: ");
serial.print(gps.day, dec); serial.print('/');
serial.print(gps.month, dec); serial.print("/20");
serial.println(gps.year, dec);
serial.print("fix: "); serial.print((int)gps.fix);
serial.print(" quality: "); serial.println((int)gps.fixquality);
if (gps.fix) {
serial.print("location: ");
serial.print(gps.latitude, 4); serial.print(gps.lat);
serial.print(", ");
serial.print(gps.longitude, 4); serial.println(gps.lon);
serial.print("location (in degrees, works google maps): ");
serial.print(gps.latitudedegrees, 4);
serial.print(", ");
serial.println(gps.longitudedegrees, 4);
serial.print("speed (knots): "); serial.println(gps.speed);
serial.print("angle: "); serial.println(gps.angle);
serial.print("altitude: "); serial.println(gps.altitude);
serial.print("satellites: "); serial.println((int)gps.satellites);
}
}
}
then "attempted" extend parse 2 units, giving me weird issues random numbers being inserted serial monitor output.
i'm not sure if should using software serial option or uart (hardware serial). i'm not sure if should try tinygps+ library on adafruit gps library.
any advice nice.
i regret show butchered code attempt parse 2 gps outputs using hardware serial.
code: [select]
// test code adafruit gps modules using mtk3329/mtk3339 driver
//
#include <adafruit_gps.h>
#include <softwareserial.h>
hardwareserial myserial_1 = serial1;
hardwareserial myserial_2 = serial2;
adafruit_gps gps_1(&myserial_1);
adafruit_gps gps_2(&myserial_2);
// set gpsecho 'false' turn off echoing gps data serial console
// set 'true' if want debug , listen raw gps sentences.
#define gpsecho true
// keeps track of whether we're using interrupt
// off default!
boolean usinginterrupt = false;
void useinterrupt(boolean); // func prototype keeps arduino 0023 happy
void setup()
{
// connect @ 115200 can read gps fast enough , echo without dropping chars
// spit out
serial.begin(115200);
serial.println("adafruit gps library basic test!");
// 9600 nmea default baud rate adafruit mtk gps's- use 4800
gps_1.begin(9600);
// uncomment line turn on rmc (recommended minimum) , gga (fix data) including altitude
gps_1.sendcommand(pmtk_set_nmea_output_rmcgga);
// uncomment line turn on "minimum recommended" data
//gps_1.sendcommand(pmtk_set_nmea_output_rmconly);
// parsing data, don't suggest using either rmc or rmc+gga since
// parser doesn't care other sentences @ time
// set update rate
gps_1.sendcommand(pmtk_set_nmea_update_1hz); // 1 hz update rate
// parsing code work nicely , have time sort thru data, and
// print out don't suggest using higher 1 hz
// request updates on antenna status, comment out keep quiet
gps_1.sendcommand(pgcmd_antenna);
// nice thing code can have timer0 interrupt go off
// every 1 millisecond, , read data gps you. makes the
// loop code heck of lot easier!
useinterrupt(true);
delay(1000);
// ask firmware version
myserial_1.println(pmtk_q_release);
// ************ added gps2******************
gps_2.begin(9600);
gps_2.sendcommand(pmtk_set_nmea_output_rmcgga);
gps_2.sendcommand(pmtk_set_nmea_update_1hz);
gps_2.sendcommand(pgcmd_antenna);
useinterrupt(true);
delay(1000);
// ask firmware version
myserial_2.println(pmtk_q_release);
}
// interrupt called once millisecond, looks new gps data, , stores it
signal(timer0_compa_vect) {
char c1 = gps_1.read();
// if want debug, time it!
#ifdef udr0
if (gpsecho)
if (c1) udr0 = c1;
// writing direct udr0 much faster serial.print
// 1 character can written @ time.
#endif
char c2 = gps_2.read();
// if want debug, time it!
#ifdef udr0
if (gpsecho)
if (c2) udr0 = c2;
// writing direct udr0 much faster serial.print
// 1 character can written @ time.
#endif
}
void useinterrupt(boolean v) {
if (v) {
// timer0 used millis() - we'll interrupt somewhere
// in middle , call "compare a" function above
ocr0a = 0xaf;
timsk0 |= _bv(ocie0a);
usinginterrupt = true;
} else {
// not call interrupt function compa anymore
timsk0 &= ~_bv(ocie0a);
usinginterrupt = false;
}
}
uint32_t timer = millis();
void loop() // run on , on again
{
// in case not using interrupt above, you'll
// need 'hand query' gps, not suggested :(
if (! usinginterrupt) {
// read data gps in 'main loop'
char c1 = gps_1.read();
// if want debug, time it!
if (gpsecho)
if (c1) serial.print(c1);
char c2 = gps_2.read();
// if want debug, time it!
if (gpsecho)
if (c2) serial.print(c2);
}
// if sentence received, can check checksum, parse it...
if (gps_1.newnmeareceived()) {
// tricky thing here if print nmea sentence, or data
// end not listening , catching other sentences!
// wary if using output_alldata , trytng print out data
//serial.println(gps_1.lastnmea()); // sets newnmeareceived() flag false
if (!gps_1.parse(gps_1.lastnmea())) // sets newnmeareceived() flag false
return; // can fail parse sentence in case should wait another
}
if (gps_2.newnmeareceived()) {
// tricky thing here if print nmea sentence, or data
// end not listening , catching other sentences!
// wary if using output_alldata , trytng print out data
//serial.println(gps_1.lastnmea()); // sets newnmeareceived() flag false
if (!gps_2.parse(gps_2.lastnmea())) // sets newnmeareceived() flag false
return; // can fail parse sentence in case should wait another
}
// if millis() or timer wraps around, we'll reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out current stats
if (millis() - timer > 2000) {
timer = millis(); // reset timer
serial.print("\ntime 1: ");
serial.print(gps_1.hour, dec); serial.print(':');
serial.print(gps_1.minute, dec); serial.print(':');
serial.print(gps_1.seconds, dec); serial.print('.');
serial.println(gps_1.milliseconds);
serial.print("date: ");
serial.print(gps_1.day, dec); serial.print('/');
serial.print(gps_1.month, dec); serial.print("/20");
serial.println(gps_1.year, dec);
serial.print("fix: "); serial.print((int)gps_1.fix);
serial.print(" quality: "); serial.println((int)gps_1.fixquality);
if (gps_1.fix) {
serial.print("location 1: ");
serial.print(gps_1.latitude, 4); serial.print(gps_1.lat);
serial.print(", ");
serial.print(gps_1.longitude, 4); serial.println(gps_1.lon);
serial.print("location (in degrees, works google maps): ");
serial.print(gps_1.latitudedegrees, 4);
serial.print(", ");
serial.println(gps_1.longitudedegrees, 4);
serial.print("speed (knots): "); serial.println(gps_1.speed);
serial.print("angle: "); serial.println(gps_1.angle);
serial.print("altitude: "); serial.println(gps_1.altitude);
serial.print("satellites: "); serial.println((int)gps_1.satellites);
}
serial.print("\n*************** gps_2********* ");
serial.print("\ntime 2: ");
serial.print(gps_2.hour, dec); serial.print(':');
serial.print(gps_2.minute, dec); serial.print(':');
serial.print(gps_2.seconds, dec); serial.print('.');
serial.println(gps_2.milliseconds);
serial.print("date: ");
serial.print(gps_2.day, dec); serial.print('/');
serial.print(gps_2.month, dec); serial.print("/20");
serial.println(gps_2.year, dec);
serial.print("fix: "); serial.print((int)gps_2.fix);
serial.print(" quality: "); serial.println((int)gps_2.fixquality);
if (gps_2.fix) {
serial.print("location 2: ");
serial.print(gps_2.latitude, 4); serial.print(gps_2.lat);
serial.print(", ");
serial.print(gps_2.longitude, 4); serial.println(gps_2.lon);
serial.print("location (in degrees, works google maps): ");
serial.print(gps_2.latitudedegrees, 4);
serial.print(", ");
serial.println(gps_2.longitudedegrees, 4);
serial.print("speed (knots): "); serial.println(gps_2.speed);
serial.print("angle: "); serial.println(gps_2.angle);
serial.print("altitude: "); serial.println(gps_2.altitude);
serial.print("satellites: "); serial.println((int)gps_2.satellites);
}
}
}
Arduino Forum > Using Arduino > Project Guidance > 2 GPS Devices Connected to Mega 2560
arduino
Comments
Post a Comment