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.

402 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
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. while (!m_motor1.ping()) {
  89. ZLOGI(TAG, "waitting for motor1 become ready");
  90. HAL_Delay(100);
  91. }
  92. m_motor1.reInitialize();
  93. m_motor1.setIHOLD_IRUN(1, 20, 0);
  94. m_motor1.setMotorShaft(true);
  95. m_motor1.setAcceleration(300000);
  96. m_motor1.setDeceleration(300000);
  97. // m_motor1.rotate(1000000);
  98. auto gstate = m_motor1.getGState();
  99. ZLOGI(TAG, "motor1: reset:%d drv_err:%d uv_cp:%d", gstate.reset, gstate.drv_err, gstate.uv_cp);
  100. }
  101. {
  102. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  103. m_motor2.initialize(&cfg);
  104. int32_t chipv = m_motor2.readChipVERSION();
  105. ZLOGI(TAG, "m_motor2:%lx", chipv);
  106. while (!m_motor2.ping()) {
  107. ZLOGI(TAG, "waitting for motor2 become ready");
  108. HAL_Delay(100);
  109. }
  110. m_motor2.reInitialize();
  111. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  112. m_motor2.setMotorShaft(true);
  113. m_motor2.setAcceleration(300000);
  114. m_motor2.setDeceleration(300000);
  115. auto gstate = m_motor2.getGState();
  116. ZLOGI(TAG, "motor2: reset:%d drv_err:%d uv_cp:%d", gstate.reset, gstate.drv_err, gstate.uv_cp);
  117. // m_motor2.rotate(1000000);
  118. }
  119. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, true, false);
  120. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, true, false);
  121. triLight_Y.initAsOutput(PD9, ZGPIO::kMode_nopull, true, false);
  122. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, true, false);
  123. m_PreportionalValveHost.initialize(&huart2);
  124. #if 1
  125. m_dp600PressureSensor2.initialize(&huart3, 2);
  126. m_dp600PressureSensor3.initialize(&huart3, 3);
  127. m_dp600PressureSensor4.initialize(&huart3, 4);
  128. OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, false, true);
  129. OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, true, false);
  130. #endif
  131. }
  132. void air_compressor_ch_select(int32_t val) {
  133. if (val == 2) { // 内管路
  134. OUT_PD15.setState(1);
  135. } else if (val == 1) { // 空气
  136. OUT_PD15.setState(0);
  137. }
  138. }
  139. void dumpdp600data(DP600PressureSensor::sensor_data_t *data) {
  140. ZLOGI(TAG, "value:%d", data->value);
  141. ZLOGI(TAG, "zero_point:%d", data->zero_point);
  142. ZLOGI(TAG, "range_full_point:%d", data->range_full_point);
  143. ZLOGI(TAG, "precision:%d", data->precision);
  144. ZLOGI(TAG, "pressure_unit:%d", data->pressure_unit);
  145. }
  146. void packet_kcmd_read_huacheng_pressure_sensor_data(int id, DP600PressureSensor::sensor_data_t *dp600data, uint8_t *receipt, int32_t &receiptsize) {
  147. receipt[0] = id;
  148. receipt[1] = 0;
  149. receipt[2] = dp600data->precision;
  150. receipt[3] = dp600data->pressure_unit;
  151. memcpy(receipt + 4, &dp600data->value, 2);
  152. memcpy(receipt + 6, &dp600data->zero_point, 2);
  153. memcpy(receipt + 8, &dp600data->range_full_point, 2);
  154. receiptsize = 10;
  155. }
  156. int32_t Hardware::process_rx_packet(from_where_t fromwhere, uint8_t *packet, int32_t len, uint8_t *receipt, int32_t &receiptsize, bool &matching) {
  157. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  158. if (fromwhere == kuart) printf("rx: cmdid:%d subcmdid:%d id:%d\n", cmdheader->cmdid, cmdheader->subcmdid, cmdheader->data[0]);
  159. /**
  160. * @brief Ping
  161. */
  162. PROCESS_CMD(kcmd_ping, 0, m_device_id) {
  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, 1) {
  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 1 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power);
  176. setmotor(&m_motor1, 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_peristaltic_pump_ctl, 1, 2) {
  185. int16_t acc = *(int16_t *)(&cmdheader->data[2]);
  186. int16_t rpm = *(int16_t *)(&cmdheader->data[4]);
  187. int16_t idlepower = cmdheader->data[6];
  188. int16_t power = cmdheader->data[7];
  189. printf("kcmd_peristaltic_pump_ctl 2 acc:%d rpm:%d idlepower:%d power:%d\n", acc, rpm, idlepower, power);
  190. setmotor(&m_motor2, acc, rpm, idlepower, power);
  191. receipt[0] = cmdheader->data[0];
  192. receiptsize = 1;
  193. return 0;
  194. }
  195. /**
  196. * @brief
  197. */
  198. PROCESS_CMD(kcmd_triple_warning_light_ctl, 0, 1) {
  199. /**
  200. * @brief 0:
  201. * cmd:
  202. * [0]:SENSORID
  203. * [2]:
  204. * [3]:
  205. * [4]:绿
  206. * [5]:
  207. * ack : b0:id
  208. * ack_datalen : 1
  209. */
  210. uint8_t id = cmdheader->data[0];
  211. uint8_t r = cmdheader->data[2];
  212. uint8_t g = cmdheader->data[3];
  213. uint8_t b = cmdheader->data[4];
  214. uint8_t beep = cmdheader->data[5];
  215. triLight_R.setState(r != 0);
  216. triLight_G.setState(g != 0);
  217. triLight_Y.setState(b != 0);
  218. triLight_BEEP.setState(beep != 0);
  219. receipt[0] = cmdheader->data[0];
  220. receiptsize = 1;
  221. }
  222. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_valve1_set) && (cmdheader->subcmdid == 0)) {
  223. uint32_t val = *(uint32_t *)(&cmdheader->data[0]);
  224. ZLOGI(TAG, "kcmd_air_compressor_valve1_set:%d", val);
  225. OUT_PD14.setState(val != 0);
  226. matching = true;
  227. }
  228. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_valve2_set) && (cmdheader->subcmdid == 0)) {
  229. uint32_t val = *(uint32_t *)(&cmdheader->data[0]);
  230. ZLOGI(TAG, "kcmd_air_compressor_valve2_set:%d", val);
  231. OUT_PD14.setState(val != 0);
  232. matching = true;
  233. }
  234. if ((cmdheader->cmdid == (uint16_t)kcmd_proportional_read_state) && (cmdheader->subcmdid == 0)) {
  235. matching = true;
  236. int32_t ack = 0;
  237. int32_t ecode = preportional_valve_is_busy(&ack);
  238. int32_t *p_receipt_32 = (int32_t *)receipt;
  239. *p_receipt_32 = ack;
  240. receiptsize = 4;
  241. }
  242. if ((cmdheader->cmdid == (uint16_t)kcmd_set_proportional_valve) && (cmdheader->subcmdid == 0)) {
  243. int32_t para0 = *(int32_t *)(&cmdheader->data[0]);
  244. int32_t para1 = *(int32_t *)(&cmdheader->data[4]);
  245. if (para0 == 1) {
  246. matching = true;
  247. m_PreportionalValveHost.setValvePos(1, para1);
  248. } else if (para0 == 2) {
  249. matching = true;
  250. m_PreportionalValveHost.setValvePos(2, para1);
  251. }
  252. }
  253. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_ch_select) && (cmdheader->subcmdid == 0)) {
  254. int32_t para0 = *(int32_t *)(&cmdheader->data[0]);
  255. air_compressor_ch_select(para0);
  256. }
  257. static DP600PressureSensor::sensor_data_t dp600data;
  258. if ((cmdheader->cmdid == (uint16_t)kcmd_air_compressor_read_pressure) && (cmdheader->subcmdid == 0)) {
  259. matching = true;
  260. bool suc = m_dp600PressureSensor2.readVal(&dp600data);
  261. if (!suc) return 1002;
  262. int32_t *p_receipt_32 = (int32_t *)receipt;
  263. *p_receipt_32 = dp600data.value;
  264. receiptsize = 4;
  265. }
  266. #if 0
  267. /**
  268. * @brief
  269. */
  270. PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0, 0) {
  271. ((int32_t *)receipt)[0] = !IO_PC7_IN.getState();
  272. receiptsize = 4;
  273. return 0;
  274. }
  275. /**
  276. * @brief
  277. */
  278. PROCESS_CMD(kcmd_proportional_read_water_immersion_sensor, 0, 1) {
  279. ((int32_t *)receipt)[0] = !IO_PD13_IN.getState();
  280. receiptsize = 4;
  281. return 0;
  282. }
  283. #endif
  284. /**
  285. * @brief
  286. */
  287. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 1) {
  288. static ModbusBlockHost modbusBlockHost;
  289. modbusBlockHost.initialize(&huart3);
  290. int16_t val[1] = {0};
  291. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  292. if (!suc) return 1002;
  293. dp600data.precision = 3;
  294. dp600data.pressure_unit = 1;
  295. dp600data.value = val[0];
  296. dp600data.zero_point = 0;
  297. dp600data.range_full_point = 0;
  298. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  299. if (fromwhere == kuart) {
  300. dumpdp600data(&dp600data);
  301. }
  302. return 0;
  303. }
  304. /**
  305. * @brief
  306. */
  307. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 2) {
  308. bool suc = m_dp600PressureSensor2.readVal(&dp600data);
  309. if (!suc) return 1002;
  310. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  311. if (fromwhere == kuart) {
  312. dumpdp600data(&dp600data);
  313. }
  314. return 0;
  315. }
  316. /**
  317. * @brief
  318. */
  319. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 3) {
  320. bool suc = m_dp600PressureSensor3.readVal(&dp600data);
  321. if (!suc) return 1002;
  322. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  323. if (fromwhere == kuart) {
  324. dumpdp600data(&dp600data);
  325. }
  326. return 0;
  327. }
  328. /**
  329. * @brief
  330. */
  331. PROCESS_CMD(kcmd_read_huacheng_pressure_sensor, 0, 4) {
  332. bool suc = m_dp600PressureSensor4.readVal(&dp600data);
  333. if (!suc) return 1002;
  334. packet_kcmd_read_huacheng_pressure_sensor_data(cmdheader->data[0], &dp600data, receipt, receiptsize);
  335. if (fromwhere == kuart) {
  336. dumpdp600data(&dp600data);
  337. }
  338. return 0;
  339. }
  340. return 0;
  341. }
  342. void Hardware::loop() {}
  343. TMC5130 *Hardware::getMotor1() { return &m_motor1; }
  344. TMC5130 *Hardware::getMotor2() { return &m_motor2; }