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.

171 lines
3.6 KiB

3 years ago
  1. #undef DEBUG_GPS
  2. #include "M5Atom.h"
  3. #include "GPSAnalyse.h"
  4. #include <SPI.h>
  5. #include "FS.h"
  6. #include <SD.h>
  7. #include <Metro.h>
  8. GPSAnalyse GPS;
  9. Metro banggps(2000);
  10. Metro bangimu(20);
  11. Metro banglog(1000);
  12. const char outfilename[] = "/wave-sample.csv";
  13. File of;
  14. typedef struct sGps {
  15. float lat;
  16. float lon;
  17. String utc;
  18. } tGps;
  19. typedef struct sImu {
  20. // accelerometer
  21. float aX = 0.0f;
  22. float aY = 0.0f;
  23. float aZ = 0.0f;
  24. // gyroscope
  25. float gX = 0.0f;
  26. float gY = 0.0f;
  27. float gZ = 0.0f;
  28. // angles
  29. float pitch = 0.0f;
  30. float roll = 0.0f;
  31. float yaw = 0.0f;
  32. float temp = 0.0f;
  33. } tImu;
  34. unsigned long mselapsed;
  35. tGps gpssample;
  36. tImu imusample;
  37. bool writeSample(String filename) {
  38. of = SD.open(filename, FILE_APPEND);
  39. if(of) {
  40. of.print( mselapsed );
  41. of.print(", ");
  42. of.print(gpssample.lat);
  43. of.print(", ");
  44. of.print(gpssample.lon);
  45. of.print(", ");
  46. of.print(gpssample.utc);
  47. of.print(", ");
  48. of.print(imusample.aX);
  49. of.print(", ");
  50. of.print(imusample.aY);
  51. of.print(", ");
  52. of.print(imusample.aZ);
  53. of.print(", ");
  54. of.print(imusample.gX);
  55. of.print(", ");
  56. of.print(imusample.gY);
  57. of.print(", ");
  58. of.print(imusample.gZ);
  59. of.print(", ");
  60. of.print(imusample.pitch);
  61. of.print(", ");
  62. of.print(imusample.roll);
  63. of.print(", ");
  64. of.print(imusample.yaw);
  65. of.print(", ");
  66. of.print(imusample.temp);
  67. of.println(); // EOL
  68. of.close();
  69. } else {
  70. return false;
  71. }
  72. return true;
  73. }
  74. void setup(){
  75. M5.begin(true, false, true);
  76. M5.IMU.Init();
  77. SPI.begin(23,33,19,-1);
  78. if(!SD.begin(-1, SPI, 40000000)){
  79. Serial.println("initialization failed!");
  80. }
  81. sdcard_type_t Type = SD.cardType();
  82. Serial.printf("SDCard Type = %d \r\n",Type);
  83. Serial.printf("SDCard Size = %d \r\n" , (int)(SD.cardSize()/1024/1024));
  84. Serial1.begin(9600,SERIAL_8N1,22,-1);
  85. GPS.setTaskName("GPS");
  86. GPS.setTaskPriority(2);
  87. GPS.setSerialPtr(Serial1);
  88. GPS.start();
  89. M5.dis.fillpix(0x000000);
  90. }
  91. void gps_loop() {
  92. GPS.upDate();
  93. gpssample.lat = GPS.s_GNRMC.Latitude;
  94. gpssample.lon = GPS.s_GNRMC.Longitude;
  95. gpssample.utc = GPS.s_GNRMC.Utc;
  96. }
  97. void imu_loop() {
  98. //Stores the triaxial gyroscope data of the inertial sensor to the relevant variable
  99. M5.IMU.getGyroData(&imusample.gX,&imusample.gY,&imusample.gZ);
  100. M5.IMU.getAccelData(&imusample.aX,&imusample.aY,&imusample.aZ); // accelero
  101. M5.IMU.getAhrsData(&imusample.pitch,&imusample.roll,&imusample.yaw); // attitude
  102. M5.IMU.getTempData(&imusample.temp); // temperature
  103. }
  104. void debugSample() {
  105. Serial.print( mselapsed );
  106. Serial.print(", ");
  107. Serial.print(gpssample.lat);
  108. Serial.print(", ");
  109. Serial.print(gpssample.lon);
  110. Serial.print(", ");
  111. Serial.print(gpssample.utc);
  112. Serial.print(", ");
  113. Serial.print(imusample.aX);
  114. Serial.print(", ");
  115. Serial.print(imusample.aY);
  116. Serial.print(", ");
  117. Serial.print(imusample.aZ);
  118. Serial.print(", ");
  119. Serial.print(imusample.gX);
  120. Serial.print(", ");
  121. Serial.print(imusample.gY);
  122. Serial.print(", ");
  123. Serial.print(imusample.gZ);
  124. Serial.print(", ");
  125. Serial.print(imusample.pitch);
  126. Serial.print(", ");
  127. Serial.print(imusample.roll);
  128. Serial.print(", ");
  129. Serial.print(imusample.yaw);
  130. Serial.print(", ");
  131. Serial.print(imusample.temp);
  132. Serial.println(); // EOL
  133. }
  134. void loop() {
  135. M5.dis.fillpix(0x000000);
  136. if(banggps.check()) {
  137. gps_loop();
  138. }
  139. if(bangimu.check()) {
  140. imu_loop();
  141. // writeSample( outfilename );
  142. }
  143. if(banglog.check()) {
  144. debugSample();
  145. }
  146. mselapsed = millis();
  147. }