main.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 acustic 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. //emit ultrasound pulse
  32. us_transmit();
  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\r\n",
  36. //acustic 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. //accelerometer
  43. (long)(mpu.getAccX()*1000),
  44. (long)(mpu.getAccY()*1000),
  45. (long)(mpu.getAccZ()*1000),
  46. //gyroscope
  47. (long)(mpu.getGyroX()*1000),
  48. (long)(mpu.getGyroY()*1000),
  49. (long)(mpu.getGyroZ()*1000),
  50. //temperature
  51. (long)(mpu.getTemperature()*1000),
  52. //execution time in microseconds
  53. micros() - loopStart
  54. );
  55. Serial.write(outputBuffer);
  56. blinkLed();
  57. }
  58. }