From 075b7c83082bc243f97cb78d6686517572cb0ed9 Mon Sep 17 00:00:00 2001 From: iNocturnis Date: Wed, 25 May 2022 09:30:02 -0700 Subject: [PATCH] first commit --- .gitignore | 5 + .vscode/extensions.json | 10 ++ .vscode/settings.json | 3 + include/README | 39 +++++++ lib/README | 46 ++++++++ platformio.ini | 49 ++++++++ src/main.cpp | 245 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++ 8 files changed, 408 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..691a8f6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "Disabled" +} \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..da38d13 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,49 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +upload_port = /dev/cu.wchusbserial533C0037781 +monitor_speed = 115200 +build_unflags = -std=gnu++11 + +build_flags = + -std=gnu++17 + -Os + -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG + -DUSER_SETUP_LOADED=1 + -DST7789_DRIVER=1 + -DTFT_WIDTH=135 + -DTFT_HEIGHT=240 + -DCGRAM_OFFSET=1 + -DTFT_MISO=-1 + -DTFT_MOSI=19 + -DTFT_SCLK=18 + -DTFT_CS=5 + -DTFT_DC=16 + -DTFT_RST=23 + -DTFT_BL=4 + -DTFT_BACKLIGHT_ON=1 + -DLOAD_GLCD=1 + -DLOAD_FONT2=1 + -DLOAD_FONT4=1 + -DLOAD_FONT6=1 + -DLOAD_FONT7=1 + -DLOAD_FONT8=1 + -DLOAD_GFXFF=1 + -DSMOOTH_FONT=1 + -DSPI_FREQUENCY=40000000 + -DSPI_READ_FREQUENCY=6000000 + +lib_deps: + bodmer/TFT_eSPI@^2.4.70 + sparkfun/SparkFun Qwiic 6Dof - LSM6DSO@^1.0.3 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..f84db0b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,245 @@ +#include +#include +#include +#include +#include +#include + +BluetoothSerial SerialBT; +LSM6DSO accel; + +float begin_values [3]= {0.0,0.0,0.0}; +float prev_values [3] = {0.0,0.0,0.0}; +float min_values [3] = {0.0,0.0,0.0}; +float max_values [3] = {0.0,0.0,0.0}; +float delta_values [3] = {0.0,0.0,0.0}; +float threshold_accel = 0.200; + +float begin_values_gyro [3]= {0.0,0.0,0.0}; +float prev_values_gyro [3] = {0.0,0.0,0.0}; +float min_values_gyro [3] = {0.0,0.0,0.0}; +float max_values_gyro [3] = {0.0,0.0,0.0}; +float delta_values_gyro [3] = {0.0,0.0,0.0}; +float threshold_gyro = 5; + +bool was_moving = false; + +int status = 0; //-1 moving close, 0 stopped, 1 moving open + +void print(auto input){ + Serial.print(input); + SerialBT.print(input); +} + +void println(auto input){ + Serial.println(input); + SerialBT.println(input); +} + +void setup() +{ + Serial.begin(115200); + SerialBT.begin("ESP32_TEST"); + Wire.begin(); + + if (accel.begin()) + { + println("Accelo online"); + } + // Initilize settings + if(accel.initialize(BASIC_SETTINGS)){ + + } + + //ACCEL + begin_values[0] = accel.readFloatAccelX(); + begin_values[1] = accel.readFloatAccelY(); + begin_values[2] = accel.readFloatAccelZ(); + + prev_values[0] = accel.readFloatAccelX(); + prev_values[1] = accel.readFloatAccelY(); + prev_values[2] = accel.readFloatAccelZ(); + + min_values[0] = accel.readFloatAccelX(); + min_values[1] = accel.readFloatAccelY(); + min_values[2] = accel.readFloatAccelZ(); + + max_values[0] = accel.readFloatAccelX(); + max_values[1] = accel.readFloatAccelY(); + max_values[2] = accel.readFloatAccelZ(); + + //GYRO + begin_values_gyro[0] = accel.readFloatGyroX(); + begin_values_gyro[1] = accel.readFloatGyroY(); + begin_values_gyro[2] = accel.readFloatGyroZ(); + + prev_values_gyro[0] = accel.readFloatGyroX(); + prev_values_gyro[1] = accel.readFloatGyroY(); + prev_values_gyro[2] = accel.readFloatGyroZ(); + + min_values_gyro[0] = accel.readFloatGyroX(); + min_values_gyro[1] = accel.readFloatGyroY(); + min_values_gyro[2] = accel.readFloatGyroZ(); + + max_values_gyro[0] = accel.readFloatGyroX(); + max_values_gyro[1] = accel.readFloatGyroY(); + max_values_gyro[2] = accel.readFloatGyroZ(); + +} + +void loop() +{ + + float current_values [3]= {accel.readFloatAccelX(),accel.readFloatAccelY(),accel.readFloatAccelZ()}; + float current_values_gyro [3]= {accel.readFloatGyroX(),accel.readFloatGyroY(),accel.readFloatGyroZ()}; + + /* + std::string X_string = std::to_string(X); + X_string = std::string(("X: ")).append(X_string); + + std::string Y_string = std::to_string(Y); + Y_string = std::string(("Y: ")).append(Y_string); + + std::string Z_string = std::to_string(Z); + Z_string = std::string(("Z: ")).append(Z_string); + + tft.drawString(X_string.c_str(),10,10); + tft.drawString(Y_string.c_str(),10,20); + tft.drawString(Z_string.c_str(),10,30); + + times_reported++; + avg[0] = (X + avg[0])/times_reported; + avg[1] = (Y + avg[1])/times_reported; + avg[2] = (Z + avg[2])/times_reported; + + peak[0] = std::max(peak[0],X); + peak[1] = std::max(peak[1],Y); + peak[2] = std::max(peak[2],Z); + + //Drawing avg + std::string X_avg = std::to_string(avg[0]); + X_avg = std::string(("Average X: ")).append(X_avg); + + std::string Y_avg = std::to_string(avg[1]); + Y_avg = std::string(("Average Y: ")).append(Y_avg); + + std::string Z_avg = std::to_string(avg[2]); + Z_avg = std::string(("Average Z: ")).append(Z_avg); + + tft.drawString(X_avg.c_str(),10,40); + tft.drawString(Y_avg.c_str(),10,50); + tft.drawString(Z_avg.c_str(),10,60); + + //Drawing peaks + std::string X_peak = std::to_string(peak[0]); + X_peak = std::string(("Peak X: ")).append(X_peak); + + std::string Y_peak = std::to_string(peak[1]); + Y_peak = std::string(("Peak Y: ")).append(Y_peak); + + std::string Z_peak = std::to_string(peak[2]); + Z_peak = std::string(("Peak Z: ")).append(Z_peak); + + tft.drawString(X_peak.c_str(),10,70); + tft.drawString(Y_peak.c_str(),10,80); + tft.drawString(Z_peak.c_str(),10,90); + */ + + if(std::abs(prev_values[0] - current_values[0]) > threshold_accel || + std::abs(prev_values[1] - current_values[1]) > threshold_accel || + std::abs(prev_values[2] - current_values[2]) > threshold_accel){ + println("-------------WE HAVE MOTION --------------"); + for(int i = 0; i < 3; i ++){ + print("Initial X,Y,Z :" ); + print(begin_values[0]); + print(","); + print(begin_values[1]); + print(","); + println(begin_values[2]); + + print("Max X,Y,Z :" ); + print(max_values[0]); + print(","); + print(max_values[1]); + print(","); + println(max_values[2]); + + print("Min X,Y,Z :" ); + print(min_values[0]); + print(","); + print(min_values[1]); + print(","); + println(min_values[2]); + + if(std::abs(prev_values[0] - current_values[0]) > threshold_accel){ + print("DELTA X: "); + println(prev_values[0]-current_values[0]); + delta_values[0]+= prev_values[0]-current_values[0]; + } + if(std::abs(prev_values[1] - current_values[1]) > threshold_accel){ + print("DELTA Y: "); + println(prev_values[1]-current_values[1]); + delta_values[1]+= prev_values[1]-current_values[1]; + } + if(std::abs(prev_values[2] - current_values[2]) > threshold_accel){ + print("DELTA Z: "); + println(prev_values[2]-current_values[2]); + delta_values[2]+= prev_values[2]-current_values[2]; + } + } + } + prev_values[0] = current_values[0]; + prev_values[1] = current_values[1]; + prev_values[2] = current_values[2]; + + max_values[0] = std::max(max_values[0],current_values[0]); + max_values[1] = std::max(max_values[1],current_values[1]); + max_values[2] = std::max(max_values[2],current_values[2]); + + min_values[0] = std::min(min_values[0],current_values[0]); + min_values[1] = std::min(min_values[1],current_values[1]); + min_values[2] = std::min(min_values[2],current_values[2]); + + bool moving = false; + for(int i = 0 ;i < 3; i ++){ + if(std::abs(prev_values_gyro[i] - current_values_gyro[i]) > threshold_gyro){ + moving = true; + } + } + + prev_values_gyro[0] = current_values_gyro[0]; + prev_values_gyro[1] = current_values_gyro[1]; + prev_values_gyro[2] = current_values_gyro[2]; + + if(moving){ + println("WE ARE MOVING"); + for(int i = 0 ;i < 3; i ++){ + print(delta_values[i]); + print(","); + } + println(""); + //Check if closing or opening + if(delta_values[0] > 0){ + status = 1; + } + if(delta_values[0] < 0){ + status = -1; + } + if(delta_values[0] == 0){ + status = 0; + } + was_moving = true; + } + if(was_moving && !moving){ + println("We stopped moving"); + was_moving = false; + delta_values[0] = 0; + delta_values[1] = 0; + delta_values[2] = 0; + } + if(was_moving){ + println(status); + } + delay(100); +} + diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html