main.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\tMAG_Off_X\tMAG_Off_Y\tMAG_Off_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. //get mpu values
  34. mpu.update();
  35. 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\t%ld\t%ld\t%ld\r\n",
  36. //acoustic RTT
  37. us_get_duration(0), us_get_duration(1),
  38. //magnetic field
  39. (long)(mpu.getMagX()*1000),
  40. (long)(mpu.getMagY()*1000),
  41. (long)(mpu.getMagZ()*1000),
  42. // magnetic bias (offsets) ###
  43. (long) (mpu.getMagBiasX()*1000),
  44. (long) (mpu.getMagBiasY()*1000),
  45. (long) (mpu.getMagBiasZ()*1000),
  46. //accelerometer
  47. (long)(mpu.getAccX()*1000),
  48. (long)(mpu.getAccY()*1000),
  49. (long)(mpu.getAccZ()*1000),
  50. //gyroscope
  51. (long)(mpu.getGyroX()*1000),
  52. (long)(mpu.getGyroY()*1000),
  53. (long)(mpu.getGyroZ()*1000),
  54. //temperature
  55. (long)(mpu.getTemperature()*1000),
  56. //execution time in microseconds
  57. micros() - loopStart
  58. );
  59. Serial.write(outputBuffer);
  60. blinkLed();
  61. //emit ultrasound pulse
  62. us_transmit();
  63. }
  64. }