diff --git a/m5__wave-sampler-gps-imu/config.h b/m5__wave-sampler-gps-imu/config.h new file mode 100644 index 0000000..891c65b --- /dev/null +++ b/m5__wave-sampler-gps-imu/config.h @@ -0,0 +1,3 @@ + +#define WIFI_SSID "spectrum2000" +#define WIFI_PASSWORD "goodlife" diff --git a/m5__wave-sampler-gps-imu/fsutils.cpp b/m5__wave-sampler-gps-imu/fsutils.cpp new file mode 100644 index 0000000..dd055f8 --- /dev/null +++ b/m5__wave-sampler-gps-imu/fsutils.cpp @@ -0,0 +1,33 @@ +#include "fsutils.h" + +void dir_list(fs::FS &fs, const char * dirname, uint8_t levels) +{ + Serial.printf("Listing directory: %s\n", dirname); + + File root = fs.open(dirname); + if(!root){ + Serial.println("Failed to open directory"); + return; + } + if(!root.isDirectory()){ + Serial.println("Not a directory"); + return; + } + + File file = root.openNextFile(); + while(file){ + if(file.isDirectory()){ + Serial.print(" DIR : "); + Serial.println(file.name()); + if(levels){ + dir_list(fs, file.name(), levels -1); + } + } else { + Serial.print(" FILE: "); + Serial.print(file.name()); + Serial.print(" SIZE: "); + Serial.println(file.size()); + } + file = root.openNextFile(); + } +} diff --git a/m5__wave-sampler-gps-imu/fsutils.h b/m5__wave-sampler-gps-imu/fsutils.h new file mode 100644 index 0000000..0a909b2 --- /dev/null +++ b/m5__wave-sampler-gps-imu/fsutils.h @@ -0,0 +1,3 @@ +#include "FS.h" + +void dir_list(fs::FS &fs, const char * dirname, uint8_t levels); diff --git a/m5__wave-sampler-gps-imu/m5__wave-sampler-gps-imu.ino b/m5__wave-sampler-gps-imu/m5__wave-sampler-gps-imu.ino index a1b5329..ceb4aaa 100644 --- a/m5__wave-sampler-gps-imu/m5__wave-sampler-gps-imu.ino +++ b/m5__wave-sampler-gps-imu/m5__wave-sampler-gps-imu.ino @@ -2,18 +2,34 @@ #include "M5Atom.h" #include "GPSAnalyse.h" +#include "fsutils.h" #include -#include "FS.h" #include #include +#include "config.h" +#include +#include +#include +#include + +WiFiUDP Udp; +const IPAddress destination(192, 168, 8, 200); +const unsigned int rxport = 54321; // listen port +const unsigned int txport = 12345; // send to port + + +//char payload[512]; +String payload = ""; + GPSAnalyse GPS; Metro banggps(2000); -Metro bangimu(20); +Metro bangimu(33); // 30 fps Metro banglog(1000); -const char outfilename[] = "/wave-sample.csv"; -File of; +bool sdready = false; +bool recording = false; +String outfn; typedef struct sGps { float lat; @@ -44,16 +60,29 @@ unsigned long mselapsed; tGps gpssample; tImu imusample; -bool writeSample(String filename) { - of = SD.open(filename, FILE_APPEND); +String filename_make_sequential() { + char fn[24]; + for (int n = 1; n < 1000; n++) { + sprintf(fn, "/wavesample__%.3d.csv", n); + if ( SD.exists(fn) ) { + continue; + } else { + break; + } + } + return String(fn); +} + +bool sample_write(String filename) { + File of = SD.open(filename, FILE_APPEND); if(of) { of.print( mselapsed ); of.print(", "); of.print(gpssample.lat); of.print(", "); of.print(gpssample.lon); - of.print(", "); - of.print(gpssample.utc); +// of.print(", "); +// of.print(gpssample.utc); of.print(", "); of.print(imusample.aX); of.print(", "); @@ -77,25 +106,73 @@ bool writeSample(String filename) { of.println(); // EOL of.close(); } else { + Serial.println("Failed to write sample to SD card"); return false; } + + return true; +} + +bool sdcard_init() { + Serial.print("SDcard init..."); + SPI.begin(23, 33, 19, -1); + if( !SD.begin(-1, SPI) ) { + Serial.println("SD card mount failed"); + return false; + } + + uint8_t cardType = SD.cardType(); + + if(cardType == CARD_NONE){ + Serial.println("No SD card attached"); + return false; + } + + Serial.print("SD Card Type: "); + + if(cardType == CARD_MMC){ + Serial.println("MMC"); + } else if(cardType == CARD_SD){ + Serial.println("SDSC"); + } else if(cardType == CARD_SDHC){ + Serial.println("SDHC"); + } else { + Serial.println("UNKNOWN"); + } + + uint64_t cardSize = SD.cardSize() / (1024 * 1024); + Serial.printf("SD Card Size: %lluMB\n", cardSize); + + dir_list(SD, "/", 0); return true; } void setup(){ M5.begin(true, false, true); - M5.IMU.Init(); - SPI.begin(23,33,19,-1); - if(!SD.begin(-1, SPI, 40000000)){ - Serial.println("initialization failed!"); - } - sdcard_type_t Type = SD.cardType(); + delay(100); - Serial.printf("SDCard Type = %d \r\n",Type); - Serial.printf("SDCard Size = %d \r\n" , (int)(SD.cardSize()/1024/1024)); + // connect to existing wifi access point as client + if( wifi_connect_as_client(WIFI_SSID, WIFI_PASSWORD) ) { + // print debugging information + wifi_print_mode(); + wifi_print_ip(); // print our known ip address + } else { + Serial.print("Failed to connect to wifi "); + Serial.print( WIFI_SSID ); + Serial.println(); + } + + Udp.begin(rxport); + Serial.print("Listening on port "); + Serial.println(rxport); + + sdready = sdcard_init(); + delay(500); + outfn = filename_make_sequential(); + Serial1.begin(9600,SERIAL_8N1,22,-1); GPS.setTaskName("GPS"); GPS.setTaskPriority(2); @@ -120,14 +197,16 @@ void imu_loop() { M5.IMU.getTempData(&imusample.temp); // temperature } -void debugSample() { +void sample_debug() { + Serial.print( outfn ); + Serial.print(", "); Serial.print( mselapsed ); Serial.print(", "); Serial.print(gpssample.lat); Serial.print(", "); Serial.print(gpssample.lon); - Serial.print(", "); - Serial.print(gpssample.utc); +// Serial.print(", "); +// Serial.print(gpssample.utc); Serial.print(", "); Serial.print(imusample.aX); Serial.print(", "); @@ -151,8 +230,112 @@ void debugSample() { Serial.println(); // EOL } +void sample_osc_dispatch() { + + payload = ""; + payload.concat(mselapsed); + payload.concat(", "); + payload.concat(gpssample.lat); + payload.concat(", "); + payload.concat(gpssample.lon); + payload.concat(", "); + payload.concat(imusample.aX); + payload.concat(", "); + payload.concat(imusample.aY); + payload.concat(", "); + payload.concat(imusample.aZ); + payload.concat(", "); + payload.concat(imusample.gX); + payload.concat(", "); + payload.concat(imusample.gY); + payload.concat(", "); + payload.concat(imusample.gZ); + payload.concat(", "); + payload.concat(imusample.pitch); + payload.concat(", "); + payload.concat(imusample.roll); + payload.concat(", "); + payload.concat(imusample.yaw); + +// sprintf(payload, "%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\0", +// mselapsed, +// dtostrf(gpssample.lat), +// dtostrf(gpssample.lon), +// dtostrf(imusample.aX), +// dtostrf(imusample.aY), +// dtostrf(imusample.aZ), +// dtostrf(imusample.gX), +// dtostrf(imusample.gY), +// dtostrf(imusample.gZ), +// dtostrf(imusample.pitch), +// dtostrf(imusample.roll), +// dtostrf(imusample.yaw), +// dtostrf(imusample.temp) +// ); + + //Serial.println( payload ); + +// these two lines craft the message (with an address and a parameter) + OSCMessage out("/wavesampler/sample"); +// out.add( payload ); + out.add(1.0f*mselapsed); + out.add(gpssample.lat); + out.add(gpssample.lon); + out.add(imusample.aX); + out.add(imusample.aY); + out.add(imusample.aZ); + out.add(imusample.gX); + out.add(imusample.gY); + out.add(imusample.gZ); + out.add(imusample.pitch); + out.add(imusample.roll); + out.add(imusample.yaw); + + // these 4 lines that follow send out the message + Udp.beginPacket(destination, txport); + out.send(Udp); + Udp.endPacket(); + out.empty(); +} + +void osc_route_sample(OSCMessage &msg, int addrOffset ) { + if( msg.match("/sample/start") ) { + outfn = filename_make_sequential(); + Serial.print("() Start recording to file "); + Serial.print(outfn); + Serial.println("..."); + recording = true; + } else if( msg.match("/sample/stop") ) { + Serial.println("[] STOP recording..."); + recording = false; + } +} + +void osc_message_pump() { + OSCMessage in; + int size; + + if( (size = Udp.parsePacket()) > 0) + { + // parse incoming OSC message + while(size--) { + in.fill( Udp.read() ); + } + + if(!in.hasError()) { + // if we had never received a message before, make sure we set the flag to avoid triggering 'demo mode' + //if(!msgReceived) { msgReceived = true; } +// Serial.print("<< osc message received"); + + in.route("/sample", osc_route_sample); + } + } // if +} + void loop() { - M5.dis.fillpix(0x000000); +// M5.dis.fillpix(0x000000); + + osc_message_pump(); if(banggps.check()) { gps_loop(); @@ -160,12 +343,15 @@ void loop() { if(bangimu.check()) { imu_loop(); -// writeSample( outfilename ); + if( recording ) { + sample_write( outfn ); + } + //sample_osc_dispatch(); } - if(banglog.check()) { - debugSample(); - } +// if(banglog.check()) { +// sample_debug(); +// } mselapsed = millis(); }