wip: currentSens

This commit is contained in:
Jakob Gegeniger committed 2026-10-25 01:15:13 +02:00
1 parent 85e2e92991
commit 855ffe5915
3 files changed
+89 -42

No files matched your search

+1 -13
View File
@@ -97,7 +97,7 @@ class MqttChannel {
fmt::println("Input queue flushed, {} Messages discarted", cnt); fmt::println("Input queue flushed, {} Messages discarted", cnt);
} }
static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function<void()> resetCall, CurrMon& mon) { static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function<void()> resetCall) {
while (true) { while (true) {
auto serMsg = instance.MasterQueue.tryDequeueFor(500ms); auto serMsg = instance.MasterQueue.tryDequeueFor(500ms);
// only resetTopic and cfgTopic arrive here // only resetTopic and cfgTopic arrive here
@@ -152,24 +152,12 @@ class MqttChannel {
break; break;
} }
instance.IoStat.SwordLatch.moveIn(); 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; } break;
case ServiceCommands_e::SetLockClose: { case ServiceCommands_e::SetLockClose: {
if (instance.IoStat.GameRunning) { if (instance.IoStat.GameRunning) {
break; break;
} }
instance.IoStat.SwordLatch.moveOut(); 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; } break;
case ServiceCommands_e::PlaySoundPromo: { case ServiceCommands_e::PlaySoundPromo: {
MediaPlayer::playWavFileAsync(instance.Config.SoundPromo); MediaPlayer::playWavFileAsync(instance.Config.SoundPromo);
+11 -3
View File
@@ -15,6 +15,15 @@ unique_ptr<DeviceIoState> IoStat;
void processProduction(MqttChannel& mqtt, SeqLogger& log); void processProduction(MqttChannel& mqtt, SeqLogger& log);
void saveFile(string type, vector<float> 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[]) { int main(int argc, char* argv[]) {
CLI::App app{"Sword Firmware"}; CLI::App app{"Sword Firmware"};
string appName = "SwordDebug"; string appName = "SwordDebug";
@@ -68,7 +77,6 @@ int main(int argc, char* argv[]) {
InputTransAware SwordEntry(sbPort); InputTransAware SwordEntry(sbPort);
InputTransAware SwordEnd(sePort); InputTransAware SwordEnd(sePort);
InputTransAware SwordPulled(spPort); InputTransAware SwordPulled(spPort);
LinMot SwordLatch(mOut1, mOut2);
I2cMaster i2cDev(II2cMaster::ClockRate::_400k); I2cMaster i2cDev(II2cMaster::ClockRate::_400k);
AdcAds1115 adc(i2cDev, AdcAds1115::I2CAddress::Gnd); AdcAds1115 adc(i2cDev, AdcAds1115::I2CAddress::Gnd);
@@ -77,6 +85,7 @@ int main(int argc, char* argv[]) {
fmt::println("Failed to connect ot Sensor {}", modeArg); fmt::println("Failed to connect ot Sensor {}", modeArg);
} }
CurrMon mon(adc); CurrMon mon(adc);
LinMot SwordLatch(mOut1, mOut2, mon, saveFile);
IoStat = make_unique<DeviceIoState>(SwordEntry, SwordEnd, SwordPulled, SwordLatch); IoStat = make_unique<DeviceIoState>(SwordEntry, SwordEnd, SwordPulled, SwordLatch);
@@ -95,7 +104,7 @@ int main(int argc, char* argv[]) {
}; };
mqtt.flushInput(); 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(); SwordLatch.moveOut();
fmt::println("Running App"); fmt::println("Running App");
@@ -320,7 +329,6 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) {
log.sendEvent("Game", "Put Back"); log.sendEvent("Game", "Put Back");
mqtt.sendEvent("put-in"); mqtt.sendEvent("put-in");
IoStat->SwordLatch.moveOut(); // Lock IoStat->SwordLatch.moveOut(); // Lock
Thread::sleep(3s);
mqtt.sendEvent("locked"); mqtt.sendEvent("locked");
// mqtt.sendEvent("lock-error"); // mqtt.sendEvent("lock-error");
} break; } break;
+77 -26
View File
@@ -2,6 +2,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <fmt/os.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h> #include <sys/poll.h>
#include <unistd.h> #include <unistd.h>
@@ -24,35 +25,13 @@
#include "Ads1115.hpp" #include "Ads1115.hpp"
#include "MqttClient.hpp" #include "MqttClient.hpp"
#include "coreExcept.hpp" #include "coreExcept.hpp"
#include "defines.hpp"
#include "gpioPort.hpp" #include "gpioPort.hpp"
#include "i2cPort.hpp" #include "i2cPort.hpp"
#include "logging.hpp" #include "logging.hpp"
#include "queue.hpp" #include "queue.hpp"
#include "textUtils.hpp" #include "textUtils.hpp"
#include "thread.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 { class CurrMon {
// https://www.monolithicpower.com/en/documentview/productdocument/index/version/2/document_type/Datasheet/lang/EN/sku/MCS1802/document_id/5066/ // 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() { float sampleCurrent() {
auto val = sample() - _refVal; auto val = sample() - _refVal;
val = val / 0.264; val = val / 0.264;
fmt::println("{:.2} A", val); // fmt::println("{:.2} A", val);
return val; return val;
} }
@@ -87,6 +66,79 @@ class CurrMon {
} }
}; };
class LinMot {
public:
using SaveCall = void (*)(string, vector<float>);
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<float> awaitMove() {
using namespace std::chrono;
auto tB = steady_clock::now();
auto tE = steady_clock::now() + 6s;
auto tMin = steady_clock::now() + 4s;
vector<float> 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<milliseconds>(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 { class DeviceIoState {
public: public:
DeviceIoState(pal::InputTransAware& entry, DeviceIoState(pal::InputTransAware& entry,
@@ -239,7 +291,7 @@ class MediaPlayer {
static thread playWavFileThread(const string& filePath) { static thread playWavFileThread(const string& filePath) {
if (filePath.empty()) { if (filePath.empty()) {
fmt::println(stderr, "ERROR: Empty file path"); fmt::println(stderr, "ERROR: Empty file path");
thread([](){ Thread::sleep(2s); }); thread([]() { Thread::sleep(2s); });
} }
return thread([filePath]() { return thread([filePath]() {
auto cmd = fmt::format("aplay '{}'", filePath); auto cmd = fmt::format("aplay '{}'", filePath);
@@ -249,5 +301,4 @@ class MediaPlayer {
} }
}); });
} }
}; };