Definition.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /**
  2. * @copyright None
  3. * @file Definition.h
  4. * @author Comment Vivre
  5. * @date 2025-11-03
  6. * @brief None
  7. */
  8. #ifndef __DEFINITION_H_
  9. #define __DEFINITION_H_
  10. // ------------------------------------------------------------------------------------------------------------------
  11. // 1.可选配置项定义
  12. // 转向
  13. #define CW (0)
  14. #define CCW (1)
  15. // 基准电压
  16. #define VREF3_0 (0xA0)
  17. #define VREF4_0 (0xB0)
  18. #define VREF4_5 (0xC0)
  19. #define VREF5_0 (0xD0)
  20. // 驱动电平
  21. #define HIGH_LEVEL (0xA0)
  22. #define LOW_LEVEL (0xB0)
  23. #define UP_H_DOWN_L (0xC0)
  24. #define UP_L_DOWN_H (0xD0)
  25. // 电流采样模式
  26. #define Single_Resistor (0xA0)
  27. #define Double_Resistor (0xB0)
  28. #define Three_Resistor (0xC0)
  29. // 内部PGA放大倍数选择
  30. #define AMP2x (2)
  31. #define AMP4x (4)
  32. #define AMP8x (8)
  33. #define AMP16x (16)
  34. // 运放模式选择
  35. #define AMP_NOMAL (0xA0)
  36. #define AMP_PGA_DUAL (0xB0)
  37. // 浮点转整数定义
  38. #define _Q7(A) (int8) ((A) * 0x7f) // Q7 128*1 = 128
  39. #define _Q8(A) (int16)((A) * 0xff) // Q8 128*2 = 256
  40. #define _Q9(A) (int16)((A) * 0x1ff) // Q9 128*4 = 512
  41. #define _Q10(A) (int16)((A) * 0x3ff) // Q10 128*8 = 1024
  42. #define _Q11(A) (int16)((A) * 0x07ff) // Q11 128*16 = 2048
  43. #define _Q12(A) (int16)((A) * 0x0fff) // Q12 128*32 = 4096
  44. #define _Q13(A) (int16)((A) * 0x1fff) // Q13 128*64 = 8192
  45. #define _Q14(A) (int16)((A) * 0x3fff) // Q14 128*128= 18383
  46. #define _Q15(A) (int16)((A) * 0x7fff) // Q15 128*256= 32767
  47. #define _Q16 (65535.0) // Q16
  48. #define _2PI (3.1415926 * 2)
  49. // 双电阻采样周期配置
  50. #define DouRes_1_Cycle (0xA0) // 周期采样完 ia, ib
  51. #define DouRes_2_Cycle (0xB0) // 交替采用ia, ib, 2周期采样完成
  52. // SVPWM模式
  53. #define SVPWM_5_Segment (0xA0)
  54. #define SVPWM_7_Segment (0xB0)
  55. // 估算器
  56. #define SMO (0xA0)
  57. #define PLL (0xB0)
  58. #define AO (0xC0)
  59. // 开环启动模式选择
  60. #define Open_Start (0xA0)
  61. #define Omega_Start (0xB0)
  62. #define Open_Omega_Start (0xC0)
  63. //调速模式
  64. #define UARTMODE (0xA0)
  65. #define NONEMODE (0xB0)
  66. // 外环
  67. #define POWER_CONTROL_MODE (0xA0)
  68. #define SPEED_CONTROL_MODE (0xB0)
  69. // ------------------------------------------------------------------------------------------------------------------
  70. // 2.电机控制参数定义与配置
  71. #define MCU_CLOCK (24.0)
  72. #define DLL_TIME (1.3) // 双电阻最小脉宽设置(us),建议值为死区时间值+0.2us以上
  73. #define OVERMOD_TIME (2.0) // 三电阻过调制时间(us),建议值2.0
  74. #define DT_TIME (1.0) // 死区补偿时间(us)适用于双电阻和三电阻,建议值是1/2死区时间
  75. #define GLI_TIME (0.5) // 桥臂窄脉宽消除(us),建议值0.5
  76. #define PWM_LEVEL_MODE (HIGH_LEVEL)
  77. // DQ轴最大限幅值
  78. #define DOUTMAX _Q15(0.99)
  79. #define DOUTMIN _Q15(-0.99)
  80. #define QOUTMAX _Q15(0.99)
  81. #define QOUTMIN _Q15(-0.99)
  82. #define LPF_K(K) _Q15(_2PI / 1000 * K)
  83. #define VC1 (1.0)
  84. #define RV ((RV1 + RV2 + RV3) / RV3)
  85. // 电流基准
  86. #define HW_BOARD_CURR_MAX (HW_ADC_REF / 2 / HW_AMPGAIN / HW_RSHUNT)
  87. #define HW_BOARD_CURR_MIN (-HW_BOARD_CURR_MAX)
  88. #define HW_BOARD_CURR_BASE (HW_BOARD_CURR_MAX * 2)
  89. // 母线电压采样分压电路
  90. #define HW_BOARD_VOLT_MAX (HW_ADC_REF * RV)
  91. #define HW_BOARD_VOLTAGE_BASE (HW_BOARD_VOLT_MAX / 1.732)
  92. #define HW_BOARD_VOLTAGE_VC ((RV1 + RV2 + RV3 * VC1) / (RV3 * VC1))
  93. #define HW_BOARD_VOLTAGE_BASE_Start (HW_ADC_REF * HW_BOARD_VOLTAGE_VC / 1.732)
  94. #define I_ValueX(Curr_Value) (Curr_Value * HW_RSHUNT * HW_AMPGAIN / (HW_ADC_REF))
  95. #define I_Value(Curr_Value) _Q15(I_ValueX(Curr_Value))
  96. // 速度带宽的滤波值,经典值为5.0-40.0
  97. #define SPD_BW (25.0)
  98. #define ATT_COEF (0.95)
  99. #define PWM_CYCLE (1000.0 / PWM_FREQUENCY) // 周期us
  100. #define SAMP_FREQ (PWM_FREQUENCY * 1000) // 采样频率(HZ)
  101. #define TPWM_VALUE (1.0 / SAMP_FREQ) // 载波周期(S)
  102. #define PWM_VALUE_LOAD (uint16)(MCU_CLOCK * 1000 *2 / PWM_FREQUENCY) // PWM 定时器重载值
  103. #define PWM_LOAD_DEADTIME (PWM_DEADTIME * MCU_CLOCK) // 死区设置值
  104. #define PWM_OVERMODULE_TIME (OVERMOD_TIME * MCU_CLOCK / 2) // 过调制时间
  105. #define PWM_DLOWL_TIME (DLL_TIME * MCU_CLOCK / 2) //下桥臂最小时间
  106. #define PWM_TS_LOAD (uint16)(_Q16 / PWM_CYCLE * MIN_WIND_TIME / 16) // 单电阻采样设置值
  107. #define PWM_DT_LOAD (uint16)(_Q16 / PWM_CYCLE * DT_TIME / 16) // 死区补偿值
  108. #define PWM_TGLI_LOAD (uint16)(_Q16 / PWM_CYCLE * (GLI_TIME + PWM_DEADTIME) / 16) // 最小脉冲
  109. #define BASE_FREQ ((MOTOR_SPEED_BASE / 60) * Pole_Pairs) // 基准频率
  110. #define MAX_BEMF_VOLTAGE ((MOTOR_SPEED_BASE*Ke)/(1000.0))
  111. #define MAX_OMEG_RAD_SEC ((float)(_2PI*BASE_FREQ))
  112. #define OBS_FBASE _Q15(BASE_FREQ*TPWM_VALUE)
  113. #define OBS_KLPF _Q15(_2PI*BASE_FREQ*TPWM_VALUE)
  114. #define SPEED_KLPF _Q15(_2PI*SPD_BW*TPWM_VALUE)
  115. #define OBS_EA_KS _Q15((2*MOTOR_SPEED_SMOMIN_RPM*_2PI*BASE_FREQ*TPWM_VALUE)/MOTOR_SPEED_BASE)
  116. // 过调制
  117. #define OverModulation (0)
  118. #define EstimateAlgorithm (AO)
  119. #define Open_Start_Mode (Omega_Start)
  120. #define Shunt_Resistor_Mode (Double_Resistor)
  121. #define CalibENDIS (1)
  122. #define SVPMW_Mode (SVPWM_7_Segment)
  123. #define DouRes_Sample_Mode (DouRes_1_Cycle)
  124. #if (EstimateAlgorithm==AO)
  125. #define OBS_K1T _Q11((3.0/(125.0*1.0))*(LQ/TPWM_VALUE)*(HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE))
  126. #define OBS_K2T _Q11(RS*HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE)
  127. #define OBS_K2T_Actual _Q11(RS*HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE)
  128. #define OBS_K3T _Q8(2.5)
  129. #define OBS_K4T _Q15(((LD-LQ)*TPWM_VALUE*MAX_OMEG_RAD_SEC)/(LD+RS*TPWM_VALUE))
  130. #else
  131. #define OBS_K1T _Q15(LD/(LD+RS*TPWM_VALUE))
  132. #define OBS_K2T _Q13((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(HW_BOARD_VOLTAGE_BASE_Start/HW_BOARD_CURR_BASE))
  133. #define OBS_K2T_Actual _Q13((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(HW_BOARD_VOLTAGE_BASE/HW_BOARD_CURR_BASE))
  134. #define OBS_K3T _Q15((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(MAX_BEMF_VOLTAGE/HW_BOARD_CURR_BASE))
  135. #define OBS_K4T _Q15(((LD-LQ)*TPWM_VALUE*MAX_OMEG_RAD_SEC)/(LD+RS*TPWM_VALUE))
  136. #endif
  137. // AO SMO 估算器增益
  138. #define OBSW_KP_GAIN _Q12(2*_2PI*ATT_COEF*ATO_BW/BASE_FREQ)
  139. #define OBSW_KI_GAIN _Q15(_2PI*ATO_BW*ATO_BW*TPWM_VALUE/BASE_FREQ)
  140. #define OBSW_KP_GAIN_RUN _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN/BASE_FREQ)
  141. #define OBSW_KI_GAIN_RUN _Q15(_2PI*ATO_BW_RUN*ATO_BW_RUN*TPWM_VALUE/BASE_FREQ)
  142. #define OBSW_KP_GAIN_RUN1 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN1/BASE_FREQ)
  143. #define OBSW_KI_GAIN_RUN1 _Q15(_2PI*ATO_BW_RUN1*ATO_BW_RUN1*TPWM_VALUE/BASE_FREQ)
  144. #define OBSW_KP_GAIN_RUN2 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN2/BASE_FREQ)
  145. #define OBSW_KI_GAIN_RUN2 _Q15(_2PI*ATO_BW_RUN2*ATO_BW_RUN2*TPWM_VALUE/BASE_FREQ)
  146. #define OBSW_KP_GAIN_RUN3 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN3/BASE_FREQ)
  147. #define OBSW_KI_GAIN_RUN3 _Q15(_2PI*ATO_BW_RUN3*ATO_BW_RUN3*TPWM_VALUE/BASE_FREQ)
  148. #define OBSW_KP_GAIN_RUN4 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN4/BASE_FREQ)
  149. #define OBSW_KI_GAIN_RUN4 _Q15(_2PI*ATO_BW_RUN4*ATO_BW_RUN4*TPWM_VALUE/BASE_FREQ)
  150. // PLL估算器增益
  151. #define OBSE_PLLKP_GAIN1 _Q11(((2*ATT_COEF*_2PI*E_BW1*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  152. #define OBSE_PLLKI_GAIN1 _Q11((_2PI*E_BW1*_2PI*E_BW1*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  153. #define OBSE_PLLKP_GAIN2 _Q11(((2*ATT_COEF*_2PI*E_BW2*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  154. #define OBSE_PLLKI_GAIN2 _Q11((_2PI*E_BW2*_2PI*E_BW2*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  155. #define OBSE_PLLKP_GAIN3 _Q11(((2*ATT_COEF*_2PI*E_BW3*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  156. #define OBSE_PLLKI_GAIN3 _Q11((_2PI*E_BW3*_2PI*E_BW3*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  157. #define OBSE_PLLKP_GAIN4 _Q11(((2*ATT_COEF*_2PI*E_BW4*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  158. #define OBSE_PLLKI_GAIN4 _Q11((_2PI*E_BW4*_2PI*E_BW4*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  159. #define OBSE_PLLKP_GAIN5 _Q11(((2*ATT_COEF*_2PI*E_BW5*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  160. #define OBSE_PLLKI_GAIN5 _Q11((_2PI*E_BW5*_2PI*E_BW5*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  161. // ------------------------------------------------------------------------------------------------------------------
  162. // 3.其他宏定义
  163. #define LED_PIN (GP03)
  164. #define LED_ON (LED_PIN = 0) // LED 点亮
  165. #define LED_OFF (LED_PIN = 1) // LED 熄灭
  166. #define LED_OFF_STATE (LED_PIN == 1) // LED 熄灭状态
  167. #define LED_TOGGLE (LED_PIN = ~LED_PIN) // LED 翻转
  168. #define VDCBUS_ADC ADC2_DR // 高压直流
  169. #define VACBUS_ADC ADC3_DR // 高压交流
  170. #define NTC1_ADC ADC6_DR // NTC1
  171. #define NTC2_ADC ADC7_DR // NTC2
  172. #define IGBT_NTC_ADC ADC11_DR // IGBT
  173. #define PRE_DRIVER_RST GP04 // 驱动复位
  174. #define MAIN_RELAY GP51 // 母线主继电器
  175. // ------------------------------------------------------------------------------------------------------------------
  176. // 4.结构体定义与声明 函数声明
  177. // LED控制
  178. typedef struct
  179. {
  180. enum
  181. {
  182. LED_STATE_OFF, // 熄灭
  183. LED_STATE_ON, // 常亮
  184. LED_STATE_BLINK_FAULT, // 故障闪烁
  185. LED_STATE_POST_FAULT_DELAY // 延时等待
  186. } LedState ;
  187. uint16_t PauseDelayCnt;
  188. uint8_t BlinkCnt;
  189. } LED_Control_t;
  190. extern volatile LED_Control_t ledControl;
  191. void LED_State_Display(uint8_t Xn0);
  192. // 上电控制
  193. typedef struct
  194. {
  195. enum
  196. {
  197. DELAY_POWER_ON, // 等待上电
  198. DELAY_FREE_RST, // 等待释放复位
  199. POWER_RUN // 上电完成
  200. } PowerSate;
  201. uint16_t PowerInCnt; // 上电时间计数
  202. } Power_Control_t;
  203. extern volatile Power_Control_t powerControl;
  204. void Power_In_Control(void);
  205. // 电机控制
  206. typedef struct
  207. {
  208. // 模拟数据
  209. uint16_t DCBus;
  210. uint16_t ACBus;
  211. uint16_t IGBTTemp;
  212. // 数据处理
  213. uint16_t ACBusMax;
  214. uint16_t ACBusMin;
  215. // 速度相关
  216. int16_t RampInc;
  217. int16_t RampDec;
  218. int16_t TargetRef;
  219. int16_t ActualRef;
  220. int16_t ActualSpeed;
  221. int16_t ActualQOutValue;
  222. int16_t QOutRef;
  223. int16_t IQRef;
  224. int16_t IDRef;
  225. // 保护相关
  226. uint16_t BackEMF;
  227. // 测试
  228. int16_t uPhaseCurr;
  229. int16_t vPhaseCurr;
  230. } Motor_Control_t;
  231. extern Motor_Control_t motorControl;
  232. typedef struct
  233. {
  234. enum
  235. {
  236. CALIB_INIT,
  237. GET_OFFSET,
  238. OFFSET_READY
  239. } OffsetCalib;
  240. // U相
  241. int16_t IuOffset;
  242. int32_t IuOffsetSum;
  243. // V相
  244. int16_t IvOffset;
  245. int32_t IvOffsetSum;
  246. // W相
  247. int16_t IwBusOffset;
  248. int32_t IwBusOffsetSum;
  249. int16_t OffsetCount;
  250. } Curr_Offset_t;
  251. extern Curr_Offset_t xdata currOffset;
  252. // ------------------------------------------------------------------------------------------------------------------
  253. // 5.硬件初始化与调用声明
  254. void Driver_Init(void);
  255. void VREF_Config_Init(void);
  256. void ADC_Init(void);
  257. void Get_ADC_Value(void);
  258. void AMP_Init(void);
  259. void GPIO_Init(void);
  260. void PreDriver_Falut_Init(void);
  261. void Sys_Tick(void);
  262. // 串口初始化
  263. void UART1_Init(void);
  264. void UART2_Init(void);
  265. extern uint8_t xdata DebugDat[64];
  266. void Dabug_Data_Update(void);
  267. // 增量式硬件PI控制器初始化
  268. void HW_Zero_PI_Init(void);
  269. void HW_One_PI_Init(void);
  270. void HW_Two_PI_Init(void);
  271. void HW_Three_PI_Init(void);
  272. // 增量式硬件PI计算
  273. int16_t HW_Zero_Calc(int16_t Xn0);
  274. int16_t HW_One_Calc(int16_t Xn0);
  275. int16_t HW_Two_Calc(int16_t Xn0);
  276. int16_t HW_Three_Calc(int16_t Xn0);
  277. // 低通滤波计算
  278. int16_t LPF_Zero_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  279. int16_t LPF_One_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  280. int16_t LPF_Two_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  281. // ------------------------------------------------------------------------------------------------------------------
  282. // 6.函数声明 变量声明
  283. extern bool data IsTick;
  284. extern bool data isCtrlPowOn;
  285. void Tick_Task(void);
  286. void Motor_Control_State(void);
  287. void Get_Target_Ref(void);
  288. #endif