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.

379 lines
12 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
  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 "driver/preportional_valve_ctrl.hpp"
  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. using namespace iflytop;
  24. #define TAG "main"
  25. TMC5130 m_motor1;
  26. TMC5130 m_motor2;
  27. ZGPIO triLight_R;
  28. ZGPIO triLight_G;
  29. ZGPIO triLight_Y;
  30. ZGPIO triLight_BEEP;
  31. ZCanBasicOrderModule m_basicOrderModule;
  32. ZCanPumpCtrlModule m_pumpCtrlModule;
  33. ZCanTrigleWarningLightCtlModule m_warningLightCtlModule;
  34. HuachengPressureSensor m_huachengPressureSensor;
  35. DP600PressureSensor m_dp600PressureSensor2;
  36. DP600PressureSensor m_dp600PressureSensor3;
  37. DP600PressureSensor m_dp600PressureSensor4;
  38. ZGPIO IO_PD13_IN;
  39. ZGPIO IO_PC7_IN;
  40. ZGPIO OUT_PD14;
  41. ZGPIO OUT_PD15;
  42. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  43. int32_t ppm = rpm / 60.0 * 51200;
  44. int32_t acc = acc_rpm2 / 60.0 * 51200;
  45. int16_t _idlepower = 1;
  46. int16_t _power = 31;
  47. if (idlepower > 0 && idlepower < 31) {
  48. _idlepower = idlepower;
  49. }
  50. if (power > 0 && power < 31) {
  51. _power = power;
  52. }
  53. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  54. motor->setAcceleration(acc);
  55. motor->setDeceleration(acc);
  56. motor->rotate(ppm);
  57. }
  58. PreportionalValveCtrl m_PreportionalValveHost;
  59. int32_t preportional_valve_is_busy(int32_t *busy) {
  60. int32_t valve1state = 0;
  61. int32_t valve2state = 0;
  62. int32_t err = 0;
  63. *busy = 1;
  64. err = m_PreportionalValveHost.isBusy(1, &valve1state);
  65. if (err != 0) return err;
  66. err = m_PreportionalValveHost.isBusy(2, &valve2state);
  67. if (err != 0) return err;
  68. if (valve1state == 0 && valve2state == 0) {
  69. *busy = 0;
  70. } else {
  71. *busy = 1;
  72. }
  73. return 0;
  74. }
  75. void air_compressor_read_pressure(int32_t *ack) { //
  76. DP600PressureSensor::sensor_data_t d = m_huachengPressureSensor.readsensordata(2);
  77. *ack = d.value;
  78. }
  79. void Hardware::initialize(int deviceId) {
  80. m_device_id = deviceId;
  81. // IO_PD13_IN.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  82. // IO_PC7_IN.initAsInput(PC7, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, false /*mirror*/);
  83. {
  84. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  85. m_motor1.initialize(&cfg);
  86. int32_t chipv = m_motor1.readChipVERSION();
  87. ZLOGI(TAG, "m_motor1:%lx", chipv);
  88. m_motor1.setIHOLD_IRUN(1, 20, 0);
  89. m_motor1.setMotorShaft(true);
  90. m_motor1.setAcceleration(300000);
  91. m_motor1.setDeceleration(300000);
  92. // m_motor1.rotate(1000000);
  93. }
  94. {
  95. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  96. m_motor2.initialize(&cfg);
  97. int32_t chipv = m_motor2.readChipVERSION();
  98. ZLOGI(TAG, "m_motor2:%lx", chipv);
  99. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  100. m_motor2.setMotorShaft(true);
  101. m_motor2.setAcceleration(300000);
  102. m_motor2.setDeceleration(300000);
  103. // m_motor2.rotate(1000000);
  104. }
  105. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, true, false);
  106. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, true, false);
  107. triLight_Y.initAsOutput(PD9, ZGPIO::kMode_nopull, true, false);
  108. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, true, false);
  109. m_PreportionalValveHost.initialize(&huart2);
  110. #if 1
  111. m_dp600PressureSensor2.initialize(&huart3, 2);
  112. m_dp600PressureSensor3.initialize(&huart3, 3);
  113. m_dp600PressureSensor4.initialize(&huart3, 4);
  114. OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, false, true);
  115. OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, true, false);
  116. #endif
  117. }
  118. void air_compressor_ch_select(int32_t val) {
  119. if (val == 2) { // 内管路
  120. OUT_PD15.setState(1);
  121. } else if (val == 1) { // 空气
  122. OUT_PD15.setState(0);
  123. }
  124. }
  125. void dumpdp600data(DP600PressureSensor::sensor_data_t *data) {
  126. ZLOGI(TAG, "value:%d", data->value);
  127. ZLOGI(TAG, "zero_point:%d", data->zero_point);
  128. ZLOGI(TAG, "range_full_point:%d", data->range_full_point);
  129. ZLOGI(TAG, "precision:%d", data->precision);
  130. ZLOGI(TAG, "pressure_unit:%d", data->pressure_unit);
  131. }
  132. void packet_kcmd_read_huacheng_pressure_sensor_data(int id, DP600PressureSensor::sensor_data_t *dp600data, uint8_t *receipt, int32_t &receiptsize) {
  133. receipt[0] = id;
  134. receipt[1] = 0;
  135. receipt[2] = dp600data->precision;
  136. receipt[3] = dp600data->pressure_unit;
  137. memcpy(receipt + 4, &dp600data->value, 2);
  138. memcpy(receipt + 6, &dp600data->zero_point, 2);
  139. memcpy(receipt + 8, &dp600data->range_full_point, 2);
  140. receiptsize = 10;
  141. }
  142. int32_t Hardware::process_rx_packet(from_where_t fromwhere, uint8_t *packet, int32_t len, uint8_t *receipt, int32_t &receiptsize, bool &matching) {
  143. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  144. if (fromwhere == kuart) printf("rx: cmdid:%d subcmdid:%d id:%d\n", cmdheader->cmdid, cmdheader->subcmdid, cmdheader->data[0]);
  145. /**
  146. * @brief Ping
  147. */
  148. PROCESS_CMD(kcmd_ping, 0, m_device_id) {
  149. receipt[0] = cmdheader->data[0];
  150. receiptsize = 1;
  151. return 0;
  152. }
  153. /**
  154. * @brief
  155. */
  156. PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1, 1) {
  157. int16_t acc = *(int16_t *)(&cmdheader->data[2]);
  158. int16_t rpm = *(int16_t *)(&cmdheader->data[4]);
  159. int16_t idlepower = cmdheader->data[6];
  160. int16_t power = cmdheader->data[7];
  161. printf("kcmd_peristaltic_pump_ctl 1 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power);
  162. setmotor(&m_motor1, acc, rpm, idlepower, power);
  163. receipt[0] = cmdheader->data[0];
  164. receiptsize = 1;
  165. return 0;
  166. }
  167. /**
  168. * @brief -
  169. */
  170. PROCESS_CMD(kcmd_peristaltic_pump_ctl, 1, 2) {
  171. int16_t acc = *(int16_t *)(&cmdheader->data[2]);
  172. int16_t rpm = *(int16_t *)(&cmdheader->data[4]);
  173. int16_t idlepower = cmdheader->data[6];
  174. int16_t power = cmdheader->data[7];
  175. printf("kcmd_peristaltic_pump_ctl 2 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power);
  176. setmotor(&m_motor2, acc, rpm, idlepower, power);
  177. receipt[0] = cmdheader->data[0];
  178. receiptsize = 1;
  179. return 0;
  180. }
  181. /**
  182. * @brief
  183. */
  184. PROCESS_CMD(kcmd_triple_warning_light_ctl, 0, 1) {
  185. /**
  186. * @brief 0:
  187. * cmd:
  188. * [0]:SENSORID
  189. * [2]:
  190. * [3]:
  191. * [4]:绿
  192. * [5]:
  193. * ack : b0:id
  194. * ack_datalen : 1
  195. */
  196. uint8_t id = cmdheader->data[0];
  197. uint8_t r = cmdheader->data[2];
  198. uint8_t g = cmdheader->data[3];
  199. uint8_t b = cmdheader->data[4];
  200. uint8_t beep = cmdheader->data[5];
  201. triLight_R.setState(r != 0);
  202. triLight_G.setState(g != 0);
  203. triLight_Y.setState(b != 0);
  204. triLight_BEEP.setState(beep != 0);
  205. receipt[0] = cmdheader->data[0];
  206. receiptsize = 1;
  207. }
  208. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_valve1_set) && (cmdheader->subcmdid == 0)) {
  209. uint32_t val = *(uint32_t *)(&cmdheader->data[0]);
  210. ZLOGI(TAG, "kcmd_air_compressor_valve1_set:%d", val);
  211. OUT_PD14.setState(val != 0);
  212. matching = true;
  213. }
  214. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_valve2_set) && (cmdheader->subcmdid == 0)) {
  215. uint32_t val = *(uint32_t *)(&cmdheader->data[0]);
  216. ZLOGI(TAG, "kcmd_air_compressor_valve2_set:%d", val);
  217. OUT_PD14.setState(val != 0);
  218. matching = true;
  219. }
  220. if ((cmdheader->cmdid == (uint16_t)kcmd_proportional_read_state) && (cmdheader->subcmdid == 0)) {
  221. matching = true;
  222. int32_t ack = 0;
  223. int32_t ecode = preportional_valve_is_busy(&ack);
  224. int32_t *p_receipt_32 = (int32_t *)receipt;
  225. *p_receipt_32 = ack;
  226. receiptsize = 4;
  227. }
  228. if ((cmdheader->cmdid == (uint16_t)kcmd_set_proportional_valve) && (cmdheader->subcmdid == 0)) {
  229. int32_t para0 = *(int32_t *)(&cmdheader->data[0]);
  230. int32_t para1 = *(int32_t *)(&cmdheader->data[4]);
  231. if (para0 == 1) {
  232. matching = true;
  233. m_PreportionalValveHost.setValvePos(1, para1);
  234. } else if (para0 == 2) {
  235. matching = true;
  236. m_PreportionalValveHost.setValvePos(2, para1);
  237. }
  238. }
  239. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_ch_select) && (cmdheader->subcmdid == 0)) {
  240. int32_t para0 = *(int32_t *)(&cmdheader->data[0]);
  241. air_compressor_ch_select(para0);
  242. }
  243. static DP600PressureSensor::sensor_data_t dp600data;
  244. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_read_pressure) && (cmdheader->subcmdid == 0)) {
  245. matching = true;
  246. bool suc = m_dp600PressureSensor2.readVal(&dp600data);
  247. if (!suc) return 1002;
  248. int32_t *p_receipt_32 = (int32_t *)receipt;
  249. *p_receipt_32 = dp600data.value;
  250. receiptsize = 4;
  251. }
  252. #if 0
  253. /**
  254. * @brief
  255. */
  256. PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0, 0) {
  257. ((int32_t *)receipt)[0] = !IO_PC7_IN.getState();
  258. receiptsize = 4;
  259. return 0;
  260. }
  261. /**
  262. * @brief
  263. */
  264. PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0, 1) {
  265. ((int32_t *)receipt)[0] = !IO_PD13_IN.getState();
  266. receiptsize = 4;
  267. return 0;
  268. }
  269. #endif
  270. /**
  271. * @brief
  272. */
  273. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 1) {
  274. static ModbusBlockHost modbusBlockHost;
  275. modbusBlockHost.initialize(&huart3);
  276. int16_t val[1] = {0};
  277. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  278. if (!suc) return 1002;
  279. dp600data.precision = 3;
  280. dp600data.pressure_unit = 1;
  281. dp600data.value = val[0];
  282. dp600data.zero_point = 0;
  283. dp600data.range_full_point = 0;
  284. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  285. if (fromwhere == kuart) {
  286. dumpdp600data(&dp600data);
  287. }
  288. return 0;
  289. }
  290. /**
  291. * @brief
  292. */
  293. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 2) {
  294. bool suc = m_dp600PressureSensor2.readVal(&dp600data);
  295. if (!suc) return 1002;
  296. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  297. if (fromwhere == kuart) {
  298. dumpdp600data(&dp600data);
  299. }
  300. return 0;
  301. }
  302. /**
  303. * @brief
  304. */
  305. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 3) {
  306. bool suc = m_dp600PressureSensor3.readVal(&dp600data);
  307. if (!suc) return 1002;
  308. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  309. if (fromwhere == kuart) {
  310. dumpdp600data(&dp600data);
  311. }
  312. return 0;
  313. }
  314. /**
  315. * @brief
  316. */
  317. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 4) {
  318. bool suc = m_dp600PressureSensor4.readVal(&dp600data);
  319. if (!suc) return 1002;
  320. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  321. if (fromwhere == kuart) {
  322. dumpdp600data(&dp600data);
  323. }
  324. return 0;
  325. }
  326. return 0;
  327. }
  328. void Hardware::loop() {}