Ciao sto studiando il codice della libreria Neo GPS sono un autodidatta e piano piano con un po' di libri e un po' di pratica sto imparando.
Ho tre domande.
1 Domanda
Perchè vengono caricate due librerie per la comunicazione seriale:
- AltSoftSerial
- SoftwareSerial.h
Studiando su internet ho capito che la prima permette la ricezione e comunicazione delle informazioni contemporaneamente mentre la seconda no ed è meno performante.
Ma il perchè ne vengano selezionate entrambe mi è oscuro.
2 Domanda
fix.valid è l'invocazione de metodo valid sull'oggetto fix?
Non riesco a capire il senso di questa procedura e i significati tecnici che ci sono dietro
if (fix.valid.location)
logFile.print(fix.longitude(), 6);
logFile.print(',');
if (fix.valid.location)
logFile.print(fix.latitude(), 6);
logFile.print(',');
if (fix.valid.altitude)
** 3 Domanda**
Perchè vengono creati due oggetti distinti gps e fix?
Ho, forse, capito la necessità di inizializzare l'oggetto gps, ma non capisco fix
Perchè inizializziamo questi due oggetti e sono distinti?
NMEAGPS gps; // NeoGPS object to be used throughout
gps_fix fix; // The latest GPS information received from the gpsPort
#define GPS_BAUD 9600 // GPS module's default baud rate
e successivamente
while (gps.available( gpsPort )) {
fix = gps.read(); // get the entire fix structure, once per second
if (logGPSData()) { // Log the GPS data
SerialMonitor.println( F("GPS logged.") ); // Print a debug message
Qui trovate il codice completo
#include <SPI.h>
#include <SdFat.h>
#include <NMEAGPS.h>
#include <Wire.h>
#define ARDUINO_USD_CS 10 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)
/////////////////////////
// Log File Defintions //
/////////////////////////
// Keep in mind, the SD library has max file name lengths of 8.3 - 8 char prefix,
// and a 3 char suffix.
char logFileName[13] = "gpslogXX.csv";
// Our log files are called "gpslogXX.csv, so "gpslog99.csv" is our max file.
#define MAX_LOG_FILES 100 // Number of log files that can be made
// Data to be logged:
#define LOG_COLUMN_HEADER \
"longitude," "latitude," "altitude," "speed," "course," "date," "time," \
"satellites," "Acc.X," "Acc.Y," "Acc.Z," "Gy.X," "Gy.Y," "Gy.Z," "Temp"
// printed at the top of the file.
SdFat SD;
File logFile;
/////////////////////////
// NeoGPS Definitions //
/////////////////////////
NMEAGPS gps; // NeoGPS object to be used throughout
gps_fix fix; // The latest GPS information received from the gpsPort
#define GPS_BAUD 9600 // GPS module's default baud rate
#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX);
/////////////////////////////////
// GPS Serial Port Definitions //
/////////////////////////////////
// If you're using a Mega, Leo or Due, use Serial1 for the GPS:
//#define gpsPort Serial1
// If you're using an Arduino Uno or other ATmega328 board that uses the
// 0/1 UART for programming/Serial monitor-ing, use AltSoftSerial:
#include <AltSoftSerial.h>
AltSoftSerial gpsPort; // Always on pins 8 & 9
//If you can't use pins 8 & 9, use this:
//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort( 2, 3 ).
// Define the serial monitor port. On the Uno, Mega, and Leonardo this is 'Serial'
// on other boards this may be 'SerialUSB'
#define SerialMonitor Serial
//#define USE_MPU
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup()
{
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
SerialMonitor.begin(9600);
gpsPort.begin(GPS_BAUD);
SerialMonitor.println( F("Setting up SD card.") );
updateFileName(); // Each time we start, create a new file, increment the number
// see if the card is present and can be initialized:
if (!SD.begin(ARDUINO_USD_CS))
{
SerialMonitor.println( F("Error initializing SD card.") );
} else {
logFile = SD.open( logFileName, FILE_WRITE );
// Print a header at the top of the new file
logFile.println( F(LOG_COLUMN_HEADER) );
}
}
void loop()
{
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
while (gps.available( gpsPort )) {
fix = gps.read(); // get the entire fix structure, once per second
if (logGPSData()) { // Log the GPS data
SerialMonitor.println( F("GPS logged.") ); // Print a debug message
} else {// If we failed to log GPS
// Print an error, don't update lastLog
SerialMonitor.println( F("Failed to log new GPS data.") );
}
}
}
byte logGPSData()
{
if (logFile.isOpen())
{ // Print longitude, latitude, altitude (in feet), speed (in mph), course
// in (degrees), date, time, and number of satellites.
if (fix.valid.location)
logFile.print(fix.longitude(), 6);
logFile.print(',');
if (fix.valid.location)
logFile.print(fix.latitude(), 6);
logFile.print(',');
if (fix.valid.altitude)
logFile.print(fix.altitude() * 3.2808, 1);
logFile.print(',');
if (fix.valid.speed)
logFile.print(fix.speed_mph(), 1);
logFile.print(',');
if (fix.valid.heading)
logFile.print(fix.heading(), 1);
logFile.print(',');
if (fix.valid.date) {
logFile.print( fix.dateTime.full_year() );
if (fix.dateTime.month < 10)
logFile.print( '0' );
logFile.print( fix.dateTime.month );
if (fix.dateTime.date < 10)
logFile.print( '0' );
logFile.print( fix.dateTime.date );
}
logFile.print(',');
if (fix.valid.time) {
if (fix.dateTime.hours < 10)
logFile.print( '0' );
logFile.print( fix.dateTime.hours );
if (fix.dateTime.minutes < 10)
logFile.print( '0' );
logFile.print( fix.dateTime.minutes );
if (fix.dateTime.seconds < 10)
logFile.print( '0' );
logFile.print( fix.dateTime.seconds );
}
logFile.print(',');
if (fix.valid.satellites)
logFile.print(fix.satellites);
logFile.print(',');
logFile.print(AcX);
logFile.print(',');
logFile.print(AcY);
logFile.print(',');
logFile.print(AcZ);
logFile.print(',');
logFile.print(GyX);
logFile.print(',');
logFile.print(GyY);
logFile.print(',');
logFile.print(GyZ);
logFile.print(',');
logFile.print(Tmp);
logFile.println();
logFile.flush(); // make sure the file contains at least this much
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
// updateFileName() - Looks through the log files already present on a card,
// and creates a new file with an incremented file index.
void updateFileName()
{
for (uint8_t i; i < MAX_LOG_FILES; i++)
{
// Set logFileName to "gpslogXX.csv":
logFileName[6] = (i/10) + '0';
logFileName[7] = (i%10) + '0';
if (!SD.exists(logFileName))
break; // We found our index
SerialMonitor.print(logFileName);
SerialMonitor.println( F(" exists") );
}
SerialMonitor.print( F("File name: ") );
SerialMonitor.println(logFileName);
}
Grazie mille per la disponibilità