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.

281 lines
8.7 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
  1. #include "hardware.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "main.h"
  6. #include "project.hpp"
  7. //
  8. // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
  9. #include "sdk/hal/zhal.hpp"
  10. #include "sdk\components\m3078\m3078_code_scaner.hpp"
  11. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  12. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  13. //
  14. #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
  15. #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
  16. #include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
  17. #include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
  18. #include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
  19. using namespace iflytop;
  20. #define TAG "main"
  21. TMC5130 m_motor1;
  22. TMC5130 m_motor2;
  23. ZGPIO triLight_R;
  24. ZGPIO triLight_G;
  25. ZGPIO triLight_Y;
  26. ZGPIO triLight_BEEP;
  27. ZCanBasicOrderModule m_basicOrderModule;
  28. ZCanPumpCtrlModule m_pumpCtrlModule;
  29. ZCanTrigleWarningLightCtlModule m_warningLightCtlModule;
  30. HuachengPressureSensor m_huachengPressureSensor;
  31. DP600PressureSensor m_dp600PressureSensor2;
  32. DP600PressureSensor m_dp600PressureSensor3;
  33. DP600PressureSensor m_dp600PressureSensor4;
  34. ZGPIO IO_PD13_IN;
  35. ZGPIO IO_PC7_IN;
  36. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  37. int32_t ppm = rpm / 60.0 * 51200;
  38. int32_t acc = acc_rpm2 / 60.0 * 51200;
  39. int16_t _idlepower = 1;
  40. int16_t _power = 31;
  41. if (idlepower > 0 && idlepower < 31) {
  42. _idlepower = idlepower;
  43. }
  44. if (power > 0 && power < 31) {
  45. _power = power;
  46. }
  47. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  48. motor->setAcceleration(acc);
  49. motor->setDeceleration(acc);
  50. motor->rotate(ppm);
  51. }
  52. void Hardware::initialize(int deviceId) {
  53. m_device_id = deviceId;
  54. IO_PD13_IN.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  55. IO_PC7_IN.initAsInput(PC7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  56. {
  57. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  58. m_motor1.initialize(&cfg);
  59. int32_t chipv = m_motor1.readChipVERSION();
  60. ZLOGI(TAG, "m_motor1:%lx", chipv);
  61. m_motor1.setIHOLD_IRUN(1, 20, 0);
  62. m_motor1.setMotorShaft(true);
  63. m_motor1.setAcceleration(300000);
  64. m_motor1.setDeceleration(300000);
  65. // m_motor1.rotate(1000000);
  66. }
  67. {
  68. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  69. m_motor2.initialize(&cfg);
  70. int32_t chipv = m_motor2.readChipVERSION();
  71. ZLOGI(TAG, "m_motor2:%lx", chipv);
  72. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  73. m_motor2.setMotorShaft(true);
  74. m_motor2.setAcceleration(300000);
  75. m_motor2.setDeceleration(300000);
  76. // m_motor2.rotate(1000000);
  77. }
  78. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false);
  79. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false);
  80. triLight_Y.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false);
  81. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false);
  82. m_dp600PressureSensor2.initialize(&huart3, 2);
  83. m_dp600PressureSensor3.initialize(&huart3, 3);
  84. m_dp600PressureSensor4.initialize(&huart3, 4);
  85. }
  86. void dumpdp600data(DP600PressureSensor::sensor_data_t *data) {
  87. ZLOGI(TAG, "value:%d", data->value);
  88. ZLOGI(TAG, "zero_point:%d", data->zero_point);
  89. ZLOGI(TAG, "range_full_point:%d", data->range_full_point);
  90. ZLOGI(TAG, "precision:%d", data->precision);
  91. ZLOGI(TAG, "pressure_unit:%d", data->pressure_unit);
  92. }
  93. void packet_kcmd_read_huacheng_pressure_sensor_data(int id, DP600PressureSensor::sensor_data_t *dp600data, uint8_t *receipt, int32_t &receiptsize) {
  94. receipt[0] = id;
  95. receipt[1] = 0;
  96. receipt[2] = dp600data->precision;
  97. receipt[3] = dp600data->pressure_unit;
  98. memcpy(receipt + 4, &dp600data->value, 2);
  99. memcpy(receipt + 6, &dp600data->zero_point, 2);
  100. memcpy(receipt + 8, &dp600data->range_full_point, 2);
  101. receiptsize = 10;
  102. }
  103. int32_t Hardware::process_rx_packet(from_where_t fromwhere, uint8_t *packet, int32_t len, uint8_t *receipt, int32_t &receiptsize, bool &matching) {
  104. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  105. if (fromwhere == kuart) printf("rx: cmdid:%d subcmdid:%d id:%d\n", cmdheader->cmdid, cmdheader->subcmdid, cmdheader->data[0]);
  106. // printf("rx: cmdid:%d subcmdid:%d id:%d\n", cmdheader->cmdid, cmdheader->subcmdid, cmdheader->data[0]);
  107. /**
  108. * @brief Ping
  109. */
  110. PROCESS_CMD(kcmd_ping, 0, m_device_id) {
  111. receipt[0] = cmdheader->data[0];
  112. receiptsize = 1;
  113. return 0;
  114. }
  115. if ((cmdheader->cmdid == (uint16_t)kcmd_read_evaporation_bin_water_detection)) {
  116. matching = true;
  117. ((int32_t *)receipt)[0] = IO_PC7_IN.getState(); // 高有效
  118. receiptsize = 4;
  119. return 0;
  120. }
  121. if ((cmdheader->cmdid == (uint16_t)kcmd_read_device_bottom_water_detection_sensor)) {
  122. matching = true;
  123. ((int32_t *)receipt)[0] = !IO_PD13_IN.getState(); // 低有效
  124. receiptsize = 4;
  125. return 0;
  126. }
  127. /**
  128. * @brief
  129. */
  130. PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1, 1) {
  131. int16_t acc = *(int16_t *)(&cmdheader->data[2]);
  132. int16_t rpm = *(int16_t *)(&cmdheader->data[4]);
  133. int16_t idlepower = cmdheader->data[6];
  134. int16_t power = cmdheader->data[7];
  135. printf("kcmd_peristaltic_pump_ctl 1 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power);
  136. setmotor(&m_motor1, acc, rpm, idlepower, power);
  137. receipt[0] = cmdheader->data[0];
  138. receiptsize = 1;
  139. return 0;
  140. }
  141. /**
  142. * @brief -
  143. */
  144. PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1, 2) {
  145. int16_t acc = *(int16_t *)(&cmdheader->data[2]);
  146. int16_t rpm = *(int16_t *)(&cmdheader->data[4]);
  147. int16_t idlepower = cmdheader->data[6];
  148. int16_t power = cmdheader->data[7];
  149. printf("kcmd_peristaltic_pump_ctl 2 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power);
  150. setmotor(&m_motor2, acc, rpm, idlepower, power);
  151. receipt[0] = cmdheader->data[0];
  152. receiptsize = 1;
  153. return 0;
  154. }
  155. /**
  156. * @brief
  157. */
  158. PROCESS_CMD(kcmd_triple_warning_light_ctl, 0, 1) {
  159. /**
  160. * @brief 0:
  161. * cmd:
  162. * [0]:SENSORID
  163. * [2]:
  164. * [3]:
  165. * [4]:绿
  166. * [5]:
  167. * ack : b0:id
  168. * ack_datalen : 1
  169. */
  170. uint8_t id = cmdheader->data[0];
  171. uint8_t r = cmdheader->data[2];
  172. uint8_t g = cmdheader->data[3];
  173. uint8_t b = cmdheader->data[4];
  174. uint8_t beep = cmdheader->data[5];
  175. triLight_R.setState(r != 0);
  176. triLight_G.setState(g != 0);
  177. triLight_Y.setState(b != 0);
  178. triLight_BEEP.setState(beep != 0);
  179. receipt[0] = cmdheader->data[0];
  180. receiptsize = 1;
  181. }
  182. static DP600PressureSensor::sensor_data_t dp600data;
  183. /**
  184. * @brief
  185. */
  186. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 1) {
  187. static ModbusBlockHost modbusBlockHost;
  188. modbusBlockHost.initialize(&huart3);
  189. int16_t val[1] = {0};
  190. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  191. if (!suc) return 1002;
  192. dp600data.precision = 3;
  193. dp600data.pressure_unit = 1;
  194. dp600data.value = val[0];
  195. dp600data.zero_point = 0;
  196. dp600data.range_full_point = 0;
  197. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  198. if (fromwhere == kuart) {
  199. dumpdp600data(&dp600data);
  200. }
  201. return 0;
  202. }
  203. /**
  204. * @brief
  205. */
  206. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 2) {
  207. bool suc = m_dp600PressureSensor2.readVal(&dp600data);
  208. if (!suc) return 1002;
  209. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  210. if (fromwhere == kuart) {
  211. dumpdp600data(&dp600data);
  212. }
  213. return 0;
  214. }
  215. /**
  216. * @brief
  217. */
  218. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 3) {
  219. bool suc = m_dp600PressureSensor3.readVal(&dp600data);
  220. if (!suc) return 1002;
  221. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  222. if (fromwhere == kuart) {
  223. dumpdp600data(&dp600data);
  224. }
  225. return 0;
  226. }
  227. /**
  228. * @brief
  229. */
  230. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 4) {
  231. bool suc = m_dp600PressureSensor4.readVal(&dp600data);
  232. if (!suc) return 1002;
  233. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  234. if (fromwhere == kuart) {
  235. dumpdp600data(&dp600data);
  236. }
  237. return 0;
  238. }
  239. return 0;
  240. }
  241. void Hardware::loop() {}