main.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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("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"));
  25. }
  26. unsigned long loopStart = 0;
  27. char outputBuffer[256];
  28. void loop() {
  29. if(micros() >= loopStart + LOOP_INTERVAL_US) {
  30. loopStart = micros();
  31. //get mpu values
  32. mpu.update();
  33. 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",
  34. //acoustic RTT
  35. us_get_duration(0), us_get_duration(1),
  36. //magnetic field
  37. (long)(mpu.getMagX()*1000),
  38. (long)(mpu.getMagY()*1000),
  39. (long)(mpu.getMagZ()*1000),
  40. //accelerometer
  41. (long)(mpu.getAccX()*1000),
  42. (long)(mpu.getAccY()*1000),
  43. (long)(mpu.getAccZ()*1000),
  44. //gyroscope
  45. (long)(mpu.getGyroX()*1000),
  46. (long)(mpu.getGyroY()*1000),
  47. (long)(mpu.getGyroZ()*1000),
  48. //temperature
  49. (long)(mpu.getTemperature()*1000),
  50. //execution time in microseconds
  51. micros() - loopStart
  52. );
  53. Serial.write(outputBuffer);
  54. blinkLed();
  55. //emit ultrasound pulse
  56. us_transmit();
  57. }
  58. }