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.

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