From 8ec4a849eee142a70a6cb235b68c2ce42db82d65 Mon Sep 17 00:00:00 2001 From: leon Date: Tue, 8 Sep 2026 18:11:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B0=84=E7=BA=BF=E7=94=B5?= =?UTF-8?q?=E6=B5=81=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/main.h | 2 +- Drivers/User/Filament/filament.c | 113 ++++++++++++++++++++++++------- 2 files changed, 89 insertions(+), 26 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index ff72e7a..8e0b20b 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -48,7 +48,7 @@ extern "C" { /* Exported macro ------------------------------------------------------------*/ /* USER CODE BEGIN EM */ -#define VERSION_STR "V1.1.0" +#define VERSION_STR "V1.3.0" #ifdef BOOTLOADER #define SW_VER "BL for " VERSION_STR #else diff --git a/Drivers/User/Filament/filament.c b/Drivers/User/Filament/filament.c index dce6c66..6e2a35a 100644 --- a/Drivers/User/Filament/filament.c +++ b/Drivers/User/Filament/filament.c @@ -14,9 +14,13 @@ static uint16_t gusPreheatDuty = 0; static uint16_t gusNormalDuty = 0; uint8_t gucWorkState = 0; +static uint32_t gulXrayOnTime = 0; -static uint16_t gusPreheatCurrent = 0; -static uint16_t gusLimitCurrent = 0; +static uint16_t gusPreheatCurrent = 2000; +static uint16_t gusLimitCurrent = 3500; +static uint16_t gusXrayCurrentSet = 1500; + +static uint16_t gusLastXrayCurrent = 0; uint16_t gusFilamentCurrent = 0; uint16_t gusFilamentVoltage = 0; @@ -42,6 +46,25 @@ uint16_t adc2Xray(uint16_t adcVal) return adc2voltage(adcVal) * 1000 / 3 / 250; } +uint16_t fastUpdate(void) +{ + int iRet = gusDuty; + if (gusXrayCurrent > gusXrayCurrentSet + 500) { + iRet = gusDuty - 20; + } else if (gusXrayCurrent > gusXrayCurrentSet + 300) { + iRet = gusDuty - 10; + } else if (gusXrayCurrent > gusXrayCurrentSet + 100) { + iRet = gusDuty - 5; + } else if (gusXrayCurrent < gusXrayCurrentSet - 500) { + iRet = gusDuty + 20; + } else if (gusXrayCurrent < gusXrayCurrentSet - 300) { + iRet = gusDuty + 10; + } else if (gusXrayCurrent < gusXrayCurrentSet - 100) { + iRet = gusDuty + 5; + } + return iRet; +} + void currentUpdate(void) { int iCurrent = gusFilamentCurrent; @@ -68,14 +91,54 @@ void currentUpdate(void) } gusDuty = gusPreheatDuty; } else if (gucWorkState == 2) { + static uint16_t ausBuf[10]; + static int iCnt = 0; + if (iCurrent > gusLimitCurrent + 200) { - gusNormalDuty -= 5; + gusDuty -= 10; } else if (iCurrent > gusLimitCurrent + 100) { - gusNormalDuty -= 2; - } else if (iCurrent > gusLimitCurrent) { - gusNormalDuty -= 1; + gusDuty -= 5; + } else if (osKernelGetTickCount() < gulXrayOnTime + 800) { + if (gusLastXrayCurrent < gusXrayCurrentSet - 200) { + gusNormalDuty = fastUpdate(); + gusDuty = gusNormalDuty; + } else { + if (gusDuty + 10 < gusNormalDuty) { + gusDuty += 10; + } else { + gusDuty = gusNormalDuty; + } + } + } else if (osKernelGetTickCount() < gulXrayOnTime + 2000) { + iCnt = 0; + } else if (osKernelGetTickCount() < gulXrayOnTime + 2500 && iCnt < 10) { + ausBuf[iCnt++] = gusXrayCurrent; + if (iCnt >= 10) { + int iSum = 0; + for (int i = 0; i < iCnt; i++) { + iSum += ausBuf[i]; + } + iSum /= iCnt; + gusLastXrayCurrent = iSum; + if (iSum > gusXrayCurrentSet + 500) { + gusNormalDuty -= 20; + } else if (iSum > gusXrayCurrentSet + 300) { + gusNormalDuty -= 10; + } else if (iSum > gusXrayCurrentSet + 100) { + gusNormalDuty -= 5; + } else if (iSum > gusXrayCurrentSet + 50) { + gusNormalDuty -= 1; + } else if (iSum < gusXrayCurrentSet - 500) { + gusNormalDuty += 20; + } else if (iSum < gusXrayCurrentSet - 300) { + gusNormalDuty += 10; + } else if (iSum < gusXrayCurrentSet - 100) { + gusNormalDuty += 5; + } else if (iSum < gusXrayCurrentSet - 50) { + gusNormalDuty += 1; + } + } } - gusDuty = gusNormalDuty; } __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); @@ -84,7 +147,7 @@ void currentUpdate(void) static void filamentThread(void *arg) { int iWorkCmd = 0; - uint32_t ulLastTick = HAL_GetTick(); + uint32_t ulLastTick = osKernelGetTickCount(); loadSysParam(); if ((gptSysParam->usPreheatCurrent < 100) || (gptSysParam->usPreheatCurrent > 5000)) { @@ -97,6 +160,11 @@ static void filamentThread(void *arg) } else { gusLimitCurrent = gptSysParam->usLimitCurrent; } + if ((gptSysParam->usXrayCurrent < 1000) || (gptSysParam->usXrayCurrent > 2000)) { + gusXrayCurrentSet = 1500; + } else { + gusXrayCurrentSet = gptSysParam->usXrayCurrent; + } HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); @@ -109,7 +177,7 @@ static void filamentThread(void *arg) HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); while (1) { - osDelay(1); + osDelay(3); SCB_InvalidateDCache_by_Addr(gausAdcBuf, ADC_CHNL_NUM * sizeof(gausAdcBuf[0])); gusXrayCurrent = adc2Xray(gausAdcBuf[0]); @@ -123,20 +191,16 @@ static void filamentThread(void *arg) iWorkCmd = 3; if (gucWorkState == 1) { gucWorkState = 2; - // if (gusNormalDuty < gusPreheatDuty * 1.5 ) { - // gusNormalDuty = gusPreheatDuty * 1.5; - // } - gusNormalDuty = gusPreheatDuty * 1.5; - gusDuty = gusNormalDuty; - __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); - osDelay(3); + gulXrayOnTime = osKernelGetTickCount(); + if (gusNormalDuty < gusPreheatDuty) { + gusNormalDuty = gusPreheatDuty; + } } else if (gucWorkState == 2) { - if (gusNormalDuty < 750) { - gusNormalDuty += 10; + if (osKernelGetTickCount() > gulXrayOnTime + 5000) { + gucWorkState = 1; + gusDuty = gusPreheatDuty; + __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); } - gusDuty = gusNormalDuty; - __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); - osDelay(3); } } } else { @@ -147,20 +211,19 @@ static void filamentThread(void *arg) gucWorkState = 1; gusDuty = gusPreheatDuty; __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); - osDelay(3); } } } currentUpdate(); - } else { + } else { gusDuty = 0; __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); } - if (HAL_GetTick() - ulLastTick < 500) { + if (osKernelGetTickCount() < ulLastTick + 500) { continue; } - ulLastTick = HAL_GetTick(); + ulLastTick = osKernelGetTickCount(); printf("\n");