Files
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

52 lines
2.8 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "config_export.h"
#include <QEventLoop>
#include <QMetaProperty>
#include <QTimer>
// Decoding and firmware compatibility belong to Commands. This adapter only
// projects its typed result; it never builds or parses vendor packets.
template<typename T> static QJsonObject toolNumbers(const T &value) {
QJsonObject out;
for (int i=0; i<T::staticMetaObject.propertyCount(); ++i) {
const auto p=T::staticMetaObject.property(i);
const auto v=p.readOnGadget(&value);
if (v.type()==QVariant::Double || v.type()==QVariant::Int || v.type()==QVariant::String)
out.insert(QString::fromLatin1(p.name()),QJsonValue::fromVariant(v));
}
return out;
}
static QJsonObject readNativeTool(VescInterface &vesc, const QString &tool) {
auto *cmd=vesc.commands();
const QMap<QString,int> codes{{"adc",COMM_GET_DECODED_ADC},{"chuk",COMM_GET_DECODED_CHUK},
{"imu",COMM_GET_IMU_DATA},{"bms",COMM_BMS_GET_VALUES}};
require(codes.contains(tool),"Unknown peripheral tool");
if (!cmd->getLimitedCompatibilityCommands().contains(codes.value(tool)))
return {{"tool",tool},{"state","unsupported"}};
QEventLoop loop; QTimer timeout; timeout.setSingleShot(true); QObject observer;
QJsonObject values; bool received=false;
auto finish=[&](const QJsonObject &v){values=v;received=true;loop.quit();};
QObject::connect(cmd,&Commands::decodedAdcReceived,&observer,[&](double v,double volts,double v2,double volts2){
if(tool=="adc")finish({{"level_1",v},{"voltage_1_v",volts},{"level_2",v2},{"voltage_2_v",volts2}});
});
QObject::connect(cmd,&Commands::decodedChukReceived,&observer,[&](double v){if(tool=="chuk")finish({{"level",v}});});
QObject::connect(cmd,&Commands::valuesImuReceived,&observer,[&](IMU_VALUES v,unsigned int mask){
if(tool=="imu"){auto out=toolNumbers(v);out.insert("mask",double(mask));finish(out);}
});
QObject::connect(cmd,&Commands::bmsValuesRx,&observer,[&](BMS_VALUES v){
if(tool!="bms")return;
auto out=toolNumbers(v);QJsonArray cells,temps,balancing;
for(auto x:v.v_cells)cells.append(x);for(auto x:v.temps)temps.append(x);for(auto x:v.is_balancing)balancing.append(x);
out.insert("v_cells",cells);out.insert("temps",temps);out.insert("is_balancing",balancing);finish(out);
});
QObject::connect(&timeout,&QTimer::timeout,&loop,&QEventLoop::quit);timeout.start(2000);
if(tool=="adc")cmd->getDecodedAdc();
if(tool=="chuk")cmd->getDecodedChuk();
if(tool=="imu")cmd->getImuData(0xffff);
if(tool=="bms")cmd->bmsGetValues();
if(!received)loop.exec();
require(vesc.isPortConnected(),"Peripheral read disconnected");
return {{"tool",tool},{"state",received?"received":"no_response"},{"values",values}};
}