#ifndef PID_CONTROLLER_EMBEDDED_H
#define PID_CONTROLLER_EMBEDDED_H

#include <stdint.h>

/* 可控硅斩波控制参数 */
#define ZCD_MIN         150
#define ZCD_MAX         890
#define ZCD_RANGE       (ZCD_MAX - ZCD_MIN)

/* 温度区定义 */
#define TEMP_ZONE_LOW_MAX   150
#define TEMP_ZONE_MID_MAX   300
#define TEMP_ZONE_HIGH_MAX  500

#define TEMP_ZONE_LOW_FACTOR    1.0f
#define TEMP_ZONE_MID_FACTOR    1.0f
#define TEMP_ZONE_HIGH_FACTOR   1.2f

/* PID参数结构体 */
typedef struct {
    float kp;
    float ki;
    float kd;
} PID_Params;

/* PID控制器状态结构体 */
typedef struct {
    /* 基础PID参数 */
    float base_kp;
    float base_ki;
    float base_kd;
    
    /* 实际应用的PID参数（含温度区补偿） */
    float kp;
    float ki;
    float kd;
    
    /* 控制器状态 */
    float setpoint;
    float last_error;
    float integral;
    float output;
    
    /* 限幅 */
    float integral_max;
    float integral_min;
    float output_max;
    float output_min;
    
    /* 当前温度区 */
    uint8_t current_zone;
} PID_Controller;

/* 性能指标结构体 */
typedef struct {
    float rise_time;
    float overshoot;
    float settling_time;
    float steady_error;
    uint8_t oscillations;
    float max_temp;
    float final_temp;
} PID_Performance;

/* 数据记录结构体（用于性能分析） */
typedef struct {
    float time;
    float temp;
    float setpoint;
    float power;
} PID_DataPoint;

/* 函数声明 */
void PID_Init(PID_Controller *pid, float kp, float ki, float kd);
void PID_SetSetpoint(PID_Controller *pid, float setpoint);
void PID_SetParams(PID_Controller *pid, float kp, float ki, float kd);
void PID_Reset(PID_Controller *pid);
float PID_Update(PID_Controller *pid, float current_temp, float dt);
uint16_t PowerToZCD(float power_percent);
float ZCDToPower(uint16_t zcd);

/* 温度区补偿 */
void PID_UpdateZoneFactors(PID_Controller *pid, float current_temp);
uint8_t PID_GetTemperatureZone(float temp);

/* 性能分析 */
void PID_Analyzer_Reset(void);
void PID_Analyzer_AddData(float time, float temp, float setpoint, float power);
PID_Performance PID_Analyzer_GetMetrics(void);
uint8_t PID_CheckSteadyState(float setpoint, float threshold, float duration, float *steady_time);

/* 自适应整定 */
typedef struct {
    uint8_t cycle;
    uint8_t max_cycles;
    float overshoot_threshold;
    float steady_time_required;
} PID_Tuner;

void PID_Tuner_Init(PID_Tuner *tuner, uint8_t max_cycles);
uint8_t PID_Tuner_AdjustParams(PID_Tuner *tuner, PID_Controller *pid, PID_Performance *perf);
uint8_t PID_Tuner_IsOptimal(PID_Tuner *tuner, PID_Performance *perf);

/* Modbus-RTU 命令定义 */
#define MODBUS_SLAVE_ADDR       0x01

/* 功能码 */
#define MODBUS_READ_REGS        0x03
#define MODBUS_WRITE_REG        0x06
#define MODBUS_WRITE_REGS       0x10

/* 寄存器地址定义 */
#define REG_CURRENT_TEMP        0x0000
#define REG_TARGET_TEMP         0x0001
#define REG_CURRENT_POWER       0x0002
#define REG_CURRENT_ZCD         0x0003
#define REG_PID_KP              0x0010
#define REG_PID_KI              0x0011
#define REG_PID_KD              0x0012
#define REG_CONTROL_MODE        0x0020  /* 0=停止, 1=运行, 2=整定 */
#define REG_TUNING_STATUS       0x0021
#define REG_OVERSHOOT           0x0030
#define REG_STEADY_ERROR        0x0031

#endif /* PID_CONTROLLER_EMBEDDED_H */
