From 1d7cc722ae87919a7b67b31a98f7f15a264023ba Mon Sep 17 00:00:00 2001 From: zecktos <56479813+zecktos@users.noreply.github.com> Date: Tue, 8 Jun 2021 12:20:41 +0200 Subject: [PATCH] init repo --- README.md | 1 + include/README | 39 ++++++++++ lib/README | 46 ++++++++++++ platformio.ini | 24 ++++++ src/main.cpp | 194 +++++++++++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 6 files changed, 315 insertions(+) create mode 100644 README.md 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/README.md b/README.md new file mode 100644 index 0000000..322a95d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# esp32 teddy 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..460b4fc --- /dev/null +++ b/platformio.ini @@ -0,0 +1,24 @@ +; 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 +monitor_speed = 115200 +board_build.f_cpu = 240000000L +board_build.f_flash = 80000000L +board_build.flash_mode = qio +board_build.partitions = huge_app.csv +lib_deps = + cnmat/OSC + hideakitai/MPU9250 + paulstoffregen/OneWire + milesburton/DallasTemperature \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b7d3037 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,194 @@ +#include "Arduino.h" +#include "MPU9250.h" +#include "WiFi.h" +#include "WiFiUdp.h" +#include "OSCBundle.h" + +// SCL Pin is 22 +// SDA PIN is 21 + +#define BL_ON 0 + +#if BL_ON +#include "BLEDevice.h" +#include "BLEScan.h" + +// bluetooth name +#define blName "Teddy" +#endif + +// WiFi stuff +#define ssid "" +#define pwd "" + +// OSC +#define host "192.168.21.232" +#define port 3030 + +#define oneWireBus 18 + +WiFiUDP Udp; +MPU9250 mpu; + + + + +float ypr[3]; +float acc[3]; +int pr; +const float threshold = 0.7; +const int thresholdP = 50; + +void set_calibration() { + mpu.setAccBias(232.00, 89.60, -581.30); + mpu.setGyroBias(14.80, 95.80, 23.00); + mpu.setMagBias(284.65, -333.85, 91.40); + mpu.setMagScale(0.59, 1.39, 1.68); +} + +void get_calibration() { + + Serial.println(); + Serial.println("Accel Gyro calibration will start in 5sec."); + Serial.println("Please leave the device still on the flat plane."); + mpu.verbose(true); + delay(5000); + mpu.calibrateAccelGyro(); + Serial.println(); + + Serial.println("Mag calibration will start in 5sec."); + Serial.println("Please Wave device in a figure eight until done."); + delay(5000); + mpu.calibrateMag(); + Serial.println(); + + Serial.println("< calibration parameters >"); + Serial.println("accel bias [g]: "); + Serial.print(mpu.getAccBiasX()); + Serial.print(", "); + Serial.print(mpu.getAccBiasY()); + Serial.print(", "); + Serial.print(mpu.getAccBiasZ()); + Serial.println(); + Serial.println("gyro bias [deg/s]: "); + Serial.print(mpu.getGyroBiasX()); + Serial.print(", "); + Serial.print(mpu.getGyroBiasY()); + Serial.print(", "); + Serial.print(mpu.getGyroBiasZ()); + Serial.println(); + Serial.println("mag bias [mG]: "); + Serial.print(mpu.getMagBiasX()); + Serial.print(", "); + Serial.print(mpu.getMagBiasY()); + Serial.print(", "); + Serial.print(mpu.getMagBiasZ()); + Serial.println(); + Serial.println("mag scale []: "); + Serial.print(mpu.getMagScaleX()); + Serial.print(", "); + Serial.print(mpu.getMagScaleY()); + Serial.print(", "); + Serial.print(mpu.getMagScaleZ()); + Serial.println(); + Serial.println(); + mpu.verbose(false); + +} + + + +void setup() { + Serial.begin(115200); + delay(500); + Wire.begin(); + delay(500); + + if (!mpu.setup(0x68)) { // change to your own address + while (1) { + Serial.println("MPU failed"); + delay(5000); + } + } + + set_calibration(); + //get_calibration(); + ypr[0] = mpu.getYaw(); + ypr[1] = mpu.getPitch(); + ypr[2] = mpu.getRoll(); + + acc[0] = mpu.getAccX(); + acc[1] = mpu.getAccY(); + acc[2] = mpu.getAccZ(); + + pr = 0; + + // connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, pwd); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(500); + } + Serial.print("WiFi connected, IP = "); + Serial.println(WiFi.localIP()); + + #if BL_ON + BLEDevice::init(blName); + #endif + + Udp.begin(9000); +} + + +void loop() { + float yprNew[3]; + float accNew[3]; + bool send = false; + + if (mpu.update()) { + yprNew[0] = mpu.getYaw(); + yprNew[1] = mpu.getPitch(); + yprNew[2] = mpu.getRoll(); + accNew[0] = mpu.getAccX(); + accNew[1] = mpu.getAccY(); + accNew[2] = mpu.getAccZ(); + } + for(int i = 0; i < 3; i++){ + if (abs(ypr[i] - yprNew[i]) > threshold){ send = true;} + if (abs(acc[i] - accNew[i]) > threshold){ send = true;} + } + if(send == true){ + OSCBundle bundle; + bundle.add("/teddy/acc").add(acc[0]).add(acc[1]).add(acc[2]); + bundle.add("/teddy/abs/acc").add(abs(acc[0])).add(abs(acc[1])).add(abs(acc[2])); + //bundle.add("/teddy/ypr").add(ypr[0]).add(ypr[1]).add(ypr[2]); + bundle.add("/teddy/abs/ypr").add(abs(ypr[0])).add(abs(ypr[1])).add(abs(ypr[2])); + + Udp.beginPacket(host, port); + bundle.send(Udp); + Udp.endPacket(); + bundle.empty(); + } + for(int i = 0; i < 3; i++){ + ypr[i] = yprNew[i]; + acc[i] = accNew[i]; + } + + int prNew = analogRead(39); + if (abs(pr-prNew) > thresholdP){ + OSCMessage msg("/teddy/druck"); + msg.add(prNew); + Udp.beginPacket(host, port); + msg.send(Udp); + Udp.endPacket(); + pr = prNew; + } + + 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