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.

285 lines
9.0 KiB

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