Files
CPY_AuxiliaryControlBoard/Drivers/User/Filament/filament.c
T

184 lines
5.2 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 uint16_t gusPreheatCurrent = 0;
static uint16_t gusLimitCurrent = 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;
}
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);
debug("ADC_CHNL_NUM = %d\n", 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(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 < 1000) {
continue;
}
iCnt = 0;
printf("ADC: %d,%d,%d\n", gausAdcBuf[0], gausAdcBuf[1], gausAdcBuf[2]);
printf("duty:%d%%,I:%.3fA,V:%.2fV\n", gusDuty, gusFilamentCurrent/1000.0, gusFilamentVoltage/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");
}
}