From be93772bd4ab8b9af5fa155565d732e5e7cff51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Mon, 30 May 2022 22:05:30 +0200 Subject: =?UTF-8?q?nedelujo=C4=8D=20MPU=20lib,=20izbiram=20drugega?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fiz/naloga/gl/fragment.glsl | 8 +- fiz/naloga/gl/helmholtz.c | 5 +- fiz/naloga/merjenje/.gitignore | 1 + fiz/naloga/merjenje/README | 4 + fiz/naloga/merjenje/data/index.html | 19 ++++ fiz/naloga/merjenje/data/settings.json | 26 +++++ fiz/naloga/merjenje/include/README | 39 ++++++++ fiz/naloga/merjenje/lib/README | 46 +++++++++ fiz/naloga/merjenje/platformio.ini | 29 ++++++ fiz/naloga/merjenje/src/main.cpp | 171 +++++++++++++++++++++++++++++++++ fiz/naloga/merjenje/test/README | 11 +++ "fiz/naloga/ro\304\215nemeritve.tsv" | 70 ++++++++++++++ 12 files changed, 423 insertions(+), 6 deletions(-) create mode 100644 fiz/naloga/merjenje/.gitignore create mode 100644 fiz/naloga/merjenje/README create mode 100644 fiz/naloga/merjenje/data/index.html create mode 100644 fiz/naloga/merjenje/data/settings.json create mode 100644 fiz/naloga/merjenje/include/README create mode 100644 fiz/naloga/merjenje/lib/README create mode 100644 fiz/naloga/merjenje/platformio.ini create mode 100644 fiz/naloga/merjenje/src/main.cpp create mode 100644 fiz/naloga/merjenje/test/README create mode 100644 "fiz/naloga/ro\304\215nemeritve.tsv" (limited to 'fiz') diff --git a/fiz/naloga/gl/fragment.glsl b/fiz/naloga/gl/fragment.glsl index 095cb78..eed4bb7 100644 --- a/fiz/naloga/gl/fragment.glsl +++ b/fiz/naloga/gl/fragment.glsl @@ -5,7 +5,6 @@ #define SEG 32 -#define I .5 #define n 100. #define PI 3.1415 @@ -33,14 +32,15 @@ vec3 tuljava (vec3 pz, float R) { void main () { + float I = -.5; float R = 0.8; - float R2 = abs(sin((time+8.2)/.6))+0.5; + float R2 = (abs(sin((time+8.2)/.6))+0.5)*0+R; vec2 uv = UVo; vec2 poz = vec2(uv*2.); // zavrteti moramo tako točke same, kot tudi vec2 offset = vec2(R/2.,0); // njihove komponente float kot = time/.4; float multi = n*I/**abs(sin(time/.2)+0.2)*/; - vec2 dodatek = vec2(sin(time/.6)*.5, cos(time/.4)*.6); + vec2 dodatek = vec2(sin(time/.6)*.5, cos(time/.4)*.6)*0; vec3 rez = tuljava( vec3(poz+offset, 1/2*R), R )*n*I + @@ -65,7 +65,7 @@ void main () { 1); rez *= 1000.; #ifdef show_vec - fragColor = vec4(rez, 1); + fragColor = vec4(rez+0.5, 1); #else fragColor = vec4(vec3(length(rez)), 1); #endif diff --git a/fiz/naloga/gl/helmholtz.c b/fiz/naloga/gl/helmholtz.c index 5f8f44d..5858039 100644 --- a/fiz/naloga/gl/helmholtz.c +++ b/fiz/naloga/gl/helmholtz.c @@ -29,7 +29,7 @@ int main (void) { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // forward compat is ogl 3.0+ - GLFWwindow * okno = glfwCreateWindow(800, 600, "okno", NULL, NULL); + GLFWwindow * okno = glfwCreateWindow(80, 60, "okno", NULL, NULL); if (!okno) { fprintf(stderr, "glfwCreateWindow failed\n"); glfwTerminate(); @@ -123,6 +123,7 @@ int main (void) { glfwSwapBuffers(okno); glfwPollEvents(); // wait / poll frame++; + /* char fn[25]; sprintf(fn, "animacija%04d.ppm", frame); int fd; @@ -148,7 +149,7 @@ int main (void) { if (close(fd) == -1) { perror("close"); abort(); - } + } */ } glfwDestroyWindow(okno); glfwTerminate(); diff --git a/fiz/naloga/merjenje/.gitignore b/fiz/naloga/merjenje/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/fiz/naloga/merjenje/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/fiz/naloga/merjenje/README b/fiz/naloga/merjenje/README new file mode 100644 index 0000000..41145b3 --- /dev/null +++ b/fiz/naloga/merjenje/README @@ -0,0 +1,4 @@ +pio run +pio run --target uploadfs +pio run --target upload +pio run --target clean diff --git a/fiz/naloga/merjenje/data/index.html b/fiz/naloga/merjenje/data/index.html new file mode 100644 index 0000000..83d10b8 --- /dev/null +++ b/fiz/naloga/merjenje/data/index.html @@ -0,0 +1,19 @@ + + + + + + spletni vmesnik merilnika magnetnega polja + + + + +

+ spletni vmesnik računalnika projektne naloge pri fiziki v tretjem letniku +

+
+

+ Anton Luka Šijanec 2022 --- izvorna koda +

+ + diff --git a/fiz/naloga/merjenje/data/settings.json b/fiz/naloga/merjenje/data/settings.json new file mode 100644 index 0000000..eb045ba --- /dev/null +++ b/fiz/naloga/merjenje/data/settings.json @@ -0,0 +1,26 @@ +{ + "ap_ssid": "b", + "ap_pass": "", + "ap_ch": 1, + "ap_hidden": 0, + "ap_ip": "10.82.66.1", + "ap_gw": "10.82.66.2", + "ap_nm": "255.255.255.0", + "ap_dns1": "", + "ap_dns2": "", + "ap_host": "b", + "sta_ssid": "OpenWrt", + "sta_pass": "", + "sta_ip": "", + "sta_dns1": "", + "sta_dns2": "", + "sta_gw": "", + "sta_host": "b", + "sta_static": 0, + "ftp_user": "b", + "ftp_pass": "", + "http_user": "", + "http_pass": "", + "host": "b", + "mpu_address": 104 // 104 oz. 0x68 ako je AD0 GND, 105 oz. 0x69 ako je AD0 + +} diff --git a/fiz/naloga/merjenje/include/README b/fiz/naloga/merjenje/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/fiz/naloga/merjenje/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/fiz/naloga/merjenje/lib/README b/fiz/naloga/merjenje/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/fiz/naloga/merjenje/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/fiz/naloga/merjenje/platformio.ini b/fiz/naloga/merjenje/platformio.ini new file mode 100644 index 0000000..16eaddd --- /dev/null +++ b/fiz/naloga/merjenje/platformio.ini @@ -0,0 +1,29 @@ +; 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:myboard] +platform = espressif8266 +board = nodemcuv2 +monitor_speed = 9600 +upload_speed = 460800 +build_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_PORT=Serial -DIP_FORWARD=1 -DMONITOR_SPEED=9600 +framework = arduino +board_build.filesystem = littlefs +board_build.ldscript = eagle.flash.4m2m.ld +src_filter = -<*> + +# http://git/anonymous/FTPClientServer <= to je mirror sicer nedelujočega +# http://github.com/dplasa/FTPClientServer <= ta ne dela, trenutni je fork njega +lib_deps = + Time + https://github.com/AddioElectronicsForks/FTPClientServer + DNSServer + ESP Async WebServer + ArduinoJson + MPU9250 diff --git a/fiz/naloga/merjenje/src/main.cpp b/fiz/naloga/merjenje/src/main.cpp new file mode 100644 index 0000000..a5209da --- /dev/null +++ b/fiz/naloga/merjenje/src/main.cpp @@ -0,0 +1,171 @@ +#define ARDUINOJSON_ENABLE_COMMENTS 1 +#include +#include +#include +#include "LittleFS.h" +#include +#include +#include +#include +#include +#include +#include +#undef __POLLEDTIMING_H__ // http://github.com/dplasa/FTPClientServer/pull/19 +#include +#define TEXT_ENC "text/plain" +#include "MPU9250.h" +const int str2pin_map[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, 0, 0, 0, 0, 0, 0, 0, 0, 0, A0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +int str2pin (const char * s) { + if (s[0] >= '0' && s[0] <= '9') + return atoi(s); + return str2pin_map[atoi(s+1) + ((s[0] == 'A') ? 20 : 0)]; +} +DynamicJsonDocument settings(1024); +void load_settings () { + if (LittleFS.exists("settings.json")) { + File s = LittleFS.open("settings.json", "r"); + if (s) + deserializeJson(settings, s); + s.close(); + } +} +void store_settings () { + File s = LittleFS.open("settings.json", "w"); + serializeJson(settings, s); + s.close(); +} +void notFound (AsyncWebServerRequest * request) { + request->send(404, "text/plain", "404"); +} +FTPServer ftpServer(LittleFS); +DNSServer dnsServer; +AsyncWebServer httpServer(80); +MPU9250 mpu; +int do_reload = 0; +int measure = 0; +void reload (bool w) { + Serial.println("reload"); + if (!w) { + Serial.println("stopping services: ftp"); + ftpServer.stop(); + Serial.println("stopping services: http"); + httpServer.reset(); + httpServer.end(); + Serial.println("stopping services: dns"); + dnsServer.stop(); + } + Serial.println("loading settings"); + load_settings(); + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, HIGH); // izklopi luč + Serial.println("access point"); + const char * ap_pass = settings["ap_pass"].as().c_str(); + if (ap_pass && strlen(ap_pass) < 8) + ap_pass = NULL; + WiFi.mode(WIFI_AP); + WiFi.softAP(settings["ap_ssid"].as().c_str(), ap_pass, settings["ap_ch"].as(), settings["ap_hidden"].as()); + Serial.println("WiFi.softAPIP() = " + WiFi.softAPIP().toString()); + Serial.println("station networking"); + const char * sta_ssid = settings["sta_ssid"].as().c_str(); + if (sta_ssid && strlen(sta_ssid)) { + Serial.println("setting up wifi station mode"); + IPAddress sta_ip(10, 69, 69, 82); + sta_ip.fromString(settings["sta_ip"].as()); + IPAddress sta_gw(10, 69, 69, 1); + sta_gw.fromString(settings["sta_gw"].as()); + IPAddress sta_nm(10, 69, 69, 255); + sta_nm.fromString(settings["sta_nm"].as()); + IPAddress sta_dns1(93, 103, 235, 126); + sta_dns1.fromString(settings["sta_dns1"].as()); + IPAddress sta_dns2(89, 212, 146, 168); + sta_dns2.fromString(settings["sta_dns2"].as()); + const char * sta_mac = settings["sta_mac"].as().c_str(); + if (sta_mac && strlen(sta_mac)) { + long long unsigned int mac; + uint8_t macar[6]; + mac = strtoull(sta_mac, NULL, 16); + memcpy(macar, (char *) &mac, 6); + wifi_set_macaddr(STATION_IF, macar); + } + WiFi.disconnect(true); + WiFi.hostname(settings["sta_host"].as()); + const char * sta_pass = settings["sta_pass"].as().c_str(); + if (sta_pass && strlen(sta_pass) < 8) + sta_pass = NULL; + WiFi.begin(sta_ssid, sta_pass); + if (settings["sta_static"].as()) + if (!WiFi.config(sta_ip, sta_gw, sta_nm, sta_dns1, sta_dns2)) + Serial.println("failed to wifi config"); + Serial.println(String("wifi local ip: "+WiFi.localIP().toString())); + } else + WiFi.disconnect(); + dnsServer.start(53, settings["host"], WiFi.softAPIP()); + ftpServer.begin(settings["ftp_user"], settings["ftp_pass"]); +#define ADD_AUTH(s) s.setAuthentication(settings["http_user"], settings["http_pass"]) + auto f = httpServer.serveStatic("/", LittleFS, "/").setDefaultFile("index.html"); + ADD_AUTH(f); + auto s = httpServer.on("/s", [](AsyncWebServerRequest * r) { + r->send(201, TEXT_ENC, "OK\n"); + ESP.restart(); + }); + ADD_AUTH(s); + auto l = httpServer.on("/l", [](AsyncWebServerRequest * r) { + r->send(201, TEXT_ENC, "OK\n"); + do_reload = 1; + }); + ADD_AUTH(l); + auto m = httpServer.on("/m", [](AsyncWebServerRequest * r) { + if (measure) { + r->send(400, TEXT_ENC, "meritev že poteka\n"); + return; + } + measure = 1; + r->send(201, TEXT_ENC, "OK\n"); + }); + ADD_AUTH(m); + auto k = httpServer.on("/k", [](AsyncWebServerRequest * r) { + if (!measure) { + r->send(400, TEXT_ENC, "trenutno ni meritve\n"); + return; + } + measure = 0; + r->send(201, TEXT_ENC, "OK\n"); + }); + ADD_AUTH(k); + auto c = httpServer.on("/c", [](AsyncWebServerRequest * r) { + r->send(201, TEXT_ENC, "glej sporočila na UART za navodila\n"); + mpu.calibrateMag(); + }); + ADD_AUTH(c); + httpServer.begin(); + Wire.begin(); + int naslov = settings["mpu_address"].as(); + if (!mpu.setup(naslov)) { + Serial.println("mpu ni dosegljiv na naslovu " + naslov); + } + mpu.verbose(true); + Serial.println("reload: done"); +} +void handleMeasure () { + if (!mpu.update()) { + Serial.println("!mpu.update()"); + } + Serial.printf("getMag: X: %f\tY: %f\tZ: %f\n", mpu.getMagX(), mpu.getMagY(), mpu.getMagZ()); + return; +} +void setup () { + Serial.begin(MONITOR_SPEED); + Serial.println("živ sem"); + LittleFS.begin(); + reload(1); +} +void loop () { + if (measure) + handleMeasure(); + ftpServer.handleFTP(); + dnsServer.processNextRequest(); + if (do_reload) { + reload(0); + do_reload = 0; + } +} diff --git a/fiz/naloga/merjenje/test/README b/fiz/naloga/merjenje/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/fiz/naloga/merjenje/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 diff --git "a/fiz/naloga/ro\304\215nemeritve.tsv" "b/fiz/naloga/ro\304\215nemeritve.tsv" new file mode 100644 index 0000000..324e130 --- /dev/null +++ "b/fiz/naloga/ro\304\215nemeritve.tsv" @@ -0,0 +1,70 @@ +ročne meritve, narejene v šoli na šolski helmholtzovi tuljavi pri laborantu +tok 0.9 A +navojev 320 +levi stolpec odmik od središča v metrih +desni stolpec jakost magnetnega pretoka v teslah +različna merjenja so ločena z iksom, uporabno za split(1) +x +0.00 0.0041 +0.01 0.0040 +0.02 0.0038 +0.03 0.0035 +0.04 0.0030 +0.05 0.0025 +0.06 0.0021 +0.07 0.0016 +0.08 0.0013 +0.09 0.0009 +0.10 0.0006 +0.11 0.0005 +0.12 0.0003 +0.13 0.0002 +0.14 0.0001 +0.15 0.0000 +x +0.00 0.0042 +0.01 0.0042 +0.02 0.0042 +0.03 0.0041 +0.04 0.0039 +0.05 0.0036 +0.06 0.0032 +0.07 0.0026 +0.08 0.0020 +0.09 0.0016 +0.10 0.0012 +0.11 0.0009 +0.12 0.0007 +0.13 0.0005 +0.14 0.0003 +x +0.14 0.0003 +0.13 0.0004 +0.12 0.0005 +0.11 0.0007 +0.10 0.0009 +0.09 0.0012 +0.08 0.0015 +0.07 0.0019 +0.06 0.0024 +0.05 0.0029 +0.04 0.0034 +0.03 0.0038 +0.02 0.0041 +0.01 0.0042 +0.00 0.0043 +0.01 0.0043 +0.02 0.0043 +0.03 0.0043 +0.04 0.0042 +0.05 0.0039 +0.06 0.0044 +0.07 0.0029 +0.08 0.0025 +0.09 0.0020 +0.10 0.0015 +0.11 0.0012 +0.12 0.0009 +0.13 0.0007 +0.14 0.0005 +0.15 0.0004 -- cgit v1.2.3