Browse Source

fix some bug

master
zhaohe 1 year ago
parent
commit
37416e33b3
  1. 202
      components/pipette_module/pipette_ctrl_module_v2.cpp
  2. 2
      components/pipette_module/pipette_ctrl_module_v2.hpp
  3. 466
      components/sensors/smtp2/smtp2.cpp
  4. 147
      components/sensors/smtp2/smtp2.hpp
  5. 41
      components/sensors/smtp2_v2/smtp2_v2.cpp
  6. 6
      components/sensors/smtp2_v2/smtp2_v2.hpp

202
components/pipette_module/pipette_ctrl_module_v2.cpp

@ -1,4 +1,5 @@
#include "pipette_ctrl_module_v2.hpp" #include "pipette_ctrl_module_v2.hpp"
#include "sdk\components\zcancmder\protocol_event_bus_sender.hpp" #include "sdk\components\zcancmder\protocol_event_bus_sender.hpp"
using namespace iflytop; using namespace iflytop;
@ -19,12 +20,11 @@ using namespace iflytop;
if (ecode != 0) { \ if (ecode != 0) { \
ZLOGE(TAG, "do %s fail, error %s(%d)", #func, err::error2str(ecode), ecode); \ ZLOGE(TAG, "do %s fail, error %s(%d)", #func, err::error2str(ecode), ecode); \
creg.module_errorcode = ecode; \ creg.module_errorcode = ecode; \
creg.m_module_status = 2; \
return; \ return; \
} \ } \
} }
void PipetteModule::initialize(int32_t id, config_t *config, hardward_config_t *hardwaredcfg) { //
void PipetteModule::initialize(int32_t id, config_t *config, StepMotorCtrlModule *zmotor, hardward_config_t *hardwaredcfg) { //
ZASSERT(config != nullptr); ZASSERT(config != nullptr);
ZASSERT(hardwaredcfg != nullptr); ZASSERT(hardwaredcfg != nullptr);
m_id = id; m_id = id;
@ -38,8 +38,11 @@ void PipetteModule::initialize(int32_t id, config_t *config, hardward_config_t *
m_config.pump_vstart = 0; m_config.pump_vstart = 0;
m_config.pump_vstop = 900; m_config.pump_vstop = 900;
m_config.pump_vmax = 1400; m_config.pump_vmax = 1400;
m_config.lld_velocity = 100;
m_config.lld_velocity = 50;
m_config.data_sample_period = 100; // 100ms m_config.data_sample_period = 100; // 100ms
ZASSERT(zmotor);
m_zmotor = zmotor;
} }
int32_t PipetteModule::getid(int32_t *id) { int32_t PipetteModule::getid(int32_t *id) {
@ -114,6 +117,7 @@ int32_t PipetteModule::pipette_ctrl_move_to_ul(int32_t ul) {
}; };
int32_t PipetteModule::pipette_clld_test(int32_t zdpos) { int32_t PipetteModule::pipette_clld_test(int32_t zdpos) {
ZLOGI(TAG, "pipette_clld_test %d", zdpos);
int32_t motor_status; int32_t motor_status;
m_zmotor->module_get_status(&motor_status); m_zmotor->module_get_status(&motor_status);
if (motor_status != 0) { if (motor_status != 0) {
@ -140,105 +144,127 @@ int32_t PipetteModule::pipette_plld_test(int32_t zdpos) {
int32_t PipetteModule::do_pipette_ctrl_init_device() { int32_t PipetteModule::do_pipette_ctrl_init_device() {
creg.m_module_status = 1; creg.m_module_status = 1;
m_thread.stop(); m_thread.stop();
m_thread.start([this]() { //
DO_BEFORE_LOOP(befor_run());
DO_BEFORE_LOOP(m_smtp2.pump_init());
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
int32_t isbusy = 0;
m_smtp2.pump_get_state(&isbusy);
if (isbusy == 0) break;
m_thread.sleep(10);
}
after_run();
});
m_thread.start(
[this]() { //
DO_BEFORE_LOOP(befor_run());
DO_BEFORE_LOOP(m_smtp2.pump_init());
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
int32_t isbusy = 0;
m_smtp2.pump_get_state(&isbusy);
if (isbusy == 0) break;
m_thread.sleep(10);
}
},
[this]() { // exit fn
ZLOGI(TAG, "do_pipette_ctrl_put_tip finish");
after_run();
} //
);
return 0;
} }
int32_t PipetteModule::do_pipette_ctrl_put_tip() { int32_t PipetteModule::do_pipette_ctrl_put_tip() {
m_thread.stop(); m_thread.stop();
creg.m_module_status = 1; creg.m_module_status = 1;
m_thread.start([this]() { //
DO_BEFORE_LOOP(befor_run());
DO_BEFORE_LOOP(m_smtp2.pump_put_tip());
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
int32_t isbusy = 0;
m_smtp2.pump_get_state(&isbusy);
if (isbusy == 0) break;
m_thread.sleep(10);
}
after_run();
});
m_thread.start(
[this]() { //
DO_BEFORE_LOOP(befor_run());
DO_BEFORE_LOOP(m_smtp2.pump_put_tip());
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
int32_t isbusy = 0;
m_smtp2.pump_get_state(&isbusy);
if (isbusy == 0) break;
m_thread.sleep(10);
}
},
[this]() { // exit fn
ZLOGI(TAG, "do_pipette_ctrl_put_tip finish");
after_run();
} //
);
return 0; return 0;
} }
int32_t PipetteModule::do_pipette_ctrl_move_to_ul(int32_t ul) { int32_t PipetteModule::do_pipette_ctrl_move_to_ul(int32_t ul) {
m_thread.stop(); m_thread.stop();
creg.m_module_status = 1; creg.m_module_status = 1;
m_thread.start([this, ul]() { //
DO_BEFORE_LOOP(befor_run());
DO_BEFORE_LOOP(m_smtp2.pump_move_to_ul(ul));
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
int32_t isbusy = 0;
m_smtp2.pump_get_state(&isbusy);
if (isbusy == 0) break;
m_thread.sleep(10);
}
after_run();
});
m_thread.start(
[this, ul]() { //
DO_BEFORE_LOOP(befor_run());
DO_BEFORE_LOOP(m_smtp2.pump_move_to_ul(ul));
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
int32_t isbusy = 0;
m_smtp2.pump_get_state(&isbusy);
if (isbusy == 0) break;
m_thread.sleep(10);
}
},
[this]() { // exit fn
ZLOGI(TAG, "do_pipette_ctrl_move_to_ul finish");
after_run();
} //
);
return 0; return 0;
} }
int32_t PipetteModule::do_pipette_clld_test(int32_t zdpos) { int32_t PipetteModule::do_pipette_clld_test(int32_t zdpos) {
m_thread.stop(); m_thread.stop();
creg.m_module_status = 1; creg.m_module_status = 1;
m_thread.start([this, zdpos]() { //
DO_BEFORE_LOOP(befor_run());
m_zmotor->module_active_cfg();
m_zmotor->step_motor_enable(1);
auto submotor = m_zmotor->getMotor();
submotor->moveBy(zdpos, m_config.lld_velocity);
int32_t zmotor_start_pos = submotor->getXACTUAL();
capturedata_num = 0;
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
bool motorstoped = false;
int32_t motorerror = 0;
int32_t motorpos = 0;
int32_t capacitance = 0;
m_smtp2.pump_get_capacitance(&capacitance);
motorpos = submotor->getXACTUAL() - zmotor_start_pos;
submotor->readMotorState(&motorstoped, &motorerror);
if (motorerror != 0) {
creg.module_errorcode = motorerror;
break;
}
if (motorstoped) {
break;
}
push_snesor_sample_data(motorpos, capacitance, 0);
m_thread.sleep(m_config.data_sample_period);
}
after_run();
});
m_thread.start(
[this, zdpos]() { //
DO_BEFORE_LOOP(befor_run());
m_zmotor->module_active_cfg();
m_zmotor->step_motor_enable(1);
auto submotor = m_zmotor->getMotor();
submotor->moveBy(zdpos, m_config.lld_velocity);
int32_t zmotor_start_pos = submotor->getXACTUAL();
capturedata_num = 0;
while (true) {
if (!check_when_run()) break;
if (m_thread.getExitFlag()) break;
bool motorstoped = false;
int32_t motorerror = 0;
int32_t motorpos = 0;
int32_t capacitance = 0;
m_smtp2.pump_get_capacitance(&capacitance);
motorpos = submotor->getXACTUAL() - zmotor_start_pos;
submotor->readMotorState(&motorstoped, &motorerror);
if (motorerror != 0) {
creg.module_errorcode = motorerror;
break;
}
if (motorstoped) {
break;
}
push_snesor_sample_data(motorpos, capacitance, 0);
m_thread.sleep(m_config.data_sample_period);
}
},
[this]() { // exit fn
ZLOGI(TAG, "do_pipette_clld_test finish");
after_run();
} //
);
return 0; return 0;
} }
int32_t PipetteModule::do_pipette_plld_test(int32_t zdpos) { return 0; } int32_t PipetteModule::do_pipette_plld_test(int32_t zdpos) { return 0; }

2
components/pipette_module/pipette_ctrl_module_v2.hpp

@ -61,7 +61,7 @@ class PipetteModule : public ZIModule, public ZIPipetteCtrlModule {
int32_t capturedata_num = 0; int32_t capturedata_num = 0;
public: public:
void initialize(int32_t id, config_t *config, hardward_config_t *cfg);
void initialize(int32_t id, config_t *config, StepMotorCtrlModule *zmotor, hardward_config_t *hardwaredcfg);
virtual int32_t getid(int32_t *id); virtual int32_t getid(int32_t *id);

466
components/sensors/smtp2/smtp2.cpp

@ -1,466 +0,0 @@
#include "smtp2.hpp"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "a8000_protocol\protocol.hpp"
using namespace iflytop;
#define TAG "SMTP2"
#define OVERTIME 30
#define DUMP_HEX 1
/**
* @brief
*
* /2A3000R\r
* /:
* 2:ID
* A:
* 3000:3000
* R:
* \r:
*
*
* 1.
* 2.
* 3.使
* 4.,:A3000A0R
* 5.[T]
*
*
*/
/**
* @brief
*
*
*
* Z
* A
* L() v<n> V<n> S<n> c<n> K<n> N<n> h<n> m<n>
* R
* X
* G<n>
* g
* M<n>
* H
* I/O1 J<n>
* T
* C
* E<n> Tip
* t<n1>,<n2>pLLD
* ^<n> cLLD
* B<n1>,<n2>
* f<n>
* +<n>
* <p>
* r LLD
* *<n1>,<n2>
* q<n1>,<n2>
* s<n> EEPROM
* e<n> EEPROM
* EEPROM
* U<n>(PAGE:52)
* u<n1>,<n2>
* Q
* =
* &
* ?<n>
*
*/
/**
*
* 40h 60h @ ` 0
* 41h 61h A a 1
* 42h 62h B b 2
* 43h 63h C c 3
* 44h 64h D d 4
* 45h 65h E e 5
* 46h 66h F f 6 LLD
* 47h 67h G g 7
* 48h 68h H h 8 Tip
* 49h 69h I i 9
* 4Ah 6Ah J j 10 Tip
* 4Bh 6Bh K k 11 CAN 线
* 4Ch 6Ch L l 12
* 4Dh 6Dh M m 13 EEPROM
* 4Eh 6Eh N n 14
* 4Fh 6Fh O o 15
* 50h 70h P p 16 Tip
* 51h 71h Q q 17
* 52h 72h R r 18 /
* 53h 73h S s 19 /
*
*/
static int32_t Get1fromfloat(const float& val) {
float temp1 = val;
int32_t t1 = (uint32_t)(temp1 * 10) % 10;
if (t1 > 4) {
temp1 = (float)(uint32_t)(temp1 + 1);
} else {
temp1 = (float)((uint32_t)(temp1));
}
return (int32_t)temp1;
}
int32_t SMTP2::init_device() {
ZLOGI(TAG, "init_device");
return doaction(fmt("/1ZR\r"));
}
int32_t SMTP2::init_device_block() {
return doaction_block([this]() { return init_device(); });
}
int32_t SMTP2::put_tip() {
// ZLOGI(TAG, "put_tip:%s", "/1E1R");
ZLOGI(TAG, "put_tip");
return doaction(fmt("/1E1R\r"));
}
int32_t SMTP2::put_tip_block() {
return doaction_block([this]() { return put_tip(); });
}
int32_t SMTP2::move_to(int32_t pos) {
ZLOGI(TAG, "move_to %d", pos);
return doaction(fmt("/1N%dA%dR\r", 0, pos));
}
int32_t SMTP2::move_to_block(int32_t pos) {
return doaction_block([this, pos]() { return move_to(pos); });
}
int32_t SMTP2::move_to_ul(int32_t ul) {
/**
* @brief 0.319ul进行操作
*/
ZLOGI(TAG, "move_to_ul %d", ul);
float stepNumfloat = ul / 0.319;
int stepNum = Get1fromfloat(stepNumfloat);
return doaction(fmt("/1N%dA%dR\r", 0, stepNum));
}
int32_t SMTP2::move_to_ul_block(int32_t ul) {
return doaction_block([this, ul]() { return move_to_ul(ul); });
}
int32_t SMTP2::set_resolution(int32_t resolution) {
ZLOGI(TAG, "set_resolution %d", resolution);
return doaction(fmt("/1N%dR\r", resolution));
}
void SMTP2::stop() {
ZLOGI(TAG, "stop");
doaction(fmt("/1T\r"));
}
int32_t SMTP2::readparaint(int32_t paramidx, int32_t& val) {
size_t rxlen = 0;
int ret = sendcmd(fmt("/1?%d\r", paramidx), m_rxbuf, sizeof(m_rxbuf), rxlen);
if (ret != 0) {
return ret;
}
iflytop::err::error_t ecode = read_ack_error_code(m_rxbuf, rxlen);
if (ecode != err::ksucc) {
return ecode;
}
val = read_ack_int_val(m_rxbuf, rxlen);
return err::ksucc;
}
int32_t SMTP2::readpara_bool(int32_t paramidx, bool& val) {
int32_t valint = 0;
int32_t ret = readparaint(paramidx, valint);
if (ret != 0) return ret;
if (valint == 1) {
val = true;
} else {
val = false;
}
return err::ksucc;
}
int32_t SMTP2::read_capacitance_val(int32_t& capval) { return readparaint(68, capval); }
int32_t SMTP2::read_tip_state(bool& hastip) { return readpara_bool(31, hastip); }
bool SMTP2::read_tip_state() {
bool hastip = false;
int32_t ret = read_tip_state(hastip);
if (ret != 0) {
return false;
}
return hastip;
}
int32_t SMTP2::read_pos_ul(int32_t& ul) {
int32_t pos = 0;
int32_t ret = readparaint(3, pos); // 读取nl
if (ret != 0) {
return ret;
}
ul = pos * 1000;
return 0;
}
int32_t SMTP2::read_pos_ul() {
int32_t ul = 0;
int32_t ret = read_pos_ul(ul);
if (ret != 0) return 0;
return ul;
}
char* SMTP2::fmt(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vsprintf(txbuf, fmt, args);
va_end(args);
return txbuf;
}
/**
* @brief Get the State object
*
* @param state
* @param error
* @return int
*/
int32_t SMTP2::getState(bool& isbusy, err::error_t& error) {
size_t rxlen = 0;
sprintf(txbuf, "/1Q\r");
int ret = sendcmd(txbuf, m_rxbuf, sizeof(m_rxbuf), rxlen);
if (ret != 0) {
return ret;
}
uint8_t errorcode = (uint8_t)m_rxbuf[2];
if (errorcode >= '`') {
isbusy = false;
errorcode = errorcode - '`';
} else if (errorcode >= '@') {
isbusy = true;
errorcode = errorcode - '@';
}
error = (err::error_t)errorcode;
return 0;
}
void SMTP2::initialize(UART_HandleTypeDef* uart, DMA_HandleTypeDef* hdma_rx, DMA_HandleTypeDef* hdma_tx) {
m_uart = uart;
m_hdma_rx = hdma_rx;
m_hdma_tx = hdma_tx;
}
int32_t SMTP2::getState() {
bool isbusy = false;
err::error_t error = err::ksucc;
int ret = getState(isbusy, error);
if (ret == 0) {
if (isbusy) {
return err::kdevice_is_busy;
}
return error;
}
return err::kdevice_is_offline;
}
bool SMTP2::isOnline() {
if (getState() != err::kdevice_is_offline) {
return true;
}
return false;
}
/*******************************************************************************
* BASIC *
*******************************************************************************/
iflytop::err::error_t SMTP2::read_ack_error_code(char* rxbuf, size_t rxlen) {
if (rxlen < 3) {
return err::kdevice_is_offline;
}
uint8_t errorcode = (uint8_t)rxbuf[2];
if (errorcode >= '`') {
errorcode = errorcode - '`';
} else if (errorcode >= '@') {
errorcode = errorcode - '@';
}
return (err::error_t)errorcode;
}
int32_t SMTP2::read_ack_int_val(char* rxbuf, size_t rxlen) {
if (rxlen < 3) {
ZLOGW(TAG, "read_ack_int_val rxlen < 3");
return 0;
}
memset(m_rxprocessbuf, 0, sizeof(m_rxprocessbuf));
memcpy(m_rxprocessbuf, rxbuf, rxlen);
for (size_t i = 0; i < rxlen; i++) {
if (m_rxprocessbuf[i] == '\r' || m_rxprocessbuf[i] == '\n' || m_rxprocessbuf[i] == 0x03) {
m_rxprocessbuf[i] = 0;
break;
}
}
// "/0`3\r\n"
int intval = atoi(m_rxprocessbuf + 3);
return intval;
}
int32_t SMTP2::doaction(char* cmd) {
//
size_t rxlen = 0;
size_t txlen = strlen(cmd);
// cmd[txlen] = '\r';
cmd[txlen] = 0;
// cmd[txlen + 1] = 0;
int ret = sendcmd(cmd, m_rxbuf, sizeof(m_rxbuf), rxlen);
if (ret != 0) {
return ret;
}
uint8_t state = (uint8_t)m_rxbuf[2];
if (state >= '`') {
state = state - '`';
} else if (state >= '@') {
state = state - '@';
}
if (state == 0) {
return 0;
}
return state + err::kSMTP2_NoError;
}
int32_t SMTP2::sendcmd(const char* cmd, char* rxbuf, size_t rxbuflen, size_t& rxlen) {
int32_t ecode = 0;
for (size_t i = 0; i < 5; i++) {
ecode = _sendcmd(cmd, rxbuf, rxbuflen, rxlen);
if (ecode == 0) {
break;
}
ZLOGI(TAG, "re send cmd %d", i);
}
return ecode;
}
int32_t SMTP2::_sendcmd(const char* cmd, char* rxbuf, size_t rxbuflen, size_t& rxlen) { return _sendcmd(cmd, strlen(cmd), rxbuf, rxbuflen, rxlen); }
int32_t SMTP2::_sendcmd(const char* cmd, size_t txlen, char* rxbuf, size_t rxbuflen, size_t& rxlen) {
if (!m_uart) return -1;
int ret = 0;
// size_t rxlen = 0;
#ifdef DUMP_HEX
if (strcmp("/1Q\r", cmd) != 0) {
ZLOGI(TAG, "tx:%s", cmd);
}
#endif
if (m_hdma_rx)
ret = _sendcmd_dma(cmd, txlen, (char*)m_rxbuf, sizeof(m_rxbuf), rxlen);
else
ret = _sendcmd_block(cmd, txlen, (char*)m_rxbuf, sizeof(m_rxbuf), rxlen);
#ifdef DUMP_HEX
if (strcmp("/1Q\r", cmd) != 0) {
ZLOGI(TAG, "rx:%s", m_rxbuf);
}
#endif
return ret;
}
int32_t SMTP2::_sendcmd_dma(const char* cmd, size_t txlen, char* rxbuf, size_t rxbuflen, size_t& rxlen) {
memset(rxbuf, 0, rxbuflen);
uint8_t rxdata;
// clear rx buf
while (HAL_UART_Receive(m_uart, &rxdata, 1, 1) == HAL_OK) {
}
HAL_UART_Transmit(m_uart, (uint8_t*)cmd, txlen, 1000);
uint32_t enter_ticket = HAL_GetTick();
HAL_UARTEx_ReceiveToIdle_DMA(m_uart, (uint8_t*)rxbuf, rxbuflen);
bool overtime_flag = false;
while (HAL_UART_GetState(m_uart) == HAL_UART_STATE_BUSY_RX || //
HAL_UART_GetState(m_uart) == HAL_UART_STATE_BUSY_TX_RX) {
osDelay(1);
if (zos_haspassedms(enter_ticket) > OVERTIME) {
// ZLOGW(TAG, "sendcmd_dma overtime");
overtime_flag = true;
break;
}
}
HAL_UART_DMAStop(m_uart);
if (overtime_flag) {
return err::kovertime;
}
rxlen = rxbuflen - __HAL_DMA_GET_COUNTER(m_hdma_rx);
return 0;
}
int32_t SMTP2::_sendcmd_block(const char* cmd, size_t txlen, char* rxbuf, size_t rxbuflen, size_t& rxlen) {
// m_huart->tx(cmd);
memset(rxbuf, 0, rxbuflen);
printf("tx:%s\n", cmd);
HAL_UART_Transmit(m_uart, (uint8_t*)cmd, txlen, 1000);
int rxnum = 0;
HAL_StatusTypeDef rxstatus = HAL_UART_Receive(m_uart, (uint8_t*)&rxbuf[0], 1, OVERTIME);
if (rxstatus != HAL_OK) {
rxlen = 0;
return err::kovertime;
}
rxnum++;
for (;; rxnum++) {
HAL_StatusTypeDef rxstatus = HAL_UART_Receive(m_uart, (uint8_t*)&rxbuf[rxnum], 1, 3);
if (rxstatus != HAL_OK) break;
if ('\n' == rxbuf[rxnum]) break;
if (rxnum >= int(rxbuflen - 2)) break;
}
// printf("SMTP2:m_rxbuf:(%d) %s\n", rxnum, rxbuf);
rxlen = rxnum;
return 0;
}
int32_t SMTP2::doaction_block(function<int32_t()> action) {
// 移液枪复位
ThisThread thisThread;
int ret = action();
if (ret != 0) {
return ret;
}
// 等待移液枪复位完成
int32_t ecodetimes = 0;
while (true) {
// int32_t state = getState();
osDelay(100);
bool isbusy = false;
err::error_t error;
int32_t ecode = getState(isbusy, error);
if (ecode != 0) {
ecodetimes++;
if (ecodetimes > 3) {
return err::kovertime;
}
continue;
}
if (error != 0) {
ZLOGI(TAG, "error:%d", error);
stop();
return error;
}
// 动作执行完成
if (!isbusy) {
return 0;
}
if (thisThread.getExitFlag()) {
stop();
return err::kmodule_opeation_break_by_user;
}
}
}

147
components/sensors/smtp2/smtp2.hpp

@ -1,147 +0,0 @@
#pragma once
#include "a8000_protocol\protocol.hpp"
#include "sdk\os\zos.hpp"
namespace iflytop {
using namespace std;
/**
* @brief
*
* ref:https://iflytop1.feishu.cn/wiki/VNNRwUJCtiuRDHkD1uScGIA1nMf
* WARNING:
* 1,
*/
class SMTP2 {
public:
private:
UART_HandleTypeDef* m_uart = nullptr;
uint8_t m_id = 0;
// const char* m_name = nullptr;
DMA_HandleTypeDef* m_hdma_rx;
DMA_HandleTypeDef* m_hdma_tx;
char m_rxbuf[20] = {0};
char m_rxprocessbuf[20] = {0};
char txbuf[20];
int32_t nowpos = 0;
public:
void initialize(UART_HandleTypeDef* uart, DMA_HandleTypeDef* hdma_rx, DMA_HandleTypeDef* hdma_tx);
/**
* @brief
*
* @return int32_t iflytop::err
*/
int32_t getState();
bool isOnline();
/**
* @brief
*
* @return int
*/
int32_t init_device();
int32_t init_device_block();
/**
* @brief Tip
*
* @return int
*/
int32_t put_tip();
int32_t put_tip_block();
/**
* @brief
*
* @param pos
* @return int
*/
int32_t move_to(int32_t pos);
int32_t move_to_block(int32_t pos);
/**
* @brief
*
* @param ul
* @return int
*/
int32_t move_to_ul(int32_t ul);
int32_t move_to_ul_block(int32_t ul);
/**
* @brief
*
* @return int (0->159)159
*/
int32_t read_capacitance_val(int32_t& capval);
/**
* @brief tip脱落状态
*
* @param hastip
* @return int
*/
int32_t read_tip_state(bool& hastip);
bool read_tip_state();
/**
* @brief
*
* @param ul
* @return int32_t
*/
int32_t read_pos_ul(int32_t& ul);
int32_t read_pos_ul();
/**
* @brief
*
* @param resolution
* 0:0.319ul
* 1:0.0199ul
* 2:nl
* @return int
*/
int32_t set_resolution(int32_t resolution);
/**
* @brief Get the State object
*
* @param isbusy
* @param error
* @return int
*/
int32_t getState(bool& isbusy, iflytop::err::error_t& error);
/**
* @brief
*/
void stop();
int32_t doaction_block(function<int32_t()> action);
private:
int32_t doaction(char* cmd);
int32_t sendcmd(const char* cmd, char* rxbuf, size_t rxbuflen, size_t& rxlen);
int32_t _sendcmd(const char* cmd, char* rxbuf, size_t rxbuflen, size_t& rxlen);
int32_t _sendcmd(const char* cmd, size_t txlen, char* rxbuf, size_t rxbuflen, size_t& rxlen);
int32_t _sendcmd_dma(const char* cmd, size_t txlen, char* rxbuf, size_t rxbuflen, size_t& rxlen);
int32_t _sendcmd_block(const char* cmd, size_t txlen, char* rxbuf, size_t rxbuflen, size_t& rxlen);
iflytop::err::error_t read_ack_error_code(char* rxbuf, size_t rxlen);
int32_t read_ack_int_val(char* rxbuf, size_t rxlen);
char* fmt(const char* fmt, ...);
int32_t readparaint(int32_t paramidx, int32_t& val);
int32_t readpara_bool(int32_t paramidx, bool& val);
private:
};
} // namespace iflytop

41
components/sensors/smtp2_v2/smtp2_v2.cpp

@ -13,7 +13,8 @@ using namespace smtp2;
#define OVERTIME 100 #define OVERTIME 100
#define DUMP_HEX 1 #define DUMP_HEX 1
#define SEND_CMD(fmt, ...) sendcmd("/1" fmt "R\r", ##__VA_ARGS__);
#define SEND_CMD(fmt, ...) sendcmd(true, "/1" fmt "R\r", ##__VA_ARGS__);
#define SEND_CMD_NLOG(fmt, ...) sendcmd(false, "/1" fmt "R\r", ##__VA_ARGS__);
void SMTP2V2::initialize(UART_HandleTypeDef* uart, uint8_t id, DMA_HandleTypeDef* hdma_rx, DMA_HandleTypeDef* hdma_tx) { void SMTP2V2::initialize(UART_HandleTypeDef* uart, uint8_t id, DMA_HandleTypeDef* hdma_rx, DMA_HandleTypeDef* hdma_tx) {
m_uart = uart; m_uart = uart;
@ -28,7 +29,7 @@ bool SMTP2V2::pump_ping() {
} }
int32_t SMTP2V2::pump_get_state(int32_t* isbusy) { int32_t SMTP2V2::pump_get_state(int32_t* isbusy) {
size_t rxlen = 0; size_t rxlen = 0;
int ret = SEND_CMD("Q");
int ret = SEND_CMD_NLOG("Q");
if (ret != 0) return ret; if (ret != 0) return ret;
uint8_t errorcode = (uint8_t)m_rxbuf[2]; uint8_t errorcode = (uint8_t)m_rxbuf[2];
@ -58,9 +59,9 @@ int32_t SMTP2V2::pump_set_vmax(int32_t vel) { return SEND_CMD("V%d", vel); }
int32_t SMTP2V2::pump_set_acc_and_dec(int32_t acc, int32_t dec) { return SEND_CMD("L%d,%d", acc, dec); } int32_t SMTP2V2::pump_set_acc_and_dec(int32_t acc, int32_t dec) { return SEND_CMD("L%d,%d", acc, dec); }
int32_t SMTP2V2::pump_put_tip() { return SEND_CMD("E"); } int32_t SMTP2V2::pump_put_tip() { return SEND_CMD("E"); }
int32_t SMTP2V2::pump_init() { return SEND_CMD("[Z]"); }
int32_t SMTP2V2::pump_init() { return SEND_CMD("Z"); }
int32_t SMTP2V2::pump_reset() { int32_t SMTP2V2::pump_reset() {
_sendcmd("/1!0R\r"); // 复位指令没有回执,所以这里只能使用方法_sendcmd
_sendcmd(true, "/1!0R\r"); // 复位指令没有回执,所以这里只能使用方法_sendcmd
return 0; return 0;
} }
@ -87,7 +88,11 @@ int32_t SMTP2V2::pump_move_to_ul(int32_t ul) {
/*********************************************************************************************************************** /***********************************************************************************************************************
* 使 * * 使 *
***********************************************************************************************************************/ ***********************************************************************************************************************/
bool SMTP2V2::write_cmd(const char* cmd) { return sendcmd(cmd); }
bool SMTP2V2::write_cmd(const char* cmd) {
int32_t ret = 0;
ret = sendcmd(true, cmd);
return ret == 0;
}
void SMTP2V2::getack(char* rx, int32_t* rxbufsize) { void SMTP2V2::getack(char* rx, int32_t* rxbufsize) {
if (m_rxNum < 3) { if (m_rxNum < 3) {
*rxbufsize = 0; *rxbufsize = 0;
@ -96,7 +101,7 @@ void SMTP2V2::getack(char* rx, int32_t* rxbufsize) {
// 指令回执后三位为 0x3(ctrl+C) 0x0D(回车) 0x0A(换行),直接丢弃 // 指令回执后三位为 0x3(ctrl+C) 0x0D(回车) 0x0A(换行),直接丢弃
*rxbufsize = m_rxNum - 2; *rxbufsize = m_rxNum - 2;
memcpy(rx, m_rxbuf, m_rxNum - 2); memcpy(rx, m_rxbuf, m_rxNum - 2);
rx[*rxbufsize] = 0;
rx[*rxbufsize - 1] = 0;
} }
/*********************************************************************************************************************** /***********************************************************************************************************************
* BASIC * * BASIC *
@ -111,7 +116,11 @@ int32_t SMTP2V2::getAckEcode() {
} else if (errorcode >= '@') { } else if (errorcode >= '@') {
errorcode = errorcode - '@'; errorcode = errorcode - '@';
} }
return (err::error_t)errorcode;
if (errorcode == 0) {
return errorcode;
} else {
return errorcode + err::kSMTP2_NoError;
}
} }
int32_t SMTP2V2::getAck0AsInt() { int32_t SMTP2V2::getAck0AsInt() {
@ -131,20 +140,20 @@ int32_t SMTP2V2::getAck0AsInt() {
return intval; return intval;
} }
int32_t SMTP2V2::sendcmd(const char* format, ...) {
int32_t SMTP2V2::sendcmd(bool dump, const char* format, ...) {
static char buf[256]; static char buf[256];
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args); vsnprintf(buf, sizeof(buf), format, args);
va_end(args); va_end(args);
sendcmd_auto_retry(buf);
sendcmd_auto_retry(dump, buf);
return getAckEcode(); return getAckEcode();
} }
bool SMTP2V2::sendcmd_auto_retry(const char* cmd) {
bool SMTP2V2::sendcmd_auto_retry(bool dump, const char* cmd) {
for (size_t i = 0; i < 1; i++) { for (size_t i = 0; i < 1; i++) {
if (_sendcmd(cmd)) {
if (_sendcmd(dump, cmd)) {
break; break;
} }
m_rxNum = 0; m_rxNum = 0;
@ -152,17 +161,21 @@ bool SMTP2V2::sendcmd_auto_retry(const char* cmd) {
} }
return true; return true;
} }
bool SMTP2V2::_sendcmd(const char* cmd) {
bool SMTP2V2::_sendcmd(bool dump, const char* cmd) {
m_rxNum = 0; m_rxNum = 0;
#ifdef DUMP_HEX #ifdef DUMP_HEX
ZLOGI(TAG, "tx:%s", cmd);
if (dump) {
ZLOGI(TAG, "tx:%s", cmd);
}
#endif #endif
bool ret = _sendcmd_dma(cmd); bool ret = _sendcmd_dma(cmd);
#ifdef DUMP_HEX #ifdef DUMP_HEX
ZLOGI(TAG, "rx:%s", m_rxbuf);
if (dump) {
ZLOGI(TAG, "rx:%s", m_rxbuf);
}
#endif #endif
return ret; return ret;

6
components/sensors/smtp2_v2/smtp2_v2.hpp

@ -190,10 +190,10 @@ class SMTP2V2 {
void getack(char* rx, int32_t* rxbufsize); void getack(char* rx, int32_t* rxbufsize);
private: private:
int32_t sendcmd(const char* format, ...);
int32_t sendcmd(bool dump, const char* format, ...);
bool sendcmd_auto_retry(const char* cmd);
bool _sendcmd(const char* cmd);
bool sendcmd_auto_retry(bool dump, const char* cmd);
bool _sendcmd(bool dump, const char* cmd);
bool _sendcmd_dma(const char* cmd); bool _sendcmd_dma(const char* cmd);
int32_t getAckEcode(); int32_t getAckEcode();

Loading…
Cancel
Save