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.

255 lines
8.3 KiB

2 years ago
2 years ago
1 year 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 year 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 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year 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. #define TAG "main"
  24. namespace iflytop {
  25. Main gmain;
  26. };
  27. using namespace iflytop;
  28. IflytopCanProtocolStackProcesser m_protocolStack;
  29. TMC5130 m_motor1;
  30. TMC5130 m_motor2;
  31. ZGPIO debuglight;
  32. ZGPIO triLight_R;
  33. ZGPIO triLight_G;
  34. ZGPIO triLight_B;
  35. ZGPIO triLight_BEEP;
  36. ZGPIO m_input1;
  37. ZGPIO m_input2;
  38. ZGPIO m_input3;
  39. ZGPIO m_input4;
  40. ZGPIO m_input5;
  41. ZGPIO m_input6;
  42. ZGPIO output1;
  43. ZGPIO output2;
  44. ZCanReceiver m_canReceiver;
  45. ZCanBasicOrderModule m_basicOrderModule;
  46. ZCanPumpCtrlModule m_pumpCtrlModule;
  47. ZCanTrigleWarningLightCtlModule m_warningLightCtlModule;
  48. HuachengPressureSensor m_huachengPressureSensor;
  49. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  50. int32_t ppm = rpm / 60.0 * 51200;
  51. int32_t acc = acc_rpm2 / 60.0 * 51200;
  52. int16_t _idlepower = 1;
  53. int16_t _power = 31;
  54. if (idlepower > 0 && idlepower < 31) {
  55. _idlepower = idlepower;
  56. }
  57. if (power > 0 && power < 31) {
  58. _power = power;
  59. }
  60. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  61. motor->setAcceleration(acc);
  62. motor->setDeceleration(acc);
  63. motor->rotate(ppm);
  64. }
  65. void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  66. ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
  67. for (size_t i = 0; i < len; i++) {
  68. printf("%02X ", packet[i]);
  69. }
  70. printf("\n");
  71. }
  72. void Main::run() {
  73. ZHALCORE::cfg_t oscfg = {
  74. .delayhtim = &DELAY_US_TIMER,
  75. .debuguart = &DEBUG_UART,
  76. };
  77. ZHALCORE::getInstance()->initialize(oscfg);
  78. ZLOGI(TAG, "liquid_path_control:%s", VERSION);
  79. printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
  80. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  81. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  82. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  83. m_canReceiver.init(cfg);
  84. m_canReceiver.registerListener(this);
  85. /**
  86. * @brief
  87. */
  88. m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  89. m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  90. m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  91. m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  92. m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  93. m_input6.initAsInput(PC7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  94. m_basicOrderModule.initialize(&m_canReceiver);
  95. m_basicOrderModule.regInputCtl([this](uint8_t id, bool &val) {
  96. if (id == 1) {
  97. val = m_input1.getState();
  98. return true;
  99. }
  100. if (id == 2) {
  101. val = m_input2.getState();
  102. return true;
  103. }
  104. if (id == 3) {
  105. val = m_input3.getState();
  106. return true;
  107. }
  108. if (id == 4) {
  109. val = m_input4.getState();
  110. return true;
  111. }
  112. if (id == 5) {
  113. val = m_input5.getState();
  114. return true;
  115. }
  116. if (id == 6) {
  117. val = m_input6.getState();
  118. return true;
  119. }
  120. return false;
  121. });
  122. // output1.reg
  123. // output2
  124. m_basicOrderModule.regOutCtl([this](uint8_t id, bool val) { return false; });
  125. ZHAL_CORE_REG(3000, {
  126. // ZLOGI(TAG, "IO1:%d IO2:%d", m_input1.getState(), m_input2.getState());
  127. 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());
  128. });
  129. /*******************************************************************************
  130. * *
  131. *******************************************************************************/
  132. {
  133. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  134. m_motor1.initialize(&cfg);
  135. int32_t chipv = m_motor1.readChipVERSION();
  136. ZLOGI(TAG, "m_motor1:%lx", chipv);
  137. m_motor1.setIHOLD_IRUN(1, 20, 0);
  138. m_motor1.setMotorShaft(true);
  139. m_motor1.setAcceleration(300000);
  140. m_motor1.setDeceleration(300000);
  141. // m_motor1.rotate(1000000);
  142. }
  143. {
  144. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  145. m_motor2.initialize(&cfg);
  146. int32_t chipv = m_motor2.readChipVERSION();
  147. ZLOGI(TAG, "m_motor2:%lx", chipv);
  148. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  149. m_motor2.setMotorShaft(true);
  150. m_motor2.setAcceleration(300000);
  151. m_motor2.setDeceleration(300000);
  152. // m_motor1.rotate(1000000);
  153. }
  154. m_pumpCtrlModule.initialize(&m_canReceiver);
  155. m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  156. ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm);
  157. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  158. });
  159. m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  160. ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm);
  161. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  162. });
  163. /*******************************************************************************
  164. * *
  165. *******************************************************************************/
  166. {
  167. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false);
  168. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false);
  169. triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false);
  170. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false);
  171. m_warningLightCtlModule.initialize(&m_canReceiver);
  172. m_warningLightCtlModule.regSubmodule(1, [&](uint8_t r, uint8_t g, uint8_t b, uint8_t beep) {
  173. ZLOGI(TAG, "warningLightCtlModule r:%d g:%d b:%d beep:%d", r, g, b, beep);
  174. triLight_R.setState(r != 0);
  175. triLight_G.setState(g != 0);
  176. triLight_B.setState(b != 0);
  177. triLight_BEEP.setState(beep != 0);
  178. });
  179. }
  180. /*******************************************************************************
  181. * *
  182. *******************************************************************************/
  183. // while (true) {
  184. // static ModbusBlockHost modbusBlockHost;
  185. // modbusBlockHost.initialize(&huart3);
  186. // int16_t val[1] = {0};
  187. // bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  188. // printf("suc:%d val:%d\n", suc, val[0]);
  189. // chip_delay_ms(1000);
  190. // }
  191. {
  192. m_huachengPressureSensor.initialize(&m_canReceiver);
  193. m_huachengPressureSensor.regSubmodule(1, [this](DP600PressureSensor::sensor_data_t *data) { //
  194. static ModbusBlockHost modbusBlockHost;
  195. modbusBlockHost.initialize(&huart3);
  196. int16_t val[1] = {0};
  197. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  198. if (!suc) {
  199. return false;
  200. }
  201. data->precision = 3;
  202. data->pressure_unit = 1;
  203. data->value = val[0];
  204. data->zero_point = 0;
  205. data->range_full_point = 0;
  206. return true;
  207. });
  208. m_huachengPressureSensor.regSubmodule(2, &huart3, 2);
  209. m_huachengPressureSensor.regSubmodule(3, &huart3, 3);
  210. m_huachengPressureSensor.regSubmodule(4, &huart3, 4);
  211. }
  212. ZLOGI(TAG, "init done");
  213. while (1) {
  214. ZHALCORE::getInstance()->loop();
  215. }
  216. }