117 lines
4.2 KiB
C++
117 lines
4.2 KiB
C++
#ifndef APPCONTEXT_H
|
|
#define APPCONTEXT_H
|
|
|
|
#include <QObject>
|
|
#include <QSettings>
|
|
#include <QVector>
|
|
#include "global.h"
|
|
#include "uart.h"
|
|
|
|
class CarCommunication;
|
|
|
|
// 故障历史记录
|
|
struct FaultRecord {
|
|
QString time; // "2026-05-16 10:08"
|
|
QString system; // 机器人底盘 / 机械臂 / 洗消系统 / 气体检测仪
|
|
QString code; // 故障代码
|
|
QString desc; // 故障名称
|
|
};
|
|
|
|
// ===================== 应用上下文(单例) =====================
|
|
// 持有:通信层、参数存储、全局状态(模式/急停/通信状态)、遥测数据快照
|
|
class AppContext : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
static AppContext* instance();
|
|
|
|
CarCommunication* comm() const { return m_comm; }
|
|
RcCommunication* rc() const { return m_rc; }
|
|
|
|
// 遥测数据快照(由通信层填充)
|
|
Telemetry telemetry;
|
|
|
|
// 全局状态
|
|
Mode mode = Mode::Drive; // 行车/机械臂模式(机械旋钮)
|
|
bool estopTriggered = false; // 急停是否触发(需人工复位)
|
|
bool gasRunning = false; // 气体检测仪检测中
|
|
|
|
// 洗消作业开关(消除/消毒共用)。作业期间**强制机械臂模式**——末端要自动俯仰摆动,
|
|
// 而 Arm_ControlCmd1 的使能位就挂在这个模式上;作业结束后回到作业前的模式。
|
|
// 页面不要直接改成员,走这个入口。
|
|
void setWashRunning(bool on);
|
|
bool isWashRunning() const { return washRunning; }
|
|
|
|
// ===================== 存储区参数(QSettings 持久化) =====================
|
|
double getDouble(const QString& key, double def) const;
|
|
void setDouble(const QString& key, double val);
|
|
QString getString(const QString& key, const QString& def) const;
|
|
void setString(const QString& key, const QString& val);
|
|
|
|
// 密码
|
|
QString password() const;
|
|
void setPassword(const QString& pwd);
|
|
QString adminPassword() const;
|
|
|
|
// 洗消/底盘参数出厂值与键名
|
|
static QString keySpeed() { return "param/speed"; } // 车速 km/h,出厂 4.5
|
|
static QString keyPumpFlow() { return "param/pump_flow"; } // 柱塞泵流量,出厂 20
|
|
static QString keyFoamRatio() { return "param/foam_ratio"; } // 发泡倍数,出厂 6
|
|
static QString keyFoamPercent(){ return "param/foam_percent"; }// 泡沫比例,出厂 3
|
|
static QString keyAirFlow() { return "param/air_flow"; } // 空压机流量,出厂 100
|
|
|
|
// 故障历史(⑫故障查询数据源)
|
|
QVector<FaultRecord> faultHistory;
|
|
void appendFault(const QString& system, const QString& code, const QString& desc);
|
|
|
|
// 急停复位(人工复位)
|
|
void resetEstop();
|
|
|
|
// 触发急停(软急停/物理急停统一入口)
|
|
void triggerEstop();
|
|
|
|
// 模式切换(行车/机械臂,由面板机械旋钮或演示按钮触发)
|
|
void setMode(Mode m);
|
|
|
|
// 遥控器:链路状态与“正在操作”判据(遥控器优先时界面按钮让位)
|
|
bool rcDriving() const { return m_rcDriving; }
|
|
|
|
signals:
|
|
void modeChanged(Mode m);
|
|
void estopChanged(bool triggered);
|
|
void commChanged(bool ok);
|
|
void rcDrivingChanged(bool driving);
|
|
|
|
private slots:
|
|
void onRcFrame(const RcFrame& f);
|
|
void onRcLinkChanged(bool ok);
|
|
|
|
private:
|
|
explicit AppContext(QObject* parent = nullptr);
|
|
void seedFaultHistory();
|
|
|
|
void applyRcChassis(int gear, int steer, bool enabled);
|
|
void applyRcArm(const RcFrame& f);
|
|
void sendChassisCommand(); // 按当前档位/转向方向下发(两者可同时)
|
|
void updateRcDriving();
|
|
|
|
QSettings* m_settings = nullptr;
|
|
CarCommunication* m_comm = nullptr;
|
|
RcCommunication* m_rc = nullptr;
|
|
|
|
// 遥控器动作边沿状态:只在方向变化时下发,避免 100ms 刷屏
|
|
int m_rcGearDir = 0; // -1=后 0=中 +1=前
|
|
int m_rcSteerDir = 0; // -1=左 0=中 +1=右
|
|
int m_rcAxisDir[6] = {0, 0, 0, 0, 0, 0}; // 顺序见 kArmAxes
|
|
bool m_rcEstopPrev = false;
|
|
bool m_rcDriving = false;
|
|
|
|
bool washRunning = false;
|
|
Mode m_modeBeforeWash = Mode::Drive; // 洗消作业前的工作模式,作业结束后还原
|
|
};
|
|
|
|
// 便捷访问宏
|
|
#define CTX AppContext::instance()
|
|
|
|
#endif // APPCONTEXT_H
|