Browse Source

supports OSC to enable and disable logging, improved SD initialization and logging

master
Luis Rodil-Fernandez 3 years ago
parent
commit
fe7c671deb
  1. 3
      m5__wave-sampler-gps-imu/config.h
  2. 33
      m5__wave-sampler-gps-imu/fsutils.cpp
  3. 3
      m5__wave-sampler-gps-imu/fsutils.h
  4. 232
      m5__wave-sampler-gps-imu/m5__wave-sampler-gps-imu.ino

3
m5__wave-sampler-gps-imu/config.h

@ -0,0 +1,3 @@
#define WIFI_SSID "spectrum2000"
#define WIFI_PASSWORD "goodlife"

33
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();
}
}

3
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);

232
m5__wave-sampler-gps-imu/m5__wave-sampler-gps-imu.ino

@ -2,18 +2,34 @@
#include "M5Atom.h" #include "M5Atom.h"
#include "GPSAnalyse.h" #include "GPSAnalyse.h"
#include "fsutils.h"
#include <SPI.h> #include <SPI.h>
#include "FS.h"
#include <SD.h> #include <SD.h>
#include <Metro.h> #include <Metro.h>
#include "config.h"
#include <easywifi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
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; GPSAnalyse GPS;
Metro banggps(2000); Metro banggps(2000);
Metro bangimu(20);
Metro bangimu(33); // 30 fps
Metro banglog(1000); Metro banglog(1000);
const char outfilename[] = "/wave-sample.csv";
File of;
bool sdready = false;
bool recording = false;
String outfn;
typedef struct sGps { typedef struct sGps {
float lat; float lat;
@ -44,16 +60,29 @@ unsigned long mselapsed;
tGps gpssample; tGps gpssample;
tImu imusample; 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) { if(of) {
of.print( mselapsed ); of.print( mselapsed );
of.print(", "); of.print(", ");
of.print(gpssample.lat); of.print(gpssample.lat);
of.print(", "); of.print(", ");
of.print(gpssample.lon); of.print(gpssample.lon);
of.print(", ");
of.print(gpssample.utc);
// of.print(", ");
// of.print(gpssample.utc);
of.print(", "); of.print(", ");
of.print(imusample.aX); of.print(imusample.aX);
of.print(", "); of.print(", ");
@ -77,24 +106,72 @@ bool writeSample(String filename) {
of.println(); // EOL of.println(); // EOL
of.close(); of.close();
} else { } else {
Serial.println("Failed to write sample to SD card");
return false; 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; return true;
} }
void setup(){ void setup(){
M5.begin(true, false, true); M5.begin(true, false, true);
M5.IMU.Init(); M5.IMU.Init();
SPI.begin(23,33,19,-1);
if(!SD.begin(-1, SPI, 40000000)){
Serial.println("initialization failed!");
delay(100);
// 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();
} }
sdcard_type_t Type = SD.cardType();
Serial.printf("SDCard Type = %d \r\n",Type);
Serial.printf("SDCard Size = %d \r\n" , (int)(SD.cardSize()/1024/1024));
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); Serial1.begin(9600,SERIAL_8N1,22,-1);
GPS.setTaskName("GPS"); GPS.setTaskName("GPS");
@ -120,14 +197,16 @@ void imu_loop() {
M5.IMU.getTempData(&imusample.temp); // temperature M5.IMU.getTempData(&imusample.temp); // temperature
} }
void debugSample() {
void sample_debug() {
Serial.print( outfn );
Serial.print(", ");
Serial.print( mselapsed ); Serial.print( mselapsed );
Serial.print(", "); Serial.print(", ");
Serial.print(gpssample.lat); Serial.print(gpssample.lat);
Serial.print(", "); Serial.print(", ");
Serial.print(gpssample.lon); Serial.print(gpssample.lon);
Serial.print(", ");
Serial.print(gpssample.utc);
// Serial.print(", ");
// Serial.print(gpssample.utc);
Serial.print(", "); Serial.print(", ");
Serial.print(imusample.aX); Serial.print(imusample.aX);
Serial.print(", "); Serial.print(", ");
@ -151,8 +230,112 @@ void debugSample() {
Serial.println(); // EOL 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() { void loop() {
M5.dis.fillpix(0x000000);
// M5.dis.fillpix(0x000000);
osc_message_pump();
if(banggps.check()) { if(banggps.check()) {
gps_loop(); gps_loop();
@ -160,12 +343,15 @@ void loop() {
if(bangimu.check()) { if(bangimu.check()) {
imu_loop(); imu_loop();
// writeSample( outfilename );
if( recording ) {
sample_write( outfn );
}
//sample_osc_dispatch();
} }
if(banglog.check()) {
debugSample();
}
// if(banglog.check()) {
// sample_debug();
// }
mselapsed = millis(); mselapsed = millis();
} }
Loading…
Cancel
Save