From 51ad7655812d34704665b8bfbce296f4a26ec20d Mon Sep 17 00:00:00 2001 From: Jens Noack Date: Fri, 18 Feb 2022 16:15:08 +0100 Subject: [PATCH] moved to esp32 and added display and counter --- vscode/Unendlichkeitsmaschine/.gitignore | 5 + .../.vscode/extensions.json | 10 ++ vscode/Unendlichkeitsmaschine/include/README | 39 ++++++ vscode/Unendlichkeitsmaschine/lib/README | 46 +++++++ vscode/Unendlichkeitsmaschine/platformio.ini | 17 +++ vscode/Unendlichkeitsmaschine/src/main.cpp | 115 ++++++++++++++++++ vscode/Unendlichkeitsmaschine/test/README | 11 ++ 7 files changed, 243 insertions(+) create mode 100644 vscode/Unendlichkeitsmaschine/.gitignore create mode 100644 vscode/Unendlichkeitsmaschine/.vscode/extensions.json create mode 100644 vscode/Unendlichkeitsmaschine/include/README create mode 100644 vscode/Unendlichkeitsmaschine/lib/README create mode 100644 vscode/Unendlichkeitsmaschine/platformio.ini create mode 100644 vscode/Unendlichkeitsmaschine/src/main.cpp create mode 100644 vscode/Unendlichkeitsmaschine/test/README diff --git a/vscode/Unendlichkeitsmaschine/.gitignore b/vscode/Unendlichkeitsmaschine/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/.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/Unendlichkeitsmaschine/.vscode/extensions.json b/vscode/Unendlichkeitsmaschine/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/.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/Unendlichkeitsmaschine/include/README b/vscode/Unendlichkeitsmaschine/include/README new file mode 100644 index 0000000..45496b1 --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/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/vscode/Unendlichkeitsmaschine/lib/README b/vscode/Unendlichkeitsmaschine/lib/README new file mode 100644 index 0000000..8c9c29c --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/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/vscode/Unendlichkeitsmaschine/platformio.ini b/vscode/Unendlichkeitsmaschine/platformio.ini new file mode 100644 index 0000000..23bbf19 --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/platformio.ini @@ -0,0 +1,17 @@ +; 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:esp32] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = + thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1 + madhephaestus/ESP32Servo@^0.11.0 diff --git a/vscode/Unendlichkeitsmaschine/src/main.cpp b/vscode/Unendlichkeitsmaschine/src/main.cpp new file mode 100644 index 0000000..7fe1bdd --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/src/main.cpp @@ -0,0 +1,115 @@ +#include +#include +#include // Only needed for Arduino 1.6.5 and earlier +#include "SSD1306Wire.h" // legacy: #include "SSD1306.h" + +SSD1306Wire display(0x3c, SDA, SCL); +Servo ESC; // Der ESC-Controller (Electronic Speed Controller bzw. elektronischer Fahrtregler) wird als Objekt mit dem Namen "ESC" festgelegt. +int Drehregler; // Ausgabewert des Drehreglers +int speed, last_speed; // Das Wort "Geschwindigkeit" steht als Variable für den Ansteuerungswert am ESC. +unsigned long turn = 0; +unsigned long speedturns = 0; +unsigned long speedchanged_ms = 0; +unsigned long speed_u_per_min = 0; + +const uint8_t LED_BUILTIN = 2; +const uint8_t POTI_PIN = 4; +const uint8_t ESC_PIN = 16; +const uint8_t LS1_PIN = 14; + +const uint8_t MAX_ESC_SPEED = 60; +const uint8_t MIN_ESC_SPEED = 35; +const uint16_t MAX_POTI_VALUE = 4095; + +void show_values(); +void ISR(); + +void setup() +{ + Serial.begin(9600); + // Initialising the UI will init the display too. + display.init(); + display.clear(); + display.flipScreenVertically(); + //display.setFont(ArialMT_Plain_10); + display.setTextAlignment(TEXT_ALIGN_CENTER); + display.setFont(ArialMT_Plain_24); + display.setFont(ArialMT_Plain_16); + display.drawString(64, 2, "Init controller"); + display.setFont(ArialMT_Plain_10); + display.drawString(64, 40, "You may hear some beeps."); + display.drawString(64, 52, "That's OK! ;-)"); + display.display(); + display.setTextAlignment(TEXT_ALIGN_LEFT); + display.setFont(ArialMT_Plain_16); + ESC.attach(ESC_PIN,1000,2000); + + for(uint8_t i=0;i<=100;i=i+10) + { + display.drawProgressBar(6, 28, 116, 6, i); + delay(500); + ESC.write(0); + display.display(); + } + + attachInterrupt(LS1_PIN, ISR, RISING); + + last_speed = 0; + speed = 0; + + +} + +void loop() +{ + + Drehregler = analogRead(POTI_PIN); // Dieser Befehl liest den Wert des Potentiometers am analogen Pin A0 aus und speichert ihn unter der Variablen "Drehregler". Der Wert liegt zwischen 0 und 1023. + speed = map(Drehregler, 0, MAX_POTI_VALUE, MIN_ESC_SPEED, MAX_ESC_SPEED); // Der "MAP-" Befehl wandelt den Messwert aus der Variablen "Drehregler" um, damit er am ESC verarbeitet werden kann. Der Zahlenbereich 0 bis 1023 wird dabei in einen Zahlenwert zwischen 0 und 180 umgewandelt. + if(speed != last_speed) + { + last_speed = speed; + speedchanged_ms = millis(); + speedturns = 0; + } + + if(millis() - speedchanged_ms > 10000) + { + speed_u_per_min = (speedturns*6); + speedturns = 0; + speedchanged_ms = millis(); + } + + //Serial.print("Drehregler:"); Serial.print(Drehregler); Serial.print(", Speed:");Serial.println(speed); + if(MIN_ESC_SPEED < speed) + ESC.write(speed); // Der endgültige Wert für den ESC wird an den ESC gesendet. Der ESC nimmt das Signal an dieser Stelle auf und steuert den Motor entsprechend der gesendeten Werte an. + else + ESC.write(0); + + show_values(); +} + +void show_values() +{ + display.clear(); + display.setTextAlignment(TEXT_ALIGN_LEFT); + display.setFont(ArialMT_Plain_10); + display.drawString(1, 1, "Speed:"); + display.drawProgressBar(40, 4, 80, 6, map(speed, MIN_ESC_SPEED,MAX_ESC_SPEED,0,100)); + display.setFont(ArialMT_Plain_10); + display.drawString(1, 15, "Turns:"); + display.drawString(35, 15, String(turn)); + display.setFont(ArialMT_Plain_24); + display.setTextAlignment(TEXT_ALIGN_RIGHT); + display.drawString(64, 37, String(speed_u_per_min)); + display.setTextAlignment(TEXT_ALIGN_LEFT); + display.drawString(68,37, "U/min"); + display.display(); +} + +void IRAM_ATTR ISR() +{ + turn++; + speedturns++; +} + + diff --git a/vscode/Unendlichkeitsmaschine/test/README b/vscode/Unendlichkeitsmaschine/test/README new file mode 100644 index 0000000..e7d1588 --- /dev/null +++ b/vscode/Unendlichkeitsmaschine/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