From d2b8e1de3ce6fc2a484a320c80f073b349dcc49b Mon Sep 17 00:00:00 2001 From: Jakob Gegeniger Date: Fri, 11 Sep 2026 21:28:54 +0200 Subject: [PATCH] currentMeas --- .vscode/launch.json | 4 ++-- platform | 2 +- readme.md | 8 +++++++- scripts/run.sh | 1 + scripts/test.sh | 1 + src/sword/CMakeLists.txt | 1 + src/sword/mqttChannel.hpp | 22 +++++++++++++++------ src/sword/swordMain.cpp | 39 ++++++++++++++++++++++++------------- src/sword/swordMain.hpp | 41 ++++++++++++++++++++++++++++++++++++--- 9 files changed, 92 insertions(+), 27 deletions(-) mode change 100644 => 100755 scripts/run.sh mode change 100644 => 100755 scripts/test.sh diff --git a/.vscode/launch.json b/.vscode/launch.json index 6f0c8ad..4e17123 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,9 +11,9 @@ "request": "launch", "program": "${workspaceFolder}/build/src/sword/SwordMain", "args": [ - //"test", - "promo", + "promo", //"test", "${workspaceFolder}/assets", + "-n", "SwordDebug", "-b", "'mqtts://nerdyssey.de:8883'", "-t", "'staging/outdoor/king-arthur/sword/'", "-p", "'awesomeexodusoutdooradventures'", diff --git a/platform b/platform index 35a7eb5..6f6a047 160000 --- a/platform +++ b/platform @@ -1 +1 @@ -Subproject commit 35a7eb5b406feb8ff5d0352bc0bb8dc669bd5577 +Subproject commit 6f6a0474cb70430b0ed8b72898a20d380974c31c diff --git a/readme.md b/readme.md index ccf0d24..6b61956 100644 --- a/readme.md +++ b/readme.md @@ -22,4 +22,10 @@ Service to connect the Throne via MQTT ## Throne-Station Throne station -Features Throne for final coronation \ No newline at end of file +Features Throne for final coronation + +## MQTT Testcommands + +```sh +mosquitto_sub -h nerdyssey.de -p 8883 -t "staging/outdoor/king-arthur/#" -u "exodususer" -P "awesomeexodusoutdooradventures" --capath /etc/ssl/certs/ -d +``` \ No newline at end of file diff --git a/scripts/run.sh b/scripts/run.sh old mode 100644 new mode 100755 index a54e7fb..50a63a0 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,6 +1,7 @@ #!/bin/bash /home/gustice/workspace/swordStation/SwordMain \ promo /home/gustice/workspace/swordStation/assets \ + -n Sword \ -b mqtts://nerdyssey.de:8883 \ -t outdoor/king-arthur/sword/ \ -p awesomeexodusoutdooradventures \ diff --git a/scripts/test.sh b/scripts/test.sh old mode 100644 new mode 100755 index db5c9e5..0992b31 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,6 +1,7 @@ #!/bin/bash /home/gustice/workspace/swordStation/SwordMain \ test /home/gustice/workspace/swordStation/assets \ + -n SwordDebug \ -b mqtts://nerdyssey.de:8883 \ -t staging/outdoor/king-arthur/sword/ \ -p awesomeexodusoutdooradventures \ diff --git a/src/sword/CMakeLists.txt b/src/sword/CMakeLists.txt index 4d68e6f..ea98a94 100644 --- a/src/sword/CMakeLists.txt +++ b/src/sword/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(SwordMain swordMain.cpp) target_link_libraries(SwordMain PkgConfig::LIBGPIOD base + adc palAdapt loRa os diff --git a/src/sword/mqttChannel.hpp b/src/sword/mqttChannel.hpp index f1bee99..fb4b15b 100644 --- a/src/sword/mqttChannel.hpp +++ b/src/sword/mqttChannel.hpp @@ -1,10 +1,10 @@ #pragma once -#include "swordMain.hpp" #include "defines.hpp" +#include "swordMain.hpp" class MqttChannel { public: - MqttChannel(string version, SwordConfig cfg, DeviceIoState& ioStat, SeqLogger& log) + MqttChannel(string name, string version, SwordConfig cfg, DeviceIoState& ioStat, SeqLogger& log) : Config(cfg), IoStat(ioStat), Logger(log), @@ -35,9 +35,7 @@ class MqttChannel { {"promoDelay", ServiceCommands_e::SetConfigPromoDelay}, }), subTopics{{runTopic, cfgTopic, resetTopic}}, - client("Sword", - cfg.BrockerUrl, - subTopics, + client(name, cfg.BrockerUrl, subTopics, {cfg.MqttUser, cfg.MqttPassword}) { fmt::println("MQTT: Starting task:"); mqttCall = [this](string topic, string payload) { @@ -86,7 +84,7 @@ class MqttChannel { fmt::println("Input queue flushed, {} Messages discarted", cnt); } - static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function resetCall) { + static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function resetCall, CurrMon& mon) { while (true) { auto serMsg = instance.MasterQueue.dequeue(); if (!serMsg) { @@ -129,9 +127,21 @@ class MqttChannel { switch (code) { case ServiceCommands_e::SetLockOpen: { instance.IoStat.SwordLatch.moveIn(); + auto tE = std::chrono::steady_clock::now() + 6s; + while (std::chrono::steady_clock::now() < tE) { + Thread::sleep(100ms); + auto curr = mon.sampleCurrent(); + } + auto currE = mon.sampleCurrent(); } break; case ServiceCommands_e::SetLockClose: { instance.IoStat.SwordLatch.moveOut(); + auto tE = std::chrono::steady_clock::now() + 6s; + while (std::chrono::steady_clock::now() < tE) { + Thread::sleep(100ms); + auto curr = mon.sampleCurrent(); + } + auto currE = mon.sampleCurrent(); } break; case ServiceCommands_e::PlaySoundPromo: { MediaPlayer::playWavFile(instance.Config.SoundPromo); diff --git a/src/sword/swordMain.cpp b/src/sword/swordMain.cpp index ba05737..db3813e 100644 --- a/src/sword/swordMain.cpp +++ b/src/sword/swordMain.cpp @@ -8,9 +8,7 @@ using namespace std::literals::string_view_literals; using namespace std::literals::chrono_literals; using namespace std; -constexpr uint8_t VersionArr[4] = {'V', 0, 8, 0}; -const string Version = "V 0.8.0"; -// const string Version = fmt::format("{} {}.{}.{}", VersionArr[0], VersionArr[1], VersionArr[2], VersionArr[3]); +const string Version = "V 0.9.0"; unique_ptr SwordCfg; unique_ptr IoStat; @@ -21,6 +19,7 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log); int main(int argc, char* argv[]) { CLI::App app{"Sword Firmware"}; + string appName = "SwordDebug"; string modeArg = "test"; string assetsBasePath = "/home/user/assets/sword"; string brocker = "mqtts://nerdyssey.de:8883"; @@ -31,6 +30,7 @@ int main(int argc, char* argv[]) { string logApiKey = "uYpJC6WyZAbFK2MUgwNH"; app.add_option("startmode", modeArg, "Startup mode")->required(); app.add_option("basepath", assetsBasePath, "Base bath for assets")->required(); + app.add_option("-n,--name", appName, "Name of App"); app.add_option("-b,--brocker", brocker, "MQTT Topic URL"); app.add_option("-t,--topic", topic, "MQTT Topic"); app.add_option("-p,--passwd", password, "MQTT Brocker password"); @@ -40,7 +40,7 @@ int main(int argc, char* argv[]) { CLI11_PARSE(app, argc, argv); - SeqLogger log(logUrl, logApiKey, "Sword"); + SeqLogger log(logUrl, logApiKey, appName); SwordCfg = make_unique(assetsBasePath, brocker, topic, user, password); fmt::println("{}", app.get_description()); @@ -78,15 +78,23 @@ int main(int argc, char* argv[]) { InputTransAware SwordPulled(spPort); LinMot SwordLatch(mOut1, mOut2); + I2cMaster i2cDev(II2cMaster::ClockRate::_400k); + AdcAds1115 adc(i2cDev, AdcAds1115::I2CAddress::Gnd); + // Check if the ADC is connected + if (!adc.isConnected()) { + fmt::println("Failed to connect ot Sensor {}", modeArg); + } + CurrMon mon(adc); + IoStat = make_unique(SwordEntry, SwordEnd, SwordPulled, SwordLatch); - MqttChannel mqtt(Version, *SwordCfg, *IoStat, log); + MqttChannel mqtt(appName, Version, *SwordCfg, *IoStat, log); auto resetCall = [&]() { log.sendEvent("Reset", ""); }; mqtt.flushInput(); - static auto conTask = Thread::createExplicit("MqttTask", MqttChannel::runMasterService, ref(mqtt), resetCall); + static auto conTask = Thread::createExplicit("MqttTask", MqttChannel::runMasterService, ref(mqtt), resetCall, ref(mon)); SwordLatch.moveOut(); fmt::println("Running App"); @@ -128,23 +136,26 @@ void processPromo(MqttChannel& mqtt, SeqLogger& log) { Tick tmu(1min); Tick tn(1s); Tick tsf(30min); + Waiter dP; auto& serCmds = mqtt.getSerCommands(); while (true) { Thread::sleep(10ms); IoStat->tick(); if (IoStat->SwordPulled.isPosEdge()) { if (!IoStat->Mute) { - log.sendEvent("Pull", "Start Audio"); + if (dP.tick(IoStat->PromoDelay)) { + log.sendEvent("Pull", "Start Audio"); - if (tsf.tick()) { - MediaPlayer::playWavFile(SwordCfg->SoundPromo); + if (tsf.tick()) { + MediaPlayer::playWavFile(SwordCfg->SoundPromo); + } else { + MediaPlayer::playWavFile(SwordCfg->SoundPromoShort); + } + dP.sample(); + fmt::println("Muting next for {}", IoStat->PromoDelay); } else { - MediaPlayer::playWavFile(SwordCfg->SoundPromoShort); + fmt::println("still muted"); } - - fmt::println("Waiting for {}", IoStat->PromoDelay); - Thread::sleep(IoStat->PromoDelay); - fmt::println("Waiting done"); } } if (tn.tick()) { diff --git a/src/sword/swordMain.hpp b/src/sword/swordMain.hpp index 988339d..6a9cecf 100644 --- a/src/sword/swordMain.hpp +++ b/src/sword/swordMain.hpp @@ -20,9 +20,11 @@ #include #include +#include "Ads1115.hpp" #include "MqttClient.hpp" #include "coreExcept.hpp" #include "gpioPort.hpp" +#include "i2cPort.hpp" #include "logging.hpp" #include "queue.hpp" #include "textUtils.hpp" @@ -50,6 +52,39 @@ class LinMot { } }; +class CurrMon { + // https://www.monolithicpower.com/en/documentview/productdocument/index/version/2/document_type/Datasheet/lang/EN/sku/MCS1802/document_id/5066/ + public: + CurrMon(AdcAds1115& adc) + : _adc(adc) { + adc.setPga(AdcAds1115::Pga::FSR_4_096V); + adc.startContinuousConversion(); + + float val{}; + for (size_t i = 0; i < 8; i++) { + val += sampe(); + } + _refVal = val / 8; + } + + float sampleCurrent() { + auto val = sampe() - _refVal; + val = val / 0.264; + fmt::println("{:.2} A", val); + return val; + } + + private: + float _refVal{}; + AdcAds1115& _adc; + + float sampe() { + float volt; + _adc.readConversionVoltage(volt); + return volt; + } +}; + class DeviceIoState { public: DeviceIoState(pal::InputTransAware& entry, @@ -75,7 +110,7 @@ class DeviceIoState { std::chrono::seconds PromoDelay{150}; int Volume{50}; - bool Mute{false}; + bool Mute{true}; }; struct SwordConfig { @@ -113,7 +148,7 @@ class MediaPlayer { return; } thread audioThread([filePath]() { - auto cmd = fmt::format("aplay '{}' 2>/dev/null", filePath); + auto cmd = fmt::format("aplay '{}'", filePath); auto result = system(cmd.c_str()); if (result != 0) { fmt::println(stderr, "ERROR: Failed to play audio file: '{}'", filePath); @@ -134,7 +169,7 @@ class MediaPlayer { fmt::println(stderr, "ERROR: No ALSA volume control found"); return; } - auto cmd = fmt::format("amixer -q sset '{}' {}% 2>/dev/null", control, volume); + auto cmd = fmt::format("amixer -q sset '{}' {}%", control, volume); auto result = system(cmd.c_str()); if (result != 0) { fmt::println(stderr, "ERROR: Failed to set volume to {}%", volume);