You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
7.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #undef DEBUG_GPS
  2. #include "M5Atom.h"
  3. #include "GPSAnalyse.h"
  4. #include "fsutils.h"
  5. #include <SPI.h>
  6. #include <SD.h>
  7. #include <Metro.h>
  8. #include "config.h"
  9. #include <easywifi.h>
  10. #include <WiFiUdp.h>
  11. #include <OSCMessage.h>
  12. #include <OSCBundle.h>
  13. WiFiUDP Udp;
  14. const IPAddress destination(192, 168, 8, 200);
  15. const unsigned int rxport = 54321; // listen port
  16. const unsigned int txport = 12345; // send to port
  17. //char payload[512];
  18. String payload = "";
  19. GPSAnalyse GPS;
  20. Metro banggps(2000);
  21. Metro bangimu(33); // 30 fps
  22. Metro banglog(1000);
  23. bool sdready = false;
  24. bool recording = false;
  25. String outfn;
  26. typedef struct sGps {
  27. float lat;
  28. float lon;
  29. String utc;
  30. } tGps;
  31. typedef struct sImu {
  32. // accelerometer
  33. float aX = 0.0f;
  34. float aY = 0.0f;
  35. float aZ = 0.0f;
  36. // gyroscope
  37. float gX = 0.0f;
  38. float gY = 0.0f;
  39. float gZ = 0.0f;
  40. // angles
  41. float pitch = 0.0f;
  42. float roll = 0.0f;
  43. float yaw = 0.0f;
  44. float temp = 0.0f;
  45. } tImu;
  46. unsigned long mselapsed;
  47. tGps gpssample;
  48. tImu imusample;
  49. String filename_make_sequential() {
  50. char fn[24];
  51. for (int n = 1; n < 1000; n++) {
  52. sprintf(fn, "/wavesample__%.3d.csv", n);
  53. if ( SD.exists(fn) ) {
  54. continue;
  55. } else {
  56. break;
  57. }
  58. }
  59. return String(fn);
  60. }
  61. bool sample_write(String filename) {
  62. File of = SD.open(filename, FILE_APPEND);
  63. if(of) {
  64. of.print( mselapsed );
  65. of.print(", ");
  66. of.print(gpssample.lat);
  67. of.print(", ");
  68. of.print(gpssample.lon);
  69. // of.print(", ");
  70. // of.print(gpssample.utc);
  71. of.print(", ");
  72. of.print(imusample.aX);
  73. of.print(", ");
  74. of.print(imusample.aY);
  75. of.print(", ");
  76. of.print(imusample.aZ);
  77. of.print(", ");
  78. of.print(imusample.gX);
  79. of.print(", ");
  80. of.print(imusample.gY);
  81. of.print(", ");
  82. of.print(imusample.gZ);
  83. of.print(", ");
  84. of.print(imusample.pitch);
  85. of.print(", ");
  86. of.print(imusample.roll);
  87. of.print(", ");
  88. of.print(imusample.yaw);
  89. of.print(", ");
  90. of.print(imusample.temp);
  91. of.println(); // EOL
  92. of.close();
  93. } else {
  94. Serial.println("Failed to write sample to SD card");
  95. return false;
  96. }
  97. return true;
  98. }
  99. bool sdcard_init() {
  100. Serial.print("SDcard init...");
  101. SPI.begin(23, 33, 19, -1);
  102. if( !SD.begin(-1, SPI) ) {
  103. Serial.println("SD card mount failed");
  104. return false;
  105. }
  106. uint8_t cardType = SD.cardType();
  107. if(cardType == CARD_NONE){
  108. Serial.println("No SD card attached");
  109. return false;
  110. }
  111. Serial.print("SD Card Type: ");
  112. if(cardType == CARD_MMC){
  113. Serial.println("MMC");
  114. } else if(cardType == CARD_SD){
  115. Serial.println("SDSC");
  116. } else if(cardType == CARD_SDHC){
  117. Serial.println("SDHC");
  118. } else {
  119. Serial.println("UNKNOWN");
  120. }
  121. uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  122. Serial.printf("SD Card Size: %lluMB\n", cardSize);
  123. dir_list(SD, "/", 0);
  124. return true;
  125. }
  126. void setup(){
  127. M5.begin(true, false, true);
  128. M5.IMU.Init();
  129. delay(100);
  130. // connect to existing wifi access point as client
  131. if( wifi_connect_as_client(WIFI_SSID, WIFI_PASSWORD) ) {
  132. // print debugging information
  133. wifi_print_mode();
  134. wifi_print_ip(); // print our known ip address
  135. } else {
  136. Serial.print("Failed to connect to wifi ");
  137. Serial.print( WIFI_SSID );
  138. Serial.println();
  139. }
  140. Udp.begin(rxport);
  141. Serial.print("Listening on port ");
  142. Serial.println(rxport);
  143. sdready = sdcard_init();
  144. delay(500);
  145. outfn = filename_make_sequential();
  146. Serial1.begin(9600,SERIAL_8N1,22,-1);
  147. GPS.setTaskName("GPS");
  148. GPS.setTaskPriority(2);
  149. GPS.setSerialPtr(Serial1);
  150. GPS.start();
  151. M5.dis.fillpix(0x000000);
  152. }
  153. void gps_loop() {
  154. GPS.upDate();
  155. gpssample.lat = GPS.s_GNRMC.Latitude;
  156. gpssample.lon = GPS.s_GNRMC.Longitude;
  157. gpssample.utc = GPS.s_GNRMC.Utc;
  158. }
  159. void imu_loop() {
  160. //Stores the triaxial gyroscope data of the inertial sensor to the relevant variable
  161. M5.IMU.getGyroData(&imusample.gX,&imusample.gY,&imusample.gZ);
  162. M5.IMU.getAccelData(&imusample.aX,&imusample.aY,&imusample.aZ); // accelero
  163. M5.IMU.getAhrsData(&imusample.pitch,&imusample.roll,&imusample.yaw); // attitude
  164. M5.IMU.getTempData(&imusample.temp); // temperature
  165. }
  166. void sample_debug() {
  167. Serial.print( outfn );
  168. Serial.print(", ");
  169. Serial.print( mselapsed );
  170. Serial.print(", ");
  171. Serial.print(gpssample.lat);
  172. Serial.print(", ");
  173. Serial.print(gpssample.lon);
  174. // Serial.print(", ");
  175. // Serial.print(gpssample.utc);
  176. Serial.print(", ");
  177. Serial.print(imusample.aX);
  178. Serial.print(", ");
  179. Serial.print(imusample.aY);
  180. Serial.print(", ");
  181. Serial.print(imusample.aZ);
  182. Serial.print(", ");
  183. Serial.print(imusample.gX);
  184. Serial.print(", ");
  185. Serial.print(imusample.gY);
  186. Serial.print(", ");
  187. Serial.print(imusample.gZ);
  188. Serial.print(", ");
  189. Serial.print(imusample.pitch);
  190. Serial.print(", ");
  191. Serial.print(imusample.roll);
  192. Serial.print(", ");
  193. Serial.print(imusample.yaw);
  194. Serial.print(", ");
  195. Serial.print(imusample.temp);
  196. Serial.println(); // EOL
  197. }
  198. void sample_osc_dispatch() {
  199. payload = "";
  200. payload.concat(mselapsed);
  201. payload.concat(", ");
  202. payload.concat(gpssample.lat);
  203. payload.concat(", ");
  204. payload.concat(gpssample.lon);
  205. payload.concat(", ");
  206. payload.concat(imusample.aX);
  207. payload.concat(", ");
  208. payload.concat(imusample.aY);
  209. payload.concat(", ");
  210. payload.concat(imusample.aZ);
  211. payload.concat(", ");
  212. payload.concat(imusample.gX);
  213. payload.concat(", ");
  214. payload.concat(imusample.gY);
  215. payload.concat(", ");
  216. payload.concat(imusample.gZ);
  217. payload.concat(", ");
  218. payload.concat(imusample.pitch);
  219. payload.concat(", ");
  220. payload.concat(imusample.roll);
  221. payload.concat(", ");
  222. payload.concat(imusample.yaw);
  223. // sprintf(payload, "%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\0",
  224. // mselapsed,
  225. // dtostrf(gpssample.lat),
  226. // dtostrf(gpssample.lon),
  227. // dtostrf(imusample.aX),
  228. // dtostrf(imusample.aY),
  229. // dtostrf(imusample.aZ),
  230. // dtostrf(imusample.gX),
  231. // dtostrf(imusample.gY),
  232. // dtostrf(imusample.gZ),
  233. // dtostrf(imusample.pitch),
  234. // dtostrf(imusample.roll),
  235. // dtostrf(imusample.yaw),
  236. // dtostrf(imusample.temp)
  237. // );
  238. //Serial.println( payload );
  239. // these two lines craft the message (with an address and a parameter)
  240. OSCMessage out("/wavesampler/sample");
  241. // out.add( payload );
  242. out.add(1.0f*mselapsed);
  243. out.add(gpssample.lat);
  244. out.add(gpssample.lon);
  245. out.add(imusample.aX);
  246. out.add(imusample.aY);
  247. out.add(imusample.aZ);
  248. out.add(imusample.gX);
  249. out.add(imusample.gY);
  250. out.add(imusample.gZ);
  251. out.add(imusample.pitch);
  252. out.add(imusample.roll);
  253. out.add(imusample.yaw);
  254. // these 4 lines that follow send out the message
  255. Udp.beginPacket(destination, txport);
  256. out.send(Udp);
  257. Udp.endPacket();
  258. out.empty();
  259. }
  260. void osc_route_sample(OSCMessage &msg, int addrOffset ) {
  261. if( msg.match("/sample/start") ) {
  262. outfn = filename_make_sequential();
  263. Serial.print("() Start recording to file ");
  264. Serial.print(outfn);
  265. Serial.println("...");
  266. recording = true;
  267. } else if( msg.match("/sample/stop") ) {
  268. Serial.println("[] STOP recording...");
  269. recording = false;
  270. }
  271. }
  272. void osc_message_pump() {
  273. OSCMessage in;
  274. int size;
  275. if( (size = Udp.parsePacket()) > 0)
  276. {
  277. // parse incoming OSC message
  278. while(size--) {
  279. in.fill( Udp.read() );
  280. }
  281. if(!in.hasError()) {
  282. // if we had never received a message before, make sure we set the flag to avoid triggering 'demo mode'
  283. //if(!msgReceived) { msgReceived = true; }
  284. // Serial.print("<< osc message received");
  285. in.route("/sample", osc_route_sample);
  286. }
  287. } // if
  288. }
  289. void loop() {
  290. // M5.dis.fillpix(0x000000);
  291. osc_message_pump();
  292. if(banggps.check()) {
  293. gps_loop();
  294. }
  295. if(bangimu.check()) {
  296. imu_loop();
  297. if( recording ) {
  298. sample_write( outfn );
  299. }
  300. //sample_osc_dispatch();
  301. }
  302. // if(banglog.check()) {
  303. // sample_debug();
  304. // }
  305. mselapsed = millis();
  306. }