subDesTagesMitExtraKaese пре 3 година
родитељ
комит
9df9922254

+ 0 - 83
arduino/.vscode/c_cpp_properties.json

@@ -1,83 +0,0 @@
-{
-    "configurations": [
-        {
-            "name": "!!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags"
-        },
-        {
-            "name": "Win32",
-            "includePath": [
-                "c:/Users/johan/repos/lern-tracking-system/arduino/include",
-                "c:/Users/johan/repos/lern-tracking-system/arduino/src",
-                "c:/Users/johan/repos/lern-tracking-system/arduino/.pio/libdeps/uno/BMP280_DEV",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
-                "c:/Users/johan/repos/lern-tracking-system/arduino/.pio/libdeps/uno/MPU9250",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/cores/arduino",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/variants/standard",
-                "C:/Users/johan/.platformio/lib/Adafruit BusIO_ID6214",
-                "C:/Users/johan/.platformio/lib/Adafruit GFX Library_ID13",
-                "C:/Users/johan/.platformio/lib/Adafruit SSD1306",
-                "C:/Users/johan/.platformio/lib/ArduinoJson_ID64/src",
-                "C:/Users/johan/.platformio/lib/ArduinoJson_ID64@5.13.4/src",
-                "C:/Users/johan/.platformio/lib/PID_ID2",
-                "C:/Users/johan/.platformio/lib/PubSubClient_ID89/src",
-                "C:/Users/johan/.platformio/lib/QC3Control_ID1856/src",
-                "C:/Users/johan/.platformio/lib/Stepper_ID874/src",
-                "C:/Users/johan/.platformio/lib/TinyGPSPlus_ID1655/src",
-                "C:/Users/johan/.platformio/lib/WifiManager_ID567",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/HID/src",
-                "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
-                "C:/Users/johan/.platformio/packages/tool-unity",
-                ""
-            ],
-            "browse": {
-                "limitSymbolsToIncludedHeaders": true,
-                "path": [
-                    "c:/Users/johan/repos/lern-tracking-system/arduino/include",
-                    "c:/Users/johan/repos/lern-tracking-system/arduino/src",
-                    "c:/Users/johan/repos/lern-tracking-system/arduino/.pio/libdeps/uno/BMP280_DEV",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
-                    "c:/Users/johan/repos/lern-tracking-system/arduino/.pio/libdeps/uno/MPU9250",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/cores/arduino",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/variants/standard",
-                    "C:/Users/johan/.platformio/lib/Adafruit BusIO_ID6214",
-                    "C:/Users/johan/.platformio/lib/Adafruit GFX Library_ID13",
-                    "C:/Users/johan/.platformio/lib/Adafruit SSD1306",
-                    "C:/Users/johan/.platformio/lib/ArduinoJson_ID64/src",
-                    "C:/Users/johan/.platformio/lib/ArduinoJson_ID64@5.13.4/src",
-                    "C:/Users/johan/.platformio/lib/PID_ID2",
-                    "C:/Users/johan/.platformio/lib/PubSubClient_ID89/src",
-                    "C:/Users/johan/.platformio/lib/QC3Control_ID1856/src",
-                    "C:/Users/johan/.platformio/lib/Stepper_ID874/src",
-                    "C:/Users/johan/.platformio/lib/TinyGPSPlus_ID1655/src",
-                    "C:/Users/johan/.platformio/lib/WifiManager_ID567",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/HID/src",
-                    "C:/Users/johan/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
-                    "C:/Users/johan/.platformio/packages/tool-unity",
-                    ""
-                ]
-            },
-            "defines": [
-                "PLATFORMIO=50003",
-                "ARDUINO_AVR_UNO",
-                "F_CPU=16000000L",
-                "ARDUINO_ARCH_AVR",
-                "ARDUINO=10808",
-                "__AVR_ATmega328P__",
-                ""
-            ],
-            "intelliSenseMode": "gcc-x64",
-            "cStandard": "c11",
-            "cppStandard": "c++11",
-            "compilerPath": "C:/Users/johan/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
-            "compilerArgs": [
-                "-mmcu=atmega328p",
-                ""
-            ]
-        }
-    ],
-    "version": 4
-}

+ 0 - 34
arduino/.vscode/launch.json

@@ -1,34 +0,0 @@
-// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
-
-// PIO Unified Debugger
-//
-// Documentation: https://docs.platformio.org/page/plus/debugging.html
-// Configuration: https://docs.platformio.org/page/projectconf/section_env_debug.html
-
-{
-    "version": "0.2.0",
-    "configurations": [
-        {
-            "type": "platformio-debug",
-            "request": "launch",
-            "name": "PIO Debug",
-            "executable": "c:/Users/johan/repos/lern-tracking-system/arduino/.pio/build/uno/firmware.elf",
-            "projectEnvName": "uno",
-            "toolchainBinDir": "C:/Users/johan/.platformio/packages/toolchain-atmelavr/bin",
-            "preLaunchTask": {
-                "type": "PlatformIO",
-                "task": "Pre-Debug"
-            },
-            "internalConsoleOptions": "openOnSessionStart"
-        },
-        {
-            "type": "platformio-debug",
-            "request": "launch",
-            "name": "PIO Debug (skip Pre-Debug)",
-            "executable": "c:/Users/johan/repos/lern-tracking-system/arduino/.pio/build/uno/firmware.elf",
-            "projectEnvName": "uno",
-            "toolchainBinDir": "C:/Users/johan/.platformio/packages/toolchain-atmelavr/bin",
-            "internalConsoleOptions": "openOnSessionStart"
-        }
-    ]
-}

+ 22 - 0
arduino/include/ultrasonic.hpp

@@ -0,0 +1,22 @@
+#ifndef ULTRASONIC_H
+#define ULTRASONIC_H
+
+#include <Arduino.h>
+#include <inttypes.h>
+
+#define US_RX_0_PIN 2
+#define US_RX_1_PIN 3
+#define US_TX_0_PIN 4
+
+struct usData_t {
+  uint32_t pulseStart;
+  long duration;
+  bool rxPending;
+  bool newData;
+};
+
+void us_init();
+void us_transmit();
+long us_get_duration(byte sensor);
+
+#endif

+ 1 - 1
arduino/platformio.ini

@@ -12,7 +12,7 @@
 platform = atmelavr
 board = uno
 framework = arduino
-monitor_speed = 115200
+monitor_speed = 1000000
 board_build.f_cpu = 16000000L
 lib_deps = 
 	hideakitai/MPU9250@^0.2.3

+ 73 - 18
arduino/src/main.cpp

@@ -1,37 +1,92 @@
 #include <Arduino.h>
 #include <BMP280_DEV.h>
 #include <MPU9250.h>
+#include "ultrasonic.hpp"
 
-#define US_TX_0_PIN 3
-#define US_RX_0_PIN 2
-#define US_RX_1_PIN 1
+#define LOOP_INTERVAL_US (10 * 1000)    // 10 * 1000us
+#define   US_INTERVAL_US (400 * 29)     // 400cm * 29us/cm
 
 MPU9250 mpu;
 
-char outputBuffer[64];
+void toggleLed() {
+  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
+}
 
 void setup() {
-  Serial.begin(115200);
-  Serial.println("boot");
-  pinMode(US_TX_0_PIN, OUTPUT);
+  Serial.begin(1000000);
+  Serial.println(F("boot"));
+  pinMode(LED_BUILTIN, OUTPUT);
+  
+  //initialize acustic sensor
+  us_init();
+
+  //initialize I2C MPU
   Wire.begin();
   delay(2000);
 
-  mpu.setup(0x68);  // change to your own address
+  Serial.println(F("setup mpu"));
+  mpu.setup(0x68); 
+
+  delay(5000);
+
+  Serial.println(F("calibrate accel/gyro"));
+  mpu.calibrateAccelGyro();
+
+  Serial.println(F("calibrate mag"));
+  mpu.calibrateMag();
+  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"));
+
 }
 
+unsigned long loopStart = 0, usTransmitStart = 0;
 void loop() {
-  snprintf(outputBuffer, sizeof(outputBuffer), "DATA:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
-    //acustic RTT 
-    0, 1,
+  if(micros() >= usTransmitStart + US_INTERVAL_US) {
+    usTransmitStart = micros();
+    //emit ultrasound pulse
+    us_transmit();
+    toggleLed();
+  }
+
+  if(micros() >= loopStart + LOOP_INTERVAL_US) {
+    loopStart = micros();
+
+    //get mpu values
+    mpu.update();
+
+    Serial.print(F("DATA:"));
+
+    //acustic travel time in microseconds 
+    for(byte i=0; i<2; i++) {
+      Serial.print(us_get_duration(i));
+      Serial.print("\t");
+    }
+
     //magnetic field
-    2, 3, 4,
+    for(byte i=0; i<2; i++) {
+      Serial.print(mpu.getMag(i));
+      Serial.print("\t");
+    }
+
     //accelerometer
-    5, 6, 7,
+    for(byte i=0; i<2; i++) {
+      Serial.print(mpu.getAcc(i));
+      Serial.print("\t");
+    }
+
     //gyroscope
-    8, 9, 10,
-    //pressure
-    11
-  );
-  Serial.write(outputBuffer);
+    for(byte i=0; i<2; i++) {
+      Serial.print(mpu.getGyro(i));
+      Serial.print("\t");
+    }
+
+    //temperature
+    Serial.print(mpu.getTemperature());
+    Serial.print("\t");
+
+    //execution time in microseconds
+    Serial.print(micros() - loopStart);
+    Serial.println();
+
+  }
+
 }

+ 66 - 0
arduino/src/ultrasonic.cpp

@@ -0,0 +1,66 @@
+#include "ultrasonic.hpp"
+
+struct usData_t usData[2];
+
+void us_0_isr() {
+  if(digitalRead(US_RX_0_PIN)) {
+    usData[0].pulseStart = micros();
+  } else if(usData[0].rxPending) {
+    usData[0].duration   = micros() - usData[0].pulseStart;
+    usData[0].rxPending = 0;
+    usData[0].newData   = 1;
+  }
+}
+
+void us_1_isr() {
+  if(digitalRead(US_RX_1_PIN)) {
+    usData[1].pulseStart = micros();
+  } else if(usData[1].rxPending) {
+    usData[1].duration   = micros() - usData[1].pulseStart;
+    usData[1].rxPending = 0;
+    usData[1].newData   = 1;
+  }
+}
+
+void us_init() {
+  attachInterrupt(digitalPinToInterrupt(US_RX_0_PIN), us_0_isr, CHANGE);
+  attachInterrupt(digitalPinToInterrupt(US_RX_1_PIN), us_1_isr, CHANGE);
+}
+
+void us_transmit() {
+  noInterrupts();
+
+  pinMode(US_RX_0_PIN, OUTPUT);
+  pinMode(US_RX_1_PIN, OUTPUT);
+  pinMode(US_TX_0_PIN, OUTPUT);
+  digitalWrite(US_RX_0_PIN, LOW);
+  digitalWrite(US_RX_1_PIN, LOW);
+  digitalWrite(US_TX_0_PIN, LOW);
+  delayMicroseconds(2);
+  digitalWrite(US_RX_0_PIN, HIGH);
+  digitalWrite(US_RX_1_PIN, HIGH);
+  digitalWrite(US_TX_0_PIN, HIGH);
+  delayMicroseconds(5);
+  digitalWrite(US_RX_0_PIN, LOW);
+  digitalWrite(US_RX_1_PIN, LOW);
+  digitalWrite(US_TX_0_PIN, LOW);
+  pinMode(US_RX_0_PIN, INPUT);
+  pinMode(US_RX_1_PIN, INPUT);
+  pinMode(US_TX_0_PIN, INPUT);
+
+  interrupts();
+
+  usData[0].rxPending = 1;
+  usData[1].rxPending = 1;
+} 
+
+long us_get_duration(byte sensor) {
+  // check if there is new sensor data
+  if(usData[sensor].newData) {
+    usData[sensor].newData = 0;
+    return usData[sensor].duration;
+  } else {
+    return usData[sensor].duration;
+    //return -1;
+  }
+}