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

133 lines
3.6 KiB

  1. //
  2. // Created by iflyt on 2025/2/28.
  3. //
  4. #include "pump_controller.h"
  5. #include <cmath>
  6. #include <base/zbasic.h>
  7. #include <bits/stl_algobase.h>
  8. #include "tim_pwm.h"
  9. PumpController::PumpController() = default;
  10. PumpController::~PumpController() {}
  11. void PumpController::init() {
  12. GPIO_InitTypeDef GPIO_InitStruct = {0};
  13. // 使能 GPIOD 和 GPIOI 时钟
  14. __HAL_RCC_GPIOD_CLK_ENABLE();
  15. __HAL_RCC_GPIOI_CLK_ENABLE();
  16. // 配置 PUMP_EN_PIN 和 PUMP_DIR_PIN 为输出模式
  17. GPIO_InitStruct.Pin = PUMP_EN_PIN | PUMP_DIR_PIN;
  18. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  19. GPIO_InitStruct.Pull = GPIO_NOPULL;
  20. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  21. HAL_GPIO_Init(PUMP_EN_PORT, &GPIO_InitStruct);
  22. // 配置 PUMP_FG_PIN 为输入模式
  23. GPIO_InitStruct.Pin = PUMP_FG_PIN;
  24. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  25. GPIO_InitStruct.Pull = GPIO_NOPULL;
  26. HAL_GPIO_Init(PUMP_FG_PORT, &GPIO_InitStruct);
  27. // 初始状态下禁用注射泵
  28. powerOff();
  29. }
  30. void PumpController::powerOn() {
  31. isPowerOn = true;
  32. HAL_GPIO_WritePin(PUMP_EN_PORT, PUMP_EN_PIN, GPIO_PIN_RESET);
  33. this->openPwm();
  34. }
  35. void PumpController::powerOff() {
  36. isPowerOn = false;
  37. HAL_GPIO_WritePin(PUMP_EN_PORT, PUMP_EN_PIN, GPIO_PIN_SET);
  38. this->closePwm();
  39. }
  40. void PumpController::setFlowSpeed(double flow) {
  41. // 设置为零 自动关闭
  42. if(flow == 0.0) {
  43. this->powerOff();
  44. ZLOGI("【PUMP】", "SET FLOW POWER OFF");
  45. return;
  46. }
  47. bool is_forward = flow > 0;
  48. const double abs_flow = fabs(flow);
  49. this->freq_ = calculateFrequency(abs_flow);
  50. if(this->freq_ == 0) {
  51. this->freq_ = is_forward ? 1 : -1;
  52. ZLOGI("【PUMP】", "[ZERO LIMIT] FREQ [%d] ", freq_);
  53. }
  54. ZLOGI("【PUMP】", "FINAL FREQ [%d] ", freq_);
  55. if(this->isPowerOn) {
  56. this->openPwm();
  57. this->powerOn();
  58. }
  59. }
  60. void PumpController::moveWithFlowSpeed(double flow) {
  61. // 设置为零 自动关闭
  62. if(flow == 0.0) {
  63. this->powerOff();
  64. ZLOGI("【PUMP】", "SET FLOW POWER OFF");
  65. return;
  66. }
  67. bool is_forward = flow > 0;
  68. const double abs_flow = fabs(flow);
  69. this->freq_ = calculateFrequency(abs_flow);
  70. if(this->freq_ == 0) {
  71. this->freq_ = is_forward ? 1 : -1;
  72. ZLOGI("【PUMP】", "[ZERO LIMIT] FREQ [%d] ", freq_);
  73. }
  74. ZLOGI("【PUMP】", "FINAL FREQ [%d] ", freq_);
  75. this->setDirection(is_forward);
  76. this->openPwm();
  77. this->powerOn();
  78. }
  79. void PumpController::setDirection(bool forward) {
  80. if (forward) {
  81. HAL_GPIO_WritePin(PUMP_DIR_PORT, PUMP_DIR_PIN, GPIO_PIN_SET);
  82. } else {
  83. HAL_GPIO_WritePin(PUMP_DIR_PORT, PUMP_DIR_PIN, GPIO_PIN_RESET);
  84. }
  85. }
  86. bool PumpController::isNoraml() {
  87. // return HAL_GPIO_ReadPin(PUMP_FG_PORT, PUMP_FG_PIN) == GPIO_PIN_SET;
  88. return true;
  89. }
  90. void PumpController::openPwm() {
  91. bsp_SetTIMOutPWM(PUMP_PUL_PORT, PUMP_PUL_PIN, PUMP_TIMER_HANDLE, PUMP_TIMER_CHANNEL, freq_, standardDutyCycle_);
  92. }
  93. void PumpController::closePwm() {
  94. #if 0
  95. bsp_SetTIMOutPWM(PUMP_PUL_PORT, PUMP_PUL_PIN, PUMP_TIMER_HANDLE, PUMP_TIMER_CHANNEL, 0, 0);
  96. #else
  97. bsp_SetTIMOutPWM(PUMP_PUL_PORT, PUMP_PUL_PIN, PUMP_TIMER_HANDLE, PUMP_TIMER_CHANNEL, 0, 10000);
  98. #endif
  99. }
  100. int32_t PumpController::calculateFrequency(double flow) const{
  101. // 确保流量值在有效范围内
  102. flow = std::max(flow, static_cast<double>(MIN_FLOW));
  103. flow = std::min(flow, static_cast<double>(MAX_FLOW));
  104. // 计算斜率
  105. double slope = static_cast<double>(MAX_FREQUENCY - MIN_FREQUENCY) / (MAX_FLOW - MIN_FLOW);
  106. // 根据线性关系计算频率
  107. return MIN_FREQUENCY + (slope * (flow - MIN_FLOW) + 0.5);
  108. }