diff --git a/src/sword/mqttChannel.hpp b/src/sword/mqttChannel.hpp index 26e175c..98dc191 100644 --- a/src/sword/mqttChannel.hpp +++ b/src/sword/mqttChannel.hpp @@ -97,7 +97,7 @@ class MqttChannel { fmt::println("Input queue flushed, {} Messages discarted", cnt); } - static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function resetCall, CurrMon& mon) { + static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function resetCall) { while (true) { auto serMsg = instance.MasterQueue.tryDequeueFor(500ms); // only resetTopic and cfgTopic arrive here @@ -152,24 +152,12 @@ class MqttChannel { break; } 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: { if (instance.IoStat.GameRunning) { break; } 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::playWavFileAsync(instance.Config.SoundPromo); diff --git a/src/sword/swordMain.cpp b/src/sword/swordMain.cpp index b1c0c8d..9ff565e 100644 --- a/src/sword/swordMain.cpp +++ b/src/sword/swordMain.cpp @@ -15,6 +15,15 @@ unique_ptr IoStat; void processProduction(MqttChannel& mqtt, SeqLogger& log); +void saveFile(string type, vector values) { + auto file = fmt::output_file(type + ".txt"); + for (float value : values) { + file.print("{}\n", value); + } +} +void sendOverCurrError(string type) { +} + int main(int argc, char* argv[]) { CLI::App app{"Sword Firmware"}; string appName = "SwordDebug"; @@ -68,7 +77,6 @@ int main(int argc, char* argv[]) { InputTransAware SwordEntry(sbPort); InputTransAware SwordEnd(sePort); InputTransAware SwordPulled(spPort); - LinMot SwordLatch(mOut1, mOut2); I2cMaster i2cDev(II2cMaster::ClockRate::_400k); AdcAds1115 adc(i2cDev, AdcAds1115::I2CAddress::Gnd); @@ -77,6 +85,7 @@ int main(int argc, char* argv[]) { fmt::println("Failed to connect ot Sensor {}", modeArg); } CurrMon mon(adc); + LinMot SwordLatch(mOut1, mOut2, mon, saveFile); IoStat = make_unique(SwordEntry, SwordEnd, SwordPulled, SwordLatch); @@ -95,7 +104,7 @@ int main(int argc, char* argv[]) { }; mqtt.flushInput(); - static auto conTask = Thread::createExplicit("MqttTask", MqttChannel::runMasterService, ref(mqtt), resetCall, ref(mon)); + static auto conTask = Thread::createExplicit("MqttTask", MqttChannel::runMasterService, ref(mqtt), resetCall); SwordLatch.moveOut(); fmt::println("Running App"); @@ -320,7 +329,6 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { log.sendEvent("Game", "Put Back"); mqtt.sendEvent("put-in"); IoStat->SwordLatch.moveOut(); // Lock - Thread::sleep(3s); mqtt.sendEvent("locked"); // mqtt.sendEvent("lock-error"); } break; diff --git a/src/sword/swordMain.hpp b/src/sword/swordMain.hpp index de479a6..cd3ba98 100644 --- a/src/sword/swordMain.hpp +++ b/src/sword/swordMain.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -24,35 +25,13 @@ #include "Ads1115.hpp" #include "MqttClient.hpp" #include "coreExcept.hpp" +#include "defines.hpp" #include "gpioPort.hpp" #include "i2cPort.hpp" #include "logging.hpp" #include "queue.hpp" #include "textUtils.hpp" #include "thread.hpp" -#include "defines.hpp" - -class LinMot { - public: - LinMot(OutputPort& o1, OutputPort& o2) - : _out1(o1), - _out2(o2) { - } - - void moveOut() { set(true); } - void moveIn() { set(false); } - bool isLocked() { return _locked; } - - private: - OutputPort& _out1; - OutputPort& _out2; - bool _locked{}; - void set(bool val) { - _out1.write(val); - _out2.write(!val); - _locked = val; - } -}; class CurrMon { // https://www.monolithicpower.com/en/documentview/productdocument/index/version/2/document_type/Datasheet/lang/EN/sku/MCS1802/document_id/5066/ @@ -72,7 +51,7 @@ class CurrMon { float sampleCurrent() { auto val = sample() - _refVal; val = val / 0.264; - fmt::println("{:.2} A", val); + // fmt::println("{:.2} A", val); return val; } @@ -87,6 +66,79 @@ class CurrMon { } }; +class LinMot { + public: + using SaveCall = void (*)(string, vector); + + LinMot(OutputPort& o1, OutputPort& o2, CurrMon& mon, SaveCall call) + : _out1(o1), + _out2(o2), + _mon(mon), + _saveCb(call) { + } + + void moveOut() { + set(true); + auto curr = awaitMove(); + _saveCb("Closing", curr); + } + void moveIn() { + set(false); + auto curr = awaitMove(); + _saveCb("Opening", curr); + } + bool isLocked() { return _locked; } + + private: + OutputPort& _out1; + OutputPort& _out2; + CurrMon& _mon; + SaveCall _saveCb; + const float FinThresh = 0.020; + const float AbortThresh = 0.050; + + bool _locked{}; + void set(bool val) { + _out1.write(val); + _out2.write(!val); + _locked = val; + } + + vector awaitMove() { + using namespace std::chrono; + auto tB = steady_clock::now(); + auto tE = steady_clock::now() + 6s; + auto tMin = steady_clock::now() + 4s; + vector iSample; + iSample.reserve(200); + + Thread::sleep(100ms); + int err{}; + while (steady_clock::now() < tE) { + Thread::sleep(50ms); + auto curr = std::abs(_mon.sampleCurrent()); + iSample.push_back(curr); + if (curr < FinThresh) { + fmt::println(".. min threshold"); + break; + } + if ((curr > AbortThresh) && + (++err > 3)) { + fmt::println(".. abort due to overcurrent"); + break; + } + } + auto t = duration_cast(steady_clock::now() - tB); + fmt::println(".. fin after {}", t); + + iSample.push_back(std::abs(_mon.sampleCurrent())); + while (std::chrono::steady_clock::now() < tMin) { + Thread::sleep(100ms); + } + return iSample; + }; +}; + class DeviceIoState { public: DeviceIoState(pal::InputTransAware& entry, @@ -239,7 +291,7 @@ class MediaPlayer { static thread playWavFileThread(const string& filePath) { if (filePath.empty()) { fmt::println(stderr, "ERROR: Empty file path"); - thread([](){ Thread::sleep(2s); }); + thread([]() { Thread::sleep(2s); }); } return thread([filePath]() { auto cmd = fmt::format("aplay '{}'", filePath); @@ -249,5 +301,4 @@ class MediaPlayer { } }); } - };