You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
14 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "main.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include "main.h"
  5. #include "project.hpp"
  6. //
  7. // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
  8. #include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp"
  9. #include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp"
  10. #include "sdk/hal/zhal.hpp"
  11. #include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp"
  12. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
  13. #include "sdk\components\m3078\m3078_code_scaner.hpp"
  14. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  15. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  16. //
  17. #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
  18. #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
  19. #include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
  20. #include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
  21. #include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
  22. //
  23. #include <stdint.h>
  24. #include "preportional_valve_ctrl.hpp"
  25. #include "sdk\components\cmdscheduler\cmd_scheduler_v2.hpp"
  26. #include "sdk\hal\zuart.hpp"
  27. #include "iwdg.h"
  28. #define TAG "main"
  29. namespace iflytop {
  30. Main gmain;
  31. };
  32. using namespace iflytop;
  33. IflytopCanProtocolStackProcesser m_protocolStack;
  34. TMC5130 m_motor1;
  35. TMC5130 m_motor2;
  36. ZGPIO debuglight;
  37. ZGPIO triLight_R;
  38. ZGPIO triLight_G;
  39. ZGPIO triLight_B;
  40. ZGPIO triLight_BEEP;
  41. ZGPIO m_input1;
  42. ZGPIO m_input2;
  43. ZGPIO m_input3;
  44. ZGPIO m_input4;
  45. ZGPIO m_input5;
  46. ZGPIO OUT_PD14;
  47. ZGPIO OUT_PD15;
  48. ZCanReceiver m_canReceiver;
  49. ZCanBasicOrderModule m_basicOrderModule;
  50. ZCanPumpCtrlModule m_pumpCtrlModule;
  51. ZCanTrigleWarningLightCtlModule m_warningLightCtlModule;
  52. HuachengPressureSensor m_huachengPressureSensor;
  53. PreportionalValveCtrl m_PreportionalValveHost;
  54. static CmdSchedulerV2 cmdScheduler;
  55. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  56. int32_t ppm = rpm / 60.0 * 51200;
  57. int32_t acc = acc_rpm2 / 60.0 * 51200;
  58. int16_t _idlepower = 1;
  59. int16_t _power = 31;
  60. if (idlepower > 0 && idlepower < 31) {
  61. _idlepower = idlepower;
  62. }
  63. if (power > 0 && power < 31) {
  64. _power = power;
  65. }
  66. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  67. motor->setAcceleration(acc);
  68. motor->setDeceleration(acc);
  69. motor->rotate(ppm);
  70. }
  71. static void air_compressor_ch_select(int32_t val) {
  72. if (val == 2) { // 内管路
  73. OUT_PD15.setState(1);
  74. } else if (val == 1) { // 空气
  75. OUT_PD15.setState(0);
  76. }
  77. }
  78. static void air_compressor_valve1_set(int32_t val) { OUT_PD14.setState(val != 0); }
  79. static void air_compressor_valve2_set(int32_t val) { OUT_PD14.setState(val != 0); }
  80. static void air_compressor_read_pressure(int32_t *ack) { //
  81. DP600PressureSensor::sensor_data_t d = m_huachengPressureSensor.readsensordata(2);
  82. *ack = d.value;
  83. }
  84. void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  85. // ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
  86. // for (size_t i = 0; i < len; i++) {
  87. // printf("%02X ", packet[i]);
  88. // }
  89. // printf("\n");
  90. }
  91. void Main::reg_debug_fn() {
  92. /*******************************************************************************
  93. * *
  94. *******************************************************************************/
  95. static ZUART uartreceiver;
  96. static ZUART::cfg_t uartreceiver_cfg = {
  97. .name = "uartreceiver",
  98. .huart = &DEBUG_UART,
  99. .rxbuffersize = 512,
  100. .rxovertime_ms = 30,
  101. };
  102. uartreceiver.initialize(&uartreceiver_cfg);
  103. cmdScheduler.initialize(&uartreceiver);
  104. cmdScheduler.regCMD("setmotor1", "(int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power)", 4,
  105. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  106. int16_t acc_rpm2 = atoi(paraV[0]);
  107. int16_t rpm = atoi(paraV[1]);
  108. int16_t idlepower = atoi(paraV[2]);
  109. int16_t power = atoi(paraV[3]);
  110. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  111. ack->setNoneAck(0);
  112. });
  113. cmdScheduler.regCMD("setmotor2", "(int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power)", 4,
  114. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  115. int16_t acc_rpm2 = atoi(paraV[0]);
  116. int16_t rpm = atoi(paraV[1]);
  117. int16_t idlepower = atoi(paraV[2]);
  118. int16_t power = atoi(paraV[3]);
  119. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  120. ack->setNoneAck(0);
  121. });
  122. cmdScheduler.regCMD("setlight", "(uint8_t r, uint8_t g, uint8_t b, uint8_t beep)", 4, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  123. uint8_t r = atoi(paraV[0]);
  124. uint8_t g = atoi(paraV[1]);
  125. uint8_t b = atoi(paraV[2]);
  126. uint8_t beep = atoi(paraV[3]);
  127. triLight_R.setState(r != 0);
  128. triLight_G.setState(g != 0);
  129. triLight_B.setState(b != 0);
  130. triLight_BEEP.setState(beep != 0);
  131. ack->setNoneAck(0);
  132. });
  133. cmdScheduler.regCMD("readio", "()", 0, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  134. uint8_t sensorid = atoi(paraV[0]);
  135. 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());
  136. ack->setNoneAck(0);
  137. });
  138. cmdScheduler.regCMD("air_compressor_ch_ctrl", "(id,val)", 2, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  139. air_compressor_ch_select(atoi(paraV[0]));
  140. ack->setNoneAck(0);
  141. });
  142. cmdScheduler.regCMD("set_proportional_valve", "(uint8_t id,uint8_t val)", 2, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  143. uint8_t id = atoi(paraV[0]);
  144. uint8_t val = atoi(paraV[1]);
  145. m_PreportionalValveHost.setValvePos(id, val);
  146. ack->setNoneAck(0);
  147. });
  148. cmdScheduler.regCMD("pressure_sensor_read", "(id)", 1, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  149. uint8_t id = atoi(paraV[0]);
  150. DP600PressureSensor::sensor_data_t d = m_huachengPressureSensor.readsensordata(id);
  151. ZLOGI(TAG, "pressure_sensor_read id:%d precision:%d pressure_unit:%d value:%d zero_point:%d range_full_point:%d", //
  152. id, d.precision, d.pressure_unit, d.value, d.zero_point, d.range_full_point);
  153. ack->setNoneAck(0);
  154. });
  155. cmdScheduler.regCMD("air_compressor_valve1_set", "(value)", 1, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  156. air_compressor_valve1_set(atoi(paraV[0]));
  157. ack->setNoneAck(0);
  158. });
  159. cmdScheduler.regCMD("air_compressor_valve2_set", "(value)", 1, [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  160. air_compressor_valve2_set(atoi(paraV[0]));
  161. ack->setNoneAck(0);
  162. });
  163. }
  164. void Main::reg_cancmd_fn() {
  165. // registerListener
  166. m_canReceiver.registerListener([this](CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  167. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  168. if (IS_CMD(cmdheader, kcmd_set_proportional_valve, 0)) {
  169. int32_t valueid = CMD_GET_PARAM(cmdheader, 0);
  170. int32_t value = CMD_GET_PARAM(cmdheader, 1);
  171. if (valueid == 1) {
  172. m_PreportionalValveHost.setValvePos(1, value);
  173. m_canReceiver.sendAck(cmdheader, NULL, 0);
  174. } else if (valueid == 2) {
  175. m_PreportionalValveHost.setValvePos(2, value);
  176. m_canReceiver.sendAck(cmdheader, NULL, 0);
  177. }
  178. }
  179. });
  180. m_canReceiver.registerListener([this](CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  181. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  182. if (IS_CMD(cmdheader, kcmd_air_compressor_read_pressure, 0)) {
  183. int32_t ack = 0;
  184. air_compressor_read_pressure(&ack);
  185. m_canReceiver.sendAck(cmdheader, (uint8_t *)&ack, 4);
  186. }
  187. });
  188. m_canReceiver.registerListener([this](CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  189. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  190. if (IS_CMD(cmdheader, kcmd_air_compressor_ch_select, 0)) {
  191. air_compressor_ch_select(CMD_GET_PARAM(cmdheader, 0));
  192. m_canReceiver.sendAck(cmdheader, NULL, 0);
  193. }
  194. });
  195. m_canReceiver.registerListener([this](CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  196. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  197. if (!IS_CMD(cmdheader, kcmd_air_compressor_valve1_set, 0)) return;
  198. air_compressor_valve1_set(CMD_GET_PARAM(cmdheader, 0));
  199. m_canReceiver.sendAck(cmdheader, NULL, 0);
  200. });
  201. m_canReceiver.registerListener([this](CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  202. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  203. if (!IS_CMD(cmdheader, kcmd_air_compressor_valve2_set, 0)) return;
  204. air_compressor_valve2_set(CMD_GET_PARAM(cmdheader, 0));
  205. m_canReceiver.sendAck(cmdheader, NULL, 0);
  206. });
  207. }
  208. void Main::run() {
  209. ZHALCORE::cfg_t oscfg = {
  210. .delayhtim = &DELAY_US_TIMER,
  211. .debuguart = &DEBUG_UART,
  212. };
  213. ZHALCORE::getInstance()->initialize(oscfg);
  214. ZLOGI(TAG, "pipeline_disinfection_liquid_path_control:%s", VERSION);
  215. printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
  216. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  217. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  218. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  219. m_canReceiver.init(cfg);
  220. m_canReceiver.registerListener(this);
  221. /**
  222. * @brief
  223. */
  224. m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  225. m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  226. m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  227. m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  228. m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  229. m_basicOrderModule.initialize(&m_canReceiver);
  230. m_basicOrderModule.regInputCtl([this](uint8_t id, bool &val) {
  231. if (id == 1) {
  232. val = m_input1.getState();
  233. return true;
  234. }
  235. if (id == 2) {
  236. val = m_input2.getState();
  237. return true;
  238. }
  239. if (id == 3) {
  240. val = m_input3.getState();
  241. return true;
  242. }
  243. if (id == 4) {
  244. val = m_input4.getState();
  245. return true;
  246. }
  247. if (id == 5) {
  248. val = m_input5.getState();
  249. return true;
  250. }
  251. return false;
  252. });
  253. m_PreportionalValveHost.initialize(&huart2);
  254. /*******************************************************************************
  255. * *
  256. *******************************************************************************/
  257. {
  258. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  259. m_motor1.initialize(&cfg);
  260. int32_t chipv = m_motor1.readChipVERSION();
  261. ZLOGI(TAG, "m_motor1:%lx", chipv);
  262. m_motor1.setIHOLD_IRUN(1, 20, 0);
  263. m_motor1.setMotorShaft(true);
  264. m_motor1.setAcceleration(300000);
  265. m_motor1.setDeceleration(300000);
  266. // m_motor1.rotate(1000000);
  267. }
  268. {
  269. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  270. m_motor2.initialize(&cfg);
  271. int32_t chipv = m_motor2.readChipVERSION();
  272. ZLOGI(TAG, "m_motor2:%lx", chipv);
  273. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  274. m_motor2.setMotorShaft(true);
  275. m_motor2.setAcceleration(300000);
  276. m_motor2.setDeceleration(300000);
  277. // m_motor1.rotate(1000000);
  278. }
  279. m_pumpCtrlModule.initialize(&m_canReceiver);
  280. m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  281. ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm);
  282. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  283. });
  284. m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  285. ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm);
  286. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  287. });
  288. /*******************************************************************************
  289. * *
  290. *******************************************************************************/
  291. {
  292. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, true, false);
  293. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, true, false);
  294. triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, true, false);
  295. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, true, false);
  296. m_warningLightCtlModule.initialize(&m_canReceiver);
  297. m_warningLightCtlModule.regSubmodule(1, [&](uint8_t r, uint8_t g, uint8_t b, uint8_t beep) {
  298. ZLOGI(TAG, "warningLightCtlModule r:%d g:%d b:%d beep:%d", r, g, b, beep);
  299. triLight_R.setState(r != 0);
  300. triLight_G.setState(g != 0);
  301. triLight_B.setState(b != 0);
  302. triLight_BEEP.setState(beep != 0);
  303. });
  304. }
  305. /*******************************************************************************
  306. * *
  307. *******************************************************************************/
  308. {
  309. m_huachengPressureSensor.initialize(&m_canReceiver);
  310. m_huachengPressureSensor.regSubmodule(1, [this](DP600PressureSensor::sensor_data_t *data) { //
  311. static ModbusBlockHost modbusBlockHost;
  312. modbusBlockHost.initialize(&huart3);
  313. int16_t val[1] = {0};
  314. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  315. if (!suc) {
  316. return false;
  317. }
  318. data->precision = 3;
  319. data->pressure_unit = 1;
  320. data->value = val[0];
  321. data->zero_point = 0;
  322. data->range_full_point = 0;
  323. return true;
  324. });
  325. m_huachengPressureSensor.regSubmodule(2, &huart3, 2);
  326. m_huachengPressureSensor.regSubmodule(3, &huart3, 3);
  327. m_huachengPressureSensor.regSubmodule(4, &huart3, 4);
  328. }
  329. OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, true, false);
  330. OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, true, false);
  331. reg_cancmd_fn();
  332. reg_debug_fn();
  333. ZLOGI(TAG, "init done");
  334. while (1) {
  335. cmdScheduler.schedule();
  336. ZHALCORE::getInstance()->loop();
  337. HAL_IWDG_Refresh(&hiwdg);
  338. // uint8_t txbuff[10] = {1,2,3,4,5};
  339. // m_canReceiver.sendPacket(txbuff,4);
  340. }
  341. }