diff --git a/dep/zlinuxcomponents b/dep/zlinuxcomponents index f423279..f34921f 160000 --- a/dep/zlinuxcomponents +++ b/dep/zlinuxcomponents @@ -1 +1 @@ -Subproject commit f4232794415264a66fb9d3841eb3e9c23b9951bb +Subproject commit f34921f0c541f97c487dd39c07f75a64308f0606 diff --git a/src/service/a8000_scirpt_processer.cpp b/src/service/a8000_scirpt_processer.cpp index 603e2b8..1f21bf9 100644 --- a/src/service/a8000_scirpt_processer.cpp +++ b/src/service/a8000_scirpt_processer.cpp @@ -39,8 +39,10 @@ void A8000ScriptProcesser::initialize(shared_ptr if m_iflytopCanProtocolControler = iflytopCanProtocolControler; }; -bool A8000ScriptProcesser::isWorking() { return m_scriptProcesser->isWorking(); } -void A8000ScriptProcesser::executeScript(string script) { m_scriptProcesser->executeScript(script); } +bool A8000ScriptProcesser::isWorking() { return m_scriptProcesser->isWorking(); } +void A8000ScriptProcesser::executeScript(string script) { m_scriptProcesser->executeScript(script); } +void A8000ScriptProcesser::executeCommandBlock(string script, string& scriptresult) { m_scriptProcesser->executeCommand(script, scriptresult); } + void A8000ScriptProcesser::stopScript() { m_scriptProcesser->stopScript(); } int32_t A8000ScriptProcesser::sendCanPacket(shared_ptr context, shared_ptr txpacket) { string cmd = context->cmd; @@ -161,9 +163,9 @@ void A8000ScriptProcesser::sleep_ms(shared_ptr context void A8000ScriptProcesser::step_motor_wait_for_idle(shared_ptr context, int32_t targetStepMotorId) { int32_t state = 0; step_motor_is_idle(context, targetStepMotorId, state); - ThisThread thisThread; + // ThisThread thisThread; while (state != 0) { - thisThread.sleepForMs(300); + usleep(300 * 1000); step_motor_is_idle(context, targetStepMotorId, state); logger->info("step_motor({}) wait_for_idle:{}", targetStepMotorId, state); } @@ -205,11 +207,36 @@ void A8000ScriptProcesser::tube_rotate_zero(shared_ptr writeReg(cpcfg.m_deviceId, cpcfg.m_ctrlPointId, 1, context); } -#define CHECK_CMD_PARAMETER(expectParaNum) \ - if (cmdandvar.size() - 1 != expectParaNum) { \ +#define SCARA_REG_ACT_CTRL (0) // 0:停止 1:运行 +#define SCARA_REG_ACT_SET_TARGET_XY (1) // +#define SCARA_REG_ACT_LOCK (2) // +#define SCARA_REG_ACT_CLEAR_EXCEPTION (5) // +#define SCARA_REG_STATE_STATE (11) // +#define SCARA_REG_STATE_EXCEPTION (12) // +#define SCARA_REG_STATE_NOWXY (13) // +#define SCARA_REG_CFG_TARGET_X (20) // +#define SCARA_REG_CFG_TARGET_Y (21) // + +void A8000ScriptProcesser::scara_move_to(shared_ptr context, int scaraid, int32_t x, int32_t y) { + uint32_t xy = ((uint32_t)x & 0xffff) | ((uint32_t)y & 0xffff) << 16; + writeReg(131, 11200 + SCARA_REG_ACT_SET_TARGET_XY, xy, context); +} +void A8000ScriptProcesser::scara_wait_for_idle(shared_ptr context, int scaraid) { + int32_t state = 0; + readReg(131, 11200 + SCARA_REG_STATE_STATE, state, context); + ThisThread thisThread; + while (state != 0) { + thisThread.sleepForMs(300); + readReg(131, 11200 + SCARA_REG_STATE_STATE, state, context); + logger->info("scara({}) wait_for_idle:{}", scaraid, state); + } +} + +#define CHECK_CMD_PARAMETER(expectParaNum) \ + if (cmdandvar.size() - 1 != expectParaNum) { \ context->printfInfo = fmt::format("error:do {} error,parameter num error,{}", cmd, cmdandvar.size()); \ - logger->error("{}", context->printfInfo); \ - return; \ + logger->error("{}", context->printfInfo); \ + return; \ } void A8000ScriptProcesser::docmd(shared_ptr context) { @@ -264,8 +291,7 @@ void A8000ScriptProcesser::docmd(shared_ptr context) { } else if (cmd == "step_motor_wait_for_idle") { CHECK_CMD_PARAMETER(1); step_motor_wait_for_idle(context, context->getvarInt(1)); - } - else if (cmd == "duoji_move_to") { + } else if (cmd == "duoji_move_to") { CHECK_CMD_PARAMETER(2); duoji_move_to(context, context->getvarInt(1), context->getvarInt(2)); } else if (cmd == "duoji_stop") { @@ -281,7 +307,15 @@ void A8000ScriptProcesser::docmd(shared_ptr context) { } else if (cmd == "tube_rotate_zero") { CHECK_CMD_PARAMETER(1); tube_rotate_zero(context, context->getvarInt(1)); + } else if (cmd == "scara_move_to") { + CHECK_CMD_PARAMETER(2); + scara_move_to(context, context->getvarInt(1), context->getvarInt(2), context->getvarInt(3)); + } else if (cmd == "scara_wait_for_idle") { + CHECK_CMD_PARAMETER(1); + scara_wait_for_idle(context, context->getvarInt(1)); } + // + // } void A8000ScriptProcesser::readReg(int32_t targetId, int32_t ctrlPointAdd, int32_t& val, shared_ptr context) { diff --git a/src/service/a8000_scirpt_processer.hpp b/src/service/a8000_scirpt_processer.hpp index 9cd6d71..aeb1560 100644 --- a/src/service/a8000_scirpt_processer.hpp +++ b/src/service/a8000_scirpt_processer.hpp @@ -69,6 +69,7 @@ class A8000ScriptProcesser : public enable_shared_from_this context, int32_t targetStepMotorId, int32_t direction); @@ -125,6 +126,9 @@ class A8000ScriptProcesser : public enable_shared_from_this context, int32_t targetStepMotorId, int32_t direction); void tube_rotate_zero(shared_ptr context, int32_t targetStepMotorId); + void scara_move_to(shared_ptr context, int scaraid, int32_t x, int32_t y); + void scara_wait_for_idle(shared_ptr context, int scaraid); + private: CtrlPointConfig getStepMotorConfig(int32_t targetStepMotorId, int32_t addoff); CtrlPointConfig getDuoJiConfig(int32_t targetStepMotorId, int32_t addoff); // 舵机 diff --git a/src/service/main_control_service.cpp b/src/service/main_control_service.cpp index c94d14d..b114489 100644 --- a/src/service/main_control_service.cpp +++ b/src/service/main_control_service.cpp @@ -23,6 +23,7 @@ void MainControlService::initialize() { m_restfulServer->regAPI("/api1/reaction_config_card_info/read_state", RESTFUL_SERVER_BIND(MainControlService::reactionConfigCardInfo_read_state)); m_restfulServer->regAPI("/api1/script_processer/doscript", RESTFUL_SERVER_BIND(MainControlService::doscript)); + m_restfulServer->regAPI("/api1/script_processer/doscriptblock", RESTFUL_SERVER_BIND(MainControlService::doscriptblock)); m_restfulServer->regAPI("/api1/script_processer/stopscript", RESTFUL_SERVER_BIND(MainControlService::stopscript)); m_restfulServer->start(20000, 20001, "0.0.0.0"); @@ -99,6 +100,12 @@ HttpResponsePtr MainControlService::doscript(HttpRequestPtr httpreq, shared_ptr< return std::make_shared(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script success"); } +HttpResponsePtr MainControlService::doscriptblock(HttpRequestPtr httpreq, shared_ptr context, std::shared_ptr conn) { + string result; + m_a8000_script_processer->executeCommandBlock(httpreq->body, result); + return std::make_shared(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), result); +} + HttpResponsePtr MainControlService::stopscript(HttpRequestPtr, shared_ptr, std::shared_ptr) { m_a8000_script_processer->stopScript(); return std::make_shared(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "stop script success"); diff --git a/src/service/main_control_service.hpp b/src/service/main_control_service.hpp index 7b96f07..45c90d5 100644 --- a/src/service/main_control_service.hpp +++ b/src/service/main_control_service.hpp @@ -76,6 +76,7 @@ class MainControlService : public enable_shared_from_this { HttpResponsePtr hello_world(HttpRequestPtr, shared_ptr, std::shared_ptr); // HttpResponsePtr doscript(HttpRequestPtr, shared_ptr, std::shared_ptr); + HttpResponsePtr doscriptblock(HttpRequestPtr, shared_ptr, std::shared_ptr); HttpResponsePtr stopscript(HttpRequestPtr, shared_ptr, std::shared_ptr); void createReactionConfigCardInfoReportAndSend(); diff --git a/src/service/scirpt_processer.cpp b/src/service/scirpt_processer.cpp index 59b8a0e..4438a17 100644 --- a/src/service/scirpt_processer.cpp +++ b/src/service/scirpt_processer.cpp @@ -29,9 +29,9 @@ void ScriptProcesser::executeScript(string script) { logger->info("script_thread start"); // ZLOGI(TAG, "cmdstr1: %s %d %d", cmdstr, strlen(cmdstr), m_uart.getRxDataLen()); - char* cmdstr = strdup(m_script.c_str()); - int cmdstr_len = strlen(cmdstr); - + char* cmdstr = strdup(m_script.c_str()); + int cmdstr_len = strlen(cmdstr); + string resultstr; for (size_t i = 0; i < cmdstr_len; i++) { if (cmdstr[i] == '\r' || cmdstr[i] == '\n') { cmdstr[i] = '\0'; @@ -41,7 +41,7 @@ void ScriptProcesser::executeScript(string script) { for (size_t i = 0; i < cmdstr_len; i++) { if (cmdstr[i] != '\0') { string cmd = string(&cmdstr[i]); - processEachCommand(cmd); + _executeCommand(cmd, resultstr); i = cmd.size() + i; } } @@ -62,10 +62,11 @@ static vector splitString(const string& str, char delimiter) { } void ScriptProcesser::processEachCommand(string cmd) { // string result; - executeCommand(cmd, result); + _executeCommand(cmd, result); } -void ScriptProcesser::executeCommand(string cmd, string& result) { // +void ScriptProcesser::_executeCommand(string cmd, string& result) { + // if (cmd.empty()) { return; } @@ -85,6 +86,16 @@ void ScriptProcesser::executeCommand(string cmd, string& result) { // logger->info("do: {} ==> {}", cmd, context->printfInfo); } +void ScriptProcesser::executeCommand(string cmd, string& result) { // + unique_ptr thread; + thread.reset(new Thread("executeCommand", [this, cmd, &result]() { _executeCommand(cmd, result); })); + while (!thread->isWaitingForJoin()) { + usleep(100 * 1000); + } + thread->join(); + return; +} + void ScriptProcesser::stopScript() { if (m_script_thread) { m_script_thread->join(); diff --git a/src/service/scirpt_processer.hpp b/src/service/scirpt_processer.hpp index c52ae20..4e44523 100644 --- a/src/service/scirpt_processer.hpp +++ b/src/service/scirpt_processer.hpp @@ -64,10 +64,12 @@ class ScriptProcesser : public enable_shared_from_this { ScriptProcesser(){}; bool isWorking(); void executeScript(string script); - void executeCommand(string cmd, string &result); + void executeCommand(string cmd, string& result); void stopScript(); private: + void _executeCommand(string cmd, string& result); + void processEachCommand(string cmd); }; } // namespace iflytop \ No newline at end of file