diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 0c64d05..5e8ee27 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -2,10 +2,10 @@ - + - + @@ -13,10 +13,10 @@ - + - + diff --git a/sdk b/sdk index 6bd928e..7a8a3c0 160000 --- a/sdk +++ b/sdk @@ -1 +1 @@ -Subproject commit 6bd928e42a0bdb1c6345b5751014d94b09ba8985 +Subproject commit 7a8a3c0c76c859f725b97a668bd96d166b245d68 diff --git a/usrc/hardware.cpp b/usrc/hardware.cpp new file mode 100644 index 0000000..db39fff --- /dev/null +++ b/usrc/hardware.cpp @@ -0,0 +1,275 @@ +#include "hardware.hpp" + +#include +#include + +#include "main.h" +#include "project.hpp" +// +// #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp" +#include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp" +#include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp" +#include "sdk/hal/zhal.hpp" +#include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp" +#include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp" +#include "sdk\components\m3078\m3078_code_scaner.hpp" +#include "sdk\components\tmc\ic\ztmc4361A.hpp" +#include "sdk\components\tmc\ic\ztmc5130.hpp" +// +#include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp" +#include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp" +#include "sdk\components\zcan_module\zcan_basic_order_module.hpp" +#include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp" +#include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp" + +using namespace iflytop; +#define TAG "main" + +TMC5130 m_motor1; +TMC5130 m_motor2; + +ZGPIO triLight_R; +ZGPIO triLight_G; +ZGPIO triLight_Y; +ZGPIO triLight_BEEP; + +ZCanBasicOrderModule m_basicOrderModule; +ZCanPumpCtrlModule m_pumpCtrlModule; +ZCanTrigleWarningLightCtlModule m_warningLightCtlModule; +HuachengPressureSensor m_huachengPressureSensor; + +DP600PressureSensor m_dp600PressureSensor2; +DP600PressureSensor m_dp600PressureSensor3; +DP600PressureSensor m_dp600PressureSensor4; + +ZGPIO IO_PD13_IN; +ZGPIO IO_PC7_IN; + +#define PROCESS_CMD(cmd, id) \ + if ((cmdheader->cmdid == (uint16_t)cmd) && (cmdheader->subcmdid == 0) && cmdheader->data[0] == id) { \ + matching = true; \ + } \ + if ((cmdheader->cmdid == (uint16_t)cmd) && (cmdheader->subcmdid == 0) && cmdheader->data[0] == id) + +void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) { + int32_t ppm = rpm / 60.0 * 51200; + int32_t acc = acc_rpm2 / 60.0 * 51200; + + int16_t _idlepower = 1; + int16_t _power = 31; + + if (idlepower > 0 && idlepower < 31) { + _idlepower = idlepower; + } + if (power > 0 && power < 31) { + _power = power; + } + + motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W + motor->setAcceleration(acc); + motor->setDeceleration(acc); + motor->rotate(ppm); +} + +void Hardware::initialize() { + IO_PD13_IN.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/); + IO_PC7_IN.initAsInput(PC7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/); + + { + TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN}; + + m_motor1.initialize(&cfg); + int32_t chipv = m_motor1.readChipVERSION(); + ZLOGI(TAG, "m_motor1:%lx", chipv); + m_motor1.setIHOLD_IRUN(1, 20, 0); + m_motor1.setMotorShaft(true); + + m_motor1.setAcceleration(300000); + m_motor1.setDeceleration(300000); + // m_motor1.rotate(1000000); + } + + { + TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN}; + + m_motor2.initialize(&cfg); + int32_t chipv = m_motor2.readChipVERSION(); + ZLOGI(TAG, "m_motor2:%lx", chipv); + m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W + m_motor2.setMotorShaft(true); + + m_motor2.setAcceleration(300000); + m_motor2.setDeceleration(300000); + // m_motor1.rotate(1000000); + } + + triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false); + triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false); + triLight_Y.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false); + triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false); + + m_dp600PressureSensor2.initialize(&huart3, 2); + m_dp600PressureSensor3.initialize(&huart3, 3); + m_dp600PressureSensor4.initialize(&huart3, 4); +} + +void dumpdp600data(DP600PressureSensor::sensor_data_t *data) { + ZLOGI(TAG, "value:%d", data->value); + ZLOGI(TAG, "zero_point:%d", data->zero_point); + ZLOGI(TAG, "range_full_point:%d", data->range_full_point); + ZLOGI(TAG, "precision:%d", data->precision); + ZLOGI(TAG, "pressure_unit:%d", data->pressure_unit); +} + +void packet_kcmd_read_huacheng_pressure_sensor_data(int id, DP600PressureSensor::sensor_data_t *dp600data, uint8_t *receipt, int32_t &receiptsize) { + receipt[0] = id; + receipt[1] = 0; + receipt[2] = dp600data->precision; + receipt[3] = dp600data->pressure_unit; + memcpy(receipt + 4, &dp600data->value, 2); + memcpy(receipt + 6, &dp600data->zero_point, 2); + memcpy(receipt + 8, &dp600data->range_full_point, 2); + receiptsize = 10; +} + +int32_t Hardware::process_rx_packet(from_where_t fromwhere, uint8_t *packet, int32_t len, uint8_t *receipt, int32_t &receiptsize, bool &matching) { + Cmdheader_t *cmdheader = (Cmdheader_t *)packet; + + /** + * @brief 获取蒸发仓水浸状态 + */ + PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0) { + ((int32_t *)receipt)[0] = !IO_PC7_IN.getState(); + receiptsize = 4; + return 0; + } + + /** + * @brief 获取设备底盘水浸传感器 + */ + PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 1) { + ((int32_t *)receipt)[0] = !IO_PD13_IN.getState(); + receiptsize = 4; + return 0; + } + + /** + * @brief 控制加液泵 + */ + PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1) { + int16_t acc = *(int16_t *)(&cmdheader->data[2]); + int16_t rpm = *(int16_t *)(&cmdheader->data[4]); + int16_t idlepower = cmdheader->data[6]; + int16_t power = cmdheader->data[7]; + + setmotor(&m_motor1, acc, rpm, idlepower, power); + receipt[0] = cmdheader->data[0]; + receiptsize = 1; + return 0; + } + + /** + * @brief 控制-喷液泵 + */ + PROCESS_CMD(kcmd_peristaltic_pump_ctl, 2) { + int16_t acc = *(int16_t *)(&cmdheader->data[2]); + int16_t rpm = *(int16_t *)(&cmdheader->data[4]); + int16_t idlepower = cmdheader->data[6]; + int16_t power = cmdheader->data[7]; + + setmotor(&m_motor2, acc, rpm, idlepower, power); + receipt[0] = cmdheader->data[0]; + receiptsize = 1; + return 0; + } + + /** + * @brief 三色指示灯控制 + */ + PROCESS_CMD(kcmd_triple_warning_light_ctl, 1) { + /** + * @brief 0:设置状态 + * cmd: + * [0]:SENSORID + * [2]:红色灯状态 + * [3]:黄色灯状态 + * [4]:绿色灯状态 + * [5]:蜂鸣器状态 + * ack : b0:id + * ack_datalen : 1 + */ + uint8_t id = cmdheader->data[0]; + uint8_t r = cmdheader->data[2]; + uint8_t g = cmdheader->data[3]; + uint8_t b = cmdheader->data[4]; + uint8_t beep = cmdheader->data[5]; + + triLight_R.setState(r != 0); + triLight_G.setState(g != 0); + triLight_Y.setState(b != 0); + triLight_BEEP.setState(beep != 0); + + receipt[0] = cmdheader->data[0]; + receiptsize = 1; + } + + static DP600PressureSensor::sensor_data_t dp600data; + + /** + * @brief 液位测量压力传感器 + */ + PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 1) { + static ModbusBlockHost modbusBlockHost; + modbusBlockHost.initialize(&huart3); + int16_t val[1] = {0}; + bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50); + if (!suc) return 1002; + dp600data.precision = 3; + dp600data.pressure_unit = 1; + dp600data.value = val[0]; + dp600data.zero_point = 0; + dp600data.range_full_point = 0; + packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize); + if (fromwhere == kuart) { + dumpdp600data(&dp600data); + } + return 0; + } + /** + * @brief 空压机压力传感器 + */ + PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 2) { + bool suc = m_dp600PressureSensor2.readVal(&dp600data); + if (!suc) return 1002; + packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize); + if (fromwhere == kuart) { + dumpdp600data(&dp600data); + } + return 0; + } + /** + * @brief 加液蠕动泵压力传感器 + */ + PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 3) { + bool suc = m_dp600PressureSensor3.readVal(&dp600data); + if (!suc) return 1002; + packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize); + if (fromwhere == kuart) { + dumpdp600data(&dp600data); + } + return 0; + } + /** + * @brief 喷射蠕动泵压力传感器 + */ + PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 4) { + bool suc = m_dp600PressureSensor4.readVal(&dp600data); + if (!suc) return 1002; + packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize); + if (fromwhere == kuart) { + dumpdp600data(&dp600data); + } + return 0; + } +} +void Hardware::loop() {} diff --git a/usrc/hardware.hpp b/usrc/hardware.hpp new file mode 100644 index 0000000..992f917 --- /dev/null +++ b/usrc/hardware.hpp @@ -0,0 +1,14 @@ +#include + +namespace iflytop { +class Hardware { + public: + typedef enum { kcan, kuart } from_where_t; + + public: + void initialize(); + int32_t process_rx_packet(from_where_t fromwhere, uint8_t *packet, int32_t len, uint8_t *receipt, int32_t &receiptsize, bool &matching); + void loop(); +}; + +} // namespace iflytop diff --git a/usrc/main.cpp b/usrc/main.cpp index 7a92ba8..a278651 100644 --- a/usrc/main.cpp +++ b/usrc/main.cpp @@ -6,7 +6,6 @@ #include "main.h" #include "project.hpp" // -#include "one_dimensional_code_laser_scanner.hpp" // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp" #include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp" #include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp" @@ -17,6 +16,7 @@ #include "sdk\components\tmc\ic\ztmc4361A.hpp" #include "sdk\components\tmc\ic\ztmc5130.hpp" // +#include "hardware.hpp" #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp" #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp" #include "sdk\components\zcan_module\zcan_basic_order_module.hpp" @@ -30,61 +30,130 @@ Main gmain; using namespace iflytop; -IflytopCanProtocolStackProcesser m_protocolStack; +/******************************************************************************* + * TOOLS * + *******************************************************************************/ +static uint8_t *hex_str_to_bytes(char *data, int32_t len, int32_t &bytelen) { + /** + * @brief + * data: + * 12 34 56 78 90 ab cd ef + * + */ + static uint8_t bytes_cache[1024] = {0}; + static uint8_t data_cache[1024] = {0}; -TMC5130 m_motor1; -TMC5130 m_motor2; + int32_t data_len = 0; -ZGPIO debuglight; + memset(bytes_cache, 0, sizeof(bytes_cache)); + memset(data_cache, 0, sizeof(data_cache)); -ZGPIO triLight_R; -ZGPIO triLight_G; -ZGPIO triLight_B; -ZGPIO triLight_BEEP; + for (int32_t i = 0; i < len; i++) { + if (data[i] == ' ') continue; + if (data[i] == '\r' || data[i] == '\n') break; + data_cache[i] = data[i]; + data_len++; + } -ZGPIO m_input1; -ZGPIO m_input2; -ZGPIO m_input3; -ZGPIO m_input4; -ZGPIO m_input5; + if (data_len % 2 != 0) { + ZLOGE(TAG, "data_len %d", data_len); + return NULL; + } -ZGPIO output1; -ZGPIO output2; + for (int32_t i = 0; i < data_len; i += 2) { + char c1 = data_cache[i]; + char c2 = data_cache[i + 1]; + if (c1 >= '0' && c1 <= '9') { + c1 = c1 - '0'; + } else if (c1 >= 'a' && c1 <= 'f') { + c1 = c1 - 'a' + 10; + } else if (c1 >= 'A' && c1 <= 'F') { + c1 = c1 - 'A' + 10; + } else { + ZLOGE(TAG, "c1 %c", c1); + return NULL; + } -ZCanReceiver m_canReceiver; -ZCanBasicOrderModule m_basicOrderModule; -ZCanPumpCtrlModule m_pumpCtrlModule; -ZCanTrigleWarningLightCtlModule m_warningLightCtlModule; -HuachengPressureSensor m_huachengPressureSensor; + if (c2 >= '0' && c2 <= '9') { + c2 = c2 - '0'; + } else if (c2 >= 'a' && c2 <= 'f') { + c2 = c2 - 'a' + 10; + } else if (c2 >= 'A' && c2 <= 'F') { + c2 = c2 - 'A' + 10; + } else { + ZLOGE(TAG, "c2 %c", c2); + return NULL; + } -void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) { - int32_t ppm = rpm / 60.0 * 51200; - int32_t acc = acc_rpm2 / 60.0 * 51200; + bytes_cache[i / 2] = (c1 << 4) | c2; + } - int16_t _idlepower = 1; - int16_t _power = 31; + bytelen = data_len / 2; + return bytes_cache; +} - if (idlepower > 0 && idlepower < 31) { - _idlepower = idlepower; - } - if (power > 0 && power < 31) { - _power = power; +void dumphexdata(uint8_t *data, int32_t len) { + for (int32_t i = 0; i < len; i++) { + printf("%02X ", data[i]); } - - motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W - motor->setAcceleration(acc); - motor->setDeceleration(acc); - motor->rotate(ppm); + printf("\n"); } +/******************************************************************************* + * GLOBAL * + *******************************************************************************/ + +IflytopCanProtocolStackProcesser m_protocolStack; +Hardware m_hardware; +ZGPIO debuglight; +ZCanReceiver m_canReceiver; + +/******************************************************************************* + * MESSAGE_HANDLER * + *******************************************************************************/ +/** + * @brief 处理CAN接收到消息 + */ void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) { - ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len); - for (size_t i = 0; i < len; i++) { - printf("%02X ", packet[i]); + // ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len); + static uint8_t rxdata[1024] = {0}; + memset(rxdata, 0, sizeof(rxdata)); + Cmdheader_t *cmdheader = (Cmdheader_t *)packet; + bool match = false; + int32_t receipt_size = 0; + int32_t ecode = m_hardware.process_rx_packet(Hardware::from_where_t::kcan, packet, len, rxdata, receipt_size, match); + if (match) { + if (ecode != 0) { + m_canReceiver.sendErrorAck(cmdheader, ecode); + } else { + m_canReceiver.sendAck(cmdheader, rxdata, sizeof(rxdata)); + } } - printf("\n"); } +/** + * @brief 处理串口接收到的消息 + */ +static void processUartRX(uint8_t *packet, size_t len) { + static uint8_t rxdata[1024] = {0}; + int32_t receipt_size = 0; + bool match = false; + + memset(rxdata, 0, sizeof(rxdata)); + // + int32_t bytelen = 0; + uint8_t *hexbytes = hex_str_to_bytes((char *)packet, len, bytelen); + if (hexbytes == NULL) { + ZLOGE(TAG, "hex_str_to_bytes failed"); + return; + } + dumphexdata(hexbytes, bytelen); + m_hardware.process_rx_packet(Hardware::kuart, packet, len, rxdata, receipt_size, match); +} + +/******************************************************************************* + * MAIN * + *******************************************************************************/ void Main::run() { ZHALCORE::cfg_t oscfg = { .delayhtim = &DELAY_US_TIMER, @@ -98,152 +167,25 @@ void Main::run() { debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false); ZHAL_CORE_REG(200, { debuglight.toggleState(); }); + m_hardware.initialize(); + + static ZUART uartreceiver; + static ZUART::cfg_t uartreceiver_cfg = { + .name = "uartreceiver", + .huart = &DEBUG_UART, + .rxbuffersize = 512, + .rxovertime_ms = 30, + }; + uartreceiver.initialize(&uartreceiver_cfg); + uartreceiver.setrxcb([this](uint8_t *data, size_t len) { processUartRX(data, len); }); + ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID); m_canReceiver.init(cfg); m_canReceiver.registerListener(this); - /** - * @brief 基础模块 - */ - m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/); - m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/); - m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/); - m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/); - m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/); - - m_basicOrderModule.initialize(&m_canReceiver); - m_basicOrderModule.regInputCtl([this](uint8_t id, bool &val) { - if (id == 1) { - val = m_input1.getState(); - return true; - } - if (id == 2) { - val = m_input2.getState(); - return true; - } - if (id == 3) { - val = m_input3.getState(); - return true; - } - if (id == 4) { - val = m_input4.getState(); - return true; - } - if (id == 5) { - val = m_input5.getState(); - return true; - } - return false; - }); - - // output1.reg - // output2 - m_basicOrderModule.regOutCtl([this](uint8_t id, bool val) { return false; }); - ZHAL_CORE_REG(3000, { - // ZLOGI(TAG, "IO1:%d IO2:%d", m_input1.getState(), m_input2.getState()); - ZLOGI(TAG, "IO1:%d IO2:%d IO3:%d IO4:%d IO5:%d", m_input1.getState(), m_input2.getState(), m_input3.getState(), m_input4.getState(), m_input5.getState()); - }); - - /******************************************************************************* - * 蠕动泵驱动 * - *******************************************************************************/ - - { - TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN}; - - m_motor1.initialize(&cfg); - int32_t chipv = m_motor1.readChipVERSION(); - ZLOGI(TAG, "m_motor1:%lx", chipv); - m_motor1.setIHOLD_IRUN(1, 20, 0); - m_motor1.setMotorShaft(true); - - m_motor1.setAcceleration(300000); - m_motor1.setDeceleration(300000); - // m_motor1.rotate(1000000); - } - - { - TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN}; - - m_motor2.initialize(&cfg); - int32_t chipv = m_motor2.readChipVERSION(); - ZLOGI(TAG, "m_motor2:%lx", chipv); - m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W - m_motor2.setMotorShaft(true); - - m_motor2.setAcceleration(300000); - m_motor2.setDeceleration(300000); - // m_motor1.rotate(1000000); - } - - m_pumpCtrlModule.initialize(&m_canReceiver); - m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) { - ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm); - setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power); - }); - m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) { - ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm); - setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power); - }); - - /******************************************************************************* - * 三色指示灯 * - *******************************************************************************/ - - { - triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false); - triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false); - triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false); - triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false); - - m_warningLightCtlModule.initialize(&m_canReceiver); - m_warningLightCtlModule.regSubmodule(1, [&](uint8_t r, uint8_t g, uint8_t b, uint8_t beep) { - ZLOGI(TAG, "warningLightCtlModule r:%d g:%d b:%d beep:%d", r, g, b, beep); - triLight_R.setState(r != 0); - triLight_G.setState(g != 0); - triLight_B.setState(b != 0); - triLight_BEEP.setState(beep != 0); - }); - } - - /******************************************************************************* - * 压力传感器 * - *******************************************************************************/ - - // while (true) { - // static ModbusBlockHost modbusBlockHost; - // modbusBlockHost.initialize(&huart3); - // int16_t val[1] = {0}; - // bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50); - // printf("suc:%d val:%d\n", suc, val[0]); - // chip_delay_ms(1000); - // } - - { - m_huachengPressureSensor.initialize(&m_canReceiver); - m_huachengPressureSensor.regSubmodule(1, [this](DP600PressureSensor::sensor_data_t *data) { // - static ModbusBlockHost modbusBlockHost; - modbusBlockHost.initialize(&huart3); - int16_t val[1] = {0}; - bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50); - if (!suc) { - return false; - } - data->precision = 3; - data->pressure_unit = 1; - data->value = val[0]; - data->zero_point = 0; - data->range_full_point = 0; - - return true; - }); - m_huachengPressureSensor.regSubmodule(2, &huart3, 2); - m_huachengPressureSensor.regSubmodule(3, &huart3, 3); - m_huachengPressureSensor.regSubmodule(4, &huart3, 4); - } - ZLOGI(TAG, "init done"); while (1) { ZHALCORE::getInstance()->loop(); + m_hardware.loop(); } } diff --git a/usrc/one_dimensional_code_laser_scanner.cpp b/usrc/one_dimensional_code_laser_scanner.cpp deleted file mode 100644 index 8c9d35a..0000000 --- a/usrc/one_dimensional_code_laser_scanner.cpp +++ /dev/null @@ -1,282 +0,0 @@ -#include "one_dimensional_code_laser_scanner.hpp" - -#include -using namespace iflytop; - -#define ONE_BIT_WIDTH ((int32_t)(51200 * 1.20003 / 8.0)) -#define MIN_POS ((int32_t)(102733)) - -#define TAG "OneDimensionalCodeLaserScanner" - -void OneDimensionalCodeLaserScanner::initialize(IflytopCanProtocolStackProcesser* protocolProcesser, int32_t regStartOff, cfg_t* cfg) { - m_protocolProcesser = protocolProcesser; - m_regStartOff = regStartOff; - m_cfg = *cfg; - - m_slave = m_protocolProcesser->createICPSSlaveModule("OneDimensionalCodeLaserScanner", this, m_regStartOff); - m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_ACT_CTRL, icps::kw, 0); - m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_ACT_CLEAR_EXCEPTION, icps::kw, 0); - m_statusReg = m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_STAT_STATUS, icps::kr, 0); - m_errReg = m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_STAT_ERROR, icps::kr, 0); - m_codeReg = m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_CODE, icps::kr, 0); - m_item = m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_ITEM, icps::kr, 0); - m_lot = m_protocolProcesser->activeReg(m_slave, REG_CODE_SCANER_LOT, icps::kr, 0); - - m_triggerGpio.initAsInput(m_cfg.triggerPin, ZGPIO::kMode_nopull, ZGPIO::kIRQ_risingAndFallingIrq, true /*mirror*/); - m_triggerGpio.regListener([this](ZGPIO* GPIO_Pin, ZGPIO::IrqTypeEvent_t irqevent) { onGpioIrq(GPIO_Pin, irqevent); }); - m_readder = m_cfg.readder; -} - -icps::error_t OneDimensionalCodeLaserScanner::onHostRegisterWriteEvent(icps::WriteEvent* writeEvent) { - int32_t regoff = writeEvent->reg->add - m_regStartOff; - switch (regoff) { - case REG_CODE_SCANER_ACT_CTRL: - if (writeEvent->newvalue == 1) { - startScan(); - } else if (writeEvent->newvalue == 0) { - stopScan(); - parseResult(); - } else { - return icps::kIllegalValue; - } - return icps::kSuccess; - case REG_CODE_SCANER_ACT_CLEAR_EXCEPTION: - return icps::kSuccess; - default: - break; - } - return icps::kRegNotFound; -} - -void OneDimensionalCodeLaserScanner::startScan() { - CriticalContext cc; - m_off = 0; - m_workflag = true; - m_idleLevel = m_triggerGpio.getState(); - m_startpos = m_readder(); - ZLOGI(TAG, "start scan startpos:%d", m_startpos); -} -void OneDimensionalCodeLaserScanner::stopScan() { - CriticalContext cc; - m_workflag = false; - m_endpos = m_readder(); - ZLOGI(TAG, "stop scan endpos:%d", m_endpos); -} - -bool OneDimensionalCodeLaserScanner::getPosLevel(int pos) { - bool level = m_idleLevel; - bool nowlevel = m_idleLevel; - - if (pos < m_posChache[0]) { - return m_idleLevel; - } - - for (int i = 0; i < m_off; i++) { - nowlevel = !nowlevel; - if (i + 1 < m_off) { - if (pos >= m_posChache[i] && pos < m_posChache[i + 1]) { - level = nowlevel; - break; - } - } else { - level = nowlevel; - } - } - - return level; -} - -int32_t OneDimensionalCodeLaserScanner::compute_point_num(int32_t startpos, int32_t endpos) { - int32_t len = endpos - startpos; - float len_mm = len / 51200.0 * 8.0; // 导程8mm - int32_t pointnum = len_mm * 10; // 1mm 分成10份 - return pointnum; -} - -void OneDimensionalCodeLaserScanner::dumpcodecache(code_cache_t* cache) { -#if 0 - ZLOGI(TAG, "startpos:%d,endpos:%d,pointnum:%d", cache->startpos, cache->endpos, cache->pointnum); - for (int i = 0; i < cache->pointnum; i++) { - printf("%d", cache->codecache[i]); - } - printf("\n"); -#endif -} - -#define BITVAL(code, off) ((code & (1 << off)) >> off) - -void OneDimensionalCodeLaserScanner::decode(uint32_t rawcode) { // - // Lot:3:6 - // bit3->bit3, bit4->bit2, bit5->bit1, bit6->bit0 - uint32_t lot = 0; - lot = BITVAL(rawcode, 3) << 3 | BITVAL(rawcode, 4) << 2 | BITVAL(rawcode, 5) << 1 | BITVAL(rawcode, 6) << 0; - - // =(bit1)*2^6+(bit2)*2^5+(bit11)*2^4+(bit10)*2^0+(bit9)*2^1+(bit8)*2^2+(bit7)*2^3 - uint32_t item = 0; - item = (BITVAL(rawcode, 1) << 6) // - | (BITVAL(rawcode, 2) << 5) // - | (BITVAL(rawcode, 11) << 4) // - | (BITVAL(rawcode, 10) << 0) // - | (BITVAL(rawcode, 9) << 1) // - | (BITVAL(rawcode, 8) << 2) // - | (BITVAL(rawcode, 7) << 3); - - ZLOGI(TAG, "item-lot: %d-%d\n", item, lot); - m_item->setValue(item); - m_lot->setValue(lot); -} - -void OneDimensionalCodeLaserScanner::parsecode(code_cache_t* cache) { // - // - - /** - * @brief - * 14:0 - * 100 XXXX XXXX XXX1 - * - * 解码思路 - * 1. 通过100标定起始位置 - * 2. 然后依据每一位长度为1.2mm计算出其他各个相对bit的坐标 - * - */ - - // 1. 找到100,1对应的坐标 - int bit14startpos = -1; - int bit14endpos = -1; - - for (int32_t i = cache->pointnum; i > 0; i--) { - if (bit14endpos == -1) { - if (cache->codecache[i]) { - bit14endpos = i; - } - - } else if (bit14startpos == -1) { - if (!cache->codecache[i]) { - bit14startpos = i; - } - } else { - break; - } - } - - if (bit14startpos == -1 || bit14endpos == -1) { - ZLOGE(TAG, "find bit14 failed"); - m_errReg->setValue(kerr_findbit14fail); - return; - } - - int bit14len = bit14endpos - bit14startpos; - int bit14off = bit14len / 2 + bit14startpos; - int bit0off = bit14off - 14 * 1.2 * 10.0; - - if (bit0off < 0) { - ZLOGE(TAG, "find bit0 failed"); - m_errReg->setValue(kerr_findbit0fail); - } - // - bool bits[15] = {0}; - for (int i = 0; i < 15; i++) { - bits[i] = cache->codecache[int32_t(bit0off + i * 1.2 * 10.0)]; - } - - int32_t code = 0; - for (int i = 0; i < 15; i++) { - code |= (bits[i] << i); - } - m_errReg->setValue(kerr_success); - m_codeReg->setValue(code); - printf("code:"); - for (int i = 0; i < 15; i++) { - printf("%d", bits[i]); - } - printf("\n"); - ZLOGI(TAG, "parse success,code:0x%0x", (uint32_t)code); - decode(code); - // return; -} - -void OneDimensionalCodeLaserScanner::parseResult() { // - m_codeReg->setValue(-1); - m_item->setValue(-1); - m_lot->setValue(-1); - - static code_cache_t rawcodecache = {0}; - memset(&rawcodecache, 0, sizeof(rawcodecache)); - - /** - * @brief 初步分析数据 - */ - int32_t len = m_endpos - m_startpos; - int32_t pointnum = compute_point_num(m_startpos, m_endpos); - - ZLOGI(TAG, "pointnum:%d", pointnum); - - if ((size_t)pointnum > sizeof(rawcodecache.codecache)) { - ZLOGE(TAG, "len too long"); - m_errReg->setVal(kerr_scanRangeTooLarge); - return; - } - - if (m_endpos <= m_cfg.codeendpos) { - ZLOGE(TAG, "stop too early"); - m_errReg->setVal(kerr_scanEndTooEarly); - return; - } - - if (m_startpos >= m_cfg.codestartpos) { - ZLOGE(TAG, "start too late"); - m_errReg->setVal(kerr_scanStartTooLate); - return; - } - - /** - * @brief 将位置信息转变成,1,0的形式,方便解析数据 - * - * 1111110000111111111100001111000 - * - */ - - int32_t sp = m_startpos; - int32_t ep = m_endpos; - - for (int i = 0; i < pointnum; i++) { - rawcodecache.codecache[i] = getPosLevel((int32_t)(sp + len / (pointnum * 1.0) * i)); - } - rawcodecache.startpos = sp; - rawcodecache.pointnum = pointnum; - rawcodecache.endpos = ep; - - dumpcodecache(&rawcodecache); - /** - * @brief 由于扫描的范围要比编码的范围大,所以需要将多余的数据去掉 - * - */ - { - int32_t after_cut_code_nums = compute_point_num(m_cfg.codestartpos, m_cfg.codeendpos); - - float distance = (m_cfg.codestartpos - rawcodecache.startpos) / 51200.0 * 8.0; - int startpointoff = distance * 10; - - rawcodecache.startpos = m_cfg.codestartpos; - rawcodecache.pointnum = after_cut_code_nums; - rawcodecache.endpos = m_cfg.codeendpos; - memmove(rawcodecache.codecache, rawcodecache.codecache + startpointoff, after_cut_code_nums); - ZLOGI(TAG, "after_cut: code_nums:%d,cutoff:%d", after_cut_code_nums, startpointoff); - } - - dumpcodecache(&rawcodecache); - - /** - * @brief 分析数据 - */ - - parsecode(&rawcodecache); -} -void OneDimensionalCodeLaserScanner::onGpioIrq(ZGPIO* GPIO_Pin, ZGPIO::IrqTypeEvent_t irqevent) { // - if (!m_workflag) { - return; - } - if (m_off >= 50) return; - m_posChache[m_off] = m_readder(); - m_off++; -} diff --git a/usrc/one_dimensional_code_laser_scanner.hpp b/usrc/one_dimensional_code_laser_scanner.hpp deleted file mode 100644 index 18684ab..0000000 --- a/usrc/one_dimensional_code_laser_scanner.hpp +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once -#include -#include - -#include "sdk/hal/zhal.hpp" -#include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp" - -#define REG_CODE_SCANER_ACT_CTRL (0) // 扫码器控制 -#define REG_CODE_SCANER_ACT_CLEAR_EXCEPTION (1) // 清除异常 - -#define REG_CODE_SCANER_STAT_STATUS (5) // 设备状态 -#define REG_CODE_SCANER_STAT_ERROR (6) // 上次采集结果是否有误 -#define REG_CODE_SCANER_CODE (7) // 码存放的地方 -#define REG_CODE_SCANER_ITEM (8) // 码存放的地方 -#define REG_CODE_SCANER_LOT (9) // 码存放的地方 - -namespace iflytop { -using namespace std; - -#define POS_CACHE_SIZE 50 - -class OneDimensionalCodeLaserScanner : public ICPSListener { - public: - typedef struct { - bool codecache[500]; - int32_t pointnum; - int32_t startpos; - int32_t endpos; - } code_cache_t; - - typedef enum { - kidle, - kworking, - } code_scan_state_t; - - typedef enum { - kerr_success = 0, // - kerr_scanRangeTooLarge = 1, // - kerr_scanRangeTooSmall = 2, // - kerr_scanStartTooLate = 3, // - kerr_scanEndTooEarly = 4, // - kerr_findbit14fail = 5, // - kerr_findbit0fail = 6, // - } error_type_t; - - typedef function PosReadder_t; - - typedef struct { - Pin_t triggerPin; - PosReadder_t readder; - int32_t codestartpos; - int32_t codeendpos; - } cfg_t; - - private: - IflytopCanProtocolStackProcesser* m_protocolProcesser = NULL; - ICPSSlaveModule* m_slave = NULL; - int m_regStartOff = 0; - PosReadder_t m_readder; - - icps::Reg_t* m_statusReg = NULL; - icps::Reg_t* m_errReg = NULL; - icps::Reg_t* m_codeReg = NULL; - icps::Reg_t* m_item = NULL; - icps::Reg_t* m_lot = NULL; - - ZGPIO m_triggerGpio; - bool m_workflag = false; - - int32_t m_off = 0; - int32_t m_posChache[POS_CACHE_SIZE]; - int32_t m_endpos = 0; - int32_t m_startpos = 0; - bool m_idleLevel = true; - - cfg_t m_cfg; - - public: - OneDimensionalCodeLaserScanner(){}; - ~OneDimensionalCodeLaserScanner(){}; - - void initialize(IflytopCanProtocolStackProcesser* protocolProcesser, int32_t regStartOff, cfg_t* cfg); - - virtual icps::error_t onHostRegisterWriteEvent(icps::WriteEvent* event); - - void startScan(); - void stopScan(); - void parseResult(); - - private: - void parsecode(code_cache_t* cache); - void decode(uint32_t rawcode); - - int32_t compute_point_num(int32_t startpos, int32_t endpos); - - void dumpcodecache(code_cache_t* cache); - - void onGpioIrq(ZGPIO* GPIO_Pin, ZGPIO::IrqTypeEvent_t irqevent); - bool getPosLevel(int pos); -}; -} // namespace iflytop