Instantly share code, notes, and snippets.
Created
January 27, 2016 03:08
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save SyncChannel/1de73893436d98d9a586 to your computer and use it in GitHub Desktop.
MultiNav FeatherWing Example Program for Feather M0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example Program for MultiNav FeatherWing | |
// For Adafruit Feather M0 | |
// | |
// By: Dan Watson | |
// syncchannel.blogspot.com | |
// 1/25/2016 | |
// | |
// This program makes use of the TinyGPS++ library to receive the GPS data from the U-Blox | |
// module, as well as the Sparkfun MPU-6050 library to interact with the MPU-9250 9DOF. | |
// TinyGPS++ Download: http://arduiniana.org/libraries/tinygpsplus/ | |
// Sparkfun MPU-6050 Download: https://github.com/sparkfun/MPU-9150_Breakout/tree/master/firmware | |
// Credit is given to both Arduiniana and Sparkfun for the use of their excellent libraries, and portions | |
// of their example programs. Thank you! | |
#include <TinyGPS++.h> | |
#include "Wire.h" | |
#include "I2Cdev.h" | |
#include "MPU6050.h" | |
// Pinout: | |
// * I2C: SDA and SCL pins in the same location on all Feather boards. | |
// * UART: For M0, connect Option B on JP3, and use SERCOM1 | |
// * Interrupts: This board has two possible interrupt signals: | |
// MPU-9250 goes to D5. Connect JP1 to use. | |
// GPS Pulse-per-second goes to D6. Connect JP2 to use. | |
// These are the I/O defines for Feather M0 | |
#define ppsPin 6 | |
#define mpu9250intPin 5 | |
#define ledPin 13 | |
// These are the defines for using SERCOM1 on Feather M0 to communicate with U-Blox GPS | |
#define PIN_SerialGPS_RX (34ul) | |
#define PIN_SerialGPS_TX (36ul) | |
#define PAD_SerialGPS_TX (UART_TX_PAD_2) | |
#define PAD_SerialGPS_RX (SERCOM_RX_PAD_3) | |
volatile bool periodic = false; | |
int16_t ax, ay, az; | |
int16_t gx, gy, gz; | |
int16_t mx, my, mz; | |
TinyGPSPlus gps; | |
MPU6050 accelgyro; | |
// Using SERCOM1 on M0 to communicate with U-Blox GPS | |
Uart SerialGPS(&sercom1, PIN_SerialGPS_RX, PIN_SerialGPS_TX, PAD_SerialGPS_RX, PAD_SerialGPS_TX); | |
void setup() | |
{ | |
Serial.begin(9600); | |
SerialGPS.begin(9600); | |
Serial.println("MultiNav FeatherWing Test Program!"); | |
Serial.println("Ensure a GPS antenna is connected and has a clear view of the sky"); | |
Serial.println(""); | |
Wire.begin(); | |
Serial.println("Initializing I2C for MPIU-9250"); | |
accelgyro.initialize(); | |
Serial.println("Testing device connections..."); | |
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); | |
Serial.println(""); | |
pinMode(ppsPin, INPUT); | |
pinMode(mpu9250intPin, INPUT); | |
pinMode(ledPin, OUTPUT); | |
// Attach interrupt for GPS PPS. Will be used to update accel data and blink the LED. | |
attachInterrupt(ppsPin, ppsHandler, RISING); | |
} | |
// Needed for Feather M0. Leave this line between setup() and loop(). | |
// Comment this out when using 32U4 or HUZZAH | |
#define Serial SerialUSB | |
void loop() | |
{ | |
// Put things in this statement that you want to occur once per second | |
// It is triggered by the GPS Pulse-per-second interrupt, so it is aligned | |
// to the beginning of the UTC second precisely! | |
if (periodic) | |
{ | |
periodic = false; | |
digitalWrite(ledPin, HIGH); | |
// Let's use this to get updated data from the 9DOF | |
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz); | |
digitalWrite(ledPin, LOW); | |
} | |
// Data strings also arrive from the GPS once per second | |
// When TinyGPS gets fresh data, print out some example strings | |
if (gps.time.isUpdated()) | |
{ | |
if (gps.time.isValid()) | |
{ | |
char buffer[24]; | |
sprintf(buffer, "UTC: %02d/%02d/%04d %02d:%02d:%02d", gps.date.month(), gps.date.day(), | |
gps.date.year(), gps.time.hour(), gps.time.minute(), gps.time.second()); | |
for (int i = 0; i < sizeof(buffer); i++) | |
{ | |
Serial.print(buffer[i]); | |
} | |
Serial.println(""); | |
} else { | |
Serial.println("UTC Error: Time Not Valid"); | |
} | |
if (gps.location.isValid()) | |
{ | |
Serial.print("Lat "); | |
Serial.print(gps.location.lat(), 6); | |
Serial.print(", Lng "); | |
Serial.println(gps.location.lng(), 6); | |
Serial.print("Altitude "); | |
Serial.print(gps.altitude.meters()); | |
Serial.println("m"); | |
} else { | |
Serial.println("Position Error: Not Valid"); | |
} | |
if (gps.satellites.isValid()) | |
{ | |
Serial.print("SVs In Use: "); | |
Serial.println(gps.satellites.value()); | |
} | |
if (gps.hdop.isValid()) | |
{ | |
Serial.print("HDOP: "); | |
Serial.println((float) gps.hdop.value() / 100); | |
} | |
Serial.print("a/g/m:\t"); | |
Serial.print(ax); Serial.print("\t"); | |
Serial.print(ay); Serial.print("\t"); | |
Serial.print(az); Serial.print("\t"); | |
Serial.print(gx); Serial.print("\t"); | |
Serial.print(gy); Serial.print("\t"); | |
Serial.print(gz); Serial.print("\t"); | |
Serial.print(mx); Serial.print("\t"); | |
Serial.print(my); Serial.print("\t"); | |
Serial.println(mz); | |
Serial.println(""); | |
} | |
// Ingest new serial strings from the GPS into TinyGPS as they arrive | |
while(SerialGPS.available() > 0) | |
{ | |
gps.encode(SerialGPS.read()); | |
} | |
} | |
// This handler is triggered by the GPS Pulse-per-second. | |
// Use it to perform actions that are precisely aligned to the UTC second! | |
void ppsHandler() | |
{ | |
periodic = true; | |
} | |
// IRQ handler needed for SERCOM 1 on M0 | |
void SERCOM1_Handler() | |
{ | |
SerialGPS.IrqHandler(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment