You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
458 lines
18 KiB
458 lines
18 KiB
#include "mainwindow.h"
|
|
|
|
#include <QDateTime>
|
|
#include <QMessageBox>
|
|
#include <QtConcurrent>
|
|
#include <QtCore/QVariant>
|
|
#include <QtSerialPort/QSerialPort>
|
|
#include <QtSerialPort/QSerialPortInfo>
|
|
#include <QtWidgets/QLineEdit>
|
|
|
|
#include "./ui_mainwindow.h"
|
|
#include "iflytop_canbus/iflytop_canbus_master.hpp"
|
|
#include "logger.hpp"
|
|
#include "qt_serial_datachannel.hpp"
|
|
#include "reglist.hpp"
|
|
#include "zexception.hpp"
|
|
#include "zui/z_function_list_box.hpp"
|
|
#include "zui/zq_vtab_page.hpp"
|
|
//
|
|
#include <thread>
|
|
|
|
#include "iflytop_canbus\regid.hpp"
|
|
#include "zui/zui.hpp"
|
|
|
|
using namespace std;
|
|
using namespace iflytop;
|
|
using namespace zcr;
|
|
typedef enum {
|
|
kone_lead_ecg,
|
|
kthree_lead_ecg,
|
|
} device_type_t;
|
|
|
|
static MainWindow *m_mainWindow;
|
|
static QTDataChannel G_QTDataChannel;
|
|
static QTDataChannel G_WaveDataChannel;
|
|
|
|
static int PublicState_DeviceIDVal = 0;
|
|
|
|
QT_REQUIRE_CONFIG(groupbox);
|
|
|
|
#define TAG "MainWindow"
|
|
|
|
static const char *fmt(const char *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
static char buf[1024] = {0};
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
va_end(args);
|
|
return buf;
|
|
}
|
|
|
|
void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) {}
|
|
void MainWindow::doinui_slot(QFunction func) {
|
|
if (func.get()) func.get()();
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* PreviewShow *
|
|
***********************************************************************************************************************/
|
|
|
|
void MainWindow::instructionPreviewShow(QString text) {
|
|
QString info;
|
|
info.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz "));
|
|
info.append(" |");
|
|
info.append(text);
|
|
emit doinui_signal(QFunction([this, info]() { ui->instructionPreview->append(info); }));
|
|
}
|
|
void MainWindow::reportPreviewShow(QString text) {
|
|
QString info;
|
|
|
|
info.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz "));
|
|
info.append(text);
|
|
emit doinui_signal(QFunction([this, info]() {
|
|
if (ui->reportPreview->document()->lineCount() > 1000) {
|
|
ui->reportPreview->document()->clear();
|
|
}
|
|
ui->reportPreview->append(info);
|
|
}));
|
|
}
|
|
void MainWindow::blockDataUploadPreviewShow(QString text) {
|
|
QString info;
|
|
|
|
info.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz "));
|
|
info.append(text);
|
|
emit doinui_signal(QFunction([this, info]() {
|
|
if (ui->uploadDataPreview->document()->lineCount() > 1000) {
|
|
ui->uploadDataPreview->document()->clear();
|
|
}
|
|
ui->uploadDataPreview->append(info);
|
|
}));
|
|
}
|
|
void MainWindow::rawDataPreviewShow(QString text) {
|
|
QString info;
|
|
|
|
info.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz"));
|
|
info.append(text);
|
|
emit doinui_signal(QFunction([this, info]() {
|
|
if (ui->rawDataPreview->document()->lineCount() > 1000) {
|
|
ui->rawDataPreview->document()->clear();
|
|
}
|
|
ui->rawDataPreview->append(info);
|
|
}));
|
|
}
|
|
|
|
void MainWindow::instructionPreviewClear() {
|
|
doinui_signal(QFunction([this]() { ui->instructionPreview->clear(); }));
|
|
}
|
|
|
|
#pragma pack(push, 1)
|
|
typedef struct {
|
|
uint16_t header;
|
|
int16_t wave1;
|
|
int16_t wave2;
|
|
int16_t wave3;
|
|
int16_t wave4;
|
|
int16_t wave5;
|
|
uint8_t check;
|
|
uint16_t tail;
|
|
} Wave_t;
|
|
#pragma pack(pop)
|
|
|
|
void MainWindow::displayWave(int16_t wave1, int16_t wave2, int16_t wave3) {
|
|
// 1. 生成波形数据
|
|
Wave_t wave;
|
|
wave.header = 0xAAAA;
|
|
wave.wave1 = wave1;
|
|
wave.wave2 = wave2;
|
|
wave.wave3 = wave3;
|
|
wave.wave4 = 0;
|
|
wave.wave5 = 0;
|
|
wave.tail = 0x5555;
|
|
wave.check = 0;
|
|
|
|
uint8_t check = 0;
|
|
for (size_t i = 2; i < sizeof(wave) - 2; i++) {
|
|
check += ((uint8_t *)&wave)[i];
|
|
}
|
|
wave.check = check;
|
|
// 2. 发送波形数据
|
|
G_WaveDataChannel.send((uint8_t *)&wave, sizeof(wave));
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* UI相关构造 *
|
|
***********************************************************************************************************************/
|
|
|
|
void MainWindow::constructBaseUI() {
|
|
/**
|
|
* @brief 指令串口UI
|
|
*/
|
|
{
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
for (const QSerialPortInfo &info : infos) {
|
|
ui->serialPortCB->addItem(info.portName());
|
|
}
|
|
}
|
|
|
|
ui->serialBaudrateCB->addItem("9600");
|
|
ui->serialBaudrateCB->addItem("14400");
|
|
ui->serialBaudrateCB->addItem("19200");
|
|
ui->serialBaudrateCB->addItem("38400");
|
|
ui->serialBaudrateCB->addItem("57600");
|
|
ui->serialBaudrateCB->addItem("115200");
|
|
ui->serialBaudrateCB->addItem("460800");
|
|
ui->serialBaudrateCB->addItem("500000");
|
|
ui->serialBaudrateCB->addItem("2000000");
|
|
ui->serialBaudrateCB->setCurrentIndex(6);
|
|
|
|
connect(ui->serialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
|
|
ui->serialPortCB->clear();
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
for (const QSerialPortInfo &info : infos) {
|
|
ui->serialPortCB->addItem(info.portName());
|
|
}
|
|
});
|
|
|
|
connect(ui->serialOpenKey, &QPushButton::clicked, this, [=](bool check) {
|
|
// 打开串口
|
|
if (ui->serialOpenKey->text() == "打开") {
|
|
G_QTDataChannel.setPortName(ui->serialPortCB->currentText().toStdString());
|
|
G_QTDataChannel.setBaudRate(ui->serialBaudrateCB->currentText().toInt());
|
|
G_QTDataChannel.setDataBits(QSerialPort::Data8);
|
|
G_QTDataChannel.setParity(QSerialPort::NoParity);
|
|
G_QTDataChannel.setFlowControl(QSerialPort::NoFlowControl);
|
|
G_QTDataChannel.setStopBits(QSerialPort::OneStop);
|
|
|
|
/**
|
|
* @brief 串口打开时,初始化相应参数
|
|
*/
|
|
|
|
if (!G_QTDataChannel.open()) {
|
|
QMessageBox::about(NULL, "提示", "串口无法打开,串口不存在或已被占??");
|
|
return;
|
|
}
|
|
ui->serialOpenKey->setText("关闭");
|
|
// 下拉菜单控件使能
|
|
ui->serialBaudrateCB->setEnabled(false);
|
|
ui->serialPortCB->setEnabled(false);
|
|
ui->serialPortRefreshKey->setEnabled(false);
|
|
|
|
IflytopCanbusMaster::ins()->updateChannelConfig();
|
|
} else {
|
|
G_QTDataChannel.close();
|
|
ui->serialOpenKey->setText("打开");
|
|
ui->serialBaudrateCB->setEnabled(true);
|
|
ui->serialPortCB->setEnabled(true);
|
|
ui->serialPortRefreshKey->setEnabled(true);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @brief 波形串口UI
|
|
*/
|
|
{
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
for (const QSerialPortInfo &info : infos) {
|
|
ui->waveSerialPortCB->addItem(info.portName());
|
|
}
|
|
}
|
|
|
|
ui->waveSerialBaudrateCB->addItem("9600");
|
|
ui->waveSerialBaudrateCB->addItem("14400");
|
|
ui->waveSerialBaudrateCB->addItem("19200");
|
|
ui->waveSerialBaudrateCB->addItem("38400");
|
|
ui->waveSerialBaudrateCB->addItem("57600");
|
|
ui->waveSerialBaudrateCB->addItem("115200");
|
|
ui->waveSerialBaudrateCB->addItem("460800");
|
|
ui->waveSerialBaudrateCB->addItem("500000");
|
|
ui->waveSerialBaudrateCB->addItem("2000000");
|
|
// ui->waveSerialBaudrateCB->addItems
|
|
ui->waveSerialBaudrateCB->setCurrentIndex(6);
|
|
|
|
connect(ui->waveSerialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
|
|
ui->waveSerialPortCB->clear();
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
for (const QSerialPortInfo &info : infos) {
|
|
ui->waveSerialPortCB->addItem(info.portName());
|
|
}
|
|
});
|
|
|
|
connect(ui->waveSerialOpenKey, &QPushButton::clicked, this, [=](bool check) {
|
|
// 打开串口
|
|
if (ui->waveSerialOpenKey->text() == "打开") {
|
|
G_WaveDataChannel.setPortName(ui->waveSerialPortCB->currentText().toStdString());
|
|
G_WaveDataChannel.setBaudRate(ui->waveSerialBaudrateCB->currentText().toInt());
|
|
G_WaveDataChannel.setDataBits(QSerialPort::Data8);
|
|
G_WaveDataChannel.setParity(QSerialPort::NoParity);
|
|
G_WaveDataChannel.setFlowControl(QSerialPort::NoFlowControl);
|
|
G_WaveDataChannel.setStopBits(QSerialPort::OneStop);
|
|
|
|
if (!G_WaveDataChannel.open()) {
|
|
QMessageBox::about(NULL, "提示", "串口无法打开,串口不存在或已被占??");
|
|
return;
|
|
}
|
|
ui->waveSerialOpenKey->setText("关闭");
|
|
// 下拉菜单控件使能
|
|
ui->waveSerialBaudrateCB->setEnabled(false);
|
|
ui->waveSerialPortCB->setEnabled(false);
|
|
ui->waveSerialPortRefreshKey->setEnabled(false);
|
|
} else {
|
|
G_WaveDataChannel.close();
|
|
ui->waveSerialOpenKey->setText("打开");
|
|
ui->waveSerialBaudrateCB->setEnabled(true);
|
|
ui->waveSerialPortCB->setEnabled(true);
|
|
ui->waveSerialPortRefreshKey->setEnabled(true);
|
|
}
|
|
});
|
|
}
|
|
|
|
void MainWindow::displayInfo(bool suc, QString info) {}
|
|
|
|
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
|
/**
|
|
* @brief QT初始化
|
|
*/
|
|
ui->setupUi(this);
|
|
m_mainWindow = this;
|
|
qRegisterMetaType<int32_t>("int32_t");
|
|
qRegisterMetaType<uint32_t>("uint32_t");
|
|
qRegisterMetaType<float>("float");
|
|
qRegisterMetaType<function<void()>>("function<void()>");
|
|
qRegisterMetaType<QFunction>("QFunction");
|
|
connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
|
|
ZQUI::ins()->initialize();
|
|
ZQUI::ins()->setBlockDataUploadPreviewShow([this](QString text) { blockDataUploadPreviewShow(text); });
|
|
ZQUI::ins()->setInstructionPreviewShow([this](QString text) { instructionPreviewShow(text); });
|
|
ZQUI::ins()->setReportPreviewShow([this](QString text) { reportPreviewShow(text); });
|
|
ZQUI::ins()->setRawDataPreviewShow([this](QString text) { rawDataPreviewShow(text); });
|
|
ZQUI::ins()->setInstructionPreviewClear([this]() { instructionPreviewClear(); });
|
|
|
|
/**
|
|
* @brief 页面逻辑初始化
|
|
*/
|
|
constructBaseUI();
|
|
constructAppUI();
|
|
|
|
/**
|
|
* @brief 业务逻辑构造
|
|
*/
|
|
G_QTDataChannel.init();
|
|
G_WaveDataChannel.init();
|
|
IflytopCanbusMaster::ins()->initialize(&G_QTDataChannel);
|
|
|
|
IflytopCanbusMaster::ins()->regOnRawData([this](raw_data_type_t type, uint8_t *hex, uint32_t hexlen) {
|
|
if (type == kcmd_cmd) {
|
|
iflytop_canbus_frame_t *frame = (iflytop_canbus_frame_t *)hex;
|
|
ZQUI::ins()->rawDataPreviewShow("[CMD ] %s %s [id:%d cmd:%d,%d]", zhex2str(hex, sizeof(iflytop_canbus_frame_t)).c_str(), //
|
|
zhex2str(frame->data, hexlen - sizeof(iflytop_canbus_frame_t)).c_str(), //
|
|
frame->device_id, frame->cmd_main_id, frame->cmd_sub_id //
|
|
);
|
|
} else if (type == kcmd_receipt) {
|
|
ZQUI::ins()->rawDataPreviewShow("[RECEIPT] %s", zhex2str(hex, hexlen).c_str());
|
|
} else if (type == kcmd_report) {
|
|
ZQUI::ins()->rawDataPreviewShow("[REPORT ] %s", zhex2str(hex, hexlen).c_str());
|
|
}
|
|
});
|
|
}
|
|
MainWindow::~MainWindow() { delete ui; }
|
|
|
|
void MainWindow::processException(const zexception &e) { //
|
|
ZQUI::ins()->instructionPreviewShow("%s", e.what());
|
|
}
|
|
|
|
int32_t MainWindow::getDeviceId() { return PublicState_DeviceIDVal; }
|
|
|
|
void MainWindow::constructAppUI() {
|
|
/***********************************************************************************************************************
|
|
* 模块操作 *
|
|
***********************************************************************************************************************/
|
|
|
|
{
|
|
ZQVTabPage *tab = new ZQVTabPage(ui->buttonTabWidget, "模块操作");
|
|
|
|
{
|
|
ZQFunctionListBox *box = new ZQFunctionListBox(tab, "模块基础操作", 4);
|
|
box->newFunc("扫描模块", {}, [this](int argn, const char **args) { //
|
|
for (size_t i = 1; i < 255; i++) {
|
|
try {
|
|
IflytopCanbusMaster::ins()->ping(i);
|
|
ZQUI::ins()->instructionPreviewShow("module :%d isOnline", i);
|
|
} catch (const zexception &e) {
|
|
if (e.ecode() != ke_overtime) {
|
|
processException(e);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
ZQUI::ins()->instructionPreviewShow("ScanModuleEnd");
|
|
});
|
|
}
|
|
|
|
{
|
|
ZQFunctionListBox *box = new ZQFunctionListBox(tab, "电机配置", 4);
|
|
box->newFunc("电机设置编码器分辨率", {"resolution"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_set_enc_resolution(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("设置IRUM", {"[0-31]"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->writereg(getDeviceId(), kreg_stepmotor_irun, atoi(args[0])); });
|
|
box->newFunc("设置IHOLD", {"[0-31]"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->writereg(getDeviceId(), kreg_stepmotor_ihold, atoi(args[0])); });
|
|
box->newFunc("设置默认速度", {"velocity"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->writereg(getDeviceId(), kreg_motor_default_velocity, atoi(args[0])); });
|
|
box->newFunc("激活配置", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->module_active_cfg(getDeviceId()); });
|
|
}
|
|
|
|
{
|
|
ZQFunctionListBox *box = new ZQFunctionListBox(tab, "电机状态读取", 4);
|
|
box->newFunc("电机读取编码器值", {}, [this](int argn, const char **args) {
|
|
int32_t retval = IflytopCanbusMaster::ins()->motor_read_enc_val(getDeviceId());
|
|
ZQUI::ins()->instructionPreviewShow("encVal %d", retval);
|
|
});
|
|
box->newFunc("电机读取编码器分辨率", {}, [this](int argn, const char **args) {
|
|
int32_t retval = IflytopCanbusMaster::ins()->motor_get_enc_resolution(getDeviceId());
|
|
ZQUI::ins()->instructionPreviewShow("encResolution %d", retval);
|
|
});
|
|
}
|
|
|
|
{
|
|
ZQFunctionListBox *box = new ZQFunctionListBox(tab, "电机操作", 4);
|
|
box->newFunc("停止电机", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->module_stop(getDeviceId()); });
|
|
box->newFunc("急停电机", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->module_break(getDeviceId()); });
|
|
box->newFunc("激活配置", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->module_active_cfg(getDeviceId()); });
|
|
box->newFunc("电机使能", {"enable"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_enable(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("电机旋转", {"direction"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_easy_rotate(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("电机移动", {"distance"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_easy_move_by(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("电机移动到", {"position"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_easy_move_to(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("电机归零", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_easy_move_to_zero(getDeviceId()); });
|
|
box->newFunc("电机设置当前位置", {"position"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_easy_set_current_pos(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("电机移动到IO", {"io"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_easy_move_to_io(getDeviceId(), atoi(args[0])); });
|
|
box->newFunc("电机设置子设备寄存器", {"regaddr", "val"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_set_subdevice_reg(getDeviceId(), atoi(args[0]), atoi(args[1])); });
|
|
box->newFunc("电机读取子设备寄存器", {"regaddr"}, [this](int argn, const char **args) {
|
|
int32_t retval = IflytopCanbusMaster::ins()->motor_get_subdevice_reg(getDeviceId(), atoi(args[0]));
|
|
ZQUI::ins()->instructionPreviewShow("regVal 0x%08x", retval);
|
|
});
|
|
}
|
|
{
|
|
ZQFunctionListBox *box = new ZQFunctionListBox(tab, "驱动底层配置", 4);
|
|
box->newFunc("电机设置子设备寄存器", {"regaddr", "val"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->motor_set_subdevice_reg(getDeviceId(), atoi(args[0]), atoi(args[1])); });
|
|
box->newFunc("电机读取子设备寄存器", {"regaddr"}, [this](int argn, const char **args) {
|
|
int32_t retval = IflytopCanbusMaster::ins()->motor_get_subdevice_reg(getDeviceId(), atoi(args[0]));
|
|
ZQUI::ins()->instructionPreviewShow("regVal 0x%08x", retval);
|
|
});
|
|
}
|
|
|
|
tab->addSpacer();
|
|
}
|
|
|
|
m_testThread.reset(new std::thread([this]() {
|
|
while (true) {
|
|
if (m_testState) {
|
|
try {
|
|
IflytopCanbusMaster::ins()->ping(getDeviceId());
|
|
} catch (const zexception &e) {
|
|
processException(e);
|
|
}
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
}));
|
|
|
|
{
|
|
ZQVTabPage *tab = new ZQVTabPage(ui->buttonTabWidget, "暴力测试");
|
|
// static
|
|
{
|
|
ZQFunctionListBox *box = new ZQFunctionListBox(tab, "模块基础操作", 4);
|
|
box->newFunc("开始测试", {"间隔"}, [this](int argn, const char **args) { //
|
|
int interval = atoi(args[0]);
|
|
m_testState = true;
|
|
});
|
|
|
|
box->newFunc("停止测试", {}, [this](int argn, const char **args) { //
|
|
m_testState = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* 寄存器列表 *
|
|
***********************************************************************************************************************/
|
|
{
|
|
ZQVTabPage *tab = new ZQVTabPage(ui->buttonTabWidget, "寄存器操作");
|
|
|
|
ZRegTableList *tableBox = new ZRegTableList(tab, "寄存器操作");
|
|
tableBox->initializeRegOperation(
|
|
[this](int32_t add, int32_t val) { //
|
|
IflytopCanbusMaster::ins()->writereg(getDeviceId(), add, val);
|
|
return true;
|
|
},
|
|
[this](int32_t add, int32_t *val) { //
|
|
IflytopCanbusMaster::ins()->readreg(getDeviceId(), add, val);
|
|
return true;
|
|
});
|
|
|
|
tableBox->newFunc("激活配置", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->module_active_cfg(getDeviceId()); });
|
|
for (auto &var : GetRegList()) {
|
|
tableBox->addReg(var.title.c_str(), var.addr, var.flag);
|
|
}
|
|
tableBox->addSpacer();
|
|
tab->addSpacer();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_PublicState_ConfirmKey_clicked() { PublicState_DeviceIDVal = ui->PublicState_DeviceID->toPlainText().toInt(); }
|