255 lines
8.1 KiB
C
255 lines
8.1 KiB
C
#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;
|
|
|
|
static uint16_t gausAdcBuf[ADC_CHNL_NUM];
|
|
static uint16_t gusDuty = 0;
|
|
static uint16_t gusPreheatDuty = 0;
|
|
static uint16_t gusNormalDuty = 0;
|
|
|
|
uint8_t gucWorkState = 0;
|
|
static uint32_t gulXrayOnTime = 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;
|
|
uint16_t gusXrayCurrent = 0;
|
|
|
|
uint16_t adc2voltage(uint16_t adcVal)
|
|
{
|
|
return adcVal * 3000 / 65535;
|
|
}
|
|
|
|
uint16_t adc2Iflla(uint16_t adcVal)
|
|
{
|
|
return adc2voltage(adcVal) * 1000 / 20 / 20;
|
|
}
|
|
|
|
uint16_t adc2Vflla(uint16_t adcVal)
|
|
{
|
|
return adc2voltage(adcVal) * 3;
|
|
}
|
|
|
|
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;
|
|
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) {
|
|
static uint16_t ausBuf[10];
|
|
static int iCnt = 0;
|
|
|
|
if (iCurrent > gusLimitCurrent + 200) {
|
|
gusDuty -= 10;
|
|
} else if (iCurrent > gusLimitCurrent + 100) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
|
|
}
|
|
|
|
static void filamentThread(void *arg)
|
|
{
|
|
int iWorkCmd = 0;
|
|
uint32_t ulLastTick = osKernelGetTickCount();
|
|
|
|
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;
|
|
}
|
|
if ((gptSysParam->usXrayCurrent < 1000) || (gptSysParam->usXrayCurrent > 2000)) {
|
|
gusXrayCurrentSet = 1500;
|
|
} else {
|
|
gusXrayCurrentSet = gptSysParam->usXrayCurrent;
|
|
}
|
|
|
|
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
|
|
|
|
HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t *)gausAdcBuf, ADC_CHNL_NUM);
|
|
|
|
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
|
|
|
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
|
|
|
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
|
|
|
|
while (1) {
|
|
osDelay(3);
|
|
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;
|
|
gulXrayOnTime = osKernelGetTickCount();
|
|
if (gusNormalDuty < gusPreheatDuty) {
|
|
gusNormalDuty = gusPreheatDuty;
|
|
}
|
|
} else if (gucWorkState == 2) {
|
|
if (osKernelGetTickCount() > gulXrayOnTime + 5000) {
|
|
gucWorkState = 1;
|
|
gusDuty = gusPreheatDuty;
|
|
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
iWorkCmd--;
|
|
if (iWorkCmd < 0) {
|
|
iWorkCmd = 0;
|
|
if (gucWorkState == 2) {
|
|
gucWorkState = 1;
|
|
gusDuty = gusPreheatDuty;
|
|
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
|
|
}
|
|
}
|
|
}
|
|
currentUpdate();
|
|
} else {
|
|
gusDuty = 0;
|
|
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty);
|
|
}
|
|
|
|
if (osKernelGetTickCount() < ulLastTick + 500) {
|
|
continue;
|
|
}
|
|
ulLastTick = osKernelGetTickCount();
|
|
|
|
|
|
printf("\n");
|
|
printf("\n");
|
|
printf("ADC: %d,%d,%d\n", gausAdcBuf[0], gausAdcBuf[1], gausAdcBuf[2]);
|
|
gusXrayCurrent = adc2Xray(gausAdcBuf[0]);
|
|
printf("workState:%d,duty:%d%%,I:%.3fA,V:%.2fV,Xray:%.3fmA\n", gucWorkState, gusDuty * 100 / 2400, gusFilamentCurrent/1000.0, gusFilamentVoltage/1000.0, gusXrayCurrent/1000.0/1000.0/1000.0);
|
|
}
|
|
}
|
|
|
|
void filamentInit(void)
|
|
{
|
|
// 创建任务
|
|
const osThreadAttr_t filament_task_attributes = {
|
|
.name = "filament_task",
|
|
.stack_size = 4096 * 2,
|
|
.priority = (osPriority_t) osPriorityNormal,
|
|
};
|
|
if (osThreadNew(filamentThread, NULL, &filament_task_attributes) == NULL)
|
|
{
|
|
printf("failed to create filament_task\n");
|
|
}
|
|
else
|
|
{
|
|
printf("create filament_task success\n");
|
|
}
|
|
}
|
|
|