main.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <Arduino.h>
  2. #include <BMP280_DEV.h>
  3. #include <MPU9250.h>
  4. #include "ultrasonic.hpp"
  5. #define LOOP_INTERVAL_US (12 * 1000) // 12 * 1000us
  6. MPU9250 mpu;
  7. void blinkLed() {
  8. digitalWrite(LED_BUILTIN, (millis()>>7) & 1);
  9. }
  10. void setup() {
  11. Serial.begin(1000000);
  12. Serial.println(F("boot"));
  13. pinMode(LED_BUILTIN, OUTPUT);
  14. //initialize acoustic sensor
  15. us_init();
  16. //initialize I2C MPU
  17. Wire.begin();
  18. delay(2000);
  19. Serial.println(F("setup mpu"));
  20. mpu.setup(0x68);
  21. delay(1000);
  22. Serial.println(F("Calibrate accel/gyro"));
  23. mpu.calibrateAccelGyro();
  24. Serial.println(F("Calibrate Magnetometer"));
  25. //mpu.calibrateMag();
  26. Serial.println(F("FIELDS:\tUS_0\tUS_1\tMAG_X\tMAG_Y\tMAG_Z\tACCEL_X\tACCEL_Y\tACCEL_Z\tGYRO_X\tGYRO_Y\tGYRO_Z\tTEMP\tEXEC_TIME"));
  27. }
  28. unsigned long loopStart = 0;
  29. char outputBuffer[256];
  30. void loop() {
  31. if(micros() >= loopStart + LOOP_INTERVAL_US) {
  32. loopStart = micros();
  33. /*if (Serial.available() > 0){
  34. char c = Serial.read(1);
  35. if (c == 'c'){
  36. mpu.calibrateMag();
  37. }
  38. }*/
  39. //get mpu values
  40. mpu.update();
  41. snprintf(outputBuffer, sizeof(outputBuffer), "DATA:\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\r\n",
  42. //acoustic RTT
  43. us_get_duration(0), us_get_duration(1),
  44. //magnetic field
  45. (long)(mpu.getMagX()*1000),
  46. (long)(mpu.getMagY()*1000),
  47. (long)(mpu.getMagZ()*1000),
  48. //accelerometer
  49. (long)(mpu.getAccX()*1000),
  50. (long)(mpu.getAccY()*1000),
  51. (long)(mpu.getAccZ()*1000),
  52. /*gyroscope (values not needed)
  53. (long)(mpu.getGyroX()*1000),
  54. (long)(mpu.getGyroY()*1000),
  55. (long)(mpu.getGyroZ()*1000),*/
  56. //temperature
  57. (long)(mpu.getTemperature()*1000),
  58. //execution time in microseconds
  59. micros() - loopStart
  60. );
  61. Serial.write(outputBuffer);
  62. blinkLed();
  63. //emit ultrasound pulse
  64. us_transmit();
  65. }
  66. }