commit 078e826f3b08efa623c6c56263bd1917c319cced Author: James Devine Date: Mon Jul 27 04:11:00 2015 +0200 diff --git a/CosmicPi1 b/CosmicPi1 new file mode 100644 index 0000000..2c9d56d --- /dev/null +++ b/CosmicPi1 @@ -0,0 +1,311 @@ +/* +Cosmic Pi fully integrated script mk1 +Designed for Arduino Due, sampling performance <500ksps (to be measured) +untested. Runs on interrupt to pins 50 (time reset) and 49 (event detection) +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#define mySerial Serial1 +#define GPSECHO true +const int timeresetpin = 50; +const int eventtrigger = 49; + +Adafruit_10DOF dof = Adafruit_10DOF(); +Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301); +Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302); +Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(18001); +Adafruit_HTU21DF htu = Adafruit_HTU21DF(); +Adafruit_GPS GPS(&mySerial); +unsigned long values[2000]; + +char gpstext[ ] = ""; +String gpstalk; +String inputString = ""; // a string to hold incoming data +boolean stringComplete = false; // whether the string is complete +boolean eventhappened = false; //has an event been detected? +boolean timereset = false; //has an event been detected? +String lastgpsread; +int deviceid = random(1, 10000); +float temperature; +double temperatureh=0; +double humidity = 0; +double baroaltitude = 0; +double accelx = 0; +double accely = 0; +double accelz = 0; +double magx = 0; +double magy = 0; +double magz = 0; + +int timereset = 0; + +int exacttime =0; +int uptime = 0; + + +/* Update this with the correct SLP for accurate altitude measurements */ +float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; + + +void setup() +{ + + initSensors(); + + // connect at 115200 so we can read the GPS fast enough and echo without dropping chars + // also spit it out + Serial.begin(115200); + + + // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800 + GPS.begin(9600); + mySerial.begin(9600); + GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); + GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate + delay(1000); + Serial.println("GPS online"); + inputString.reserve(200); +//timing reset pin, hook up to 1pps output pin from GPSmodule + pinMode(timeresetpin, INPUT); + attachInterrupt(timeresetpin, timeset, RISING); +//event trigger pin, makes readout happen + pinMode(timeresetpin, INPUT); + attachInterrupt(timeresetpin, eventtrigger, RISING); + + + unsigned int adcloopctr; + adcloopctr=0; + REG_ADC_MR = 0x10380180; // change from 10380200 to 10380180, 1 is the PREESCALER and 8 means FREERUN + ADC -> ADC_CHER = 0x03; // enable ADC on pin A0 + +} + +void loop() // run over and over again +{ +exacttime++; + +//if the ADC conversion is completed, then read it into the buffer and increment the index +if((ADC->ADC_ISR & 0x03)==0){; // wait for conversion + values[i]=ADC->ADC_CDR[0]; + values[i+1000]=ADC->ADC_CDR[1];//get values + adcloopctr++; + } + +if (stringComplete) { + lastgpsread = inputString; + lastgpsread.trim(); + //debug printout + //Serial.println(inputString); + // clear the string: + inputString = ""; + stringComplete = false; + } + +if (eventhappened) { + Serial.println("Event Detection on pin 49"); + eventhappened = false; + sensorreadout(); + printdatajson(adcloopctr); + } + + +if (timereset) { + + Serial.println("Timereset Detection on pin 50") + exacttime = 0; + } + +if (adcloopctr==999) { + adcloopctr=0; + } + +} + +void printdatajson(int adcindex) +{ + //start the data output on in json format + Serial.print("{ "); + Serial.print(" \"gps\":"); + Serial.print(" \""); + Serial.print(lastgpsread); + Serial.print("\""); + Serial.print(" ,"); + + Serial.print(" \"timing\": "); + Serial.print(exacttime); + Serial.print(','); + + Serial.print(" \"energy\": {"); + Serial.print(" \"channel1\": "); + for (int printctr = adcloopctr; printctr < 999+adcloopctr; printctr++) { + Serial.print(values[(printctr-adcloopctr)]); + Serial.print(','); + } + Serial.print(values[(adcloopctr-1)]); + Serial.print(" \"channel2\": "); + for (int printctr = adcloopctr; printctr < 999+adcloopctr; printctr++) { + Serial.print(values[(1000+printctr-adcloopctr)]); + Serial.print(','); + } + Serial.print(values[(999+adcloopctr)]); + Serial.print(" },"); + } + + Serial.print(" \"altitude\": "); + Serial.print(baroaltitude); + Serial.print(","); + + Serial.print(" \"humidity\": "); + Serial.print(humidity); + Serial.print(","); + + Serial.print(" \"gravitationalOrientation\": {"); + Serial.print(" \"x\": "); + Serial.print(accelx); + Serial.print(","); + Serial.print(" \"y\": "); + Serial.print(accely); + Serial.print(","); + Serial.print(" \"z\": "); + Serial.print(accelz); + Serial.print(" },"); + + Serial.print(" \"magneticOrientation\": {"); + Serial.print(" \"x\": "); + Serial.print(magx); + Serial.print(","); + Serial.print(" \"y\": "); + Serial.print(magy); + Serial.print(","); + Serial.print(" \"z\": "); + Serial.print(magz); + Serial.print(" },"); + + Serial.print(" \"temperature\": {"); + Serial.print(" \"value1\": "); + Serial.print(temperatureh); //val1 is from the humidity sensor + Serial.print(","); + Serial.print(" \"value2\": "); + Serial.print(temperature); + Serial.print(" },"); + + Serial.print(" \"uptime\": "); + Serial.print(uptime); + Serial.print(","); + + Serial.print(" \"id\": "); + Serial.print(deviceid); + Serial.println("}"); +} + +void serialEvent1() { + while (Serial1.available()) { + // get the new byte: + char inChar = (char)Serial1.read(); + // add it to the inputString: + if (inChar != char(10)) + { + inputString += inChar; + } + // if the incoming character is a newline, set a flag + // so the main loop can do something about it: + if (inChar == '\n') { + stringComplete = true; + } + } +} + +void serialEventRun(void) +{ + if (Serial1.available()) + serialEvent1(); +} + +void initSensors() +{ + if (!accel.begin()) + { + /* There was a problem detecting the LSM303 ... check your connections */ + Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!")); + while (1); + } + if (!mag.begin()) + { + /* There was a problem detecting the LSM303 ... check your connections */ + Serial.println("Ooops, no LSM303 detected ... Check your wiring!"); + while (1); + } + if (!bmp.begin()) + { + /* There was a problem detecting the BMP180 ... check your connections */ + Serial.println("Ooops, no BMP180 detected ... Check your wiring!"); + while (1); + } + if (!htu.begin()) { + Serial.println("Couldn't find HTU21DF sensor!"); + while (1); + } +} + + +void sensorreadout() +{ + sensors_event_t accel_event; + sensors_event_t mag_event; + sensors_event_t bmp_event; + sensors_vec_t orientation; + + /* Calculate pitch and roll from the raw accelerometer data */ + accel.getEvent(&accel_event); + if (dof.accelGetOrientation(&accel_event, &orientation)) + { + accelx = (accel_event.acceleration.x); + accely = (accel_event.acceleration.y); + accelz = (accel_event.acceleration.z); + + } + + /* Calculate the heading using the magnetometer */ + mag.getEvent(&mag_event); + if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation)) + { + magx = (mag_event.magnetic.x); + magy = (mag_event.magnetic.y); + magz = (mag_event.magnetic.z); + } + + /* Calculate the altitude using the barometric pressure sensor */ + bmp.getEvent(&bmp_event); + if (bmp_event.pressure) + { + /* Get ambient temperature in C */ + bmp.getTemperature(&temperature); + /* Convert atmospheric pressure, SLP and temp to altitude */ + baroaltitude = (bmp.pressureToAltitude(seaLevelPressure,bmp_event.pressure,temperature)); + + temperatureh = (htu.readTemperature()); + humidity = (htu.readHumidity()); + } + + energy1= energy1 + random(-100,100); + energy2= energy2 + random(-100,100); + uptime++; + +} + +void timeset() +{ + timereset = true; +} + +void eventtrigger() +{ + eventhappened = true; +}