更新通讯协议,完善灯丝控制

This commit is contained in:
2026-08-26 16:06:02 +08:00
parent 29e22659ce
commit 653c7ec412
10 changed files with 228 additions and 113 deletions
+108 -25
View File
@@ -1,34 +1,26 @@
#include "main.h"
#include "cmsis_os.h"
#include "filament.h"
#include "CPY_PROT/cpy_prot.h"
#include "Param/param.h"
extern TIM_HandleTypeDef htim1;
extern ADC_HandleTypeDef hadc1;
extern DMA_HandleTypeDef hdma_adc1;
uint16_t gausAdcBuf[ADC_CHNL_NUM];
static uint16_t gausAdcBuf[ADC_CHNL_NUM];
static uint16_t gusDuty = 0;
static uint16_t gusPreheatDuty = 0;
static uint16_t gusNormalDuty = 0;
void ifilaAdust(void)
{
static uint32_t ulLastTick = 0;
uint32_t ulTick = osKernelGetTickCount();
if (ulTick - ulLastTick < 5000) {
return;
}
ulLastTick = ulTick;
uint8_t gucWorkState = 0;
if (gusDuty == 0) {
gusDuty = 300;
} else if (gusDuty == 300) {
gusDuty = 600;
} else if (gusDuty == 600) {
gusDuty = 300;
}
static uint16_t gusPreheatCurrent = 0;
static uint16_t gusLimitCurrent = 0;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
}
uint16_t gusFilamentCurrent = 0;
uint16_t gusFilamentVoltage = 0;
uint16_t gusXrayCurrent = 0;
uint16_t adc2voltage(uint16_t adcVal)
{
@@ -45,11 +37,67 @@ uint16_t adc2Vflla(uint16_t adcVal)
return adc2voltage(adcVal) * 3;
}
uint16_t adc2Xray(uint16_t adcVal)
{
return adc2voltage(adcVal) * 1000 / 3 / 250;
}
void currentUpdate(void)
{
int iCurrent = gusFilamentCurrent;
int iXrayCurrent = gusXrayCurrent;
if (gucWorkState == 1) {
uint16_t usAimCurrent = gusPreheatCurrent;
if (iCurrent > usAimCurrent + 500) {
gusPreheatDuty -= 10;
} else if (iCurrent > usAimCurrent + 300) {
gusPreheatDuty -= 5;
} else if (iCurrent > usAimCurrent + 100) {
gusPreheatDuty -= 2;
} else if (iCurrent > usAimCurrent + 10) {
gusPreheatDuty -= 1;
} else if (iCurrent < usAimCurrent - 500) {
gusPreheatDuty += 10;
} else if (iCurrent < usAimCurrent - 300) {
gusPreheatDuty += 5;
} else if (iCurrent < usAimCurrent - 100) {
gusPreheatDuty += 2;
} else if (iCurrent < usAimCurrent - 10) {
gusPreheatDuty += 1;
}
gusDuty = gusPreheatDuty;
} else if (gucWorkState == 2) {
if (iCurrent > gusLimitCurrent + 200) {
gusNormalDuty -= 5;
} else if (iCurrent > gusLimitCurrent + 100) {
gusNormalDuty -= 2;
} else if (iCurrent > gusLimitCurrent) {
gusNormalDuty -= 1;
}
gusDuty = gusNormalDuty;
}
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
}
static void filamentThread(void *arg)
{
int iWorkCmd = 0;
int iCnt = 0;
loadSysParam();
if ((gptSysParam->usPreheatCurrent < 100) || (gptSysParam->usPreheatCurrent > 5000)) {
gusPreheatCurrent = 2000;
} else {
gusPreheatCurrent = gptSysParam->usPreheatCurrent;
}
if ((gptSysParam->usLimitCurrent < 100) || (gptSysParam->usLimitCurrent > 5000)) {
gusLimitCurrent = 3500;
} else {
gusLimitCurrent = gptSysParam->usLimitCurrent;
}
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t *)gausAdcBuf, ADC_CHNL_NUM);
@@ -62,21 +110,56 @@ static void filamentThread(void *arg)
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
ifilaAdust();
while (1) {
osDelay(10);
// ifilaAdust();
osDelay(1);
SCB_InvalidateDCache_by_Addr(gausAdcBuf, ADC_CHNL_NUM * sizeof(gausAdcBuf[0]));
gusXrayCurrent = adc2Xray(gausAdcBuf[0]);
gusFilamentCurrent = adc2Iflla(gausAdcBuf[1]);
gusFilamentVoltage = adc2Vflla(gausAdcBuf[2]);
if (gucWorkState != 0) {
if (HAL_GPIO_ReadPin(WORK_CMD_GPIO_Port, WORK_CMD_Pin) == GPIO_PIN_RESET) {
iWorkCmd++;
if (iWorkCmd > 3) {
iWorkCmd = 3;
if (gucWorkState == 1) {
gucWorkState = 2;
if (gusNormalDuty == 0) {
gusNormalDuty = gusPreheatDuty * 2;
}
gusDuty = gusNormalDuty;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
osDelay(3);
}
}
} else {
iWorkCmd--;
if (iWorkCmd < 0) {
iWorkCmd = 0;
if (gucWorkState == 2) {
gucWorkState = 1;
gusDuty = gusPreheatDuty;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
osDelay(3);
}
}
}
currentUpdate();
} else {
gusDuty = 0;
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
}
iCnt++;
if (iCnt < 100) {
if (iCnt < 1000) {
continue;
}
iCnt = 0;
SCB_InvalidateDCache_by_Addr(gausAdcBuf, ADC_CHNL_NUM * sizeof(gausAdcBuf[0]));
printf("ADC: %d,%d,%d\n", gausAdcBuf[0], gausAdcBuf[1], gausAdcBuf[2]);
printf("duty:%d%%,I:%.2fA,V:%.2fV\n", gusDuty*100/2400, adc2Iflla(gausAdcBuf[1]) / 1000.0, adc2Vflla(gausAdcBuf[2])/1000.0);
printf("duty:%d%%,I:%.3fA,V:%.2fV\n", gusDuty, gusFilamentCurrent/1000.0, gusFilamentVoltage/1000.0);
}
}
+4
View File
@@ -6,6 +6,10 @@
#include "main.h"
/* Exported types ------------------------------------------------------------*/
extern uint8_t gucWorkState;
extern uint16_t gusFilamentCurrent;
extern uint16_t gusFilamentVoltage;
extern uint16_t gusXrayCurrent;
void filamentInit(void);