基质喷涂
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.

219 lines
5.9 KiB

  1. #include "apphardware.hpp"
  2. #include <can/can_dbg.h>
  3. #include "apphal.hpp"
  4. #include "LED/led.h"
  5. #include "status/motor_manager.h"
  6. using namespace iflytop;
  7. #define TAG "AppHardware"
  8. extern SPI_HandleTypeDef hspi1;
  9. AppHardware::AppHardware()
  10. {
  11. mutex_ = osMutexNew(NULL);
  12. if (mutex_ == NULL) {
  13. // 处理互斥锁创建失败
  14. ZLOGI(TAG, "mutex_ create failed");
  15. }
  16. }
  17. void AppHardware::initialize() {
  18. AppHal::tmc_spi_init();
  19. // 配置MOTO电源引脚
  20. MOTO_POWER_EN.initAsOutput(MOTO_POWER_EN_IO, kxs_gpio_nopull, false, true);
  21. // 初始化 MOTO1 相关引脚
  22. MOTO1_CSN.initAsOutput(MOTO1_CSN_IO, kxs_gpio_nopull, false, true);
  23. MOTO1_DRV_ENN.initAsOutput(MOTO1_DRV_ENN_IO, kxs_gpio_nopull, false, true);
  24. MOTOR1_REF_L.initAsInput(MOTOR1_REF_L_IO, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  25. MOTOR1_REF_R.initAsInput(MOTOR1_REF_R_IO, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  26. // 初始化 MOTO2 相关引脚
  27. MOTO2_CSN.initAsOutput(MOTO2_CSN_IO, kxs_gpio_nopull, false, true);
  28. MOTO2_DRV_ENN.initAsOutput(MOTO2_DRV_ENN_IO, kxs_gpio_nopull, false, true);
  29. MOTOR2_REF_L.initAsInput(MOTOR2_REF_L_IO, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  30. MOTOR2_REF_R.initAsInput(MOTOR2_REF_R_IO, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  31. // 初始化 MOTO3 相关引脚
  32. MOTO3_CSN.initAsOutput(MOTO3_CSN_IO, kxs_gpio_nopull, false, true);
  33. MOTO3_DRV_ENN.initAsOutput(MOTO3_DRV_ENN_IO, kxs_gpio_nopull, false, true);
  34. MOTOR3_REF_L.initAsInput(MOTOR3_REF_L_IO, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  35. MOTOR3_REF_R.initAsInput(MOTOR3_REF_R_IO, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  36. osDelay(1500);
  37. MOTO_POWER_EN.setState(false);
  38. TMC51X0Cfg tmc5130cfg1 = {&MOTOR_SPI_INS, MOTO1_CSN_IO, MOTO1_DRV_ENN_IO};
  39. MOTO1.initialize(0, tmc5130cfg1);
  40. ZLOGI(TAG, "motor1 initialize TMC51X0: %02X", MOTO1.readICVersion());
  41. MOTO1.enable(false);
  42. #if 0
  43. MOTO1.setIHOLD_IRUN(0, 7, 100);
  44. #else
  45. MOTO1.setIHOLD_IRUN(0, 5, 100);
  46. #endif
  47. MOTO1.setScale(1000);
  48. MOTO1.setEncResolution(-1000);
  49. // 配置 MOTO2
  50. TMC51X0Cfg tmc5130cfg2 = {&MOTOR_SPI_INS, MOTO2_CSN_IO, MOTO2_DRV_ENN_IO};
  51. MOTO2.initialize(1, tmc5130cfg2);
  52. ZLOGI(TAG, "motor2 initialize TMC51X0: %02X", MOTO2.readICVersion());
  53. MOTO2.enable(false);
  54. #if 0
  55. MOTO2.setIHOLD_IRUN(0, 10, 100);
  56. #else
  57. MOTO2.setIHOLD_IRUN(0, 5, 100);
  58. #endif
  59. MOTO2.setScale(1000);
  60. MOTO2.setEncResolution(-1000);
  61. // 配置 MOTO3
  62. TMC51X0Cfg tmc5130cfg3 = {&MOTOR_SPI_INS, MOTO3_CSN_IO, MOTO3_DRV_ENN_IO};
  63. MOTO3.initialize(2, tmc5130cfg3);
  64. ZLOGI(TAG, "motor3 initialize TMC51X0: %02X", MOTO3.readICVersion());
  65. MOTO3.enable(false);
  66. MOTO3.setIHOLD_IRUN(0, 5, 100);
  67. MOTO3.setScale(1000);
  68. MOTO3.setEncResolution(-1000);
  69. MOTO1.enable(true);
  70. osDelay(10);
  71. MOTO2.enable(true);
  72. osDelay(10);
  73. MOTO3.enable(true);
  74. osDelay(10);
  75. // GPIO 初始化
  76. AirValve::initGPIO();
  77. three_way_valve.initGPIO();
  78. pump_controller.init();
  79. high_voltage_pack.init(true);
  80. laser_control.init(true);
  81. // 初始化电机索引
  82. MotorManager::ins()->motors[0].setMotorIndex(0);
  83. MotorManager::ins()->motors[1].setMotorIndex(1);
  84. MotorManager::ins()->motors[2].setMotorIndex(2);
  85. #if 1
  86. RK3588_POWER_OFF; // 3588 下电
  87. #else
  88. RK3588_POWER_ON;
  89. #endif
  90. LED_KEY_OFF; // 按键灯关闭
  91. #if CAN_MODULE_ENABLE
  92. ZLOGI(TAG, "===== CAN Controller Start...");
  93. AppHardware::ins()->can0Controller.start(&hcan1); // 启动CAN
  94. ZLOGI(TAG, "===== CAN Controller Start Suc");
  95. #endif
  96. ZLOGI(TAG, "===== UART Controller Start...");
  97. AppHardware::ins()->uart_control.start(); // 启动CAN
  98. ZLOGI(TAG, "===== UART Controller Start Suc");
  99. isLaunched_ = false;
  100. isStarted_ = false;
  101. }
  102. bool AppHardware::isHardInitOk() { return hardwareInitedOK; }
  103. void AppHardware::SystemPowerOn() {
  104. if(isStarted_) {
  105. return;
  106. }
  107. tri_color_light(COLOR_GREEN); // 开启绿灯
  108. LIGHT_FLOOD_ON; // 照明灯开
  109. osDelay(10);
  110. ZLOGI(TAG, "======================= System Start =======================");
  111. isStarted_ = true;
  112. }
  113. void AppHardware::SystemPowerOff(bool is_force) {
  114. if(!is_force) {
  115. if(!isStarted_) {
  116. return;
  117. }
  118. }
  119. tri_color_light(COLOR_OFF); // 关灯
  120. LIGHT_FLOOD_OFF; // 照明灯关闭
  121. high_voltage_pack.turnOffPower();
  122. pump_controller.powerOff();
  123. three_way_valve.setMode(ThreeWayValve::ON_C);
  124. laser_control.powerOff();
  125. AirValve::closeCleaning();
  126. AirValve::closeDehumidification();
  127. AirValve::closeNozzle();
  128. MotorManager::ins()->motors[0].runE_Stop();
  129. osDelay(10);
  130. MotorManager::ins()->motors[1].runE_Stop();
  131. osDelay(10);
  132. MotorManager::ins()->motors[2].runE_Stop();
  133. osDelay(10);
  134. ZLOGI(TAG, "======================= System Shut Down =======================");
  135. isStarted_ = false;
  136. }
  137. void AppHardware::setE_Stop(bool isE_Stop) {
  138. this->isE_Stop_ = isE_Stop;
  139. }
  140. bool AppHardware::isE_Stop() {
  141. return isE_Stop_;
  142. }
  143. void AppHardware::toggleLaunched() {
  144. this->isLaunched_ = !isLaunched_;
  145. }
  146. bool AppHardware::isLaunched() {
  147. return isLaunched_;
  148. }
  149. bool AppHardware::isStarted() {
  150. return isStarted_;
  151. }
  152. void AppHardware::setFlowSpeed(uint16_t flowSpeed) {
  153. osMutexAcquire(mutex_, osWaitForever);
  154. osMutexRelease(mutex_);
  155. }
  156. void AppHardware::setHumidity(int16_t humidity) {
  157. osMutexAcquire(mutex_, osWaitForever);
  158. osMutexRelease(mutex_);
  159. }
  160. void AppHardware::setTemp(int16_t temp) {
  161. osMutexAcquire(mutex_, osWaitForever);
  162. osMutexRelease(mutex_);
  163. }
  164. uint16_t AppHardware::getFlowSpeed() {
  165. osMutexAcquire(mutex_, osWaitForever);
  166. osMutexRelease(mutex_);
  167. }
  168. int16_t AppHardware::getHumidity() {
  169. osMutexAcquire(mutex_, osWaitForever);
  170. osMutexRelease(mutex_);
  171. }
  172. int16_t AppHardware::getTemp() {
  173. osMutexAcquire(mutex_, osWaitForever);
  174. osMutexRelease(mutex_);
  175. }