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.

335 lines
12 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
  1. #include "main.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include "main.h"
  5. #include "project.hpp"
  6. //
  7. #include "one_dimensional_code_laser_scanner.hpp"
  8. // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
  9. #include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp"
  10. #include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp"
  11. #include "sdk/hal/zhal.hpp"
  12. #include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp"
  13. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
  14. #include "sdk\components\m3078\m3078_code_scaner.hpp"
  15. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  16. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  17. //
  18. #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
  19. #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
  20. #include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
  21. #include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
  22. #include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
  23. //
  24. #include <stdint.h>
  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. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  53. int32_t ppm = rpm / 60.0 * 51200;
  54. int32_t acc = acc_rpm2 / 60.0 * 51200;
  55. int16_t _idlepower = 1;
  56. int16_t _power = 31;
  57. if (idlepower > 0 && idlepower < 31) {
  58. _idlepower = idlepower;
  59. }
  60. if (power > 0 && power < 31) {
  61. _power = power;
  62. }
  63. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  64. motor->setAcceleration(acc);
  65. motor->setDeceleration(acc);
  66. motor->rotate(ppm);
  67. }
  68. void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  69. ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
  70. for (size_t i = 0; i < len; i++) {
  71. printf("%02X ", packet[i]);
  72. }
  73. printf("\n");
  74. }
  75. void Main::run() {
  76. ZHALCORE::cfg_t oscfg = {
  77. .delayhtim = &DELAY_US_TIMER,
  78. .debuguart = &DEBUG_UART,
  79. };
  80. ZHALCORE::getInstance()->initialize(oscfg);
  81. ZLOGI(TAG, "pipeline_disinfection_liquid_path_control:%s", VERSION);
  82. printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
  83. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  84. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  85. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  86. m_canReceiver.init(cfg);
  87. m_canReceiver.registerListener(this);
  88. /**
  89. * @brief
  90. */
  91. m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  92. m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  93. m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  94. m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  95. m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  96. m_basicOrderModule.initialize(&m_canReceiver);
  97. m_basicOrderModule.regInputCtl([this](uint8_t id, bool &val) {
  98. if (id == 1) {
  99. val = m_input1.getState();
  100. return true;
  101. }
  102. if (id == 2) {
  103. val = m_input2.getState();
  104. return true;
  105. }
  106. if (id == 3) {
  107. val = m_input3.getState();
  108. return true;
  109. }
  110. if (id == 4) {
  111. val = m_input4.getState();
  112. return true;
  113. }
  114. if (id == 5) {
  115. val = m_input5.getState();
  116. return true;
  117. }
  118. return false;
  119. });
  120. output0.initAsOutput(PD14, ZGPIO::kMode_nopull, false, false);
  121. output1.initAsOutput(PD15, ZGPIO::kMode_nopull, false, false);
  122. m_basicOrderModule.regOutCtl([this](uint8_t id, bool val) {
  123. if (id == 20) {
  124. output0.setState(val);
  125. return true;
  126. }
  127. if (id == 21) {
  128. output1.setState(val);
  129. return true;
  130. }
  131. // output0
  132. // output1
  133. return false;
  134. });
  135. ZHAL_CORE_REG(3000, {
  136. // ZLOGI(TAG, "IO1:%d IO2:%d", m_input1.getState(), m_input2.getState());
  137. // ZLOGI(TAG, "IO1:%d IO2:%d IO3:%d IO4:%d IO5:%d", m_input1.getState(), m_input2.getState(), m_input3.getState(),
  138. // m_input4.getState(), m_input5.getState());
  139. });
  140. /*******************************************************************************
  141. * *
  142. *******************************************************************************/
  143. {
  144. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  145. m_motor1.initialize(&cfg);
  146. int32_t chipv = m_motor1.readChipVERSION();
  147. ZLOGI(TAG, "m_motor1:%lx", chipv);
  148. m_motor1.setIHOLD_IRUN(1, 20, 0);
  149. m_motor1.setMotorShaft(true);
  150. m_motor1.setAcceleration(300000);
  151. m_motor1.setDeceleration(300000);
  152. // m_motor1.rotate(1000000);
  153. }
  154. {
  155. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  156. m_motor2.initialize(&cfg);
  157. int32_t chipv = m_motor2.readChipVERSION();
  158. ZLOGI(TAG, "m_motor2:%lx", chipv);
  159. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  160. m_motor2.setMotorShaft(true);
  161. m_motor2.setAcceleration(300000);
  162. m_motor2.setDeceleration(300000);
  163. // m_motor1.rotate(1000000);
  164. }
  165. m_pumpCtrlModule.initialize(&m_canReceiver);
  166. m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  167. ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm);
  168. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  169. });
  170. m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  171. ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm);
  172. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  173. });
  174. /*******************************************************************************
  175. * *
  176. *******************************************************************************/
  177. {
  178. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false);
  179. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false);
  180. triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false);
  181. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false);
  182. m_warningLightCtlModule.initialize(&m_canReceiver);
  183. m_warningLightCtlModule.regSubmodule(1, [&](uint8_t r, uint8_t g, uint8_t b, uint8_t beep) {
  184. ZLOGI(TAG, "warningLightCtlModule r:%d g:%d b:%d beep:%d", r, g, b, beep);
  185. triLight_R.setState(r != 0);
  186. triLight_G.setState(g != 0);
  187. triLight_B.setState(b != 0);
  188. triLight_BEEP.setState(beep != 0);
  189. });
  190. }
  191. /*******************************************************************************
  192. * *
  193. *******************************************************************************/
  194. // while (true) {
  195. // static ModbusBlockHost modbusBlockHost;
  196. // modbusBlockHost.initialize(&huart3);
  197. // int16_t val[1] = {0};
  198. // bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  199. // printf("suc:%d val:%d\n", suc, val[0]);
  200. // chip_delay_ms(1000);
  201. // }
  202. {
  203. m_huachengPressureSensor.initialize(&m_canReceiver);
  204. m_huachengPressureSensor.regSubmodule(1, [this](DP600PressureSensor::sensor_data_t *data) { //
  205. static ModbusBlockHost modbusBlockHost;
  206. modbusBlockHost.initialize(&huart3);
  207. int16_t val[1] = {0};
  208. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  209. if (!suc) {
  210. return false;
  211. }
  212. data->precision = 3;
  213. data->pressure_unit = 1;
  214. data->value = val[0];
  215. data->zero_point = 0;
  216. data->range_full_point = 0;
  217. return true;
  218. });
  219. m_huachengPressureSensor.regSubmodule(2, &huart3, 2);
  220. m_huachengPressureSensor.regSubmodule(3, &huart3, 3);
  221. m_huachengPressureSensor.regSubmodule(4, &huart3, 4);
  222. }
  223. /*******************************************************************************
  224. * *
  225. *******************************************************************************/
  226. static ZUART uartreceiver;
  227. static CmdSchedulerV2 cmdScheduler;
  228. static ZUART::cfg_t uartreceiver_cfg = {
  229. .name = "uartreceiver",
  230. .huart = &DEBUG_UART,
  231. .rxbuffersize = 512,
  232. .rxovertime_ms = 30,
  233. };
  234. uartreceiver.initialize(&uartreceiver_cfg);
  235. cmdScheduler.initialize(&uartreceiver);
  236. cmdScheduler.regCMD("setmotor1", "(int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power)", 4, //
  237. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  238. int16_t acc_rpm2 = atoi(paraV[0]);
  239. int16_t rpm = atoi(paraV[1]);
  240. int16_t idlepower = atoi(paraV[2]);
  241. int16_t power = atoi(paraV[3]);
  242. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  243. ack->setNoneAck(0);
  244. });
  245. cmdScheduler.regCMD("setmotor2", "(int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power)", 4, //
  246. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  247. int16_t acc_rpm2 = atoi(paraV[0]);
  248. int16_t rpm = atoi(paraV[1]);
  249. int16_t idlepower = atoi(paraV[2]);
  250. int16_t power = atoi(paraV[3]);
  251. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  252. ack->setNoneAck(0);
  253. });
  254. cmdScheduler.regCMD("setlight", "(uint8_t r, uint8_t g, uint8_t b, uint8_t beep)", 4, //
  255. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  256. uint8_t r = atoi(paraV[0]);
  257. uint8_t g = atoi(paraV[1]);
  258. uint8_t b = atoi(paraV[2]);
  259. uint8_t beep = atoi(paraV[3]);
  260. triLight_R.setState(r != 0);
  261. triLight_G.setState(g != 0);
  262. triLight_B.setState(b != 0);
  263. triLight_BEEP.setState(beep != 0);
  264. ack->setNoneAck(0);
  265. });
  266. cmdScheduler.regCMD("readio", "()", 0, //
  267. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  268. uint8_t sensorid = atoi(paraV[0]);
  269. ZLOGI(TAG, "IO1:%d IO2:%d IO3:%d IO4:%d IO5:%d", //
  270. m_input1.getState(), m_input2.getState(), m_input3.getState(), m_input4.getState(), m_input5.getState());
  271. ack->setNoneAck(0);
  272. });
  273. cmdScheduler.regCMD("pressure_sensor_read", "(uint8_t sensorid)", 1, //
  274. [](int32_t paramN, const char **paraV, ICmdParserACK *ack) {
  275. uint8_t sensorid = atoi(paraV[0]);
  276. DP600PressureSensor::sensor_data_t data = m_huachengPressureSensor.readsensordata(sensorid);
  277. ZLOGI(TAG, "precision:%d", data.precision);
  278. ZLOGI(TAG, "pressure_unit:%d", data.pressure_unit);
  279. ZLOGI(TAG, "value:%d", data.value);
  280. ZLOGI(TAG, "zero_point:%d", data.zero_point);
  281. ZLOGI(TAG, "range_full_point:%d", data.range_full_point);
  282. ack->setNoneAck(0);
  283. });
  284. ZLOGI(TAG, "init done");
  285. while (1) {
  286. cmdScheduler.schedule();
  287. ZHALCORE::getInstance()->loop();
  288. }
  289. }