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
#include <MsTimer2.h> | |
#include <Wire.h> | |
#include <MS561101BA.h> | |
#include <Adafruit_GPS.h> | |
#include <SoftwareSerial.h> | |
#include <SakuraIO.h> | |
const uint16_t CIRCUMFERENCE = 2136; | |
const int mgntPin = 2; // the number of the pushbutton pin | |
MS561101BA baro; | |
SoftwareSerial mySerial(6, 7); | |
Adafruit_GPS GPS(&mySerial); | |
#define GPSECHO false | |
boolean usingInterrupt = false; | |
void useInterrupt(boolean); | |
SakuraIO_SPI sakuraio(10); | |
//SakuraIO_I2C sakuraio; | |
volatile uint16_t mgnt_cnt = 0; | |
volatile uint16_t cycle_speed = 0; | |
volatile uint32_t distance = 0; | |
volatile uint16_t sec = 0; | |
void timer_interrupt(){ | |
static int cnt = 0; | |
if( ++cnt >= 10 ){ | |
noInterrupts(); | |
cycle_speed = mgnt_cnt; | |
mgnt_cnt = 0; | |
interrupts(); | |
cycle_speed *= CIRCUMFERENCE; | |
distance += cycle_speed; | |
sec++; | |
cnt = 0; | |
} | |
if (GPS.newNMEAreceived()) { | |
//Serial.println(GPS.lastNMEA()); | |
GPS.parse(GPS.lastNMEA()); | |
} | |
} | |
void mgnt_interrupt(){ | |
mgnt_cnt++; | |
} | |
SIGNAL(TIMER0_COMPA_vect) { | |
char c = GPS.read(); | |
if (GPSECHO) | |
if (c) UDR0 = c; | |
} | |
void useInterrupt(boolean v) { | |
if (v) { | |
OCR0A = 0xAF; | |
TIMSK0 |= _BV(OCIE0A); | |
usingInterrupt = true; | |
} else { | |
TIMSK0 &= ~_BV(OCIE0A); | |
usingInterrupt = false; | |
} | |
} | |
void setup(){ | |
Serial.begin(9600); | |
GPS.begin(9600); | |
useInterrupt(true); | |
Wire.begin(); | |
//pinMode(SDA, INPUT); //Disable internal pullup | |
//pinMode(SCL, INPUT); //Disable internal pullup | |
delay(100); | |
baro.init(MS561101BA_ADDR_CSB_LOW); | |
delay(100); | |
#if 1 | |
Serial.print("Waiting to come online"); | |
for(;;){ | |
if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break; | |
Serial.print("."); | |
delay(1000); | |
} | |
Serial.println(""); | |
#endif | |
pinMode(mgntPin, INPUT_PULLUP); | |
attachInterrupt(0, mgnt_interrupt, FALLING); | |
MsTimer2::set(100, timer_interrupt); | |
MsTimer2::start(); | |
} | |
void loop(){ | |
float temperature = NULL, pressure = NULL; | |
float latitude = NULL, longitude = NULL, altitude = NULL; | |
uint16_t satellites = NULL; | |
int fix; | |
unsigned int speed = 0; | |
uint32_t dist; | |
noInterrupts(); | |
speed = cycle_speed; | |
latitude = GPS.latitudeDegrees; | |
longitude = GPS.longitudeDegrees; | |
altitude = GPS.altitude; | |
satellites = GPS.satellites; | |
dist = distance; | |
fix = GPS.fix; | |
interrupts(); | |
Serial.println(speed); | |
Serial.print("Fix: "); Serial.print((int)GPS.fix); | |
Serial.print(" quality: "); Serial.println((int)GPS.fixquality); | |
if (fix) { | |
Serial.print("Location: "); | |
Serial.print(latitude, 6); | |
Serial.print(", "); | |
Serial.println(longitude, 6); | |
Serial.print("Altitude: "); Serial.println(altitude); | |
Serial.print("Satellites: "); Serial.println(satellites); | |
for(int i=0;i<10;i++){ | |
float t = NULL, p = NULL; | |
while(t == NULL) { | |
t = baro.getTemperature(MS561101BA_OSR_4096); | |
} | |
while(p == NULL) { | |
p = baro.getPressure(MS561101BA_OSR_4096); | |
} | |
temperature += t; | |
pressure += p; | |
} | |
temperature /= 10; | |
pressure /= 10; | |
Serial.print("pres: "); | |
Serial.print(pressure); | |
Serial.println(" mbar"); | |
uint8_t avail; | |
uint8_t queued; | |
sakuraio.getTxQueueLength(&avail, &queued); | |
if( avail >= 7 ){ | |
if(sakuraio.enqueueTx(0,(uint32_t)speed) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(1,latitude) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(2,longitude) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(3,altitude) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(4,(uint32_t)satellites) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(5,temperature) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(6,pressure) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
if(sakuraio.enqueueTx(7,dist) != CMD_ERROR_NONE){ | |
Serial.println("[ERR] enqueue error"); | |
} | |
} | |
sakuraio.send(); | |
} | |
Serial.println(); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment