Browse Source

update

master
zhaohe 2 years ago
parent
commit
e3640d3205
  1. 2
      dep/zlinuxcomponents
  2. 54
      src/service/a8000_scirpt_processer.cpp
  3. 6
      src/service/a8000_scirpt_processer.hpp
  4. 7
      src/service/main_control_service.cpp
  5. 1
      src/service/main_control_service.hpp
  6. 23
      src/service/scirpt_processer.cpp
  7. 4
      src/service/scirpt_processer.hpp

2
dep/zlinuxcomponents

@ -1 +1 @@
Subproject commit f4232794415264a66fb9d3841eb3e9c23b9951bb
Subproject commit f34921f0c541f97c487dd39c07f75a64308f0606

54
src/service/a8000_scirpt_processer.cpp

@ -39,8 +39,10 @@ void A8000ScriptProcesser::initialize(shared_ptr<IflytopCanProtocolControler> 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<ScriptProcesser::Context> context, shared_ptr<icps::Packet> txpacket) {
string cmd = context->cmd;
@ -161,9 +163,9 @@ void A8000ScriptProcesser::sleep_ms(shared_ptr<ScriptProcesser::Context> context
void A8000ScriptProcesser::step_motor_wait_for_idle(shared_ptr<ScriptProcesser::Context> 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<ScriptProcesser::Context>
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<ScriptProcesser::Context> 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<ScriptProcesser::Context> 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<ScriptProcesser::Context> context) {
@ -264,8 +291,7 @@ void A8000ScriptProcesser::docmd(shared_ptr<ScriptProcesser::Context> 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<ScriptProcesser::Context> 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<ScriptProcesser::Context> context) {

6
src/service/a8000_scirpt_processer.hpp

@ -69,6 +69,7 @@ class A8000ScriptProcesser : public enable_shared_from_this<A8000ScriptProcesser
bool isWorking();
void executeScript(string script);
void executeCommandBlock(string script, string &scriptresult);
void stopScript();
private:
@ -103,7 +104,7 @@ class A8000ScriptProcesser : public enable_shared_from_this<A8000ScriptProcesser
* 8.
* 9.
*
*
* 12. -x,y
*/
void step_motor_rotate(shared_ptr<ScriptProcesser::Context> context, int32_t targetStepMotorId, int32_t direction);
@ -125,6 +126,9 @@ class A8000ScriptProcesser : public enable_shared_from_this<A8000ScriptProcesser
void tube_rotate(shared_ptr<ScriptProcesser::Context> context, int32_t targetStepMotorId, int32_t direction);
void tube_rotate_zero(shared_ptr<ScriptProcesser::Context> context, int32_t targetStepMotorId);
void scara_move_to(shared_ptr<ScriptProcesser::Context> context, int scaraid, int32_t x, int32_t y);
void scara_wait_for_idle(shared_ptr<ScriptProcesser::Context> context, int scaraid);
private:
CtrlPointConfig getStepMotorConfig(int32_t targetStepMotorId, int32_t addoff);
CtrlPointConfig getDuoJiConfig(int32_t targetStepMotorId, int32_t addoff); // 舵机

7
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<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "do script success");
}
HttpResponsePtr MainControlService::doscriptblock(HttpRequestPtr httpreq, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState> conn) {
string result;
m_a8000_script_processer->executeCommandBlock(httpreq->body, result);
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), result);
}
HttpResponsePtr MainControlService::stopscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>) {
m_a8000_script_processer->stopScript();
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "stop script success");

1
src/service/main_control_service.hpp

@ -76,6 +76,7 @@ class MainControlService : public enable_shared_from_this<MainControlService> {
HttpResponsePtr hello_world(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>);
//
HttpResponsePtr doscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>);
HttpResponsePtr doscriptblock(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>);
HttpResponsePtr stopscript(HttpRequestPtr, shared_ptr<RestfulServer::Context>, std::shared_ptr<ConnectionState>);
void createReactionConfigCardInfoReportAndSend();

23
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<string> 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;
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();

4
src/service/scirpt_processer.hpp

@ -64,10 +64,12 @@ class ScriptProcesser : public enable_shared_from_this<ScriptProcesser> {
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
Loading…
Cancel
Save