GrabBag/Tools/ControlApp/SimulationMode.h
2026-03-01 18:11:32 +08:00

78 lines
2.1 KiB
C++

#ifndef SIMULATIONMODE_H
#define SIMULATIONMODE_H
#include <QObject>
#include <QString>
#include <QTimer>
#include <random>
/**
* @brief 模拟模式类
*
* 用于在没有真实设备的情况下测试应用程序
* 模拟ModbusTCP控制器和机械臂的行为
*/
class SimulationMode : public QObject
{
Q_OBJECT
public:
explicit SimulationMode(QObject *parent = nullptr);
// 启用/禁用模拟模式
void setEnabled(bool enabled);
bool isEnabled() const { return m_enabled; }
// 模拟控制器连接
bool simulateControllerConnect();
void simulateControllerDisconnect();
// 模拟机械臂连接
bool simulateRobotConnect();
void simulateRobotDisconnect();
// 模拟ModbusTCP寄存器读写
bool simulateReadRegister(int address, uint16_t& value);
bool simulateWriteRegister(int address, uint16_t value);
bool simulateReadRegisters(int startAddress, int quantity, std::vector<uint16_t>& values);
// 模拟机械臂运动
bool simulateMoveL(double x, double y, double z, double rx, double ry, double rz);
bool simulateMoveJ(double x, double y, double z, double rx, double ry, double rz);
// 设置模拟检测结果
void setSimulatedDetectionResult(float x, float y, float z, float roll, float pitch, float yaw);
signals:
void logMessage(const QString& message);
private:
bool m_enabled = false;
// 模拟寄存器
std::map<int, uint16_t> m_registers;
// 模拟检测结果
float m_detectionX = 100.0f;
float m_detectionY = 200.0f;
float m_detectionZ = 300.0f;
float m_detectionRoll = 0.0f;
float m_detectionPitch = 0.0f;
float m_detectionYaw = 90.0f;
// 随机数生成器
std::mt19937 m_randomEngine;
std::uniform_real_distribution<float> m_positionNoise;
std::uniform_real_distribution<float> m_angleNoise;
// 模拟检测延迟定时器
QTimer* m_detectionTimer = nullptr;
// 辅助函数
void initializeRegisters();
void simulateDetectionProcess();
float addNoise(float value, float noiseRange);
};
#endif // SIMULATIONMODE_H