Compare commits

..

3 Commits

33 changed files with 4412 additions and 1160 deletions

View File

@ -74,7 +74,7 @@ void PeriphCommonClock_Config(void);
* @retval int
*/
int main(void)
{
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@ -1,6 +1,6 @@
/*
* Auto generated Run-Time-Environment Configuration File
* Auto generated Run-Time-Environment Component Configuration File
* *** Do not modify ! ***
*
* Project: 'controller-v2'
@ -17,5 +17,4 @@
#define CMSIS_device_header "stm32l4xx.h"
#endif /* RTE_COMPONENTS_H */

View File

@ -114,13 +114,13 @@
<tDlgDll></tDlgDll>
<tDlgPa></tDlgPa>
<tIfile></tIfile>
<pMon>STLink\ST-LINKIII-KEIL_SWO.dll</pMon>
<pMon>Segger\JL2CM3.dll</pMon>
</DebugOpt>
<TargetDriverDllRegistry>
<SetRegEntry>
<Number>0</Number>
<Key>DLGUARM</Key>
<Name>(105=-1,-1,-1,-1,0)</Name>
<Name>?</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -260,6 +260,26 @@
<WinNumber>1</WinNumber>
<ItemText>s_watch,0x0A</ItemText>
</Ww>
<Ww>
<count>20</count>
<WinNumber>1</WinNumber>
<ItemText>uDevice.ProcessChange_Flag,0x0A</ItemText>
</Ww>
<Ww>
<count>21</count>
<WinNumber>1</WinNumber>
<ItemText>Kp_watch</ItemText>
</Ww>
<Ww>
<count>22</count>
<WinNumber>1</WinNumber>
<ItemText>Ki_watch</ItemText>
</Ww>
<Ww>
<count>23</count>
<WinNumber>1</WinNumber>
<ItemText>Kd_watch</ItemText>
</Ww>
</WatchWindow1>
<Tracepoint>
<THDelay>0</THDelay>
@ -305,7 +325,6 @@
<pMultCmdsp></pMultCmdsp>
<DebugDescription>
<Enable>1</Enable>
<EnableFlashSeq>0</EnableFlashSeq>
<EnableLog>0</EnableLog>
<Protocol>2</Protocol>
<DbgClock>10000000</DbgClock>
@ -663,7 +682,7 @@
<Group>
<GroupName>Application/MDK-ARM</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -683,7 +702,7 @@
<Group>
<GroupName>Application/User/Core</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -1259,7 +1278,7 @@
<Group>
<GroupName>User/system</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>

View File

@ -10,14 +10,14 @@
<TargetName>controller-v2</TargetName>
<ToolsetNumber>0x4</ToolsetNumber>
<ToolsetName>ARM-ADS</ToolsetName>
<pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCC</pCCUsed>
<pCCUsed>5060061::V5.06 update 1 (build 61)::ARMCC</pCCUsed>
<uAC6>0</uAC6>
<TargetOption>
<TargetCommonOption>
<Device>STM32L476VGTx</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32L4xx_DFP.2.6.2</PackID>
<PackURL>https://www.keil.com/pack/</PackURL>
<PackID>Keil.STM32L4xx_DFP.2.1.0</PackID>
<PackURL>http://www.keil.com/pack</PackURL>
<Cpu>IRAM(0x20000000,0x00018000) IRAM2(0x10000000,0x00008000) IROM(0x08000000,0x00100000) CPUTYPE("Cortex-M4") FPU2 DSP CLOCK(12000000) ELITTLE</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
@ -184,8 +184,6 @@
<hadXRAM>0</hadXRAM>
<uocXRam>0</uocXRam>
<RvdsVP>1</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>1</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -352,7 +350,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<uClangAs>0</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -993,7 +991,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -1045,8 +1043,8 @@
<TargetCommonOption>
<Device>STM32L476VGTx</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32L4xx_DFP.2.6.2</PackID>
<PackURL>https://www.keil.com/pack/</PackURL>
<PackID>Keil.STM32L4xx_DFP.2.1.0</PackID>
<PackURL>http://www.keil.com/pack</PackURL>
<Cpu>IRAM(0x20000000,0x00018000) IRAM2(0x10000000,0x00008000) IROM(0x08000000,0x00100000) CPUTYPE("Cortex-M4") FPU2 DSP CLOCK(12000000) ELITTLE</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
@ -1213,8 +1211,6 @@
<hadXRAM>0</hadXRAM>
<uocXRam>0</uocXRam>
<RvdsVP>1</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>1</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -1381,7 +1377,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<uClangAs>0</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -1884,8 +1880,8 @@
<TargetCommonOption>
<Device>STM32L476VGTx</Device>
<Vendor>STMicroelectronics</Vendor>
<PackID>Keil.STM32L4xx_DFP.2.6.2</PackID>
<PackURL>https://www.keil.com/pack/</PackURL>
<PackID>Keil.STM32L4xx_DFP.2.1.0</PackID>
<PackURL>http://www.keil.com/pack</PackURL>
<Cpu>IRAM(0x20000000,0x00018000) IRAM2(0x10000000,0x00008000) IROM(0x08000000,0x00100000) CPUTYPE("Cortex-M4") FPU2 DSP CLOCK(12000000) ELITTLE</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile></StartupFile>
@ -2052,8 +2048,6 @@
<hadXRAM>0</hadXRAM>
<uocXRam>0</uocXRam>
<RvdsVP>1</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>1</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -2220,7 +2214,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<uClangAs>0</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2309,7 +2303,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2388,7 +2382,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2512,7 +2506,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2646,7 +2640,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2725,7 +2719,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2814,7 +2808,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -2943,7 +2937,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -3052,7 +3046,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -3231,7 +3225,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -3340,7 +3334,7 @@
<NoWarn>2</NoWarn>
<uSurpInc>2</uSurpInc>
<useXO>2</useXO>
<ClangAsOpt>0</ClangAsOpt>
<uClangAs>2</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>

View File

@ -11,12 +11,8 @@
#define TIME_CYCLE 0.01f // 定义时间周期:10ms
#define FSM_WAIT(st) BIT_SET(st, BIT7)
#define FSM_IS_WAIT(st) (st & BIT7)
#define DIFF_ADC_MAX 6U
#define DIFF_ADC_MAX 10U
/*阶跃信号大小*/
#define FULL_TRIP_SIGNAL 925 // 925 2020
#define HALF_TRIP_SIGNAL 753
#define SMALL_TRIP_SIGNAL 650
///////////////////////////////////////*算法整定部分宏定义END*///////////////////////////////////////////
#define EXECUTE_PLAN EXECUTE_PLAN_1
@ -36,12 +32,13 @@ typedef struct
uint8_t tuned_flag; // 是否整定过 1整定过 0未整定过
uint8_t tuned_state; // 自整定结果
uint16_t bleeding; // 放气值
uint16_t inflation; // 充气值
uint16_t startup_value; // 阀位启动值
uint16_t arr_diff; // 控制区间
uint16_t ad_diff; // 磁条区间
uint16_t pwmp_min; // 控制最小值
uint16_t pwmp_max; // 控制最大值
uint16_t pwmp_min_origin;// 整定控制最小值
uint16_t pwmp_max_origin;// 整定控制最大值
uint16_t trip_0; // 磁条0位置AD
uint16_t trip_100; // 词条100位置AD
uint16_t trip_min0; // 小回路行程0位置AD
@ -111,6 +108,8 @@ typedef enum
PWMP_HD_ADJUST_ACCURATE_POSITION0, // 精调位置0
PWMP_HD_ADJUST_ROUGH_POSITION100, // 粗调位置100
PWMP_HD_ADJUST_ACCURATE_POSITION100, // 精调位置100
PWMP_HD_ADJUST_ALL_CLOSE_TIME, // 全关时间
PWMP_HD_ADJUST_ALL_OPEN_TIME, // 全开时间
PWMP_HD_ADJUST_CALCULATE, // 阀门参数计算
PWMP_HD_ADJUST_PID_CALCULATE, // PID参数计算
PWMP_HD_ADJUST_PID_TUNING, // PID参数自整定过程(整定得到K、T、L)
@ -155,8 +154,9 @@ typedef struct
uint16_t arr_record_1; // 最小推动阀门的ad值
uint16_t arr_record_2; // 最大位置阀门的ad值
uint32_t all_open_time; // 全开时间
uint32_t all_close_time; // 全关时间
uint32_t all_open_time; // 全开时间
uint32_t all_open_time_full; // 信号为最大输出时的全开时间
uint32_t all_close_time; // 全关时间
BOOL all_close_time_flag;
uint32_t tmp_time; // 临时用来记录全开全关/整定时间

View File

@ -84,9 +84,9 @@ static void pwmp_control_update(filter_e type)
}
/*PID参数获取*/
static float32 get_pwmp_control_kp(void)
static float32 get_pwmp_hd_control_kp(void)
{
if (mode_pwmp_hd->pwmp_save->storage.kp < 1)
if (mode_pwmp_hd->pwmp_save->storage.kp < 2)
{
return mode_pwmp_hd->pwmp_save->storage.kp * 10;
}
@ -96,14 +96,14 @@ static float32 get_pwmp_control_kp(void)
}
}
static float32 get_pwmp_control_ki(void)
static float32 get_pwmp_hd_control_ki(void)
{
return mode_pwmp_hd->pwmp_save->storage.ki;
return mode_pwmp_hd->pwmp_save->storage.ki < 0.001 ? mode_pwmp_hd->pwmp_save->storage.ki * 10 : 0.018;
}
static float32 get_pwmp_control_kd(void)
static float32 get_pwmp_hd_control_kd(void)
{
return mode_pwmp_hd->pwmp_save->storage.kd;
return mode_pwmp_hd->pwmp_save->storage.kd < 0.01 ? mode_pwmp_hd->pwmp_save->storage.kd : 0.005;
}
/*将自己结构体变量中的参数保存到公共参数中*/
@ -206,7 +206,7 @@ void execute_pid_init(execute_plan_hd_e plan)
case EXECUTE_PLAN_1:
_pid.type = PID_TYPE_CUSTOM_HANGDIAN;
pid_constructor(&_pid);
_pid.pid_u.hd.set_ctrl_prm_position(&_pid.pid_u.hd, get_pwmp_control_kp(), get_pwmp_control_ki(), get_pwmp_control_kd());
_pid.pid_u.hd.set_ctrl_prm_position(&_pid.pid_u.hd, get_pwmp_hd_control_kp(), get_pwmp_hd_control_ki(), get_pwmp_hd_control_kd());
break;
case EXECUTE_PLAN_2:
@ -352,12 +352,6 @@ static void pid_autotune_way_set(pid_autotune_way_e state)
pid_autotune_hd->autotune_way = state;
}
/*阶跃信号设定*/
static void set_step_signal(uint16_t signal_in)
{
pdctrl_out(signal_in);
}
/*判断阀门状态:移动、停止、停止但仍在等待*/
static valve_position_change_e pwmp_adjust_hd_valve_position_change(uint8_t *state, uint8_t next_state, uint8_t diff_adc_max)
{
@ -443,8 +437,8 @@ static void pwmp_adjust_hd_idle(uint8_t *state, mode_pwmp_hd_adjust_state_e next
{
case POSITION_NO_CHANGE_FOREVER:
// 位置不再改变记录此时阀门位置AD值
mode_pwmp_hd_adjust->adc_record_1 = mode_pwmp_hd_adjust->psb_adc;
mode_pwmp_hd_adjust->adc_record_0 = mode_pwmp_hd_adjust->psb_adc;
mode_pwmp_hd_adjust->adc_record_1 = pid_autotune_hd->data.adjust_tmp_actual;
mode_pwmp_hd_adjust->adc_record_0 = pid_autotune_hd->data.adjust_tmp_actual;
break;
case POSITION_CHANGE:
*state = PWMP_HD_ADJUST_IDEL;
@ -460,13 +454,11 @@ static void pwmp_adjust_hd_idle(uint8_t *state, mode_pwmp_hd_adjust_state_e next
/*整定粗调0*/
static void pwmp_adjust_hd_rough_position0(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
// 找最小推动值arr
uint16_t current_adc = 0;
if (!FSM_IS_WAIT(*state))
{
// 以10%增加提高DAC输出找到能推动阀门的最小值
mode_pwmp_hd_adjust->arr_current = 50;
mode_pwmp_hd_adjust->arr_current = 100;
mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
pdctrl_out(mode_pwmp_hd_adjust->arr_current);
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 20;
@ -477,8 +469,8 @@ static void pwmp_adjust_hd_rough_position0(uint8_t *state, mode_pwmp_hd_adjust_s
{
valve_position_change_e s = pwmp_adjust_hd_valve_position_change(state, next_state, DIFF_ADC_MAX);
current_adc = mode_pwmp_hd_adjust->psb_adc;
if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_1) >= DIFF_ADC_MAX)
current_adc = pid_autotune_hd->data.adjust_tmp_actual;
if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_1) >= DIFF_ADC_MAX) // 阀位发生改变
{
if (mode_pwmp_hd_adjust->arr_last > 4000)
{
@ -487,14 +479,14 @@ static void pwmp_adjust_hd_rough_position0(uint8_t *state, mode_pwmp_hd_adjust_s
}
else
{
/*修正因启动值过大而导致的信号增加过大*/
/*得到一个粗略的,小于启动量的值*/
mode_pwmp_hd_adjust->arr_record_1 = mode_pwmp_hd_adjust->arr_last > 1000 ? mode_pwmp_hd_adjust->arr_last * 0.9 : mode_pwmp_hd_adjust->arr_last;
/**/
/*判断位置反馈磁条是正装还是反装*/
mode_pwmp_hd_adjust->adc_0_100_flag = mode_pwmp_hd_adjust->adc_record_0 < current_adc ? TRUE : FALSE;
}
*state = next_state;
}
else
else // 阀位未发生变化:继续增大输出
{
switch (s)
{
@ -507,11 +499,11 @@ static void pwmp_adjust_hd_rough_position0(uint8_t *state, mode_pwmp_hd_adjust_s
mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
if (mode_pwmp_hd_adjust->arr_last < 1000)
{
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_current * 1.1;
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_current * 1.05;
}
else
{
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_current * 1.05;
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_current * 1.02;
}
/*若输出信号为最大值时认定为整定错误*/
if (mode_pwmp_hd_adjust->arr_current > 4000)
@ -522,7 +514,7 @@ static void pwmp_adjust_hd_rough_position0(uint8_t *state, mode_pwmp_hd_adjust_s
}
pdctrl_out(mode_pwmp_hd_adjust->arr_current);
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 20;
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 40;
*state = PWMP_HD_ADJUST_ROUGH_POSITION0;
FSM_WAIT(*state); // 设置等待状态
}
@ -546,7 +538,7 @@ static void pwmp_adjust_hd_rough_position0(uint8_t *state, mode_pwmp_hd_adjust_s
}
}
/*不充不放值0信号的整定*/
/*0信号的整定*/
static void pwmp_adjust_hd_bleeding_position0(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
BOOL flag = FALSE;
@ -560,32 +552,41 @@ static void pwmp_adjust_hd_bleeding_position0(uint8_t *state, mode_pwmp_hd_adjus
}
else
{
if (mode_pwmp_hd_adjust->adc_0_100_flag)
// if (mode_pwmp_hd_adjust->adc_0_100_flag)
// {
// if (mode_pwmp_hd_adjust->psb_adc <= mode_pwmp_hd_adjust->last_adc)
// {
// if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->last_adc) > DIFF_ADC_MAX)
// {
// mode_pwmp_hd_adjust->arr_record_0 = mode_pwmp_hd_adjust->arr_last + DIFF_ADC_MAX;
// flag = TRUE;
// }
// }
// }
// else
// {
// if (mode_pwmp_hd_adjust->psb_adc >= mode_pwmp_hd_adjust->last_adc)
// {
// if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->last_adc) > DIFF_ADC_MAX)
// {
// mode_pwmp_hd_adjust->arr_record_0 = mode_pwmp_hd_adjust->arr_last + DIFF_ADC_MAX;
// flag = TRUE;
// }
// }
// }
if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->adc_record_1) < 6)
{
if (mode_pwmp_hd_adjust->psb_adc <= mode_pwmp_hd_adjust->last_adc)
{
if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->last_adc) > DIFF_ADC_MAX)
{
mode_pwmp_hd_adjust->arr_record_0 = mode_pwmp_hd_adjust->arr_last + DIFF_ADC_MAX;
flag = TRUE;
}
}
}
else
{
if (mode_pwmp_hd_adjust->psb_adc >= mode_pwmp_hd_adjust->last_adc)
{
if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->last_adc) > DIFF_ADC_MAX)
{
mode_pwmp_hd_adjust->arr_record_0 = mode_pwmp_hd_adjust->arr_last + DIFF_ADC_MAX;
flag = TRUE;
}
}
mode_pwmp_hd_adjust->arr_record_0 = mode_pwmp_hd_adjust->arr_current;
flag = TRUE;
}
if (flag == TRUE)
{
mode_pwmp_hd_adjust->arr_record_2 = 4095; // 给定最大值输出
*state = next_state;
pdctrl_out(0); // 这里要先放气5秒然后计算全开时间
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 50;
}
else
{
@ -597,13 +598,13 @@ static void pwmp_adjust_hd_bleeding_position0(uint8_t *state, mode_pwmp_hd_adjus
mode_pwmp_hd_adjust->arr_current = last - 4;
pdctrl_out(mode_pwmp_hd_adjust->arr_current);
if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->last_adc) > 4)
if (ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->last_adc) > 6)
{
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 10;
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 10;
}
else
{
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 30;
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 30;
}
mode_pwmp_hd_adjust->last_adc = mode_pwmp_hd_adjust->psb_adc;
@ -612,68 +613,69 @@ static void pwmp_adjust_hd_bleeding_position0(uint8_t *state, mode_pwmp_hd_adjus
}
}
/*启动值细调*/
static void pwmp_adjust_hd_accurate_position0(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
// 精确的找到最小推动值
uint16_t current_adc = 0;
if (!FSM_IS_WAIT(*state))
{
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_record_1;
mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
mode_pwmp_hd_adjust->adjust_state = (mode_pwmp_hd_adjust_state_e)*state;
FSM_WAIT(mode_pwmp_hd_adjust->adjust_state);
*state = PWMP_HD_ADJUST_BLEEDING;
}
else
{
valve_position_change_e s = pwmp_adjust_hd_valve_position_change(state, next_state, DIFF_ADC_MAX * 10);
// /*启动值细调*/
// static void pwmp_adjust_hd_accurate_position0(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
// {
// uint16_t current_adc = 0;
// if (!FSM_IS_WAIT(*state))
// {
// mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_record_1;
// mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
// mode_pwmp_hd_adjust->adjust_state = (mode_pwmp_hd_adjust_state_e)*state;
// FSM_WAIT(mode_pwmp_hd_adjust->adjust_state);
// *state = PWMP_HD_ADJUST_BLEEDING;
// }
// else
// {
// valve_position_change_e s = pwmp_adjust_hd_valve_position_change(state, next_state, DIFF_ADC_MAX);
current_adc = mode_pwmp_hd_adjust->psb_adc;
/*执行器发生动作*/
if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_1) >= DIFF_ADC_MAX)
{
mode_pwmp_hd_adjust->arr_record_1 = mode_pwmp_hd_adjust->arr_last;
mode_pwmp_hd_adjust->arr_record_2 = mode_pwmp_hd_adjust->arr_last + 100 < 4000 ? mode_pwmp_hd_adjust->arr_last + 100 : 100;
*state = next_state;
pdctrl_out(0); // 这里要先放气5秒然后计算全开时间
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 50;
}
else
{
switch (s)
{
case POSITION_NO_CHANGE_FOREVER: // 位置不再改变
{
if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_1) <= DIFF_ADC_MAX)
{
// 没有发生变化
mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_current + DIFF_ADC_MAX;
pdctrl_out(mode_pwmp_hd_adjust->arr_current);
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 5;
*state = PWMP_HD_ADJUST_ACCURATE_POSITION0;
FSM_WAIT(*state); // 设置等待状态
}
else
{
DBG_ASSERT(FALSE __DBG_LINE);
}
break;
}
case POSITION_CHANGE:
DBG_ASSERT(FALSE __DBG_LINE);
break;
default:
break;
}
}
}
}
// current_adc = pid_autotune_hd->data.adjust_tmp_actual;
// /*执行器发生动作*/
// if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_1) >= DIFF_ADC_MAX)
// {
// mode_pwmp_hd_adjust->arr_record_1 = mode_pwmp_hd_adjust->arr_last;
// mode_pwmp_hd_adjust->arr_record_2 = 4095; // 给定最大值输出
// *state = next_state;
// pdctrl_out(0); // 这里要先放气5秒然后计算全开时间
// mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 50;
// }
// else
// {
// switch (s)
// {
// case POSITION_NO_CHANGE_FOREVER: // 位置不再改变
// {
// /*若执行器在0位置*/
// if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_1) <= DIFF_ADC_MAX)
// {
// // 阀位没有发生变化以每6增大信号输出
// mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
// mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_current + 6;
// pdctrl_out(mode_pwmp_hd_adjust->arr_current);
// mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 30;
// *state = PWMP_HD_ADJUST_ACCURATE_POSITION0;
// FSM_WAIT(*state); // 设置等待状态
// }
// else
// {
// DBG_ASSERT(FALSE __DBG_LINE);
// }
// break;
// }
// case POSITION_CHANGE:
// DBG_ASSERT(FALSE __DBG_LINE);
// break;
// default:
// break;
// }
// }
// }
// }
/*粗略整定冲顶值*/
/*粗略整定得到满位置adc位置反馈*/
static void pwmp_adjust_hd_rough_position100(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
uint16_t current_adc = 0;
// 找最大推动值arr
if (!FSM_IS_WAIT(*state))
{
@ -685,24 +687,25 @@ static void pwmp_adjust_hd_rough_position100(uint8_t *state, mode_pwmp_hd_adjust
}
sys_millis_reset();
mode_pwmp_hd_adjust->all_open_time = sys_millis(); // 记录全开起始时间
mode_pwmp_hd_adjust->all_open_time_full = sys_millis(); // 记录全开起始时间
mode_pwmp_hd_adjust->tmp_time = 0;
pdctrl_out(mode_pwmp_hd_adjust->arr_record_2); // 以上一步求出的冲顶值进行输出
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 100;
pdctrl_out(mode_pwmp_hd_adjust->arr_record_2); // 以最大值进行输出
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 50; // 等待时间设置为5s
mode_pwmp_hd_adjust->adjust_state = (mode_pwmp_hd_adjust_state_e)*state;
FSM_WAIT(*state); // 设置等待状态
}
else
{
valve_position_change_e s = pwmp_adjust_hd_valve_position_change(state, next_state, DIFF_ADC_MAX);
current_adc = pid_autotune_hd->data.adjust_tmp_actual;
switch (s)
{
case POSITION_NO_CHANGE_FOREVER:
// 位置不再改变记录此时阀门位置AD值
mode_pwmp_hd_adjust->adc_record_2 = mode_pwmp_hd_adjust->psb_adc;
mode_pwmp_hd_adjust->all_open_time = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_open_time;
mode_pwmp_hd_adjust->adc_record_2 = current_adc;
mode_pwmp_hd_adjust->all_open_time_full = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_open_time_full;
break;
case POSITION_CHANGE:
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 50;
@ -723,13 +726,10 @@ static void pwmp_adjust_hd_rough_position100(uint8_t *state, mode_pwmp_hd_adjust
/*精调冲顶值*/
static void pwmp_adjust_hd_accurate_position100(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
// 精确找最大推动值arr
uint16_t current_adc = 0;
if (!FSM_IS_WAIT(*state))
{
mode_pwmp_hd_adjust->all_close_time_flag = TRUE;
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_record_1;
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_record_1; // 初始值为前一步整定出的最小启动值
mode_pwmp_hd_adjust->arr_last = mode_pwmp_hd_adjust->arr_current;
mode_pwmp_hd_adjust->adjust_state = (mode_pwmp_hd_adjust_state_e)*state;
mode_pwmp_hd_adjust->preheat = TRUE;
@ -739,9 +739,10 @@ static void pwmp_adjust_hd_accurate_position100(uint8_t *state, mode_pwmp_hd_adj
else
{
valve_position_change_e s = pwmp_adjust_hd_valve_position_change(state, next_state, DIFF_ADC_MAX);
mode_pwmp_hd_adjust->psb_adc = pid_autotune_hd->data.adjust_tmp_actual;
current_adc = mode_pwmp_hd_adjust->psb_adc;
uint16_t adc_diff = ABS(current_adc - mode_pwmp_hd_adjust->adc_record_2);
/*当前反馈AD值与最大位置AD值的差值*/
uint16_t adc_diff = ABS(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->adc_record_2);
if (mode_pwmp_hd_adjust->arr_current == 0 && mode_pwmp_hd_adjust->preheat == TRUE)
{
@ -775,7 +776,7 @@ static void pwmp_adjust_hd_accurate_position100(uint8_t *state, mode_pwmp_hd_adj
}
else
{
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 10;
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 20;
}
mode_pwmp_hd_adjust->last_adc = mode_pwmp_hd_adjust->psb_adc;
@ -796,7 +797,7 @@ static void pwmp_adjust_hd_accurate_position100(uint8_t *state, mode_pwmp_hd_adj
}
else
{
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 3;
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 20;
}
break;
@ -806,19 +807,100 @@ static void pwmp_adjust_hd_accurate_position100(uint8_t *state, mode_pwmp_hd_adj
}
}
/*计算控制的全关时间以ATO类型角度命名*/
static void pwmp_adjust_hd_all_close_time(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
if (!FSM_IS_WAIT(*state))
{
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_record_0 - 100; // 此处的输入信号大小根据实际控制的最小值来设定
/*记录起始的全开时间*/
sys_millis_reset();
mode_pwmp_hd_adjust->all_close_time = sys_millis();
mode_pwmp_hd_adjust->tmp_time = 0;
pdctrl_out(mode_pwmp_hd_adjust->arr_current);
FSM_WAIT(*state); // 设置等待状态
}
else
{
mode_pwmp_hd_adjust->psb_adc = get_actual_travel_adc();
if (abs(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->adc_record_0) < DIFF_ADC_MAX)
{
if (uDevice.eAirAction == ATO) // 若调节阀为气开阀
{
mode_pwmp_hd_adjust->tmp_time = sys_millis();
mode_pwmp_hd_adjust->all_close_time = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_close_time;
}
else // 若调节阀为气关阀
{
mode_pwmp_hd_adjust->tmp_time = sys_millis();
mode_pwmp_hd_adjust->all_open_time = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_close_time;
}
*state = next_state;
}
}
}
/*计算控制的全开时间*/
static void pwmp_adjust_hd_all_open_time(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
if (!FSM_IS_WAIT(*state))
{
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_record_2 + 100; // 此处的输入信号大小根据实际控制的最大值来设定
/*记录起始的全开时间*/
sys_millis_reset();
mode_pwmp_hd_adjust->all_open_time = sys_millis();
mode_pwmp_hd_adjust->tmp_time = 0;
pdctrl_out(mode_pwmp_hd_adjust->arr_current);
FSM_WAIT(*state); // 设置等待状态
}
else
{
mode_pwmp_hd_adjust->psb_adc = get_actual_travel_adc();
if (abs(mode_pwmp_hd_adjust->psb_adc - mode_pwmp_hd_adjust->adc_record_2) < DIFF_ADC_MAX)
{
if (uDevice.eAirAction == ATO)
{
mode_pwmp_hd_adjust->tmp_time = sys_millis();
mode_pwmp_hd_adjust->all_open_time = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_open_time;
}
else
{
mode_pwmp_hd_adjust->tmp_time = sys_millis();
mode_pwmp_hd_adjust->all_close_time = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_open_time;
}
*state = next_state;
}
}
}
/*计算阀门自整定参数*/
static void pwmp_adjust_hd_calculate(uint8_t *state, mode_pwmp_hd_adjust_state_e next_state)
{
if (!FSM_IS_WAIT(*state))
{
/*行程上下限*/
mode_pwmp_hd->pwmp_save->storage.trip_0 = mode_pwmp_hd_adjust->adc_record_0;
mode_pwmp_hd->pwmp_save->storage.trip_100 = mode_pwmp_hd_adjust->adc_record_2;
mode_pwmp_hd->pwmp_save->storage.ad_diff = ABS(mode_pwmp_hd->pwmp_save->storage.trip_0 - mode_pwmp_hd->pwmp_save->storage.trip_100);
mode_pwmp_hd->pwmp_save->storage.pwmp_max = mode_pwmp_hd_adjust->arr_record_2;
mode_pwmp_hd->pwmp_save->storage.pwmp_min = mode_pwmp_hd_adjust->arr_record_0;
mode_pwmp_hd->pwmp_save->storage.arr_diff = ABS(mode_pwmp_hd->pwmp_save->storage.pwmp_max - mode_pwmp_hd->pwmp_save->storage.pwmp_min);
/*控制区间*/
mode_pwmp_hd->pwmp_save->storage.pwmp_max_origin = mode_pwmp_hd_adjust->arr_record_2;
mode_pwmp_hd->pwmp_save->storage.pwmp_min_origin = mode_pwmp_hd_adjust->arr_record_0;
mode_pwmp_hd->pwmp_save->storage.arr_diff = ABS(mode_pwmp_hd_adjust->arr_record_2 - mode_pwmp_hd_adjust->arr_record_0);
mode_pwmp_hd->pwmp_save->storage.pwmp_max = mode_pwmp_hd_adjust->arr_record_2 + (mode_pwmp_hd->pwmp_save->storage.arr_diff * 0.2);
mode_pwmp_hd->pwmp_save->storage.pwmp_min = mode_pwmp_hd_adjust->arr_record_0 - (mode_pwmp_hd->pwmp_save->storage.arr_diff * 0.2);
/*启动值*/
mode_pwmp_hd->pwmp_save->storage.startup_value = mode_pwmp_hd_adjust->adc_record_1;
/*全开及全关时间*/
mode_pwmp_hd->pwmp_save->storage.all_open_time = mode_pwmp_hd_adjust->all_open_time;
mode_pwmp_hd->pwmp_save->storage.all_close_time = mode_pwmp_hd_adjust->all_close_time;
/*判断调节阀类型(气开/气关),对最低行程和最高行程进行标定*/
if (uDevice.eAirAction == ATO)
{
mode_pwmp_hd->pwmp_save->storage.trip_100 = mode_pwmp_hd->pwmp_save->storage.trip_100;
@ -859,15 +941,8 @@ static void pwmp_adjust_hd_calculate(uint8_t *state, mode_pwmp_hd_adjust_state_e
/*排气状态*/
static void pwmp_adjust_hd_bleeding(uint8_t *state)
{
uint16_t current_adc = 0;
if (!FSM_IS_WAIT(*state))
{
if (mode_pwmp_hd_adjust->all_close_time_flag == TRUE)
{
sys_millis_reset();
mode_pwmp_hd_adjust->all_close_time = sys_millis(); // 记录全关起始时间
mode_pwmp_hd_adjust->tmp_time = 0;
}
mode_pwmp_hd_adjust->arr_current = mode_pwmp_hd_adjust->arr_default;
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 80;
pdctrl_out(0);
@ -876,22 +951,17 @@ static void pwmp_adjust_hd_bleeding(uint8_t *state)
else
{
valve_position_change_e s = pwmp_adjust_hd_valve_position_change(state, mode_pwmp_hd_adjust->adjust_state, DIFF_ADC_MAX);
current_adc = mode_pwmp_hd_adjust->psb_adc;
mode_pwmp_hd_adjust->psb_adc = mode_pwmp_hd_adjust->psb_adc;
switch (s)
{
case POSITION_NO_CHANGE_FOREVER:
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 30; // 排气状态结束后在跳转后的状态中等待3s
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 50; // 排气状态结束后在跳转后的状态中等待5s
mode_pwmp_hd_adjust->arr_current = 0;
if (current_adc != mode_pwmp_hd_adjust->adc_record_1)
{
mode_pwmp_hd_adjust->adc_record_1 = current_adc;
mode_pwmp_hd->pwmp_save->storage.trip_0 = current_adc;
}
if (mode_pwmp_hd_adjust->all_close_time_flag == TRUE)
{
mode_pwmp_hd_adjust->all_close_time = mode_pwmp_hd_adjust->tmp_time - mode_pwmp_hd_adjust->all_close_time;
mode_pwmp_hd_adjust->all_close_time_flag = FALSE;
}
// if (mode_pwmp_hd_adjust->psb_adc != mode_pwmp_hd_adjust->adc_record_1)
// {
// mode_pwmp_hd_adjust->adc_record_1 = mode_pwmp_hd_adjust->psb_adc;
// mode_pwmp_hd->pwmp_save->storage.trip_0 = mode_pwmp_hd_adjust->psb_adc;
// }
break;
case POSITION_CHANGE:
if (mode_pwmp_hd_adjust->adc_record_0 == 0)
@ -905,18 +975,6 @@ static void pwmp_adjust_hd_bleeding(uint8_t *state)
mode_pwmp_hd_adjust->tmp_time = 0;
break;
case POSITION_NO_CHANGE:
if (mode_pwmp_hd_adjust->all_close_time_flag == TRUE)
{
if (mode_pwmp_hd_adjust->tmp_time == 0)
{
mode_pwmp_hd_adjust->tmp_time = sys_millis();
}
if (ABS(current_adc - mode_pwmp_hd_adjust->adc_record_0) > 50)
{
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 80;
}
}
break;
default:
*state = PWMP_HD_ADJUST_STOP;
@ -967,8 +1025,9 @@ static void pwmp_adjust_hd_PID_tuning(uint8_t *state, mode_pwmp_hd_adjust_state_
/*整定方法选择*/
pid_autotune_way_set(PID_AUTOTUNE_WAY_ZN);
/*阶跃信号设置*/
set_step_signal(FULL_TRIP_SIGNAL);
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 40; // 等待八秒
pid_autotune_hd->data.step_signal = mode_pwmp_hd_adjust->arr_record_2;
pdctrl_out(pid_autotune_hd->data.step_signal);
mode_pwmp_hd_adjust->wait_count = mode_pwmp_hd->wait_count_max * 40; // 等待四秒
FSM_WAIT(*state); // 设置等待状态
}
else
@ -987,8 +1046,6 @@ static void pwmp_adjust_hd_PID_tuning(uint8_t *state, mode_pwmp_hd_adjust_state_
{
pid_autotune_hd->data.slope = pid_autotune_hd->data.actual_error / TIME_CYCLE;
}
/*K的计算*/
pid_autotune_hd->k_hd = 1;
/*记录下变化率最大点值及行程坐标、时间*/
if (pid_autotune_hd->data.slope > pid_autotune_hd->data.slope_Max)
{
@ -1014,6 +1071,9 @@ static void pwmp_adjust_hd_PID_tuning(uint8_t *state, mode_pwmp_hd_adjust_state_
pid_autotune_hd->l_hd = get_time_tangent(0);
/*求得时间系数T*/
pid_autotune_hd->t_hd = get_time_tangent(pid_autotune_hd->data.actual_stable) - pid_autotune_hd->l_hd;
/*K的计算*/
pid_autotune_hd->k_hd = ((pid_autotune_hd->data.actual_stable) / (mode_pwmp_hd_adjust->adc_record_2 - mode_pwmp_hd_adjust->adc_record_0)) / ((pid_autotune_hd->data.step_signal) / (mode_pwmp_hd_adjust->arr_record_2 - mode_pwmp_hd_adjust->arr_record_0));
*state = next_state;
break;
case POSITION_CHANGE:
@ -1062,9 +1122,9 @@ static void pwmp_adjust_hd_pid_calculate(uint8_t *state, mode_pwmp_hd_adjust_sta
default:
break;
}
mode_pwmp_hd->pwmp_save->storage.kp = pid_autotune_hd->p_auto_hd;
mode_pwmp_hd->pwmp_save->storage.ki = pid_autotune_hd->i_auto_hd;
mode_pwmp_hd->pwmp_save->storage.kd = pid_autotune_hd->d_auto_hd;
mode_pwmp_hd->pwmp_save->storage.kp = fabs(pid_autotune_hd->p_auto_hd);
mode_pwmp_hd->pwmp_save->storage.ki = fabs(pid_autotune_hd->i_auto_hd);
mode_pwmp_hd->pwmp_save->storage.kd = fabs(pid_autotune_hd->d_auto_hd);
*state = next_state; // 将状态切换
}
@ -1103,16 +1163,22 @@ void pwmp_adjust(uint8_t *state)
pwmp_adjust_hd_rough_position0(state, PWMP_HD_ADJUST_BLEEDING_POSITION0);
break;
case PWMP_HD_ADJUST_BLEEDING_POSITION0:
pwmp_adjust_hd_bleeding_position0(state, PWMP_HD_ADJUST_ACCURATE_POSITION0);
break;
case PWMP_HD_ADJUST_ACCURATE_POSITION0:
pwmp_adjust_hd_accurate_position0(state, PWMP_HD_ADJUST_ROUGH_POSITION100);
pwmp_adjust_hd_bleeding_position0(state, PWMP_HD_ADJUST_ROUGH_POSITION100);
break;
// case PWMP_HD_ADJUST_ACCURATE_POSITION0:
// pwmp_adjust_hd_accurate_position0(state, PWMP_HD_ADJUST_ROUGH_POSITION100);
// break;
case PWMP_HD_ADJUST_ROUGH_POSITION100:
pwmp_adjust_hd_rough_position100(state, PWMP_HD_ADJUST_ACCURATE_POSITION100);
break;
case PWMP_HD_ADJUST_ACCURATE_POSITION100:
pwmp_adjust_hd_accurate_position100(state, PWMP_HD_ADJUST_CALCULATE);
pwmp_adjust_hd_accurate_position100(state, PWMP_HD_ADJUST_ALL_CLOSE_TIME);
break;
case PWMP_HD_ADJUST_ALL_CLOSE_TIME:
pwmp_adjust_hd_all_close_time(state, PWMP_HD_ADJUST_ALL_OPEN_TIME);
break;
case PWMP_HD_ADJUST_ALL_OPEN_TIME:
pwmp_adjust_hd_all_open_time(state, PWMP_HD_ADJUST_CALCULATE);
break;
case PWMP_HD_ADJUST_CALCULATE:
pwmp_adjust_hd_calculate(state, PWMP_HD_ADJUST_PID_TUNING);
@ -1130,7 +1196,7 @@ void pwmp_adjust(uint8_t *state)
pwmp_adjust_hd_bleeding(state);
break;
case PWMP_HD_ADJUST_IDEL:
pwmp_adjust_hd_idle(state, PWMP_HD_ADJUST_PID_TUNING);
pwmp_adjust_hd_idle(state, PWMP_HD_ADJUST_ROUGH_POSITION0);
break;
case PWMP_HD_ADJUST_STOP:
pwmp_adjust_hd_stop(state);
@ -1203,7 +1269,7 @@ void mode_pwmp_hd_process(void)
}
else
{
sprintf(ble_data, "%f\r\n", mode_pwmp_hd->control.ctrl_feedback);
sprintf(ble_data, "%f,%f,%f\r\n", mode_pwmp_hd->control.ctrl_feedback, mode_pwmp_hd->control.ctrl_target, mode_pwmp_hd->control.real_error);
}
ble_len = osel_mstrlen((unsigned char *)ble_data);
if (ble_len != 0)
@ -1289,25 +1355,34 @@ void mode_pwmp_hd_init(uint16_t positioner_model, mode_pwmp_hd_params_u *params,
mode_pwmp_hd->pwmp_save = params;
mode_pwmp_hd->params_save_cb = params_save_cb;
/*设定PWMP方式的工作模式*/
pwmp_hd_process_state_set(PWMP_HD_PROCESS_TEST);
pwmp_hd_process_state_set(PWMP_HD_PROCESS_CONTROL);
/*PID初始化*/
execute_pid_init(EXECUTE_PLAN);
/*计数最大值*/
mode_pwmp_hd->wait_count_max = 100 / ((MODE_DEFAULT_AUTOLOAD + 1) * 0.1); // =10每减一消耗10ms
mode_pwmp_hd->wait_count_max = 100 / ((MODE_DEFAULT_AUTOLOAD + 1) * 0.1); // =10每减一消耗10ms总消耗时间100ms
#if 0 // 手动控制接口
/*临时参数设置(后续整定)*/
mode_pwmp_hd->pwmp_save->storage.trip_0 = 2583;
mode_pwmp_hd->pwmp_save->storage.trip_100 = 1722;
mode_pwmp_hd->pwmp_save->storage.trip_0 = 2579;
mode_pwmp_hd->pwmp_save->storage.trip_100 = 1727;
#if VIP_H_EN_HD == 1
mode_pwmp_hd->pwmp_save->storage.pwmp_min = 500;
mode_pwmp_hd->pwmp_save->storage.pwmp_max = 925;
// /*大电流输出模式控制范围(fisher)*/
// mode_pwmp_hd->pwmp_save->storage.pwmp_min = 500;
// mode_pwmp_hd->pwmp_save->storage.pwmp_max = 925;
/*大电流输出模式控制范围(自制)*/
mode_pwmp_hd->pwmp_save->storage.pwmp_min = 518;
mode_pwmp_hd->pwmp_save->storage.pwmp_max = 716;
#else
/*小电流输出模式控制范围*/
mode_pwmp_hd->pwmp_save->storage.pwmp_min = 1025;
mode_pwmp_hd->pwmp_save->storage.pwmp_max = 2050;
#endif
mode_pwmp_hd->pwmp_save->storage.valve_type = ATO; // 气开阀
#if INTEGRAL_SEPARATION == 1
#if 1
mode_pwmp_hd->pwmp_save->storage.kp = 5;
// mode_pwmp_hd->pwmp_save->storage.kp = 5;
// mode_pwmp_hd->pwmp_save->storage.ki = 0.02;
// mode_pwmp_hd->pwmp_save->storage.kd = 0.005;
mode_pwmp_hd->pwmp_save->storage.kp = 0.9;
mode_pwmp_hd->pwmp_save->storage.ki = 0.02;
mode_pwmp_hd->pwmp_save->storage.kd = 0.005;
#else
@ -1320,8 +1395,9 @@ void mode_pwmp_hd_init(uint16_t positioner_model, mode_pwmp_hd_params_u *params,
mode_pwmp_hd->pwmp_save->storage.ki = 0.5619;
mode_pwmp_hd->pwmp_save->storage.kd = 0.5504;
#endif
/*阀门参数校准*/
// calib_parapos_perent();
#endif
/*保存参数*/
mode_pwmp_hd->params_save_cb();
/*更新公共参数*/

View File

@ -3,6 +3,8 @@
float32 out_pos; // 位置式pid输出
float32 Kp_watch; // 观测Kp的大小
float32 Ki_watch; // 观测Kp的大小
float32 Kd_watch; // 观测Kp的大小
/// <summary>
/// 杭电设置增量式PID参数
@ -16,6 +18,11 @@ static void _set_ctrl_prm_position(struct PID_HD *self, float32 kp, float32 ki,
pid_hd_position_t *pri = &self->pri_u.position;
osel_memset((uint8_t *)pri, 0, sizeof(pid_hd_position_t));
/*观测传进来的Kp、Ki、Kd*/
Kp_watch = kp;
Ki_watch = ki;
Kd_watch = kd;
pri->kp = kp;
pri->ki = ki;
pri->kd = kd;

View File

@ -95,6 +95,11 @@
*/
#define FL_SEM_RELEASE(sem) FLOW_SEM_RELEASE((sem))
/**
*
*/
#define FL_SEM_IS_RELEASE(fl ,sem) FLOW_SEM_IS_RELEASE((fl), (sem))
/**
*
*/

View File

@ -33,6 +33,8 @@
--(s)->count; \
} while (0)
#define FLOW_SEM_RELEASE(s) (++(s)->count) // 释放信号量s的计数值
#define FLOW_SEM_RELEASE(s) (++(s)->count)
#define FLOW_SEM_IS_RELEASE(f, s) (flow_tick - (f)->time) < ((s)->time)
#endif /* __FLOW_SEM_H__ */

View File

@ -21,7 +21,8 @@
#define __IO volatile
#endif
typedef unsigned char BOOL; /* boolean data */
typedef unsigned char BOOL; /* boolean data */
typedef unsigned char bool_t; /* boolean data */
#if !defined(__stdint_h) && !defined(_GCC_WRAP_STDINT_H)
typedef unsigned char uint8_t;

View File

@ -33,12 +33,12 @@ typedef struct
typedef struct
{
uint16_t size; // 滑动窗口大小
float32 *window; // 滑动窗口
float32 out; // 滤波结果
uint16_t index; // 滑动窗口索引
uint16_t sum;
} lpf_window_t; // 滑动窗口滤波器
uint16_t size; // 滑动窗口大小
float32 *window; // 滑动窗口
volatile float32 sum; // 滑动窗口和
volatile float32 out; // 滤波结果
uint16_t index; // 滑动窗口索引
} lpf_window_t; // 滑动窗口滤波器
void kalman_init(kalman_t *cfg);
void kalman_reset(kalman_t *cfg);

View File

@ -60,14 +60,15 @@ extern uint16_t crc16_compute(const uint8_t *const uc_ptr, uint16_t uc_len); //
extern uint8_t xor_compute(const uint8_t *const uc_ptr, uint16_t uc_len); // 异或校验
extern uint8_t get_bit_num(uint8_t bit); // 获取bit位的值
extern BOOL is_bit_set(int x, int k); // 判断x的第k位是否为1
extern uint8_t isLeap(uint16_t year); // 检查是否是闰年
extern uint8_t isLeap(uint16_t year); // 检查是否是闰年
extern uint16_t dayOfyear(uint16_t year, uint8_t month, uint8_t day); // 计算一年中的第几天
extern uint16_t weekOfyear(uint16_t year, uint8_t month, uint8_t day); // 计算一年中的第几周
extern uint8_t get_weekday(uint16_t year, uint8_t month, uint8_t day); // 获取今天星期几
extern uint8_t hex_format_dec(uint8_t hex); // 十六进制转十进制
extern uint8_t dec_format_hex(uint8_t dec); // 十进制转十六进制
extern void quicksort(uint16_t arr[], int low, int high); // 快速排序
uint32_t time2Stamp(rtc_date_t date, rtc_time_t time); // 北京时间转时间戳
extern uint32_t time2Stamp(rtc_date_t date, rtc_time_t time); // 北京时间转时间戳
extern void stamp2Time(uint32_t stamp, rtc_date_t *date, rtc_time_t *time); // 时间戳转北京时间
#endif //__LIB_H

View File

@ -15,7 +15,7 @@
// mem1内存参数设定.mem1完全处于内部SRAM里面.(设置内部SARM的内存池和内存表的参数)
#define MEM1_BLOCK_SIZE 32 // 一个内存块大小为32字节
#define MEM1_MAX_SIZE 30 * 1024 // 最大管理内存 1K (我们这个内存管理系统的内部SRAM可控制的内存大小)
#define MEM1_MAX_SIZE 20 * 1024 // 最大管理内存 1K (我们这个内存管理系统的内部SRAM可控制的内存大小)
#define MEM1_ALLOC_TABLE_SIZE MEM1_MAX_SIZE / MEM1_BLOCK_SIZE // 内存表大小(有多少块内存块)
// mem2内存参数设定.mem2的内存池处于外部SRAM里面

View File

@ -15,16 +15,28 @@
#define hal_int_state_t char
#ifdef STM32
#include "stm32l4xx.h"
#define HAL_ENTER_CRITICAL(__HANDLE__) \
do \
{ \
if ((__HANDLE__)->Lock == HAL_LOCKED) \
{ \
return HAL_BUSY; \
} \
else \
{ \
(__HANDLE__)->Lock = HAL_LOCKED; \
} \
} while (0U)
#define HAL_ENTER_CRITICAL(s) \
s = s; \
__ASM volatile("cpsid i");
#define HAL_EXIT_CRITICAL(s) \
__ASM volatile("cpsie i");
#define HAL_EXIT_CRITICAL(__HANDLE__) \
do \
{ \
(__HANDLE__)->Lock = HAL_UNLOCKED; \
} while (0U)
#else
#define HAL_ENTER_CRITICAL(s)
#define HAL_ENTER_CRITICAL(__HANDLE__)
#define HAL_EXIT_CRITICAL(s)
#define HAL_EXIT_CRITICAL(__HANDLE__)
#endif
@ -39,7 +51,6 @@
#define osel_mem_free2 _free2
#define osel_mstrlen _mstrlen
#define osel_quick_sort _quick_sort
static inline void *_malloc(uint32_t size)
{
@ -127,32 +138,4 @@ static inline unsigned int _mstrlen(const unsigned char *s)
return ss - s;
}
// 快速排序
static inline void _quick_sort(uint16_t *array, int left, int right)
{
if (left >= right)
{
return;
}
int i = left;
int j = right;
uint16_t key = array[left];
while (i < j)
{
while (i < j && array[j] > key)
{
j--;
}
array[i] = array[j];
while (i < j && array[i] <= key)
{
i++;
}
array[j] = array[i];
}
array[i] = key;
_quick_sort(array, left, i - 1);
_quick_sort(array, i + 1, right);
}
#endif // __OSEL_ARCH_H__

View File

@ -81,8 +81,8 @@ static GUI_CONST_STORAGE unsigned char ac0032[60] = {
________, ______XX, XXX_____,
________, _______X, XX______};
/* char: code:0x0034 */
static GUI_CONST_STORAGE unsigned char ac0034[60] = {
/* char: code:0x0033 */
static GUI_CONST_STORAGE unsigned char ac0033[60] = {
________, ________, ________,
________, ________, ________,
________, ________, ________,
@ -104,24 +104,90 @@ static GUI_CONST_STORAGE unsigned char ac0034[60] = {
________, ________, ________,
________, ________, ________};
static GUI_CONST_STORAGE GUI_CHARINFO Cinfo[4] = {
/* char: code:0x0034 */
static GUI_CONST_STORAGE unsigned char ac0034[60] = {
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, _______X, ________,
___X____, _X____X_, X_______,
__X_X___, X_X__X__, ________,
___X____, _X____X_, X_______,
________, _______X, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________};
/* char: code:0x0035 */
static GUI_CONST_STORAGE unsigned char ac0035[60] = {
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
___X____, ________, ________,
__X_XX__, _X_____X, ________,
_____X__, X_X___X_, X_______,
__X_XX__, _X_____X, ________,
___X____, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________};
/* char: code:0x0036 */
static GUI_CONST_STORAGE unsigned char ac0036[60] = {
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, _X______, ________,
___X____, X_X____X, ________,
__X_X__X, ___X__X_, X_______,
___X____, X_X____X, ________,
________, _X______, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________,
________, ________, ________};
static GUI_CONST_STORAGE GUI_CHARINFO Cinfo[7] = {
{19, 19, 3, (unsigned char *)&ac0030}, /*0: */
{20, 20, 3, (unsigned char *)&ac0031}, /*1: */
{20, 20, 3, (unsigned char *)&ac0032}, /*2: */
{19, 19, 3, (unsigned char *)&ac0034}, /*3: */
{19, 19, 3, (unsigned char *)&ac0033}, /*3: */
{17, 17, 3, (unsigned char *)&ac0034}, /*4: */
{17, 17, 3, (unsigned char *)&ac0035}, /*5: */
{17, 17, 3, (unsigned char *)&ac0036}, /*6: */
};
static GUI_CONST_STORAGE GUI_FONT_PROP Prop2 = {
0x0034, /*start :*/
0x0034, /*end :, len=1*/
&Cinfo[3],
(void *)0};
static GUI_CONST_STORAGE GUI_FONT_PROP Prop1 = {
0x0030, /*start :*/
0x0032, /*end :, len=3*/
0x0036, /*end :, len=7*/
&Cinfo[0],
&Prop2};
(void *)0};
GUI_CONST_STORAGE GUI_FONT GUI_FontHZ20x20 = {
GUI_FONTTYPE_PROP_SJIS,

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
/**
线使
线使
*/
#ifndef __LCDS_H__
#define __LCDS_H__

Binary file not shown.

View File

@ -93,20 +93,20 @@ void lpf_reset(lpf_t *cfg)
/**
*
*/
lpf_window_t* lpf_window_init(uint16_t size)
lpf_window_t *lpf_window_init(uint16_t size)
{
lpf_window_t* cfg = (lpf_window_t*)osel_mem_alloc(sizeof(lpf_window_t));
lpf_window_t *cfg = (lpf_window_t *)osel_mem_alloc(sizeof(lpf_window_t));
DBG_ASSERT(cfg != NULL __DBG_LINE);
osel_memset((uint8_t*)cfg, 0, sizeof(lpf_window_t));
osel_memset((uint8_t *)cfg, 0, sizeof(lpf_window_t));
cfg->size = size;
cfg->window = (float32*)osel_mem_alloc(sizeof(float32) * size);
cfg->window = (float32 *)osel_mem_alloc(sizeof(float32) * size);
DBG_ASSERT(cfg->window != NULL __DBG_LINE);
cfg->index = 0;
cfg->sum = 0;
return cfg;
}
void lpf_window_dinit(lpf_window_t* cfg)
void lpf_window_dinit(lpf_window_t *cfg)
{
if (cfg != NULL)
{
@ -119,14 +119,14 @@ void lpf_window_dinit(lpf_window_t* cfg)
}
// 滑动平均窗口重置
void lpf_window_reset(lpf_window_t* cfg)
void lpf_window_reset(lpf_window_t *cfg)
{
cfg->index = 0;
cfg->sum = 0;
// osel_memset((uint8_t *)cfg->window, 0, sizeof(float32) * cfg->size);
}
float32 lpf_window_update(lpf_window_t* cfg, float32 input)
float32 lpf_window_update(lpf_window_t *cfg, float32 input)
{
cfg->sum = 0;
// 如果窗口未满,直接添加新值到当前索引位置
@ -152,4 +152,3 @@ float32 lpf_window_update(lpf_window_t* cfg, float32 input)
cfg->out = cfg->sum / cfg->index;
return cfg->out;
}

View File

@ -327,3 +327,37 @@ void stamp2Time(uint32_t stamp, rtc_date_t *date, rtc_time_t *time)
}
}
}
/**************************排序**************************/
static void swap(uint16_t *a, uint16_t *b)
{
uint16_t t = *a;
*a = *b;
*b = t;
}
static int partition(uint16_t arr[], int low, int high)
{
uint16_t pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quicksort(uint16_t arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}

View File

@ -76,6 +76,28 @@ void adc_init(adcs_e num, ADC_TypeDef *adc, DMA_TypeDef *dma, uint32_t dma_chann
}
}
/**
* @brief ADC反初始化
* @param {adcs_e} num ADC编号
* @return {*}
* @note
*/
void adc_dinit(adcs_e num)
{
DBG_ASSERT(num < ADCS_MAX __DBG_LINE);
adcs_t *p = &adcs[num];
LL_ADC_REG_StopConversion(p->adc);
LL_DMA_DisableChannel(p->dma, p->dma_channel);
LL_ADC_Disable(p->adc);
if (p->adc_value != NULL)
{
#if defined(SRAM2_BASE)
osel_mem_free2(p->adc_value);
#else
osel_mem_free(p->adc_value);
#endif
}
}
/**
* @brief ADC转换结果,
* @param {adcs_e} num
@ -92,10 +114,6 @@ uint16_t adc_result_only_one(adcs_e num, uint8_t chan)
return gram[0][chan];
}
/**
*
*/
/**
* @brief ,ADC转换结果
* @param {adcs_e} num ADC编号
@ -107,35 +125,18 @@ uint16_t adc_result_median_average(adcs_e num, uint8_t chan)
{
DBG_ASSERT(num < ADCS_MAX __DBG_LINE);
uint16_t res = 0;
uint16_t temp = 0;
uint16_t *value = NULL;
adcs_t *p = &adcs[num];
DBG_ASSERT(p != NULL __DBG_LINE);
uint16_t adc_temp[p->adc_cct];
uint32_t adc_sum = 0;
value = (uint16_t *)osel_mem_alloc(sizeof(uint16_t) * p->adc_sum);
DBG_ASSERT(value != NULL __DBG_LINE);
osel_memcpy((uint8_t *)value, (uint8_t *)p->adc_value, sizeof(uint16_t) * p->adc_sum);
uint16_t(*gram)[p->adc_chans_count] = (uint16_t(*)[p->adc_chans_count])value;
for (uint8_t i = 0; i < p->adc_cct; i++)
{
adc_temp[i] = gram[i][chan];
adc_temp[i] = p->adc_value[i * p->adc_chans_count + chan];
}
osel_mem_free(value);
for (uint8_t i = 0; i < p->adc_cct - 1; i++)
{
for (uint8_t j = 0; j < p->adc_cct - 1 - i; j++)
{
if (adc_temp[j] > adc_temp[j + 1])
{
temp = adc_temp[j];
adc_temp[j] = adc_temp[j + 1];
adc_temp[j + 1] = temp;
}
}
}
quicksort(adc_temp, 0, p->adc_cct - 1);
for (uint8_t i = 0; i < p->adc_cct; i++) // 遍历所有ADC通道
{
adc_sum += adc_temp[i]; // 将每个ADC通道的数据累加到adc_sum中
@ -156,32 +157,14 @@ uint16_t adc_result_median(adcs_e num, uint8_t chan)
{
DBG_ASSERT(num < ADCS_MAX __DBG_LINE);
uint16_t res = 0;
uint16_t temp = 0;
uint16_t *value = NULL;
adcs_t *p = &adcs[num];
uint16_t adc_temp[p->adc_cct];
value = (uint16_t *)osel_mem_alloc(sizeof(uint16_t) * p->adc_sum);
DBG_ASSERT(p != NULL __DBG_LINE);
DBG_ASSERT(value != NULL __DBG_LINE);
osel_memcpy((uint8_t *)value, (uint8_t *)p->adc_value, sizeof(uint16_t) * p->adc_sum);
uint16_t(*gram)[p->adc_chans_count] = (uint16_t(*)[p->adc_chans_count])value;
for (uint8_t i = 0; i < p->adc_cct; i++)
{
adc_temp[i] = gram[i][chan];
}
osel_mem_free(value);
for (uint8_t i = 0; i < p->adc_cct - 1; i++)
{
for (uint8_t j = 0; j < p->adc_cct - 1 - i; j++)
{
if (adc_temp[j] > adc_temp[j + 1])
{
temp = adc_temp[j];
adc_temp[j] = adc_temp[j + 1];
adc_temp[j + 1] = temp;
}
}
adc_temp[i] = p->adc_value[i * p->adc_chans_count + chan];
}
// 使用快速排序
quicksort(adc_temp, 0, p->adc_cct - 1);
res = adc_temp[p->adc_cct / 2];
return res;
}
@ -196,20 +179,20 @@ uint16_t adc_result_median(adcs_e num, uint8_t chan)
uint16_t adc_result_average(adcs_e num, uint8_t chan)
{
DBG_ASSERT(num < ADCS_MAX __DBG_LINE);
uint16_t res = 0;
adcs_t *p = &adcs[num];
DBG_ASSERT(p != NULL __DBG_LINE);
DBG_ASSERT(p->adc_cct != 0 __DBG_LINE); // 避免除以零的错误
uint32_t adc_sum = 0;
uint16_t(*gram)[p->adc_chans_count] = (uint16_t(*)[p->adc_chans_count])p->adc_value;
for (uint8_t i = 0; i < p->adc_cct; i++)
{
adc_sum += gram[i][chan];
uint32_t next_sum = adc_sum + gram[i][chan];
DBG_ASSERT(next_sum >= adc_sum __DBG_LINE); // 避免溢出
adc_sum = next_sum;
}
res = adc_sum / p->adc_cct;
return res;
return adc_sum / p->adc_cct;
}
/**
@ -224,26 +207,18 @@ uint16_t adc_result_n_average(adcs_e num, uint8_t chan)
DBG_ASSERT(num < ADCS_MAX __DBG_LINE);
uint16_t res = 0;
uint32_t adc_sum = 0;
uint16_t *value = NULL;
adcs_t *p = &adcs[num];
DBG_ASSERT(p != NULL __DBG_LINE);
uint16_t adc_temp[p->adc_cct];
uint8_t n = p->adc_cct / 4;
uint8_t count = p->adc_cct > (2 * n) ? n : 0;
value = (uint16_t *)osel_mem_alloc(sizeof(uint16_t) * p->adc_sum);
DBG_ASSERT(value != NULL __DBG_LINE);
osel_memcpy((uint8_t *)value, (uint8_t *)p->adc_value, sizeof(uint16_t) * p->adc_sum);
uint16_t(*gram)[p->adc_chans_count] = (uint16_t(*)[p->adc_chans_count])value;
for (uint8_t i = 0; i < p->adc_cct; i++)
{
adc_temp[i] = gram[i][chan];
adc_temp[i] = p->adc_value[i * p->adc_chans_count + chan];
}
osel_mem_free(value);
for (uint8_t i = 0; i < p->adc_cct; i++)
{
adc_temp[i] = gram[i][chan];
}
osel_quick_sort(adc_temp, 0, p->adc_cct - 1);
quicksort(adc_temp, 0, p->adc_cct - 1);
for (uint8_t i = count; i < p->adc_cct - count; i++)
{

View File

@ -84,6 +84,7 @@ typedef struct
} adcs_t;
extern void adc_init(adcs_e num, ADC_TypeDef *adc, DMA_TypeDef *dma, uint32_t dma_channel, uint8_t adc_cct, uint32_t channels);
extern void adc_dinit(adcs_e num); // ADC反初始化
extern uint16_t adc_result_only_one(adcs_e num, uint8_t chan); // 获取ADC转换结果,只需要第一个值
extern uint16_t adc_result_median_average(adcs_e num, uint8_t chan); // 中位值平均滤波,获取ADC转换结果

View File

@ -10,7 +10,6 @@
#include "eeprom.h"
#include "spis.h"
#include "i2cs.h"
#include "iwdgs.h"
#define DMA_ClEAR_FLAG(DMAX, CHx, Flag) \
do \

530
User/system/bsp/flash.c Normal file
View File

@ -0,0 +1,530 @@
/**
* @file flash.c
* @author xxx
* @date 2024-02-07 11:49:34
* @brief
* @copyright Copyright (c) 2024 by xxx, All Rights Reserved.
* @attention
*
* ST LL flash
*
* 1. stm32l4xx_ll_system.h FLASH ACR寄存器的处理
*
*
* 2. Main memory
* 1 FLASH_ACR
* 2 FLASH_PDKEYR
* 3 FLASH_KEYR
* 4 FLASH_OPTKEYR
* 5 FLASH_SR
* 6 FLASH_CR
* 7 FLASH_ECCR
* 8 FLASH_OPTR
* 9 FLASH_PCROP1SR
*
* 3. Information block
* - System memory
* - OTP area
* - Option bytes
* 4. HAL
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "flash.h"
#include "stm32l4xx_ll_rcc.h"
#include "stm32l4xx_ll_system.h"
#include "stm32l4xx_ll_pwr.h"
#ifdef USE_FULL_ASSERT
#include "stm32_assert.h"
#else
#define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */
#define WHILE_MAX 10000U
/** @addtogroup STM32L4xx_LL_Driver
* @{
*/
/** @addtogroup FLASH_LL
* @{
*/
/* Private types -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private constants ---------------------------------------------------------*/
/** @addtogroup FLASH_LL_Private_Constants
* @{
*/
/**
* @}
*/
/* Private macros ------------------------------------------------------------*/
/** @addtogroup FLASH_LL_Private_Macros
* @{
*/
#define IS_LL_FLASH_WRITE_ADDR(__ADDR__) ((__ADDR__) % LL_FLASH_ALIGNMENT_MIN_SIZE == 0)
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup FLASH_LL_Private_Functions FLASH Private functions
* @{
*/
/**
* @}
*/
/* Exported functions --------------------------------------------------------*/
/** @addtogroup FLASH_LL_Exported_Functions
* @{
*/
/**
* @brief Clear All Error in SR
* @param FLASHx FLASH Instance
* @retval None
*/
void LL_FLASH_ClearAllErrorFlag(void)
{
LL_FLASH_ClearFlag_OPTVERR(FLASH);
LL_FLASH_ClearFlag_RDERR(FLASH);
LL_FLASH_ClearFlag_FASTERR(FLASH);
LL_FLASH_ClearFlag_MISERR(FLASH);
LL_FLASH_ClearFlag_PGSERR(FLASH);
LL_FLASH_ClearFlag_SIZERR(FLASH);
LL_FLASH_ClearFlag_PGAERR(FLASH);
LL_FLASH_ClearFlag_WRPERR(FLASH);
LL_FLASH_ClearFlag_PROGERR(FLASH);
LL_FLASH_ClearFlag_OPERR(FLASH);
}
/**
* @brief Flush the instruction and data caches.
* @retval None
*/
void LL_FLASH_FlushCaches(void)
{
/* Flush instruction cache */
if (LL_FLASH_IsEnabledInstructionCache(FLASH))
{
LL_FLASH_DisableInstructionCache(FLASH);
/* Reset instruction cache */
LL_FLASH_InstructionCacheReset(FLASH);
/* Enable instruction cache */
LL_FLASH_EnableInstructionCache(FLASH);
}
/* Flush data cache */
if (LL_FLASH_IsEnabledDataCache(FLASH))
{
LL_FLASH_ZCS_DisableDataCache(FLASH);
/* Reset data cache */
LL_FLASH_DataCacheReset(FLASH);
/* Enable data cache */
LL_FLASH_ZCS_EnableDataCache(FLASH);
}
}
/**
* @brief Erase Page
* @param pageno Page number
* @retval None
*/
ErrorStatus LL_FLASH_ErasePage(uint32_t pageno)
{
uint16_t count = 0;
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check and clear all error programming flags due to a previous programming. If not, PGSERR is set. */
LL_FLASH_ClearAllErrorFlag();
/* Set the PER bit and select the page you wish to erase (PNB) with the associated bank (BKER) in the Flash control register (FLASH_CR). */
LL_FLASH_EnablePageErase(FLASH);
if (pageno >= LL_FLASH_BANK1_PAGE_NUM)
{
pageno -= LL_FLASH_BANK1_PAGE_NUM;
LL_FLASH_SetErasePageBank(FLASH, LL_FLASH_BANK2);
}
else
{
LL_FLASH_SetErasePageBank(FLASH, LL_FLASH_BANK1);
}
LL_FLASH_SetErasePageNo(FLASH, pageno);
/* Set the STRT bit in the FLASH_CR register. */
LL_FLASH_EraseStart(FLASH);
/* Wait for the BSY bit to be cleared in the FLASH_SR register. */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* 完成只有需要清除擦除标志. */
LL_FLASH_DisablePageErase(FLASH);
/* Flush the caches to be sure of the data consistency */
LL_FLASH_FlushCaches();
return SUCCESS;
}
/**
* @brief Erase bank
* @param bank This parameter can be one of the following values:
* @arg @ref LL_FLASH_BANK1
* @arg @ref LL_FLASH_BANK2
* @retval None
*/
ErrorStatus LL_FLASH_EraseBank(uint32_t bank)
{
uint16_t count = 0;
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check and clear all error programming flags due to a previous programming. If not, PGSERR is set. */
LL_FLASH_ClearAllErrorFlag();
/* Set the MER1 bit or/and MER2 (depending on the bank) in the Flash control register (FLASH_CR).
Both banks can be selected in the same operation. */
if (bank == LL_FLASH_BANK1)
{
LL_FLASH_EnableBank1Erase(FLASH);
}
else
{
LL_FLASH_EnableBank2Erase(FLASH);
}
/* Set the STRT bit in the FLASH_CR register. */
LL_FLASH_EraseStart(FLASH);
/* Wait for the BSY bit to be cleared in the FLASH_SR register. */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* 完成只有需要清除擦除标志. */
LL_FLASH_DisableBank1Erase(FLASH);
LL_FLASH_DisableBank2Erase(FLASH);
/* Flush the caches to be sure of the data consistency */
LL_FLASH_FlushCaches();
return SUCCESS;
}
/**
* @brief Erase Chip
* @param None
* @retval None
*/
ErrorStatus LL_FLASH_EraseChip(void)
{
uint16_t count = 0;
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check and clear all error programming flags due to a previous programming. If not, PGSERR is set. */
LL_FLASH_ClearAllErrorFlag();
/* Set the MER1 bit or/and MER2 (depending on the bank) in the Flash control register (FLASH_CR).
Both banks can be selected in the same operation. */
LL_FLASH_EnableBank1Erase(FLASH);
LL_FLASH_EnableBank2Erase(FLASH);
/* Set the STRT bit in the FLASH_CR register. */
LL_FLASH_EraseStart(FLASH);
/* Wait for the BSY bit to be cleared in the FLASH_SR register. */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* 完成只有需要清除擦除标志. */
LL_FLASH_DisableBank1Erase(FLASH);
LL_FLASH_DisableBank2Erase(FLASH);
/* Flush the caches to be sure of the data consistency */
LL_FLASH_FlushCaches();
return SUCCESS;
}
/**
* @brief Program Double Word
* @param address specifies the address to be programmed.
* @param data specifies the data to be programmed.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: Write successfully
* - ERROR: error
*/
ErrorStatus LL_FLASH_ProgramDoubleWord(uint32_t address, uint64_t data)
{
assert_param(!IS_LL_FLASH_WRITE_ADDR(address));
uint16_t count = 0;
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check and clear all error programming flags due to a previous programming. If not, PGSERR is set. */
LL_FLASH_ClearAllErrorFlag();
/* Set the PG bit in the Flash control register (FLASH_CR). */
LL_FLASH_EnableProgram(FLASH);
/* Perform the data write operation at the desired memory address, inside main memory
block or OTP area. Only double word can be programmed. */
/* Program the double word */
*(__IO uint32_t *)address = (uint32_t)data;
*(__IO uint32_t *)(address + 4U) = (uint32_t)(data >> 32);
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check that EOP flag is set in the FLASH_SR register (meaning that the programming
operation has succeed), and clear it by software. */
if (LL_FLASH_IsActiveFlag_EOP(FLASH))
{
LL_FLASH_ClearFlag_EOP(FLASH);
}
/* Clear the PG bit in the FLASH_CR register if there no more programming request anymore. */
LL_FLASH_DisableProgram(FLASH);
/* Flush the caches to be sure of the data consistency */
LL_FLASH_FlushCaches();
return SUCCESS;
}
/**
* @brief Program
* @param address specifies the address to be programmed.
* @param data specifies the data to be programmed.
* @param num specifies the data number
* @retval An ErrorStatus enumeration value:
* - SUCCESS: Write successfully
* - ERROR: error
*/
ErrorStatus LL_FLASH_Program(uint32_t address, uint8_t data[], uint32_t num)
{
static uint64_t DataT = 0;
uint32_t T = 0, S = 0;
uint16_t count = 0;
assert_param(!IS_LL_FLASH_WRITE_ADDR(address));
if (num == 0)
{
return SUCCESS;
}
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check and clear all error programming flags due to a previous programming. If not, PGSERR is set. */
LL_FLASH_ClearAllErrorFlag();
/* Set the PG bit in the Flash control register (FLASH_CR). */
LL_FLASH_EnableProgram(FLASH);
/* Perform the data write operation at the desired memory address, inside main memory
block or OTP area. Only double word can be programmed. */
T = num;
while (num > 0)
{
DataT = 0;
if (num >= 8)
{
for (int i = 0; i < LL_FLASH_ALIGNMENT_MIN_SIZE; i++)
{
DataT = DataT << 8;
DataT |= data[S + 7 - i];
}
S += LL_FLASH_ALIGNMENT_MIN_SIZE;
num -= LL_FLASH_ALIGNMENT_MIN_SIZE;
}
else
{
for (int i = 0; i < num; i++)
{
DataT = DataT << 8;
DataT |= data[T - 1 - i];
}
num = 0;
}
/* Program the double word */
*(__IO uint32_t *)address = (uint32_t)DataT;
*(__IO uint32_t *)(address + 4U) = (uint32_t)(DataT >> 32);
/* Check that no Flash memory operation is ongoing by checking the BSY bit */
while (LL_FLASH_IsActiveFlag_BSY(FLASH))
{
if (count++ > WHILE_MAX)
{
return ERROR;
}
}
count = 0;
/* Check that EOP flag is set in the FLASH_SR register (meaning that the programming
operation has succeed), and clear it by software. */
if (LL_FLASH_IsActiveFlag_EOP(FLASH))
{
LL_FLASH_ClearFlag_EOP(FLASH);
}
address += LL_FLASH_ALIGNMENT_MIN_SIZE;
}
/* Clear the PG bit in the FLASH_CR register if there no more programming request anymore. */
LL_FLASH_DisableProgram(FLASH);
/* Flush the caches to be sure of the data consistency */
LL_FLASH_FlushCaches();
return SUCCESS;
}
/**
* @}
*/
/** @addtogroup FLASH_LL_Private_Functions
* @{
*/
/**
* @brief Fast program a row double-word (64-bit) at a specified address.
* @param address: specifies the address to be programmed.
* @param DataAddress: specifies the address where the data are stored.
* @retval None
*/
void LL_FLASH_ProgramFast(uint32_t address, uint32_t DataAddress)
{
uint8_t row_index = (2 * LL_FLASH_ROW_SIZE);
__IO uint32_t *dest_addr = (__IO uint32_t *)address;
__IO uint32_t *src_addr = (__IO uint32_t *)DataAddress;
/* Check the parameters */
assert_param(IS_FLASH_MAIN_MEM_ADDRESS(address));
/* Set FSTPG bit */
LL_FLASH_EnableFastProgram(FLASH);
/* Disable interrupts to avoid any interruption during the loop */
__disable_irq();
/* Program the double word of the row */
do
{
*dest_addr = *src_addr;
dest_addr++;
src_addr++;
row_index--;
} while (row_index != 0U);
/* Re-enable the interrupts */
__enable_irq();
LL_FLASH_DisableFastProgram(FLASH);
}
/**
* @brief Fast program a row double-word (64-bit) at a specified address.
* @param address: specifies the address to be programmed.
* @param data: specifies the data to be programmed.
* @retval None
*/
ErrorStatus LL_FLASH_Read(uint32_t address, uint8_t data[], uint32_t num)
{
if (num == 0)
{
return SUCCESS;
}
for (uint32_t i = 0; i < num; i++)
{
data[i] = *(__IO uint8_t *)(address + i);
}
return SUCCESS;
}
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

1725
User/system/bsp/flash.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -287,7 +287,7 @@ static BOOL _read_mem_dma(i2c_t *handle, uint16_t mem_address, uint16_t mem_adds
{
i2c_reset(handle->i2c); // xsh:重置I2C,修复一段时间后无法读写的问题
}
uint16_t count = 10000;
uint16_t count = 5000;
handle->txsize = 0;
handle->tx_dma_ok = FALSE;
handle->rx_dma_ok = FALSE;
@ -301,7 +301,7 @@ static BOOL _read_mem_dma(i2c_t *handle, uint16_t mem_address, uint16_t mem_adds
LL_DMA_EnableChannel(handle->dma, handle->dma_tx_channel);
LL_I2C_HandleTransfer(handle->i2c, handle->w_address, LL_I2C_ADDRSLAVE_7BIT, handle->txsize, LL_I2C_MODE_SOFTEND, LL_I2C_GENERATE_START_WRITE);
count = 10000;
count = 5000;
while (!handle->tx_dma_ok)
{
if (count-- == 0)
@ -311,7 +311,7 @@ static BOOL _read_mem_dma(i2c_t *handle, uint16_t mem_address, uint16_t mem_adds
}
}
count = 10000;
count = 5000;
while (LL_I2C_IsActiveFlag_TC(handle->i2c) != 1)
{
if (count-- == 0)

View File

@ -46,8 +46,8 @@ struct I2CS
uint16_t txsize;
uint8_t w_address; // 7位地址
uint8_t r_address; // 7位地址
volatile BOOL rx_dma_ok; // 接收DMA完成标志
volatile BOOL tx_dma_ok; // 发送DMA完成标志
__IO BOOL rx_dma_ok; // 接收DMA完成标志
__IO BOOL tx_dma_ok; // 发送DMA完成标志
i2cs_dma_callback *dma_rx_cb; // DMA接收完成回调函数
i2cs_dma_callback *dma_tx_cb; // DMA发送完成回调函数

View File

@ -25,8 +25,7 @@ BOOL check_watchdog_reset(void)
*/
void debug_freeze_watchdog(void)
{
// LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_DBGMCU);
SET_BIT(DBGMCU->APB1FZR1, DBGMCU_APB1FZR1_DBG_IWDG_STOP);
LL_DBGMCU_APB1_GRP1_FreezePeriph(LL_DBGMCU_APB1_GRP1_IWDG_STOP);
}
/**
@ -36,5 +35,5 @@ void debug_freeze_watchdog(void)
*/
void debug_unfreeze_watchdog(void)
{
CLEAR_BIT(DBGMCU->APB1FZR1, DBGMCU_APB1FZR1_DBG_IWDG_STOP);
LL_DBGMCU_APB1_GRP1_UnFreezePeriph(LL_DBGMCU_APB1_GRP1_IWDG_STOP);
}

View File

@ -1,3 +1,10 @@
/**
* @file iwdgs.h
* @author xxx
* @date 2023-12-27 14:44:03
* @brief
* @copyright Copyright (c) 2024 by xxx, All Rights Reserved.
*/
#ifndef __IWDGS_H__
#define __IWDGS_H__

View File

@ -121,8 +121,8 @@ struct SPIS
DMA_TypeDef *dma; // 外部设置
uint32_t dma_rx_channel; // 外部设置
uint32_t dma_tx_channel; // 外部设置
volatile BOOL rx_dma_ok;
volatile BOOL tx_dma_ok;
__IO BOOL rx_dma_ok;
__IO BOOL tx_dma_ok;
spis_dma_callback *dma_rx_cb; // DMA接收回调函数
spis_dma_callback *dma_tx_cb; // DMA发送回调函数
};

View File

@ -9,6 +9,12 @@ typedef void (*rx_interupt_cb_t)(uint8_t uart_index, uint8_t *data, uint16_t len
//
typedef void (*tx_complete_cb_t)(void);
typedef enum
{
UART_OK = 0x00u, /**< The action was successful. */
UART_ERROR = 0xFFu /**< Generic error. */
} uart_status_e;
//
typedef struct
{
@ -27,7 +33,7 @@ typedef struct
BOOL tx_dma_en;
uint8_t *txbuf;
uint16_t txsize;
volatile BOOL tx_dma_ok; // 发送DMA完成标志
__IO BOOL tx_dma_ok; // 发送DMA完成标志
// 接收中断回调
rx_interupt_cb_t rx_interupt_cb;

View File

@ -9,10 +9,10 @@
*/
#include "sys.h"
volatile uint32_t uwTick;
volatile uint32_t scheduler_start_time; // 调度器开始时间
volatile uint32_t scheduler_end_time; // 调度器结束时间
volatile uint32_t scheduler_occupancy_time = 0; // 调度器占用时间
__IO uint32_t uwTick;
__IO uint32_t scheduler_start_time; // 调度器开始时间
__IO uint32_t scheduler_end_time; // 调度器结束时间
__IO uint32_t scheduler_occupancy_time = 0; // 调度器占用时间
/**
* @brief
* @param baseaddr :
@ -32,7 +32,7 @@ void sys_nvic_set_vector_table(uint32_t baseaddr, uint32_t offset)
*/
void sys_wfi_set(void)
{
__ASM volatile("wfi");
__ASM __IO("wfi");
}
/**
@ -42,7 +42,7 @@ void sys_wfi_set(void)
*/
void sys_intx_disable(void)
{
__ASM volatile("cpsid i");
__ASM __IO("cpsid i");
}
/**
@ -52,7 +52,7 @@ void sys_intx_disable(void)
*/
void sys_intx_enable(void)
{
__ASM volatile("cpsie i");
__ASM __IO("cpsie i");
}
/**
@ -125,7 +125,7 @@ void sys_millis_reset(void)
*/
uint32_t sys_to_seconds(uint32_t start_time)
{
return (uwTick - start_time) / 1000;
return (sys_millis() - start_time) / 1000;
}
/**