Refactor split main.c

This commit is contained in:
Guriy
2026-07-03 18:05:11 +03:00
parent 90abf51075
commit 81106742dd
14 changed files with 1117 additions and 987 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
build

34
Inc/app_state.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef __APP_STATE_H
#define __APP_STATE_H
#include "main.h"
#include "fatfs.h" /*для типа FRESULT*/
/* HAL-хендлы (определены в main.c) */
extern ADC_HandleTypeDef hadc1;
extern ADC_HandleTypeDef hadc3;
extern SD_HandleTypeDef hsd1;
extern TIM_HandleTypeDef htim4;
extern TIM_HandleTypeDef htim8;
extern TIM_HandleTypeDef htim10;
extern TIM_HandleTypeDef htim11;
extern UART_HandleTypeDef huart8;
/* Разделяемое состояние приложения (определено в main.c, секция USER CODE PV) */
extern uint32_t TO6, TO6_before, TO6_stop, TO6_uart, SD_SEEK, SD_SLIDE, temp32,
TO7, TO7_before, TO7_PID, TO10, TO10_counter, TIM10_period;
extern uint8_t uart_buf, CPU_state, CPU_state_old, UART_transmission_request,
State_Data[2], UART_DATA[DL_8], flg_tmt, u_tx_flg, u_rx_flg, TIM10_coflag;
extern uint16_t UART_rec_incr, UART_header, CS_result, temp16,
Long_Data[DL_16], COMMAND[CL_16];
extern FRESULT fresult;
extern int test;
extern unsigned long fgoto, sizeoffile;
extern LDx_SetupTypeDef LD1_curr_setup, LD2_curr_setup, LD1_def_setup, LD2_def_setup;
extern Work_SetupTypeDef Curr_setup, Def_setup;
extern LDx_ParamTypeDef LD1_param, LD2_param;
extern LD_Blinker_StateTypeDef LD_blinker;
extern task_t task;
#endif /* __APP_STATE_H */

13
Inc/laser_hw.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef __LASER_HW_H
#define __LASER_HW_H
#include "main.h"
/* Set_LTEC() объявлена в main.h (USER CODE EFP) — оставляем там,
чтобы её видели и main.c, и it.c, и protocol.c без правок. */
uint16_t MPhD_T(uint8_t num);
uint16_t Get_ADC(uint8_t num);
void OUT_trigger(uint8_t out_n);
#endif /* __LASER_HW_H */

10
Inc/modulation.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __MODULATION_H
#define __MODULATION_H
#include "main.h"
void Stop_TIM10(void);
void Sweep_RunCurrent1(void); /* тело case TT_CHANGE_CURR_1 */
void Blink_RunCurrent2(void); /* тело case TT_CHANGE_CURR_2 */
#endif /* __MODULATION_H */

13
Inc/protocol.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef __PROTOCOL_H
#define __PROTOCOL_H
#include "main.h"
void Decode_uart(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup);
void Decode_task(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup);
uint8_t CheckChecksum(uint16_t *pbuff);
uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len);
void USART_TX(uint8_t* dt, uint16_t sz);
void USART_TX_DMA(uint16_t sz);
#endif /* __PROTOCOL_H */

10
Inc/storage.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __STORAGE_H
#define __STORAGE_H
#include "main.h"
int SD_SAVE(uint16_t *pbuff);
int SD_READ(uint16_t *pbuff);
int SD_REMOVE(void);
#endif /* __STORAGE_H */

9
Inc/temp_control.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __TEMP_CONTROL_H
#define __TEMP_CONTROL_H
#include "main.h"
uint16_t PID_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num);
uint16_t Advanced_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num);
#endif /* __TEMP_CONTROL_H */

View File

@ -73,6 +73,11 @@ Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_ll_tim.c \
Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_ll_usart.c \
Src/system_stm32f7xx.c \
Src/File_Handling.c \
Src/storage.c \
Src/protocol.c \
Src/temp_control.c \
Src/laser_hw.c \
Src/modulation.c \
Middlewares/Third_Party/FatFs/src/diskio.c \
Middlewares/Third_Party/FatFs/src/ff.c \
Middlewares/Third_Party/FatFs/src/ff_gen_drv.c \

261
Src/laser_hw.c Normal file
View File

@ -0,0 +1,261 @@
#include "laser_hw.h"
#include "app_state.h" /* hadc1, hadc3 — нужны для Get_ADC */
/* --- Set_LTEC, MPhD_T, Get_ADC, OUT_trigger из main.c --- */
void OUT_trigger(uint8_t out_n)
{
switch (out_n)
{
case 0:
HAL_GPIO_WritePin(OUT_0_GPIO_Port, OUT_0_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_0_GPIO_Port, OUT_0_Pin, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(OUT_1_GPIO_Port, OUT_1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_1_GPIO_Port, OUT_1_Pin, GPIO_PIN_RESET);
break;
case 2:
HAL_GPIO_WritePin(OUT_2_GPIO_Port, OUT_2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_2_GPIO_Port, OUT_2_Pin, GPIO_PIN_RESET);
break;
case 3:
HAL_GPIO_WritePin(OUT_3_GPIO_Port, OUT_3_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_3_GPIO_Port, OUT_3_Pin, GPIO_PIN_RESET);
break;
case 4:
HAL_GPIO_WritePin(OUT_4_GPIO_Port, OUT_4_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_4_GPIO_Port, OUT_4_Pin, GPIO_PIN_RESET);
break;
case 5:
HAL_GPIO_WritePin(OUT_5_GPIO_Port, OUT_5_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_5_GPIO_Port, OUT_5_Pin, GPIO_PIN_RESET);
break;
case 6:
HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_RESET);
break;
case 7:
HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_RESET);
break;
case 8:
HAL_GPIO_WritePin(OUT_8_GPIO_Port, OUT_8_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_8_GPIO_Port, OUT_8_Pin, GPIO_PIN_RESET);
break;
case 9:
HAL_GPIO_WritePin(OUT_9_GPIO_Port, OUT_9_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(OUT_9_GPIO_Port, OUT_9_Pin, GPIO_PIN_RESET);
break;
}
}
void Set_LTEC(uint8_t num, uint16_t DATA)
{
uint32_t tmp32;
switch (num)
{
case 1:
HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_RESET);//Start operation with LDAC1
//tmp32=0;
//while(tmp32<500){tmp32++;}
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_TXE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
LL_SPI_TransmitData16(SPI2, DATA);//Transmit word to Laser1 DAC
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_RXNE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
(void) SPI2->DR;
break;
case 2:
//HAL_GPIO_TogglePin(OUT_11_GPIO_Port, OUT_11_Pin); //for debug purposes
HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_RESET);//Start operation with LDAC2
//tmp32=0;
//while(tmp32<500){tmp32++;}
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_TXE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
LL_SPI_TransmitData16(SPI6, DATA);//Transmit word to Laser1 DAC
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_RXNE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
(void) SPI6->DR;
break;
case 3:
HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_RESET);//Start operation with TECDAC1
//tmp32=0;
//while(tmp32<500){tmp32++;}
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_TXE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
LL_SPI_TransmitData16(SPI2, DATA);//Transmit word to Laser1 DAC
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_RXNE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
(void) SPI2->DR;
break;
case 4:
HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_RESET);//Start operation with TECDAC2
//tmp32=0;
//while(tmp32<500){tmp32++;}
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_TXE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
LL_SPI_TransmitData16(SPI6, DATA);//Transmit word to Laser1 DAC
tmp32 = 0;
while((!LL_SPI_IsActiveFlag_RXNE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end.
(void) SPI6->DR;
break;
}
HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_SET);//End operation with LDAC1
HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_SET);//End operation with LDAC2
HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_SET);//End operation with TEC1
HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_SET);//End operation with TEC2
}
uint16_t MPhD_T(uint8_t num)
{
uint16_t P;
uint32_t tmp32;
HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_RESET);//Prepare conversion
HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_RESET);//Prepare conversion
tmp32=0;
while(tmp32<500){tmp32++;}
HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_SET);//Stop acqusition & start conversion
HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_SET);//Stop acqusition & start conversion
tmp32=0;
while(tmp32<500){tmp32++;}
if (num==1)//MPD1
{
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_RESET);
tmp32=0;
while(tmp32<500){tmp32++;}
//LL_SPI_TransmitData16(SPI4, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data...
LL_SPI_Enable(SPI4);//Enable SPI for MPhD1 ADC
tmp32 = 0;
while(((!LL_SPI_IsActiveFlag_RXNE(SPI4))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end.
LL_SPI_Disable(SPI4);//Enable SPI for MPhD1 ADC
while(tmp32<500){tmp32++;}
//HAL_SPI_Receive(&hspi4, &P[0], 1, 100);
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET);
P = LL_SPI_ReceiveData16(SPI4);
}
else if (num==2)//MPD2
{
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_RESET);
tmp32=0;
while(tmp32<500){tmp32++;}
//LL_SPI_TransmitData16(SPI5, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data...
LL_SPI_Enable(SPI5);//Enable SPI for MPhD2 ADC
tmp32 = 0;
while(((!LL_SPI_IsActiveFlag_RXNE(SPI5))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end.
LL_SPI_Disable(SPI5);//Enable SPI for MPhD2 ADC
while(tmp32<500){tmp32++;}
//HAL_SPI_Receive(&hspi4, &P[0], 1, 100);
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET);
P = LL_SPI_ReceiveData16(SPI5);
}
else if (num==3)//ThrLD1
{
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_RESET);
tmp32=0;
while(tmp32<500){tmp32++;}
//LL_SPI_TransmitData16(SPI4, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data...
LL_SPI_Enable(SPI4);//Enable SPI for ThrLD1 ADC
tmp32 = 0;
while(((!LL_SPI_IsActiveFlag_RXNE(SPI4))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end.
LL_SPI_Disable(SPI4);//Enable SPI for ThrLD1 ADC
while(tmp32<500){tmp32++;}
//HAL_SPI_Receive(&hspi4, &P[0], 1, 100);
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET);
P = LL_SPI_ReceiveData16(SPI4);
}
else if (num==4)//ThrLD2
{
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_RESET);
tmp32=0;
while(tmp32<500){tmp32++;}
//LL_SPI_TransmitData16(SPI5, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data...
LL_SPI_Enable(SPI5);//Enable SPI for ThrLD2 ADC
tmp32 = 0;
while(((!LL_SPI_IsActiveFlag_RXNE(SPI5))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end.
LL_SPI_Disable(SPI5);//Enable SPI for ThrLD2 ADC
while(tmp32<500){tmp32++;}
//HAL_SPI_Receive(&hspi4, &P[0], 1, 100);
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET);
P = LL_SPI_ReceiveData16(SPI5);
}
/*float I_LD, Ith, I0m, T0m, Inorm, Tnorm1, Tnorm2, P, T_C, A, Pnorm;
Inorm = (float) (65535) / (float) (100);
Tnorm1 = (float) (65535) / (float) (50);
Tnorm2 = 4;
Pnorm = (float)(65535) / (float)(20);
I0m = 8.1568;//@4 C - lowest temperature of system
T0m = 48.6282;
T_C = (float) (T_LD) / Tnorm1 + Tnorm2;
Ith = I0m * expf(T_C/T0m);
I_LD = (float) (C_LD) / Inorm;
if (I_LD > Ith)
{
A = (float) (2.24276128270098e-07) * T_C * T_C * T_C - (float) (4.73392579025590e-05) * T_C * T_C + (float) (0.00157250618257057) * T_C + (float) (0.228565407377466);
P = A * (I_LD - Ith) * Pnorm;
}
else
{
P = 0;
} */
return P;
}
/*static uint16_t Temp_LD(uint16_t T_LD_before, uint16_t T_LD, uint32_t Timer_before, uint32_t Timer)
{
uint16_t Result;
// uint8_t randf;
randf = 0;
for (uint8_t i = 0; i < 32; i++)
{
randf = ((Timer>>i)&0x0001)^randf;
}
Result = ((float)(T_LD - T_LD_before))*((float)(1-expf(((float)(Timer_before)-(float)(Timer))/((float)(100))))) + T_LD_before + (float)(randf);
return (uint16_t)(Result);
}*/
uint16_t Get_ADC(uint8_t num)
{
uint16_t OUT;
switch (num)
{
case 0:
HAL_ADC_Start(&hadc1); // Power on
break;
case 1:
HAL_ADC_PollForConversion(&hadc1, 100); // Waiting for conversion
OUT = HAL_ADC_GetValue(&hadc1); // Get value adc
break;
case 2:
HAL_ADC_Stop(&hadc1); // Power off
break;
case 3:
HAL_ADC_Start(&hadc3); // Power on
break;
case 4:
HAL_ADC_PollForConversion(&hadc3, 100); // Waiting for conversion
OUT = HAL_ADC_GetValue(&hadc3); // Get value adc
break;
case 5:
HAL_ADC_Stop(&hadc3); // Power off
break;
}
return OUT;
}

File diff suppressed because it is too large Load Diff

298
Src/modulation.c Normal file
View File

@ -0,0 +1,298 @@
#include "modulation.h"
#include "app_state.h" /* task, TIM10_coflag, TO10, TIM10_period, TO10_counter, LD_blinker,
LD1_param, LD2_param, LD1_curr_setup, LD2_curr_setup, temp16,
htim4, htim8, htim10, htim11 */
#include "laser_hw.h" /* MPhD_T */
#include "temp_control.h" /* PID_Controller_Temp */
/* Set_LTEC() приходит из main.h */
/* --- Stop_TIM10 --- */
void Stop_TIM10(void)
{
HAL_TIM_Base_Stop_IT(&htim10);
TIM10_coflag = 0;
TO10 = 0;
}
void Sweep_RunCurrent1(void)
{
HAL_StatusTypeDef st;
/* --- тело case TT_CHANGE_CURR_1 --- */
//calculating timer periods for ADC clock and Mach-Zander modulator
//ADC clock
//TIM4 -> ARR = 60; // for 1.5 MHz
//TIM4 -> ARR = 91; // for 1 MHz
//TIM4 -> ARR = 45; // for 2 MHz
//online calculation for debug purposes:
//manually varying TIM4 -> ARR by debugger while running
//TIM4 -> CCR3 = (TIM4 -> ARR +1)/2 - 1;
//Mach-Zander clock (should be half of ADC clock freq)
//TIM11 -> ARR = (TIM4 -> ARR +1)*2 - 1;
//TIM11 -> CCR1 = (TIM11 -> ARR +1)/2 - 1;
//for stm_ADC
//TIM4 -- ADC clock. Rising edge -- for SYNC_DETector_ON
//TIM4 -- ADC clock. Falling edge -- for SYNC_DETector_OFF
//TIM4 freq is equal to TIM11
//TIM4 phase should be adjustable to get correct sampling moment
Set_LTEC(TT_CHANGE_CURR_2, task.curr);
(void) MPhD_T(TT_CHANGE_TEMP_1);
LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1);
(void) MPhD_T(TT_CHANGE_TEMP_2);
LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2);
temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1);
Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1
temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2);
Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2
// Toggle pin for oscilloscope
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); //start of the whole frequency sweep procedure (start trigger pulse)
//for (uint32_t i = 100000; i; --i){;}
st = HAL_TIM_Base_Start_IT(&htim10);
if (st != HAL_OK)
while(1);
uint16_t step_counter = 0;
uint16_t trigger_counter = 3;
//uint16_t trigger_step = (uint8_t )((task.max_param - task.current_param)/task.delta_param * 10);
uint16_t trigger_step = 50;
uint16_t task_sheduler = 0;
HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator
HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); //start ADC clock
TIM11 -> CR1 &= ~(1 << 3); //disables one-pulse mode
TIM4 -> CR1 &= ~(1 << 3); //disables one-pulse mode
TIM11 -> CNT = 0;
TIM4 -> CNT = 0;
HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); //start ADC clock
//TIM4 -> CNT = 0;
TIM4 -> CNT = TIM4 -> ARR - 20; // not zero to make phase shift that will be robust to big delay in RF subsystem (up to ~400 ns)
TIM11 -> CNT = 0;
uint32_t curr_step_N = 0;
uint32_t postAmpl_1_en_curr_step_N = ((task.max_param - task.current_param)/3.0 * 1.0) / task.delta_param;
uint32_t postAmpl_2_en_curr_step_N = ((task.max_param - task.current_param)/3.0 * 2.0) / task.delta_param;
while (task.current_param < task.max_param)
{
if (TIM10_coflag)
{
Set_LTEC(TT_CHANGE_CURR_1, task.current_param);
HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_9); //toggle PG9. Change indicates that current step started.
//TIM11 -> CNT = 0; // to link modulator phase
//TIM4 -> CNT = 0; // to link ADC clock phase
task.current_param += task.delta_param;
TO10 = 0;
TIM10_coflag = 0;
if (curr_step_N == postAmpl_1_en_curr_step_N){
HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_SET);
}
if (curr_step_N == postAmpl_2_en_curr_step_N){
HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_SET);;
}
//HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_SET); // set the current step laser current trigger
//HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET);
//*
if (step_counter % trigger_step == 0){ //trigger at every 60 step
//OUT_trigger(trigger_counter);
++trigger_counter;
}
++step_counter;
//HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); //stop trigger pulse
//*/
/*
++task_sheduler;
if (task_sheduler >= 10){
task_sheduler = 0;
}
//maintain stable temperature of laser 2
if (task_sheduler == 0){
(void) MPhD_T(TT_CHANGE_TEMP_2);
LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2);
temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2);
Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2
}
//maintain stable temperature of laser 1
//*
if (task_sheduler == 5){
(void) MPhD_T(TT_CHANGE_TEMP_1);
LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1);
temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1);
Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1
}
//*/
}
}
TIM11 -> DIER |= 1; //enable update interrupt. In this IRQ handler we will set both tims to one-pulse mode.
//TIM11 -> CR1 |= 1 << 3; //sets timer to one-pulse mode. So it will turn off at the next UpdateEvent
//TIM4 -> CR1 |= 1 << 3; //sets timer to one-pulse mode. So it will turn off at the next UpdateEvent
//but one-pulse mode should be disabled
//HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator
//HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); //start ADC clock
Stop_TIM10();
HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); //stop trigger pulse
task.current_param = task.min_param;
Set_LTEC(TT_CHANGE_CURR_1, task.current_param);
if (task.tau > 3)
{
TIM10_period = htim10.Init.Period;
htim10.Init.Period = 9999;
TO10_counter = (task.tau - 1) * 100;
}
HAL_TIM_Base_Start_IT(&htim10);
}
void Blink_RunCurrent2(void)
{
HAL_StatusTypeDef st;
/* --- тело case TT_CHANGE_CURR_2 (без break;) --- */
//Blink laser 2
//*
Set_LTEC(TT_CHANGE_CURR_1, task.curr);
(void) MPhD_T(TT_CHANGE_TEMP_1);
LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1);
(void) MPhD_T(TT_CHANGE_TEMP_2);
LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2);
temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1);
Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1
temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2);
Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2
LD_blinker.task_type = 2;
LD_blinker.state = 0; // 0 -- disabled (do nothing); 1 -- update LD current; 2 -- blinking, LD ON now; 3 -- blinking, LD OFF now
//LD_blinker.param = task.current_param;
LD_blinker.param = 0;
LD_blinker.param = 1000; // LD2 current (in unspecified units)
LD_blinker.signal_port = OUT_9_GPIO_Port;
LD_blinker.signal_pin = OUT_9_Pin;
TIM8->ARR = 10000; //zero to LD_blinker.param change frequency (also in unspecified units).
//When it is too low -- Desktop app crashes (there is not so much compute sources on MCU to anwser to desktop`s questions)
st = HAL_TIM_Base_Start_IT(&htim8);
if (st != HAL_OK)
while(1);
// */
// Toggle pin for oscilloscope
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET);
uint32_t i = 10000; while (--i){}
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET);
LD_blinker.state = 2;
st = HAL_TIM_Base_Start_IT(&htim10);
if (st != HAL_OK)
while(1);
while (task.current_param < task.max_param)
{
if (TIM10_coflag)
{
//Set_LTEC(TT_CHANGE_CURR_2, task.current_param);
//LD_blinker.param = task.current_param;
//++LD_blinker.param;
task.current_param += task.delta_param;
TO10 = 0;
TIM10_coflag = 0;
}
}
HAL_TIM_Base_Stop(&htim10);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET);
HAL_TIM_Base_Stop_IT(&htim8);
TIM8->CNT = 0;
Stop_TIM10();
task.current_param = task.min_param;
Set_LTEC(TT_CHANGE_CURR_2, task.current_param);
if (task.tau > 3)
{
TIM10_period = htim10.Init.Period;
htim10.Init.Period = 9999;
TO10_counter = (task.tau - 1) * 100;
}
HAL_TIM_Base_Start_IT(&htim10);
//*/
/* // Backup
Set_LTEC(TT_CHANGE_CURR_1, task.curr);
(void) MPhD_T(TT_CHANGE_TEMP_1);
LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1);
(void) MPhD_T(TT_CHANGE_TEMP_2);
LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2);
temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1);
Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1
temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2);
Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2
// Toggle pin for oscilloscope
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET);
st = HAL_TIM_Base_Start_IT(&htim10);
if (st != HAL_OK)
while(1);
while (task.current_param < task.max_param)
{
if (TIM10_coflag)
{
Set_LTEC(TT_CHANGE_CURR_2, task.current_param);
task.current_param += task.delta_param;
TO10 = 0;
TIM10_coflag = 0;
}
}
Stop_TIM10();
task.current_param = task.min_param;
Set_LTEC(TT_CHANGE_CURR_2, task.current_param);
if (task.tau > 3)
{
TIM10_period = htim10.Init.Period;
htim10.Init.Period = 9999;
TO10_counter = (task.tau - 1) * 100;
}
HAL_TIM_Base_Start_IT(&htim10);
*/
}

256
Src/protocol.c Normal file
View File

@ -0,0 +1,256 @@
#include "protocol.h"
#include "app_state.h" /* test, Long_Data, COMMAND, CS_result, UART_header, u_tx_flg, task, TO10_counter */
#include "File_Handling.h" /* Mount_SD, Create_File, Write_File_byte, Update_File_byte, Remove_File, Unmount_SD */
/* --- 6 функций из main.c --- */
void Decode_uart(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup)
{
// uint8_t *temp1;
uint16_t *temp2;
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
test=0;
if ((HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin) == GPIO_PIN_RESET)&&
(HAL_GPIO_ReadPin(USB_FLAG_GPIO_Port, USB_FLAG_Pin) == GPIO_PIN_SET))//if exist sd && connect usb write comand to c
{
test = Mount_SD("/");
if (test == 0) //0 - suc
{
//Format_SD();
test = Remove_File ("COMMAND.TXT");
test = Create_File("COMMAND.TXT"); // 0 -succ
test = Write_File_byte("COMMAND.TXT", (uint8_t *)Command, CL_8);
test = Update_File_byte("COMMAND.TXT", (uint8_t *)Command, CL_8);
test = Unmount_SD("/"); // 0 - succ
}
}
temp2 = (uint16_t *)Command;
Curr_setup->WORK_EN = ((uint8_t)((*temp2)>>0))&0x01;
Curr_setup->U5V1_EN = ((uint8_t)((*temp2)>>1))&0x01;
Curr_setup->U5V2_EN = ((uint8_t)((*temp2)>>2))&0x01;
Curr_setup->LD1_EN = ((uint8_t)((*temp2)>>3))&0x01;
Curr_setup->LD2_EN = ((uint8_t)((*temp2)>>4))&0x01;
Curr_setup->REF1_EN = ((uint8_t)((*temp2)>>5))&0x01;
Curr_setup->REF2_EN = ((uint8_t)((*temp2)>>6))&0x01;
Curr_setup->TEC1_EN = ((uint8_t)((*temp2)>>7))&0x01;
Curr_setup->TEC2_EN = ((uint8_t)((*temp2)>>8))&0x01;
Curr_setup->TS1_EN = ((uint8_t)((*temp2)>>9))&0x01;
Curr_setup->TS2_EN = ((uint8_t)((*temp2)>>10))&0x01;
Curr_setup->SD_EN = ((uint8_t)((*temp2)>>11))&0x01;
Curr_setup->PI1_RD = ((uint8_t)((*temp2)>>12))&0x01;
Curr_setup->PI2_RD = ((uint8_t)((*temp2)>>13))&0x01;
temp2++;
LD1_curr_setup->LD_TEMP = (uint16_t)(*temp2);
temp2++;
LD2_curr_setup->LD_TEMP = (uint16_t)(*temp2);
temp2++;
temp2++;
temp2++;
Curr_setup->AVERAGES = (uint16_t)(*temp2);
temp2++;
LD1_curr_setup->P_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10)));
temp2++;
LD1_curr_setup->I_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10)));
temp2++;
LD2_curr_setup->P_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10)));
temp2++;
LD2_curr_setup->I_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10)));
temp2++;
Long_Data[13] = (uint16_t)(*temp2);//Message ID
temp2++;
LD1_curr_setup->CURRENT = (uint16_t)(*temp2);
temp2++;
LD2_curr_setup->CURRENT = (uint16_t)(*temp2);
temp2++;
if (Curr_setup->U5V1_EN)
{
HAL_GPIO_WritePin(EN_5V1_GPIO_Port, EN_5V1_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(EN_5V1_GPIO_Port, EN_5V1_Pin, GPIO_PIN_RESET);
}
if (Curr_setup->U5V2_EN)
{
HAL_GPIO_WritePin(EN_5V2_GPIO_Port, EN_5V2_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(EN_5V2_GPIO_Port, EN_5V2_Pin, GPIO_PIN_RESET);
}
if (Curr_setup->LD1_EN)
{
HAL_GPIO_WritePin(LD1_EN_GPIO_Port, LD1_EN_Pin, GPIO_PIN_SET);
//LL_SPI_Enable(SPI2);//Enable SPI for Laser1 DAC
}
else
{
HAL_GPIO_WritePin(LD1_EN_GPIO_Port, LD1_EN_Pin, GPIO_PIN_RESET);
//LL_SPI_Disable(SPI2);//Disable SPI for Laser1 DAC
}
if (Curr_setup->LD2_EN)
{
HAL_GPIO_WritePin(LD2_EN_GPIO_Port, LD2_EN_Pin, GPIO_PIN_SET);
//LL_SPI_Enable(SPI6);//Enable SPI for Laser2 DAC
}
else
{
HAL_GPIO_WritePin(LD2_EN_GPIO_Port, LD2_EN_Pin, GPIO_PIN_RESET);
//LL_SPI_Disable(SPI6);//Disable SPI for Laser2 DAC
}
if (Curr_setup->REF1_EN)
{
HAL_GPIO_WritePin(REF0_EN_GPIO_Port, REF0_EN_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(REF0_EN_GPIO_Port, REF0_EN_Pin, GPIO_PIN_RESET);
}
if (Curr_setup->REF2_EN)
{
HAL_GPIO_WritePin(REF2_ON_GPIO_Port, REF2_ON_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(REF2_ON_GPIO_Port, REF2_ON_Pin, GPIO_PIN_RESET);
}
if ((Curr_setup->TS1_EN)&&(Curr_setup->TEC1_EN))
{
Set_LTEC(3,32767);
Set_LTEC(3,32767);
HAL_GPIO_WritePin(TEC1_PD_GPIO_Port, TEC1_PD_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(TECEN1_GPIO_Port, TECEN1_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(TECEN1_GPIO_Port, TECEN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(TEC1_PD_GPIO_Port, TEC1_PD_Pin, GPIO_PIN_RESET);
}
if ((Curr_setup->TS2_EN)&&(Curr_setup->TEC2_EN))
{
Set_LTEC(4,32767);
Set_LTEC(4,32767);
HAL_GPIO_WritePin(TEC2_PD_GPIO_Port, TEC2_PD_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(TECEN2_GPIO_Port, TECEN2_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(TECEN2_GPIO_Port, TECEN2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(TEC2_PD_GPIO_Port, TEC2_PD_Pin, GPIO_PIN_RESET);
}
if (Curr_setup->PI1_RD==0)
{
LD1_curr_setup->P_coef_temp = 10;
LD1_curr_setup->I_coef_temp = 0.01;
}
if (Curr_setup->PI2_RD==0)
{
LD2_curr_setup->P_coef_temp = 10;
LD2_curr_setup->I_coef_temp = 0.01;
}
}
void Decode_task(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup)
{
uint16_t *temp2;
temp2 = (uint16_t *)Command;
Curr_setup->WORK_EN = ((uint8_t)((*temp2)>>0))&0x01;
Curr_setup->U5V1_EN = ((uint8_t)((*temp2)>>1))&0x01;
Curr_setup->U5V2_EN = ((uint8_t)((*temp2)>>2))&0x01;
Curr_setup->LD1_EN = ((uint8_t)((*temp2)>>3))&0x01;
Curr_setup->LD2_EN = ((uint8_t)((*temp2)>>4))&0x01;
Curr_setup->REF1_EN = ((uint8_t)((*temp2)>>5))&0x01;
Curr_setup->REF2_EN = ((uint8_t)((*temp2)>>6))&0x01;
Curr_setup->TEC1_EN = ((uint8_t)((*temp2)>>7))&0x01;
Curr_setup->TEC2_EN = ((uint8_t)((*temp2)>>8))&0x01;
Curr_setup->TS1_EN = ((uint8_t)((*temp2)>>9))&0x01;
Curr_setup->TS2_EN = ((uint8_t)((*temp2)>>10))&0x01;
Curr_setup->SD_EN = ((uint8_t)((*temp2)>>11))&0x01;
Curr_setup->PI1_RD = ((uint8_t)((*temp2)>>12))&0x01;
Curr_setup->PI2_RD = ((uint8_t)((*temp2)>>13))&0x01;
temp2++;
task.task_type = (uint8_t)(*temp2); temp2++;
task.min_param = (float)(*temp2); temp2++;
task.max_param = (float)(*temp2); temp2++;
task.delta_param = (float)(*temp2); temp2++;
task.dt = (float)(*temp2) / 100.0; temp2++;
task.sec_param = (float)(*temp2); temp2++;
task.curr = (float)(*temp2); temp2++;
task.temp = (float)(*temp2); temp2++;
task.tau = (float)(*temp2); temp2++;
task.p_coef_1 = (float)(*temp2) * 256.0; temp2++;
task.i_coef_1 = (float)(*temp2) * 256.0; temp2++;
task.p_coef_2 = (float)(*temp2) * 256.0; temp2++;
task.i_coef_2 = (float)(*temp2) * 256.0; temp2++;
TO10_counter = task.dt / 10;
}
uint8_t CheckChecksum(uint16_t *pbuff)
{
uint16_t cl_ind;
switch (UART_header)
{
case 0x7777:
cl_ind = TSK_16 - 2;
break;
case 0x1111:
cl_ind = CL_16 - 2;
break;
default:
return 0;
break;
}
CS_result = CalculateChecksum(pbuff, cl_ind);
return ((CS_result == COMMAND[cl_ind]) ? 1 : 0);
}
uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len)
{
short i;
uint16_t cs = *pbuff;
for(i = 1; i < len; i++)
{
cs ^= *(pbuff+i);
}
return cs;
}
void USART_TX (uint8_t* dt, uint16_t sz)
{
uint16_t ind = 0;
while (ind<sz)
{
while (!LL_USART_IsActiveFlag_TXE(USART1)) {}
LL_USART_TransmitData8(USART1,*(uint8_t*)(dt+ind));
ind++;
}
}
void USART_TX_DMA (uint16_t sz)
{
while (u_tx_flg) {}//Wait until previous transfer not complete. u_tx_flg is resetting in DMA interupt function.
LL_DMA_DisableStream(DMA2, LL_DMA_STREAM_7);
LL_DMA_SetDataLength(DMA2, LL_DMA_STREAM_7, sz);
LL_DMA_EnableStream(DMA2, LL_DMA_STREAM_7);
u_tx_flg = 1;//indicate that transfer begin
}

99
Src/storage.c Normal file
View File

@ -0,0 +1,99 @@
#include "storage.h"
#include "app_state.h" /* fgoto */
#include "File_Handling.h" /* Mount_SD, Update_File_byte, Seek_Read_File, Remove_File, Create_File, FR_OK */
/* --- SD_SAVE / SD_READ / SD_REMOVE из main.c (дословно) --- */
int SD_SAVE(uint16_t *pbuff)
{
int test=0;
if (HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin)==GPIO_PIN_RESET)
{
test = Mount_SD("/");
if (test == 0) //0 - suc
{
//Format_SD();
test = Update_File_byte("FILE1.TXT", (uint8_t *)pbuff, DL_8);
test = Unmount_SD("/"); // 0 - succ
return test;
}
else
{
return 1;
}
}
else
{
return 1;
}
}
//uint32_t Get_Length(void)
//{
// return SD_matr[0][0] + ((uint32_t) (SD_matr[0][1])<<16);
//}
int SD_READ(uint16_t *pbuff)
{
int test=0;
if (HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin)==GPIO_PIN_RESET)
{
test = Mount_SD("/");
if (test == 0) //0 - suc
{
//Format_SD();
test = Seek_Read_File ("FILE1.TXT", (uint8_t *)pbuff, DL_8, fgoto);//Read next 246 bytes
fgoto+=DL_8;
test = Unmount_SD("/"); // 0 - succ
return test;
}
else
{
return 1;
}
}
else
{
return 1;
}
/* for (uint16_t j = 0; j < DL_16; j++)
{
*(pbuff+j) = SD_matr[SD_SLIDE][j];
}
if (SD_SLIDE<Get_Length())
{
SD_SLIDE++;
return 0x0000;
}
else
{
return 0x0001;
}*/
}
int SD_REMOVE(void)
{
int test=0;
if (HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin)==GPIO_PIN_RESET)
{
test = Mount_SD("/");
if (test==FR_OK)
{
test = Remove_File ("FILE1.TXT");
test = Create_File("FILE1.TXT"); // 0 -suc
//test = Write_File ("FILE1.TXT", "____OSGG main borad information. Program made by Kazakov Viktor. Part ?01 (for DFB-1550-14BF lasers). Parameters of plate: Ilaser: 0...66.7 mA; Vlaser: 0...2 V; Itec: -1.27...1.27 A; Vtec: -2.56...2.56 V; IMphD: 0...519 uA; Tint: -1.2...+45.8 C; Text: -25.8...+43.4 C. Place for your advertising:..................................................................................................................................");
test = Unmount_SD("/"); // 0 - succ
return test;
}
else
{
return 1;
}
}
else
{
return 1;
}
}

100
Src/temp_control.c Normal file
View File

@ -0,0 +1,100 @@
#include "temp_control.h"
#include "app_state.h" /* TO7, TO7_PID */
/* --- PID_Controller_Temp и Advanced_Controller_Temp из main.c --- */
uint16_t Advanced_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num)
{
// Main idea:
// I is responsible to maintaining constant temperature difference between laser and room temperature.
// As room temperature can be approximated as constant at current-varying time -- I should be kept constant too.
// As current through laser diode heats it -- we can estimate excessive power on laser diode and trim peltier current according to it.
// So, equation should be look like this:
// x_output = x_output_original + I(laser)*(a + (t - b)c)
// t -- cycle phase
// a,b,c -- constants
//
// How can we control laser diode temperature?
// -- We can set laser to fixed current at the time we need to measure.
// Then we should measure wavelength.
// Calibration sequence:
// 1) n
int e_pid;
float P_coef_current;//, I_coef_current;
float e_integral;
int x_output;
e_pid = (int) LDx_results->LD_CURR_TEMP - (int) LDx_curr_setup->LD_TEMP;
e_integral = LDx_results->e_integral;
if((e_pid < 3000) && (e_pid > - 3000)){
e_integral += LDx_curr_setup->I_coef_temp * (float)(e_pid) * (float)(TO7 - TO7_PID) / (float) 100;//100 - timer is too fast
}
P_coef_current = LDx_curr_setup->P_coef_temp;
if (e_integral > 32000){
e_integral = 32000;
}
else if (e_integral < - 32000){
e_integral = -32000;
}
LDx_results->e_integral = e_integral;
x_output = 32768 + P_coef_current * e_pid + (int)e_integral;//32768 - P_coef_current * e_pid - (int)e_integral;//
if(x_output < 1000){
x_output = 8800;
}
else if(x_output > 56800){
x_output = 56800;
}
if (num==2)
TO7_PID = TO7;//Save current time only on 2nd laser
return (uint16_t)x_output;
}
uint16_t PID_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num)
{
int e_pid;
float P_coef_current;//, I_coef_current;
float e_integral;
int x_output;
e_pid = (int) LDx_results->LD_CURR_TEMP - (int) LDx_curr_setup->LD_TEMP;
e_integral = LDx_results->e_integral;
if((e_pid < 3000) && (e_pid > - 3000)){
e_integral += LDx_curr_setup->I_coef_temp * (float)(e_pid) * (float)(TO7 - TO7_PID) / (float) 100;//100 - timer is too fast
}
P_coef_current = LDx_curr_setup->P_coef_temp;
if (e_integral > 32000){
e_integral = 32000;
}
else if (e_integral < - 32000){
e_integral = -32000;
}
LDx_results->e_integral = e_integral;
x_output = 32768 + P_coef_current * e_pid + (int)e_integral;//32768 - P_coef_current * e_pid - (int)e_integral;//
if(x_output < 1000){
x_output = 8800;
}
else if(x_output > 56800){
x_output = 56800;
}
if (num==2)
TO7_PID = TO7;//Save current time only on 2nd laser
return (uint16_t)x_output;
}