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.

275 lines
8.8 KiB

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() {
  61. IO_PD13_IN.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  62. IO_PC7_IN.initAsInput(PC7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  63. {
  64. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  65. m_motor1.initialize(&cfg);
  66. int32_t chipv = m_motor1.readChipVERSION();
  67. ZLOGI(TAG, "m_motor1:%lx", chipv);
  68. m_motor1.setIHOLD_IRUN(1, 20, 0);
  69. m_motor1.setMotorShaft(true);
  70. m_motor1.setAcceleration(300000);
  71. m_motor1.setDeceleration(300000);
  72. // m_motor1.rotate(1000000);
  73. }
  74. {
  75. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  76. m_motor2.initialize(&cfg);
  77. int32_t chipv = m_motor2.readChipVERSION();
  78. ZLOGI(TAG, "m_motor2:%lx", chipv);
  79. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  80. m_motor2.setMotorShaft(true);
  81. m_motor2.setAcceleration(300000);
  82. m_motor2.setDeceleration(300000);
  83. // m_motor1.rotate(1000000);
  84. }
  85. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, false, false);
  86. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, false, false);
  87. triLight_Y.initAsOutput(PD9, ZGPIO::kMode_nopull, false, false);
  88. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, false, false);
  89. m_dp600PressureSensor2.initialize(&huart3, 2);
  90. m_dp600PressureSensor3.initialize(&huart3, 3);
  91. m_dp600PressureSensor4.initialize(&huart3, 4);
  92. }
  93. void dumpdp600data(DP600PressureSensor::sensor_data_t *data) {
  94. ZLOGI(TAG, "value:%d", data->value);
  95. ZLOGI(TAG, "zero_point:%d", data->zero_point);
  96. ZLOGI(TAG, "range_full_point:%d", data->range_full_point);
  97. ZLOGI(TAG, "precision:%d", data->precision);
  98. ZLOGI(TAG, "pressure_unit:%d", data->pressure_unit);
  99. }
  100. void packet_kcmd_read_huacheng_pressure_sensor_data(int id, DP600PressureSensor::sensor_data_t *dp600data, uint8_t *receipt, int32_t &receiptsize) {
  101. receipt[0] = id;
  102. receipt[1] = 0;
  103. receipt[2] = dp600data->precision;
  104. receipt[3] = dp600data->pressure_unit;
  105. memcpy(receipt + 4, &dp600data->value, 2);
  106. memcpy(receipt + 6, &dp600data->zero_point, 2);
  107. memcpy(receipt + 8, &dp600data->range_full_point, 2);
  108. receiptsize = 10;
  109. }
  110. int32_t Hardware::process_rx_packet(from_where_t fromwhere, uint8_t *packet, int32_t len, uint8_t *receipt, int32_t &receiptsize, bool &matching) {
  111. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  112. /**
  113. * @brief
  114. */
  115. PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0) {
  116. ((int32_t *)receipt)[0] = !IO_PC7_IN.getState();
  117. receiptsize = 4;
  118. return 0;
  119. }
  120. /**
  121. * @brief
  122. */
  123. PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 1) {
  124. ((int32_t *)receipt)[0] = !IO_PD13_IN.getState();
  125. receiptsize = 4;
  126. return 0;
  127. }
  128. /**
  129. * @brief
  130. */
  131. PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1) {
  132. int16_t acc = *(int16_t *)(&cmdheader->data[2]);
  133. int16_t rpm = *(int16_t *)(&cmdheader->data[4]);
  134. int16_t idlepower = cmdheader->data[6];
  135. int16_t power = cmdheader->data[7];
  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, 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. setmotor(&m_motor2, acc, rpm, idlepower, power);
  150. receipt[0] = cmdheader->data[0];
  151. receiptsize = 1;
  152. return 0;
  153. }
  154. /**
  155. * @brief
  156. */
  157. PROCESS_CMD(kcmd_triple_warning_light_ctl, 1) {
  158. /**
  159. * @brief 0:
  160. * cmd:
  161. * [0]:SENSORID
  162. * [2]:
  163. * [3]:
  164. * [4]:绿
  165. * [5]:
  166. * ack : b0:id
  167. * ack_datalen : 1
  168. */
  169. uint8_t id = cmdheader->data[0];
  170. uint8_t r = cmdheader->data[2];
  171. uint8_t g = cmdheader->data[3];
  172. uint8_t b = cmdheader->data[4];
  173. uint8_t beep = cmdheader->data[5];
  174. triLight_R.setState(r != 0);
  175. triLight_G.setState(g != 0);
  176. triLight_Y.setState(b != 0);
  177. triLight_BEEP.setState(beep != 0);
  178. receipt[0] = cmdheader->data[0];
  179. receiptsize = 1;
  180. }
  181. static DP600PressureSensor::sensor_data_t dp600data;
  182. /**
  183. * @brief
  184. */
  185. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 1) {
  186. static ModbusBlockHost modbusBlockHost;
  187. modbusBlockHost.initialize(&huart3);
  188. int16_t val[1] = {0};
  189. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  190. if (!suc) return 1002;
  191. dp600data.precision = 3;
  192. dp600data.pressure_unit = 1;
  193. dp600data.value = val[0];
  194. dp600data.zero_point = 0;
  195. dp600data.range_full_point = 0;
  196. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  197. if (fromwhere == kuart) {
  198. dumpdp600data(&dp600data);
  199. }
  200. return 0;
  201. }
  202. /**
  203. * @brief
  204. */
  205. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 2) {
  206. bool suc = m_dp600PressureSensor2.readVal(&dp600data);
  207. if (!suc) return 1002;
  208. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  209. if (fromwhere == kuart) {
  210. dumpdp600data(&dp600data);
  211. }
  212. return 0;
  213. }
  214. /**
  215. * @brief
  216. */
  217. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 3) {
  218. bool suc = m_dp600PressureSensor3.readVal(&dp600data);
  219. if (!suc) return 1002;
  220. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  221. if (fromwhere == kuart) {
  222. dumpdp600data(&dp600data);
  223. }
  224. return 0;
  225. }
  226. /**
  227. * @brief
  228. */
  229. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 4) {
  230. bool suc = m_dp600PressureSensor4.readVal(&dp600data);
  231. if (!suc) return 1002;
  232. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  233. if (fromwhere == kuart) {
  234. dumpdp600data(&dp600data);
  235. }
  236. return 0;
  237. }
  238. }
  239. void Hardware::loop() {}