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.

221 lines
6.8 KiB

2 years ago
  1. #include "device.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #define TAG "DEV"
  5. namespace iflytop {
  6. IflytopCanProtocolStackProcesser m_protocolStack;
  7. TMC5130 m_motor1;
  8. TMC5130 m_motor2;
  9. ZGPIO debuglight;
  10. ZGPIO triLight_R;
  11. ZGPIO triLight_G;
  12. ZGPIO triLight_B;
  13. ZGPIO triLight_BEEP;
  14. ZGPIO m_input1;
  15. ZGPIO m_input2;
  16. ZGPIO m_input3;
  17. ZGPIO m_input4;
  18. ZGPIO m_input5;
  19. ZGPIO OUT_PD14;
  20. ZGPIO OUT_PD15;
  21. ZCanReceiver m_canReceiver;
  22. ZCanBasicOrderModule m_basicOrderModule;
  23. ZCanPumpCtrlModule m_pumpCtrlModule;
  24. HuachengPressureSensor m_huachengPressureSensor;
  25. PreportionalValveCtrl m_PreportionalValveHost;
  26. CmdSchedulerV2 cmdScheduler;
  27. void device_init() {
  28. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  29. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  30. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  31. m_canReceiver.init(cfg);
  32. // m_canReceiver.registerListener(this);
  33. /**
  34. * @brief
  35. */
  36. m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  37. m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  38. m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  39. m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  40. m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
  41. m_basicOrderModule.initialize(&m_canReceiver);
  42. m_basicOrderModule.regInputCtl([](uint8_t id, bool &val) {
  43. if (id == 1) {
  44. val = m_input1.getState();
  45. return true;
  46. }
  47. if (id == 2) {
  48. val = m_input2.getState();
  49. return true;
  50. }
  51. if (id == 3) {
  52. val = m_input3.getState();
  53. return true;
  54. }
  55. if (id == 4) {
  56. val = m_input4.getState();
  57. return true;
  58. }
  59. if (id == 5) {
  60. val = m_input5.getState();
  61. return true;
  62. }
  63. return false;
  64. });
  65. m_PreportionalValveHost.initialize(&huart2);
  66. /*******************************************************************************
  67. * *
  68. *******************************************************************************/
  69. {
  70. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
  71. m_motor1.initialize(&cfg);
  72. int32_t chipv = m_motor1.readChipVERSION();
  73. ZLOGI(TAG, "m_motor1:%lx", chipv);
  74. m_motor1.setIHOLD_IRUN(1, 20, 0);
  75. m_motor1.setMotorShaft(true);
  76. m_motor1.setAcceleration(300000);
  77. m_motor1.setDeceleration(300000);
  78. // m_motor1.rotate(1000000);
  79. }
  80. {
  81. TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
  82. m_motor2.initialize(&cfg);
  83. int32_t chipv = m_motor2.readChipVERSION();
  84. ZLOGI(TAG, "m_motor2:%lx", chipv);
  85. m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
  86. m_motor2.setMotorShaft(true);
  87. m_motor2.setAcceleration(300000);
  88. m_motor2.setDeceleration(300000);
  89. // m_motor1.rotate(1000000);
  90. }
  91. m_pumpCtrlModule.initialize(&m_canReceiver);
  92. m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  93. ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm);
  94. setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
  95. });
  96. m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  97. ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm);
  98. setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
  99. });
  100. /*******************************************************************************
  101. * *
  102. *******************************************************************************/
  103. {
  104. triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, true, false);
  105. triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, true, false);
  106. triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, true, false);
  107. triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, true, false);
  108. }
  109. /*******************************************************************************
  110. * *
  111. *******************************************************************************/
  112. {
  113. m_huachengPressureSensor.initialize(&m_canReceiver);
  114. m_huachengPressureSensor.regSubmodule(1, [](DP600PressureSensor::sensor_data_t *data) { //
  115. static ModbusBlockHost modbusBlockHost;
  116. modbusBlockHost.initialize(&huart3);
  117. int16_t val[1] = {0};
  118. bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
  119. if (!suc) {
  120. return false;
  121. }
  122. data->precision = 3;
  123. data->pressure_unit = 1;
  124. data->value = val[0];
  125. data->zero_point = 0;
  126. data->range_full_point = 0;
  127. return true;
  128. });
  129. m_huachengPressureSensor.regSubmodule(2, &huart3, 2);
  130. m_huachengPressureSensor.regSubmodule(3, &huart3, 3);
  131. m_huachengPressureSensor.regSubmodule(4, &huart3, 4);
  132. }
  133. OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, false, true);
  134. OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, true, false);
  135. }
  136. void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
  137. int32_t ppm = rpm / 60.0 * 51200;
  138. int32_t acc = acc_rpm2 / 60.0 * 51200;
  139. int16_t _idlepower = 1;
  140. int16_t _power = 31;
  141. if (idlepower > 0 && idlepower < 31) {
  142. _idlepower = idlepower;
  143. }
  144. if (power > 0 && power < 31) {
  145. _power = power;
  146. }
  147. motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
  148. motor->setAcceleration(acc);
  149. motor->setDeceleration(acc);
  150. motor->rotate(ppm);
  151. }
  152. void air_compressor_ch_select(int32_t val) {
  153. if (val == 2) { // 内管路
  154. OUT_PD15.setState(1);
  155. } else if (val == 1) { // 空气
  156. OUT_PD15.setState(0);
  157. }
  158. }
  159. void air_compressor_valve1_set(int32_t val) { OUT_PD14.setState(val != 0); }
  160. void air_compressor_valve2_set(int32_t val) { OUT_PD14.setState(val != 0); }
  161. void air_compressor_read_pressure(int32_t *ack) { //
  162. DP600PressureSensor::sensor_data_t d = m_huachengPressureSensor.readsensordata(2);
  163. *ack = d.value;
  164. }
  165. void triple_warning_light_ctl(uint8_t r, uint8_t g, uint8_t b, uint8_t warning) {
  166. triLight_R.setState(r != 0);
  167. triLight_G.setState(g != 0);
  168. triLight_B.setState(b != 0);
  169. triLight_BEEP.setState(warning != 0);
  170. }
  171. int32_t preportional_valve_is_busy(int32_t *busy) {
  172. int32_t valve1state = 0;
  173. int32_t valve2state = 0;
  174. int32_t err = 0;
  175. *busy = 1;
  176. err = m_PreportionalValveHost.isBusy(1, &valve1state);
  177. if (err != 0) return err;
  178. err = m_PreportionalValveHost.isBusy(2, &valve2state);
  179. if (err != 0) return err;
  180. if (valve1state == 0 && valve2state == 0) {
  181. *busy = 0;
  182. } else {
  183. *busy = 1;
  184. }
  185. return 0;
  186. }
  187. } // namespace iflytop