diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 85fd87c..10c2faa 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/Core/Src/main.c b/Core/Src/main.c index 3261b76..c9f416b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -106,7 +106,7 @@ int main(void) MX_CAN1_Init(); MX_USART2_UART_Init(); MX_USART3_UART_Init(); - MX_IWDG_Init(); + // MX_IWDG_Init(); /* USER CODE BEGIN 2 */ extern void umain(); diff --git a/sdk b/sdk index 22d7d68..5751b82 160000 --- a/sdk +++ b/sdk @@ -1 +1 @@ -Subproject commit 22d7d685c4a5adaa0e3515a290fdb5c31d594b39 +Subproject commit 5751b8278aab13053c715341d3c1fde48f770280 diff --git a/usrc/can_cmd_reg_fn_reg.cpp b/usrc.bak/can_cmd_reg_fn_reg.cpp similarity index 100% rename from usrc/can_cmd_reg_fn_reg.cpp rename to usrc.bak/can_cmd_reg_fn_reg.cpp diff --git a/usrc/device.cpp b/usrc.bak/device.cpp similarity index 99% rename from usrc/device.cpp rename to usrc.bak/device.cpp index c47e3a3..58e4d10 100644 --- a/usrc/device.cpp +++ b/usrc.bak/device.cpp @@ -155,7 +155,7 @@ void device_init() { } OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, false, true); - OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, false, true); + OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, true, false); } void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) { diff --git a/usrc/device.hpp b/usrc.bak/device.hpp similarity index 100% rename from usrc/device.hpp rename to usrc.bak/device.hpp diff --git a/usrc.bak/driver/preportional_valve_ctrl.cpp b/usrc.bak/driver/preportional_valve_ctrl.cpp new file mode 100644 index 0000000..e6200b8 --- /dev/null +++ b/usrc.bak/driver/preportional_valve_ctrl.cpp @@ -0,0 +1,104 @@ +#include "preportional_valve_ctrl.hpp" +using namespace iflytop; + +#define WORK_STATE_REG 0x0000 +#define CTRL_STATE_REG 0x0001 +#define POS_STATE_REG 0x0013 + +void PreportionalValveCtrl::initialize(UART_HandleTypeDef* huart) { m_modbusBlockHost.initialize(huart); } + +int32_t PreportionalValveCtrl::setValvePos(int32_t valueid, int32_t pos) { // + + /** + * @brief 发送失败重发3次 + */ + for (size_t i = 0; i < 3; i++) { + int32_t err = _setValvePos(valueid, pos); + if (err == 0) return 0; + } + return 1; +} +int32_t PreportionalValveCtrl::_setValvePos(int32_t valueid, int32_t pos) { // + if (valueid > 255 || valueid < 1) { + return 1; + } + bool ret = m_modbusBlockHost.writeReg06(valueid, CTRL_STATE_REG, pos, 30); + if (!ret) return 1; + m_last_set_valve_ticket = HAL_GetTick(); + m_targetpos[valueid] = pos; + return 0; +} +int32_t PreportionalValveCtrl::getValvePos(int32_t valueid, int32_t* pos) { + // return m_modbusBlockHost.readReg03(valueid, POS_STATE_REG, pos, 30); + uint16_t pos16 = 0; + bool ret = m_modbusBlockHost.readReg03(valueid, POS_STATE_REG, &pos16, 30); + *pos = pos16; + if (!ret) return 1; + return 0; +} +int32_t PreportionalValveCtrl::getValveOrderPos(int32_t valueid, int32_t* pos) { + uint16_t pos16 = 0; + bool ret = m_modbusBlockHost.readReg03(valueid, CTRL_STATE_REG, &pos16, 30); + *pos = pos16; + if (!ret) return 1; + return 0; +} +#define TAG "PreportionalValveCtrl" + +int32_t PreportionalValveCtrl::isBusy(int32_t valueid, int32_t* busy) { +#if 1 + int32_t orderpos = 0; + int32_t pos = 0; + int32_t err = 0; + +#if 0 + err = getValveOrderPos(valueid, &orderpos); + if (err != 0) return err; +#endif + orderpos = m_targetpos[valueid]; + + err = getValvePos(valueid, &pos); + if (err != 0) return err; + + if (abs(m_targetpos[valueid] - pos) <= 11) { + *busy = 0; + } else { + *busy = 1; + } + return 0; +#endif + +#if 0 + + /** + * @brief + * 比例阀当前状态的更新是有延迟的,例如下发指令控制比例阀开合到90%,下发执行后(收到指令的回执), + * 此时读取比例阀状态,比例阀的状态并非运动中,而是stop,需要等待一段时间后,再读取状态,状态才是运动中。 + */ + if (haspassedms(m_last_set_valve_ticket) < 300) { + *busy = 1; + return 0; + } + int32_t valve1state = 0; + int32_t err = 0; + + err = getValveWorkState(1, &valve1state); + if (err != 0) return err; + + if (valve1state == kstate_stop) { + *busy = 0; + } else { + *busy = 1; + } + return 0; + +#endif +} + +int32_t PreportionalValveCtrl::getValveWorkState(int32_t valueid, int32_t* state) { + uint16_t state16 = 0; + bool ret = m_modbusBlockHost.readReg03(valueid, WORK_STATE_REG, &state16, 30); + *state = state16; + if (!ret) return 1; + return 0; +} \ No newline at end of file diff --git a/usrc.bak/driver/preportional_valve_ctrl.hpp b/usrc.bak/driver/preportional_valve_ctrl.hpp new file mode 100644 index 0000000..3baf7d5 --- /dev/null +++ b/usrc.bak/driver/preportional_valve_ctrl.hpp @@ -0,0 +1,51 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include + +#include "sdk/hal/zhal.hpp" +#include "sdk\components\modbus\modbus_block_host.hpp" + +/** + * @brief + * + * https://iflytop1.feishu.cn/wiki/GQwCwHMqFiaJRwks80ncwaYKnSe + */ + +namespace iflytop { +using namespace std; +class PreportionalValveCtrl { + public: + typedef enum { + kstate_stop = 0x0, + kstate_running_forward = 0xaa, + kstate_running_backward = 0xbb, + kstate_err_state = 0xea, + } work_state_t; + + private: + /* data */ + ModbusBlockHost m_modbusBlockHost; + int32_t val = 0; + uint32_t m_last_set_valve_ticket = 0; + uint16_t m_targetpos[255]; + + public: + PreportionalValveCtrl(){}; + ~PreportionalValveCtrl(){}; + + void initialize(UART_HandleTypeDef* huart); + int32_t setValvePos(int32_t valueid, int32_t pos); + int32_t getValvePos(int32_t valueid, int32_t* pos); + int32_t getValveOrderPos(int32_t valueid, int32_t* pos); + int32_t isBusy(int32_t valueid, int32_t* busy); + int32_t getValveWorkState(int32_t valueid, int32_t* state); + + private: + int32_t _setValvePos(int32_t valueid, int32_t pos); +}; + +} // namespace iflytop \ No newline at end of file diff --git a/usrc.bak/main.cpp b/usrc.bak/main.cpp new file mode 100644 index 0000000..64fe625 --- /dev/null +++ b/usrc.bak/main.cpp @@ -0,0 +1,29 @@ +#include "device.hpp" +#define TAG "main" +namespace iflytop { +extern void uart_debug_fn_reg(); +extern void can_cmd_reg_fn_reg(); +}; // namespace iflytop + +using namespace iflytop; +extern "C"{ +void umain(void) { + ZHALCORE::cfg_t oscfg = { + .delayhtim = &DELAY_US_TIMER, + .debuguart = &DEBUG_UART, + }; + ZHALCORE::getInstance()->initialize(oscfg); + + ZLOGI(TAG, "pipeline_disinfection_liquid_path_control:%s", VERSION); + + device_init(); + uart_debug_fn_reg(); + can_cmd_reg_fn_reg(); + ZLOGI(TAG, "init done"); + while (1) { + cmdScheduler.schedule(); + ZHALCORE::getInstance()->loop(); + HAL_IWDG_Refresh(&hiwdg); + } +} +} diff --git a/usrc.bak/project.hpp b/usrc.bak/project.hpp new file mode 100644 index 0000000..074c2c0 --- /dev/null +++ b/usrc.bak/project.hpp @@ -0,0 +1,24 @@ +#pragma once + +#define VERSION "v1.2" +// 设备ID +#define DEVICE_ID (2) +// 调试串口 +#define DEBUG_UART huart1 +// 调试指示灯 +#define DEBUG_LIGHT_GPIO PE8 +// 微秒延迟定时器,注意该延时定时器需要按照以下文档进行配置 +// http://192.168.1.3:3000/zwikipedia/iflytop_wikipedia/src/branch/master/doc/stm32cubemx_us_timer.md +#define DELAY_US_TIMER htim6 +#define MICROSWITCH_NUM 6 +// +#define MOTOR_SPI hspi1 + +#define TMC5130_MOTOR_NUM 2 +#define MOTOR_CH(n) (n) +// FYBJ_PY +#define MOTOR2_CSN PA4 // +#define MOTOR2_ENN PE11 +// FYBJ_TJ_DRV +#define MOTOR1_CSN PC4 // +#define MOTOR1_ENN PE12 diff --git a/usrc/uart_debug_fn_reg.cpp b/usrc.bak/uart_debug_fn_reg.cpp similarity index 100% rename from usrc/uart_debug_fn_reg.cpp rename to usrc.bak/uart_debug_fn_reg.cpp diff --git a/usrc/driver/preportional_valve_ctrl.cpp b/usrc/driver/preportional_valve_ctrl.cpp index e6200b8..4cc79e9 100644 --- a/usrc/driver/preportional_valve_ctrl.cpp +++ b/usrc/driver/preportional_valve_ctrl.cpp @@ -67,32 +67,6 @@ int32_t PreportionalValveCtrl::isBusy(int32_t valueid, int32_t* busy) { } return 0; #endif - -#if 0 - - /** - * @brief - * 比例阀当前状态的更新是有延迟的,例如下发指令控制比例阀开合到90%,下发执行后(收到指令的回执), - * 此时读取比例阀状态,比例阀的状态并非运动中,而是stop,需要等待一段时间后,再读取状态,状态才是运动中。 - */ - if (haspassedms(m_last_set_valve_ticket) < 300) { - *busy = 1; - return 0; - } - int32_t valve1state = 0; - int32_t err = 0; - - err = getValveWorkState(1, &valve1state); - if (err != 0) return err; - - if (valve1state == kstate_stop) { - *busy = 0; - } else { - *busy = 1; - } - return 0; - -#endif } int32_t PreportionalValveCtrl::getValveWorkState(int32_t valueid, int32_t* state) { diff --git a/usrc/hardware.cpp b/usrc/hardware.cpp new file mode 100644 index 0000000..c002161 --- /dev/null +++ b/usrc/hardware.cpp @@ -0,0 +1,363 @@ +#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 "driver/preportional_valve_ctrl.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; + +ZGPIO OUT_PD14; +ZGPIO OUT_PD15; + +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); +} +PreportionalValveCtrl m_PreportionalValveHost; + +int32_t preportional_valve_is_busy(int32_t *busy) { + int32_t valve1state = 0; + int32_t valve2state = 0; + int32_t err = 0; + *busy = 1; + + err = m_PreportionalValveHost.isBusy(1, &valve1state); + if (err != 0) return err; + err = m_PreportionalValveHost.isBusy(2, &valve2state); + if (err != 0) return err; + + if (valve1state == 0 && valve2state == 0) { + *busy = 0; + } else { + *busy = 1; + } + return 0; +} + +void air_compressor_read_pressure(int32_t *ack) { // + DP600PressureSensor::sensor_data_t d = m_huachengPressureSensor.readsensordata(2); + *ack = d.value; +} + +void Hardware::initialize(int deviceId) { + m_device_id = deviceId; + // 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_motor2.rotate(1000000); + } + + triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, true, false); + triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, true, false); + triLight_Y.initAsOutput(PD9, ZGPIO::kMode_nopull, true, false); + triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, true, false); + +#if 0 + m_dp600PressureSensor2.initialize(&huart3, 2); + m_dp600PressureSensor3.initialize(&huart3, 3); + m_dp600PressureSensor4.initialize(&huart3, 4); + + OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, false, true); + OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, true, false); +#endif +} + +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; + if (fromwhere == kuart) printf("rx: cmdid:%d subcmdid:%d id:%d\n", cmdheader->cmdid, cmdheader->subcmdid, cmdheader->data[0]); + + /** + * @brief Ping + */ + PROCESS_CMD(kcmd_ping, 0, m_device_id) { + receipt[0] = cmdheader->data[0]; + receiptsize = 1; + return 0; + } + + /** + * @brief 控制加液泵 + */ + PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1, 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]; + + printf("kcmd_peristaltic_pump_ctl 1 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power); + + setmotor(&m_motor1, acc, rpm, idlepower, power); + receipt[0] = cmdheader->data[0]; + receiptsize = 1; + return 0; + } + + /** + * @brief 控制-喷液泵 + */ + PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1, 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]; + printf("kcmd_peristaltic_pump_ctl 2 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power); + + setmotor(&m_motor2, acc, rpm, idlepower, power); + receipt[0] = cmdheader->data[0]; + receiptsize = 1; + return 0; + } + + /** + * @brief 三色指示灯控制 + */ + PROCESS_CMD(kcmd_triple_warning_light_ctl, 0, 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; + } + + if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_valve1_set) && (cmdheader->subcmdid == 0)) { + uint32_t val = *(uint32_t *)(&cmdheader->data[0]); + OUT_PD14.setState(val != 0); + matching = true; + } + + if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_valve2_set) && (cmdheader->subcmdid == 0)) { + uint32_t val = *(uint32_t *)(&cmdheader->data[0]); + OUT_PD14.setState(val != 0); + matching = true; + } + + if ((cmdheader->cmdid == (uint16_t)kcmd_proportional_read_state) && (cmdheader->subcmdid == 0)) { + matching = true; + + int32_t ack = 0; + int32_t ecode = preportional_valve_is_busy(&ack); + + int32_t *p_receipt_32 = (int32_t *)receipt; + *p_receipt_32 = ack; + receiptsize = 4; + } + + if ((cmdheader->cmdid == (uint16_t)kcmd_set_proportional_valve) && (cmdheader->subcmdid == 0)) { + int32_t para0 = *(int32_t *)(&cmdheader->data[0]); + int32_t para1 = *(int32_t *)(&cmdheader->data[4]); + if (para0 == 1) { + matching = true; + m_PreportionalValveHost.setValvePos(1, para1); + } else if (para0 == 2) { + matching = true; + m_PreportionalValveHost.setValvePos(2, para1); + } + } + static DP600PressureSensor::sensor_data_t dp600data; + if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_read_pressure) && (cmdheader->subcmdid == 0)) { + matching = true; + bool suc = m_dp600PressureSensor2.readVal(&dp600data); + if (!suc) return 1002; + int32_t *p_receipt_32 = (int32_t *)receipt; + *p_receipt_32 = dp600data.value; + receiptsize = 4; + } + +#if 0 + /** + * @brief 获取蒸发仓水浸状态 + */ + PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0, 0) { + ((int32_t *)receipt)[0] = !IO_PC7_IN.getState(); + receiptsize = 4; + return 0; + } + + /** + * @brief 获取设备底盘水浸传感器 + */ + PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0, 1) { + ((int32_t *)receipt)[0] = !IO_PD13_IN.getState(); + receiptsize = 4; + return 0; + } + +#endif + + /** + * @brief 液位测量压力传感器 + */ + PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 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, 0, 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, 0, 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, 0, 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; + } + + return 0; +} +void Hardware::loop() {} diff --git a/usrc/hardware.hpp b/usrc/hardware.hpp new file mode 100644 index 0000000..be3a8dc --- /dev/null +++ b/usrc/hardware.hpp @@ -0,0 +1,15 @@ +#include + +namespace iflytop { +class Hardware { + public: + typedef enum { kcan, kuart } from_where_t; + int32_t m_device_id = 0; + + public: + void initialize(int deviceId); + 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 64fe625..41c5549 100644 --- a/usrc/main.cpp +++ b/usrc/main.cpp @@ -1,29 +1,157 @@ -#include "device.hpp" +#include "main.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 "hardware.hpp" +#include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp" +#include "sdk\components\string_utils.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" + #define TAG "main" namespace iflytop { -extern void uart_debug_fn_reg(); -extern void can_cmd_reg_fn_reg(); -}; // namespace iflytop +Main gmain; +}; using namespace iflytop; -extern "C"{ -void umain(void) { + +void dumphexdata(uint8_t *data, int32_t len) { + for (int32_t i = 0; i < len; i++) { + printf("%02X ", data[i]); + } + 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); + 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, receipt_size); + } + } +} +/** + * @brief 处理串口接收到的消息 + */ + +static void processUARTEachLine(char *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 = StringUtils::hex_str_to_bytes((char *)packet, len, bytelen); + if (hexbytes == NULL) { + ZLOGE(TAG, "hex_str_to_bytes failed"); + return; + } + dumphexdata(hexbytes, bytelen); + int32_t ecode = m_hardware.process_rx_packet(Hardware::kuart, hexbytes, bytelen, rxdata, receipt_size, match); + if (match) { + printf("match\n"); + if (ecode < 0) { + printf("ecode :%d\n", ecode); + return; + } + dumphexdata(rxdata, receipt_size); + } + printf("\n"); +} + +static void processUartRX(uint8_t *packet, size_t len) { + for (size_t i = 0; i < len; i++) { + if (packet[i] == '\n' || packet[i] == '\r') { + packet[i] = '\0'; + } + } + + for (size_t i = 0; i < len; i++) { + if (i == 0) { + processUARTEachLine((char *)packet, strlen((char *)packet)); + } else if (packet[i - 1] == '\0' && packet[i] != '\0') { + processUARTEachLine((char *)packet + i, strlen((char *)packet + i)); + } + } +} + +/******************************************************************************* + * MAIN * + *******************************************************************************/ +void Main::run() { ZHALCORE::cfg_t oscfg = { .delayhtim = &DELAY_US_TIMER, .debuguart = &DEBUG_UART, }; ZHALCORE::getInstance()->initialize(oscfg); - ZLOGI(TAG, "pipeline_disinfection_liquid_path_control:%s", VERSION); + ZLOGI(TAG, "little_disinfection_liquid_path_control:%s", VERSION); + + debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false); + ZHAL_CORE_REG(200, { debuglight.toggleState(); }); + + m_hardware.initialize(DEVICE_ID); + + 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); }); + uartreceiver.startRxIt(); + + ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID); + m_canReceiver.init(cfg); + m_canReceiver.registerListener(this); - device_init(); - uart_debug_fn_reg(); - can_cmd_reg_fn_reg(); ZLOGI(TAG, "init done"); while (1) { - cmdScheduler.schedule(); ZHALCORE::getInstance()->loop(); - HAL_IWDG_Refresh(&hiwdg); + m_hardware.loop(); + uartreceiver.forceCchedule(); } } -} diff --git a/usrc/main.hpp b/usrc/main.hpp new file mode 100644 index 0000000..5767d46 --- /dev/null +++ b/usrc/main.hpp @@ -0,0 +1,27 @@ +#pragma once +#include +#include + +#include + +#include "sdk\components\iflytop_can_slave_modules\device_base_control_service.hpp" +#include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp" +#include "sdk\components\zcanreceiver\zcanreceiver.hpp" +namespace iflytop { +using namespace std; +class Main : public ZCanRceiverListener { + private: + /* data */ + public: + virtual void onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len); + + Main(/* args */){}; + ~Main(){}; + void run(); +}; +extern Main gmain; +} // namespace iflytop + +extern "C" { +void umain(void) { iflytop::gmain.run(); } +} diff --git a/usrc/project.hpp b/usrc/project.hpp index 074c2c0..5c48e57 100644 --- a/usrc/project.hpp +++ b/usrc/project.hpp @@ -1,6 +1,6 @@ #pragma once -#define VERSION "v1.2" +#define VERSION "v1.1" // 设备ID #define DEVICE_ID (2) // 调试串口 @@ -21,4 +21,4 @@ #define MOTOR2_ENN PE11 // FYBJ_TJ_DRV #define MOTOR1_CSN PC4 // -#define MOTOR1_ENN PE12 +#define MOTOR1_ENN PE12 \ No newline at end of file