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.

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