diff --git a/.vscode/settings.json b/.vscode/settings.json index c0703da..d24c438 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -97,7 +97,12 @@ "qtextbrowser": "cpp", "qchartview": "cpp", "qgroupbox": "cpp", - "qmenubar": "cpp" + "qmenubar": "cpp", + "qtextedit": "cpp", + "qpushbutton": "cpp", + "qspaceritem": "cpp", + "xlocinfo": "cpp", + "xstring": "cpp" }, "files.autoGuessEncoding": false, } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4eb5f4b..154c2e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,9 @@ set(PROJECT_SOURCES libzqt/zui/z_reg_table_list_box.cpp libzqt/zui/zqui.cpp + src/tab/step_motor_ctrl_tab.cpp + src/tab/module_opera_tab.cpp + a8000_protocol/api/apibasic/errorcode.cpp ) diff --git a/a8000_protocol b/a8000_protocol index 00a25ec..f41c0ba 160000 --- a/a8000_protocol +++ b/a8000_protocol @@ -1 +1 @@ -Subproject commit 00a25ecee58be21d0325226ea677a25f224e388a +Subproject commit f41c0ba7a5ca666a6038621f813001a530316a97 diff --git a/iflytop_canbus/iflytop_canbus_master.cpp b/iflytop_canbus/iflytop_canbus_master.cpp index 953722e..27b4d0a 100644 --- a/iflytop_canbus/iflytop_canbus_master.cpp +++ b/iflytop_canbus/iflytop_canbus_master.cpp @@ -104,6 +104,9 @@ void IflytopCanbusMaster::readreg(int32_t device_id, int32_t regaddr, int32_t *v callcmd1(device_id, kmodule_get_reg, regaddr, 10); *val = *(int32_t *)(&m_receipt_frame->data[0]); } + +int32_t IflytopCanbusMaster::getAck(int32_t index) { return *(int32_t *)(&m_receipt_frame->data[index]); } + void IflytopCanbusMaster::writereg(int32_t device_id, int32_t regaddr, int32_t val) { callcmd2(device_id, kmodule_set_reg, regaddr, val, 10); } void IflytopCanbusMaster::callcmd0(int32_t device_id, int32_t cmdid, int32_t overtime) { callcmd(device_id, cmdid, nullptr, 0, overtime); } diff --git a/iflytop_canbus/iflytop_canbus_master.hpp b/iflytop_canbus/iflytop_canbus_master.hpp index 4d7f193..dbf35ef 100644 --- a/iflytop_canbus/iflytop_canbus_master.hpp +++ b/iflytop_canbus/iflytop_canbus_master.hpp @@ -96,6 +96,8 @@ class IflytopCanbusMaster { void sendraw(int32_t fromId, uint8_t *data, size_t len); + int32_t getAck(int32_t index); + public: /*********************************************************************************************************************** * FUNC IMPL * @@ -104,7 +106,6 @@ class IflytopCanbusMaster { void readreg(int32_t device_id, int32_t regaddr, int32_t *val); void writereg(int32_t device_id, int32_t regaddr, int32_t val); - void step_motor_enable(int32_t device_id, int32_t enable) { callcmd1(device_id, kstep_motor_enable, enable); } void step_motor_easy_rotate(int32_t device_id, int32_t direction) { callcmd1(device_id, kstep_motor_easy_rotate, direction); } void step_motor_easy_move_by(int32_t device_id, int32_t distance) { callcmd1(device_id, kstep_motor_easy_move_by, distance); } @@ -136,3 +137,5 @@ class IflytopCanbusMaster { }; } // namespace iflytop + +#define ICM IflytopCanbusMaster::ins() \ No newline at end of file diff --git a/libzqt/zui/z_function_list_box.cpp b/libzqt/zui/z_function_list_box.cpp index 6184178..c78176c 100644 --- a/libzqt/zui/z_function_list_box.cpp +++ b/libzqt/zui/z_function_list_box.cpp @@ -69,9 +69,9 @@ void ZQFunctionListBox::newFunc(QString zh_name, QStringList params, std::functi try { ZQUI::ins()->instructionPreviewClear(); onButtonClick(params.size(), (const char **)args); - ZQUI::ins()->instructionPreviewShow("OK"); + ZQUI::ins()->ishow("OK"); } catch (const std::zexception &e) { - ZQUI::ins()->instructionPreviewShow("%s", e.what()); + ZQUI::ins()->ishow("%s", e.what()); } }); }); diff --git a/libzqt/zui/z_reg_table_list_box.cpp b/libzqt/zui/z_reg_table_list_box.cpp index 7f60a2c..499ff1e 100644 --- a/libzqt/zui/z_reg_table_list_box.cpp +++ b/libzqt/zui/z_reg_table_list_box.cpp @@ -155,7 +155,7 @@ void ZRegTableList::readAll() { ZRegItem *item = it.value(); int32_t val; try { - ZQUI::ins()->instructionPreviewShow("try read reg %s", it.key().toStdString().c_str()); + ZQUI::ins()->ishow("try read reg %s", it.key().toStdString().c_str()); m_readreg_fn(item->m_addr, &val); QString displayval = formatRegVal(val, item->m_flag); QLineEdit *displayiterm = item->m_val; @@ -164,7 +164,9 @@ void ZRegTableList::readAll() { displayiterm->setText(displayval); }); } catch (std::zexception &e) { - ZQUI::ins()->doinui([item]() { item->hide(); }); + ZQUI::ins()->doinui([item]() { + // item->hide(); + }); } } } diff --git a/libzqt/zui/zqui.cpp b/libzqt/zui/zqui.cpp index d3fe7ad..9870a06 100644 --- a/libzqt/zui/zqui.cpp +++ b/libzqt/zui/zqui.cpp @@ -11,14 +11,14 @@ ZQUI *ZQUI::ins() { return &instance; } -void ZQUI::instructionPreviewShow(const char *fmt, ...) { +void ZQUI::ishow(const char *fmt, ...) { va_list args; va_start(args, fmt); char buf[1024] = {0}; vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); QString text(buf); - if (m_instructionPreviewShow) m_instructionPreviewShow(text); + if (m_ishow) m_ishow(text); } void ZQUI::instructionPreviewClear() { diff --git a/libzqt/zui/zqui.hpp b/libzqt/zui/zqui.hpp index 70c772e..fe8c4a5 100644 --- a/libzqt/zui/zqui.hpp +++ b/libzqt/zui/zqui.hpp @@ -41,7 +41,7 @@ class ZQUI : public QObject { public: typedef std::function display_func_t; - std::function m_instructionPreviewShow; + std::function m_ishow; std::function m_reportPreviewShow; std::function m_blockDataUploadPreviewShow; std::function m_rawshow; @@ -53,13 +53,13 @@ class ZQUI : public QObject { void initialize(); void doinui(std::function dowhat); - void instructionPreviewShow(const char *fmt, ...); + void ishow(const char *fmt, ...); void reportPreviewShow(const char *fmt, ...); void blockDataUploadPreviewShow(const char *fmt, ...); void rawshow(const char *fmt, ...); void instructionPreviewClear(); - void setInstructionPreviewShow(std::function func) { m_instructionPreviewShow = func; } + void setishow(std::function func) { m_ishow = func; } void setReportPreviewShow(std::function func) { m_reportPreviewShow = func; } void setBlockDataUploadPreviewShow(std::function func) { m_blockDataUploadPreviewShow = func; } void setrawshow(std::function func) { m_rawshow = func; } diff --git a/src/main.cpp b/src/main.cpp index 277c72d..1181b22 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,7 +29,6 @@ #define TAG "Main" using namespace std; QT_CHARTS_USE_NAMESPACE - int main(int argc, char *argv[]) { WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d2df7d7..b603979 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -20,6 +20,9 @@ #include #include "zui/zui.hpp" +// +#include "tab/module_opera_tab.hpp" +#include "tab/step_motor_ctrl_tab.hpp" using namespace std; using namespace iflytop; @@ -33,20 +36,24 @@ static MainWindow *m_mainWindow; static QTDataChannel G_QTDataChannel; static QTDataChannel G_WaveDataChannel; -static int PublicState_DeviceIDVal = 0; +int PublicState_DeviceIDVal = 0; + +int getDeviceId() { return PublicState_DeviceIDVal; } + +Ui::MainWindow *main_ui; 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; -} +// static inline 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) { @@ -57,7 +64,7 @@ void MainWindow::doinui_slot(QFunction func) { * PreviewShow * ***********************************************************************************************************************/ -void MainWindow::instructionPreviewShow(QString text) { +void MainWindow::ishow(QString text) { QString info; info.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz ")); info.append(" |"); @@ -207,6 +214,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi /** * @brief QT初始化 */ + + main_ui = ui; + ui->setupUi(this); m_mainWindow = this; qRegisterMetaType("int32_t"); @@ -217,7 +227,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi 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()->setishow([this](QString text) { ishow(text); }); ZQUI::ins()->setReportPreviewShow([this](QString text) { reportPreviewShow(text); }); ZQUI::ins()->setrawshow([this](QString text) { rawshow(text); }); ZQUI::ins()->setInstructionPreviewClear([this]() { instructionPreviewClear(); }); @@ -236,12 +246,13 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi IflytopCanbusMaster::ins()->initialize(&G_QTDataChannel); IflytopCanbusMaster::ins()->regOnRawData([this](raw_data_type_t type, uint8_t *hex, uint32_t hexlen) { - if (type == kcmd_cmd) { - zcr_cmd_header_t *frame = (zcr_cmd_header_t *)hex; + zcr_cmd_header_t *frame = (zcr_cmd_header_t *)hex; + int cmdId = frame->cmdMainId * 256 + frame->cmdSubId; + int32_t *param = (int32_t *)frame->data; + int32_t mid = frame->subModuleid; + int32_t packetType = frame->packetType; - int cmdId = frame->cmdMainId * 8 + frame->cmdSubId; - int32_t *param = (int32_t *)frame->data; - int32_t mid = frame->subModuleid; + if (type == kcmd_cmd) { ZQUI::ins()->rawshow("[CMD ] %s", zhex2str(hex, hexlen).c_str()); if (cmdId == kmodule_set_reg) { ZQUI::ins()->rawshow(" [-->] module_set_reg %d(mid) %d(reg) %d(val)", mid, param[0], param[1]); @@ -252,7 +263,11 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi } } else if (type == kcmd_receipt) { - ZQUI::ins()->rawshow("[RECEIPT] %s", zhex2str(hex, hexlen).c_str()); + if (kptv2_error_ack == packetType) { + ZQUI::ins()->rawshow("[E-RECEI] %s(%d)", err::error2str(param[0]), param[0]); + } else { + ZQUI::ins()->rawshow("[ RECEI] %s", zhex2str(hex, hexlen).c_str()); + } } else if (type == kcmd_report) { ZQUI::ins()->rawshow("[REPORT ] %s", zhex2str(hex, hexlen).c_str()); } @@ -261,142 +276,16 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi MainWindow::~MainWindow() { delete ui; } void MainWindow::processException(const zexception &e) { // - ZQUI::ins()->instructionPreviewShow("%s", e.what()); + ZQUI::ins()->ishow("%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_step_motor_irun, atoi(args[0])); }); - box->newFunc("设置IHOLD", {"[0-31]"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->writereg(getDeviceId(), kreg_step_motor_ihold, atoi(args[0])); }); - box->newFunc("设置默认速度", {"velocity"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->writereg(getDeviceId(), kreg_step_motor_default_velocity, atoi(args[0])); }); - box->newFunc("激活配置", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_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); - // }); - // } - - { - // PublicState_DeviceIDVal = ui->PublicState_DeviceID->toPlainText().toInt(); - - ZQFunctionListBox *box = new ZQFunctionListBox(tab, "电机操作", 4); - box->newFunc("停止电机", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_stop(getDeviceId(), 0); }); - box->newFunc("激活配置", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_active_cfg(getDeviceId()); }); - box->newFunc("电机使能", {"enable"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_enable(getDeviceId(), atoi(args[0])); }); - box->newFunc("电机旋转", {"direction"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_easy_rotate(getDeviceId(), atoi(args[0])); }); - box->newFunc("电机移动", {"distance"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_easy_move_by(getDeviceId(), atoi(args[0])); }); - box->newFunc("电机移动到", {"position"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_easy_move_to(getDeviceId(), atoi(args[0])); }); - box->newFunc("电机归零", {}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_easy_move_to_zero(getDeviceId()); }); - box->newFunc("电机设置当前位置", {"position"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_motor_easy_set_current_pos(getDeviceId(), atoi(args[0])); }); - box->newFunc("电机移动到IO", {"io"}, [this](int argn, const char **args) { IflytopCanbusMaster::ins()->step_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; - }); - } - } + ModuleOperaTab::inst()->construct(ui->buttonTabWidget); - /*********************************************************************************************************************** - * 寄存器列表 * - ***********************************************************************************************************************/ - // { - // 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(); - // } + StepMotorCtrlTab::inst()->construct(ui->buttonTabWidget); + StepMotorCtrlTab::inst()->constructRegTab(ui->buttonTabWidget); } void MainWindow::on_PublicState_ConfirmKey_clicked() {} diff --git a/src/mainwindow.h b/src/mainwindow.h index 23d8ad3..5e77fff 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -99,7 +99,7 @@ signals: static void log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg); void instructionPreviewClear(); - void instructionPreviewShow(QString info); + void ishow(QString info); void reportPreviewShow(QString info); void blockDataUploadPreviewShow(QString info); void rawshow(QString info); diff --git a/src/qt_serial_datachannel.cpp b/src/qt_serial_datachannel.cpp index 6eadfba..5446e92 100644 --- a/src/qt_serial_datachannel.cpp +++ b/src/qt_serial_datachannel.cpp @@ -1,3 +1,8 @@ +#include +// +#include +// + #include "qt_serial_datachannel.hpp" // @@ -5,8 +10,6 @@ #include #include -#pragma comment(lib, "ws2_32.lib") - #include "logger.hpp" using namespace iflytop; @@ -127,11 +130,12 @@ bool QTDataChannel::send(const uint8_t *data, size_t len) { ZLOGI(TAG, "send %s", zhex2str(data, len).c_str()); DWORD dwBytesWrite = len; - BOOL bWriteStat = WriteFile(m_CommHandler, // 串口句柄 - (char *)data, // 数据首地址 - dwBytesWrite, // 要发送的数据字节数 - &dwBytesWrite, // DWORD*,用来接收返回成功发送的数据字节数 - NULL); // NULL为同步发送,OVERLAPPED*为异步发送 + // BOOL bWriteStat = + WriteFile(m_CommHandler, // 串口句柄 + (char *)data, // 数据首地址 + dwBytesWrite, // 要发送的数据字节数 + &dwBytesWrite, // DWORD*,用来接收返回成功发送的数据字节数 + NULL); // NULL为同步发送,OVERLAPPED*为异步发送 return dwBytesWrite; } void QTDataChannel::regRxListener(function cb) { m_rxcb = cb; } @@ -146,11 +150,12 @@ int QTDataChannel::com_receive(uint8_t *rxbuf, int rxbufsize) { // PurgeComm(m_CommHandler, PURGE_RXCLEAR); - DWORD wCount = rxbufsize; // 成功读取的数据字节数 - BOOL bReadStat = ReadFile(m_CommHandler, // 串口句柄 - rxbuf, // 数据首地址 - wCount, // 要读取的数据最大字节数 - &wCount, // DWORD*,用来接收返回成功读取的数据字节数 - NULL); + DWORD wCount = rxbufsize; // 成功读取的数据字节数 + // BOOL bReadStat = + ReadFile(m_CommHandler, // 串口句柄 + rxbuf, // 数据首地址 + wCount, // 要读取的数据最大字节数 + &wCount, // DWORD*,用来接收返回成功读取的数据字节数 + NULL); return wCount; } diff --git a/src/qt_serial_datachannel.hpp b/src/qt_serial_datachannel.hpp index 2c71827..a057353 100644 --- a/src/qt_serial_datachannel.hpp +++ b/src/qt_serial_datachannel.hpp @@ -13,9 +13,8 @@ #include // -#include -// -#include + + // #include #include diff --git a/src/tab/module_opera_tab.cpp b/src/tab/module_opera_tab.cpp new file mode 100644 index 0000000..20e9793 --- /dev/null +++ b/src/tab/module_opera_tab.cpp @@ -0,0 +1,52 @@ +#include "module_opera_tab.hpp" + +#include "iflytop_canbus/iflytop_canbus_master.hpp" +#include "logger.hpp" +#include "qt_serial_datachannel.hpp" +#include "zexception.hpp" +#include "zui/z_function_list_box.hpp" +#include "zui/zq_vtab_page.hpp" +// +#include "./mainwindow.h" +#include "zui\zqui.hpp" +// +using namespace iflytop; +using namespace std; + +extern Ui::MainWindow *main_ui; + +extern int getDeviceId(); + + +ModuleOperaTab *ModuleOperaTab::inst() { + static ModuleOperaTab *ins = new ModuleOperaTab(); + return ins; +} + +void ModuleOperaTab::construct(QTabWidget *fathertab) { + /*********************************************************************************************************************** + * 模块操作 * + ***********************************************************************************************************************/ + // main_ui-> + ZQVTabPage *tab = new ZQVTabPage(fathertab, "模块操作"); + + { + 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()->ishow("module :%d isOnline", i); + } catch (const zexception &e) { + if (e.ecode() != ke_overtime) { + ZQUI::ins()->ishow("%s", e.what()); + break; + } + } + } + ZQUI::ins()->ishow("ScanModuleEnd"); + }); + } + + tab->addSpacer(); +} diff --git a/src/tab/module_opera_tab.hpp b/src/tab/module_opera_tab.hpp new file mode 100644 index 0000000..aa81294 --- /dev/null +++ b/src/tab/module_opera_tab.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include + +namespace iflytop { +using namespace std; + +class ModuleOperaTab { + public: + static ModuleOperaTab* inst(); + + void construct(QTabWidget *fathertab); +}; +} // namespace iflytop diff --git a/src/tab/step_motor_ctrl_tab.cpp b/src/tab/step_motor_ctrl_tab.cpp new file mode 100644 index 0000000..9338224 --- /dev/null +++ b/src/tab/step_motor_ctrl_tab.cpp @@ -0,0 +1,130 @@ +#include "step_motor_ctrl_tab.hpp" + +#include "iflytop_canbus/iflytop_canbus_master.hpp" +#include "logger.hpp" +#include "qt_serial_datachannel.hpp" +#include "zexception.hpp" +#include "zui/z_function_list_box.hpp" +#include "zui/z_reg_table_list_box.hpp" +#include "zui/zq_vtab_page.hpp" +// +#include "./mainwindow.h" +#include "zui\zqui.hpp" +// +using namespace iflytop; +using namespace std; + +extern Ui::MainWindow *main_ui; + +extern int getDeviceId(); + +StepMotorCtrlTab *StepMotorCtrlTab::inst() { + static StepMotorCtrlTab *ins = new StepMotorCtrlTab(); + return ins; +} + +void StepMotorCtrlTab::construct(QTabWidget *fathertab) { + /*********************************************************************************************************************** + * 模块操作 * + ***********************************************************************************************************************/ + // kstep_motor_enable = NEW_CMDID(101, 1), // para:{1}, ack:{} + // kstep_motor_read_pos = NEW_CMDID(101, 2), // para:{}, ack:{4} + // kstep_motor_easy_rotate = NEW_CMDID(101, 3), // para:{4}, ack:{} + // kstep_motor_easy_move_by = NEW_CMDID(101, 4), // para:{4}, ack:{} + // kstep_motor_easy_move_to = NEW_CMDID(101, 5), // para:{4}, ack:{} + // kstep_motor_easy_move_to_zero = NEW_CMDID(101, 6), // para:{1}, ack:{} + // kstep_motor_easy_set_current_pos = NEW_CMDID(101, 7), // para:{4}, ack:{} + // kstep_motor_easy_move_to_io = NEW_CMDID(101, 8), // para:{4,4}, ack:{} + // kstep_motor_active_cfg = NEW_CMDID(101, 9), // para:{4,4}, ack:{} + // kstep_motor_stop = NEW_CMDID(101, 10), // para:{4}, ack:{} + // kstep_motor_read_io_state = NEW_CMDID(101, 11), // para:{4}, ack:{4} + + ZQVTabPage *tab = new ZQVTabPage(fathertab, "步进电机"); + + { + ZQFunctionListBox *box = new ZQFunctionListBox(tab, "方法", 4); + // kmodule_get_status = CMDID(1, 4), // para:{}, ack:{4} + // kmodule_stop = CMDID(1, 1), // para:{}, ack:{} + // kmodule_get_error = CMDID(1, 10), // para:{}, ack:{1} + // kmodule_clear_error = CMDID(1, 11), // para:{}, ack:{} + // kmodule_active_cfg = CMDID(1, 16), // para:{}, ack:{} + + box->newFunc("M-获取状态", {}, [this](int argn, const char **args) { + int32_t status = 0; + ICM->callcmd0(getDeviceId(), kmodule_get_status); + ZQUI::ins()->ishow("Status:%d", ICM->getAck(0)); + }); + box->newFunc("M-停止", {}, [this](int argn, const char **args) { ICM->callcmd0(getDeviceId(), kmodule_stop); }); + box->newFunc("M-获取错误", {}, [this](int argn, const char **args) { + ICM->callcmd0(getDeviceId(), kmodule_get_error); + ZQUI::ins()->ishow("Error:%d", ICM->getAck(0)); + }); + box->newFunc("M-清除错误", {}, [this](int argn, const char **args) { ICM->callcmd0(getDeviceId(), kmodule_clear_error); }); + + box->newFunc("使能", {"enable"}, [this](int argn, const char **args) { ICM->step_motor_enable(getDeviceId(), atoi(args[0])); }); + box->newFunc("读取位置", {}, [this](int argn, const char **args) { + int32_t pos = 0; + ICM->step_motor_read_pos(getDeviceId(), &pos); + ZQUI::ins()->ishow("Pos:%d", pos); + }); + box->newFunc("旋转", {"direction"}, [this](int argn, const char **args) { ICM->step_motor_easy_rotate(getDeviceId(), atoi(args[0])); }); + box->newFunc("相对移动", {"distance"}, [this](int argn, const char **args) { ICM->step_motor_easy_move_by(getDeviceId(), atoi(args[0])); }); + box->newFunc("移动到", {"position"}, [this](int argn, const char **args) { ICM->step_motor_easy_move_to(getDeviceId(), atoi(args[0])); }); + box->newFunc("归零", {}, [this](int argn, const char **args) { ICM->step_motor_easy_move_to_zero(getDeviceId()); }); + box->newFunc("设置当前位置", {"position"}, [this](int argn, const char **args) { ICM->step_motor_easy_set_current_pos(getDeviceId(), atoi(args[0])); }); + box->newFunc("移动到IO", {"ioindex"}, [this](int argn, const char **args) { ICM->step_motor_easy_move_to_io(getDeviceId(), atoi(args[0])); }); + box->newFunc("激活配置", {}, [this](int argn, const char **args) { ICM->step_motor_active_cfg(getDeviceId()); }); + box->newFunc("停止", {}, [this](int argn, const char **args) { ICM->step_motor_stop(getDeviceId(), 0); }); + box->newFunc("读取IO状态", {"ioindex"}, [this](int argn, const char **args) { + int32_t io = 0; + ICM->step_motor_read_io_state(getDeviceId(), atoi(args[0]), &io); + ZQUI::ins()->ishow("IO:%d", io); + }); + } + tab->addSpacer(); +} + +void StepMotorCtrlTab::constructRegTab(QTabWidget *fathertab) { + ZQVTabPage *tab = new ZQVTabPage(fathertab, "步进电机-寄存器"); + ZRegTableList *tableBox = new ZRegTableList(tab, "寄存器操作"); + tableBox->initializeRegOperation( + [this](int32_t add, int32_t val) { // + ICM->writereg(getDeviceId(), add, val); + return true; + }, + [this](int32_t add, int32_t *val) { // + ICM->readreg(getDeviceId(), add, val); + return true; + }); + + tableBox->addReg("mod-version", kreg_module_version, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("mod-type", kreg_module_type, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("mod-status", kreg_module_status, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("mod-errorcode", kreg_module_errorcode, ZRegItem::krw | ZRegItem::kdec); + + tableBox->addReg("motor_pos", kreg_step_motor_pos, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_dpos", kreg_step_motor_dpos, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_shift", kreg_step_motor_shift, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_shaft", kreg_step_motor_shaft, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_one_circle_pulse", kreg_step_motor_one_circle_pulse, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_one_circle_pulse_denominator", kreg_step_motor_one_circle_pulse_denominator, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_default_velocity", kreg_step_motor_default_velocity, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_default_acc", kreg_step_motor_default_acc, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_default_dec", kreg_step_motor_default_dec, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_default_break_dec", kreg_step_motor_default_break_dec, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_ihold", kreg_step_motor_ihold, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_irun", kreg_step_motor_irun, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_iholddelay", kreg_step_motor_iholddelay, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_iglobalscaler", kreg_step_motor_iglobalscaler, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_run_to_zero_max_d", kreg_step_motor_run_to_zero_max_d, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_look_zero_edge_max_d", kreg_step_motor_look_zero_edge_max_d, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_run_to_zero_speed", kreg_step_motor_run_to_zero_speed, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_run_to_zero_dec", kreg_step_motor_run_to_zero_dec, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_look_zero_edge_speed", kreg_step_motor_look_zero_edge_speed, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_look_zero_edge_dec", kreg_step_motor_look_zero_edge_dec, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_max_d", kreg_step_motor_max_d, ZRegItem::krw | ZRegItem::kdec); + tableBox->addReg("motor_min_d", kreg_step_motor_min_d, ZRegItem::krw | ZRegItem::kdec); + + tableBox->addSpacer(); + tab->addSpacer(); +} diff --git a/src/tab/step_motor_ctrl_tab.hpp b/src/tab/step_motor_ctrl_tab.hpp new file mode 100644 index 0000000..c86afe3 --- /dev/null +++ b/src/tab/step_motor_ctrl_tab.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include + +namespace iflytop { +using namespace std; + +class StepMotorCtrlTab { + public: + static StepMotorCtrlTab *inst(); + + void construct(QTabWidget *fathertab); + void constructRegTab(QTabWidget *fathertab); +}; +} // namespace iflytop