Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
104 lines
6.0 KiB
C++
104 lines
6.0 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Bounded Sample Data receiver; Commands owns wire encoding and transport.
|
|
#pragma once
|
|
#include "config_export.h"
|
|
#include <QTimer>
|
|
#include <QElapsedTimer>
|
|
#include <QUuid>
|
|
#include <cmath>
|
|
|
|
static QJsonObject decodeSample(QByteArray raw, int sequence, int decimation) {
|
|
require(raw.size()==34 || raw.size()==38 || raw.size()==44,"Unknown Sample Data packet length");
|
|
const bool indexed=raw.size()==44, third=raw.size()!=34;
|
|
VByteArray b(raw);
|
|
int index=indexed?b.vbPopFrontInt16():sequence;
|
|
require(index>=0 && index<2000,"Sample index outside buffer");
|
|
QJsonObject row{{"index",index},{"indexed",indexed},{"third_current_measured",third}};
|
|
auto take=[&](const QString &key){double v=b.vbPopFrontDouble32Auto();require(std::isfinite(v),"Non-finite sample");row.insert(key,v);return v;};
|
|
double i1=take("i1"),i2=take("i2");
|
|
if(third)take("i3");else row.insert("i3",-(i1+i2));
|
|
for(const auto &key:{"v1","v2","v3","v0","current","switching_hz"})take(key);
|
|
const double hz=row.value("switching_hz").toDouble();
|
|
require(hz>0 && hz<=1000000,"Invalid sample frequency");
|
|
const quint8 status=b.vbPopFrontInt8(),phase=b.vbPopFrontInt8();
|
|
row.insert("commutation",status&7);row.insert("hall",(status>>3)&7);
|
|
row.insert("phase_deg",phase/250.0*360.0);
|
|
row.insert("dt_s",decimation/hz);
|
|
if(indexed)require(b.vbPopFrontInt32()==index,"Sample index trailer mismatch");
|
|
require(b.isEmpty(),"Unexpected sample trailer");
|
|
return row;
|
|
}
|
|
|
|
class NativeSamples {
|
|
public:
|
|
Commands *commands=nullptr;
|
|
QTimer timer;
|
|
QElapsedTimer lifetime,quiet;
|
|
QString id,state="idle",error;
|
|
QJsonArray rows;
|
|
int mode=0,count=0,decimation=1,expected=0,timeout=30,malformed=0;
|
|
bool active() const {return state=="armed" || state=="receiving" || state=="stored_wait";}
|
|
void attach(Commands *c) {
|
|
commands=c; timer.setInterval(100);
|
|
QObject::connect(c,&Commands::samplesReceived,[this](QByteArray raw){receive(raw);});
|
|
QObject::connect(&timer,&QTimer::timeout,[this]{
|
|
if(lifetime.isValid() && lifetime.elapsed()>=300000){if(active())off();rows={};state="expired";timer.stop();return;}
|
|
if(!active())return;
|
|
if(mode==7 && !rows.isEmpty() && quiet.elapsed()>=1000){off();state="unverified";return;}
|
|
if(lifetime.elapsed()>=timeout*1000){off();state=rows.isEmpty()?"timeout":"incomplete";}
|
|
});
|
|
}
|
|
void off(){commands->samplePrint(DEBUG_SAMPLING_OFF,0,1,false);}
|
|
QJsonObject status() const {
|
|
return {{"capture_id",id},{"state",state},{"received",rows.size()},{"expected",expected},
|
|
{"capacity",2000},{"mode",mode},{"count",count},{"decimation",decimation},
|
|
{"malformed",malformed},{"error",error},{"indexed",false},{"raw_supported",false},
|
|
{"third_current_measured",false},{"acknowledged",false},
|
|
{"elapsed_ms",lifetime.isValid()?double(lifetime.elapsed()):0},
|
|
{"provenance",mode==7?"controller_last_buffer":"requested_capture"}};
|
|
}
|
|
QJsonObject start(const QJsonObject &p) {
|
|
if(active())return {{"accepted",false},{"reason","Sample Data capture is already active"}};
|
|
if(quiet.isValid() && quiet.elapsed()<1000)return {{"accepted",false},{"reason","Previous Sample Data transfer is still draining"}};
|
|
auto integer=[&](const char *k,int lo,int hi){auto v=p.value(k);double n=v.toDouble(-1);require(v.isDouble()&&n==int(n)&&n>=lo&&n<=hi,"Invalid Sample Data argument");return int(n);};
|
|
mode=integer("mode",1,7);count=integer("count",1,2000);decimation=integer("decimation",1,255);timeout=integer("timeout_s",2,120);
|
|
require(!p.value("raw").toBool(),"FW5.02 does not support raw ADC sampling");
|
|
expected=mode==7?0:mode>=3?2000:count;
|
|
id="sample_"+QUuid::createUuid().toString(QUuid::Id128);rows={};error.clear();malformed=0;
|
|
state=(mode==5||mode==6)?"stored_wait":"armed";lifetime.start();quiet.invalidate();timer.start();
|
|
commands->samplePrint(debug_sampling_mode(mode),count,decimation,false);
|
|
auto result=status();result.insert("accepted",true);return result;
|
|
}
|
|
void receive(const QByteArray &raw) {
|
|
quiet.restart();if(!active())return;
|
|
try {
|
|
// Only the reviewed FW5.02 live profile is admitted. New formats
|
|
// are decoded in offline checks, never guessed from a live device.
|
|
require(raw.size()==34,"FW5.02 Sample Data layout mismatch");
|
|
require(rows.size()<2000,"Sample Data buffer exceeded");
|
|
rows.append(decodeSample(raw,rows.size(),decimation));state="receiving";
|
|
if(expected && rows.size()==expected)state="complete";
|
|
}catch(const std::exception &e){++malformed;error=QString::fromUtf8(e.what());off();state="incomplete";}
|
|
}
|
|
QJsonObject run(const QString &method,const QJsonObject &p) {
|
|
if(method=="sample_start")return start(p);
|
|
if(method=="sample_status" && !p.contains("capture_id"))return status();
|
|
if(p.value("capture_id").toString()!=id || id.isEmpty())return {{"state","expired"}};
|
|
if(method=="sample_stop" && active()){off();state="cancelled";}
|
|
if(method=="sample_fetch") {
|
|
if(state!="stored_wait")return {{"accepted",false},{"reason","No pending stored capture"}};
|
|
// No trigger-complete ACK exists; a returned older buffer must not
|
|
// be represented as a confirmed capture of this trigger.
|
|
mode=7;expected=0;state="armed";rows={};timeout=30;lifetime.restart();
|
|
commands->samplePrint(DEBUG_SAMPLING_SEND_LAST_SAMPLES,0,1,false);
|
|
}
|
|
auto result=status();
|
|
if(method=="sample_read"){
|
|
double offset=p.value("offset").toDouble(-1);require(offset==int(offset)&&offset>=0&&offset<=2000,"Invalid sample offset");
|
|
QJsonArray page;for(int i=int(offset);i<rows.size()&&page.size()<128;++i)page.append(rows[i]);
|
|
result.insert("rows",page);result.insert("offset",int(offset));
|
|
}
|
|
return result;
|
|
}
|
|
};
|