Definition.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 OBS_KSLIDE _Q15(0.85) // SMO算法里的滑膜增益值
  100. #define PWM_CYCLE (1000.0 / PWM_FREQUENCY) // 周期us
  101. #define SAMP_FREQ (PWM_FREQUENCY * 1000) // 采样频率(HZ)
  102. #define TPWM_VALUE (1.0 / SAMP_FREQ) // 载波周期(S)
  103. #define PWM_VALUE_LOAD (uint16)(MCU_CLOCK * 1000 *2 / PWM_FREQUENCY) // PWM 定时器重载值
  104. #define PWM_LOAD_DEADTIME (PWM_DEADTIME * MCU_CLOCK) // 死区设置值
  105. #define PWM_OVERMODULE_TIME (OVERMOD_TIME * MCU_CLOCK / 2) // 过调制时间
  106. #define PWM_DLOWL_TIME (DLL_TIME * MCU_CLOCK / 2) //下桥臂最小时间
  107. #define PWM_TS_LOAD (uint16)(_Q16 / PWM_CYCLE * MIN_WIND_TIME / 16) // 单电阻采样设置值
  108. #define PWM_DT_LOAD (uint16)(_Q16 / PWM_CYCLE * DT_TIME / 16) // 死区补偿值
  109. #define PWM_TGLI_LOAD (uint16)(_Q16 / PWM_CYCLE * (GLI_TIME + PWM_DEADTIME) / 16) // 最小脉冲
  110. #define BASE_FREQ ((MOTOR_SPEED_BASE / 60) * Pole_Pairs) // 基准频率
  111. #define MAX_BEMF_VOLTAGE ((MOTOR_SPEED_BASE*Ke)/(1000.0))
  112. #define MAX_OMEG_RAD_SEC ((float)(_2PI*BASE_FREQ))
  113. #define OBS_FBASE _Q15(BASE_FREQ*TPWM_VALUE)
  114. #define OBS_KLPF _Q15(_2PI*BASE_FREQ*TPWM_VALUE)
  115. #define SPEED_KLPF _Q15(_2PI*SPD_BW*TPWM_VALUE)
  116. #define OBS_EA_KS _Q15((2*MOTOR_SPEED_SMOMIN_RPM*_2PI*BASE_FREQ*TPWM_VALUE)/MOTOR_SPEED_BASE)
  117. // 过调制
  118. #define OverModulation (0)
  119. #define EstimateAlgorithm (AO)
  120. #define Open_Start_Mode (Omega_Start)
  121. #define Shunt_Resistor_Mode (Double_Resistor)
  122. #define CalibENDIS (1)
  123. #define SVPMW_Mode (SVPWM_7_Segment)
  124. #define DouRes_Sample_Mode (DouRes_1_Cycle)
  125. #if (EstimateAlgorithm==AO)
  126. #define OBS_K1T _Q11((3.0/(125.0*1.0))*(LQ/TPWM_VALUE)*(HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE))
  127. #define OBS_K2T _Q11(RS*HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE)
  128. #define OBS_K2T_Actual _Q11(RS*HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE)
  129. #define OBS_K3T _Q8(2.5)
  130. #define OBS_K4T _Q15(((LD-LQ)*TPWM_VALUE*MAX_OMEG_RAD_SEC)/(LD+RS*TPWM_VALUE))
  131. #else
  132. #define OBS_K1T _Q15(LD/(LD+RS*TPWM_VALUE))
  133. #define OBS_K2T _Q13((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(HW_BOARD_VOLTAGE_BASE_Start/HW_BOARD_CURR_BASE))
  134. #define OBS_K2T_Actual _Q13((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(HW_BOARD_VOLTAGE_BASE/HW_BOARD_CURR_BASE))
  135. #define OBS_K3T _Q15((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(MAX_BEMF_VOLTAGE/HW_BOARD_CURR_BASE))
  136. #define OBS_K4T _Q15(((LD-LQ)*TPWM_VALUE*MAX_OMEG_RAD_SEC)/(LD+RS*TPWM_VALUE))
  137. #endif
  138. // AO SMO 估算器增益
  139. #define OBSW_KP_GAIN _Q12(2*_2PI*ATT_COEF*ATO_BW/BASE_FREQ)
  140. #define OBSW_KI_GAIN _Q15(_2PI*ATO_BW*ATO_BW*TPWM_VALUE/BASE_FREQ)
  141. #define OBSW_KP_GAIN_RUN _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN/BASE_FREQ)
  142. #define OBSW_KI_GAIN_RUN _Q15(_2PI*ATO_BW_RUN*ATO_BW_RUN*TPWM_VALUE/BASE_FREQ)
  143. #define OBSW_KP_GAIN_RUN1 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN1/BASE_FREQ)
  144. #define OBSW_KI_GAIN_RUN1 _Q15(_2PI*ATO_BW_RUN1*ATO_BW_RUN1*TPWM_VALUE/BASE_FREQ)
  145. #define OBSW_KP_GAIN_RUN2 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN2/BASE_FREQ)
  146. #define OBSW_KI_GAIN_RUN2 _Q15(_2PI*ATO_BW_RUN2*ATO_BW_RUN2*TPWM_VALUE/BASE_FREQ)
  147. #define OBSW_KP_GAIN_RUN3 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN3/BASE_FREQ)
  148. #define OBSW_KI_GAIN_RUN3 _Q15(_2PI*ATO_BW_RUN3*ATO_BW_RUN3*TPWM_VALUE/BASE_FREQ)
  149. #define OBSW_KP_GAIN_RUN4 _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN4/BASE_FREQ)
  150. #define OBSW_KI_GAIN_RUN4 _Q15(_2PI*ATO_BW_RUN4*ATO_BW_RUN4*TPWM_VALUE/BASE_FREQ)
  151. // PLL估算器增益
  152. #define OBSE_PLLKP_GAIN1 _Q11(((2*ATT_COEF*_2PI*E_BW1*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  153. #define OBSE_PLLKI_GAIN1 _Q11((_2PI*E_BW1*_2PI*E_BW1*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  154. #define OBSE_PLLKP_GAIN2 _Q11(((2*ATT_COEF*_2PI*E_BW2*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  155. #define OBSE_PLLKI_GAIN2 _Q11((_2PI*E_BW2*_2PI*E_BW2*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  156. #define OBSE_PLLKP_GAIN3 _Q11(((2*ATT_COEF*_2PI*E_BW3*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  157. #define OBSE_PLLKI_GAIN3 _Q11((_2PI*E_BW3*_2PI*E_BW3*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  158. #define OBSE_PLLKP_GAIN4 _Q11(((2*ATT_COEF*_2PI*E_BW4*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  159. #define OBSE_PLLKI_GAIN4 _Q11((_2PI*E_BW4*_2PI*E_BW4*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  160. #define OBSE_PLLKP_GAIN5 _Q11(((2*ATT_COEF*_2PI*E_BW5*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  161. #define OBSE_PLLKI_GAIN5 _Q11((_2PI*E_BW5*_2PI*E_BW5*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
  162. // ------------------------------------------------------------------------------------------------------------------
  163. // 3.其他宏定义
  164. #define LED_PIN (GP03)
  165. #define LED_ON (LED_PIN = 0) // LED 点亮
  166. #define LED_OFF (LED_PIN = 1) // LED 熄灭
  167. #define LED_OFF_STATE (LED_PIN == 1) // LED 熄灭状态
  168. #define LED_TOGGLE (LED_PIN = ~LED_PIN) // LED 翻转
  169. #define VDCBUS_ADC ADC2_DR // 高压直流
  170. #define VACBUS_ADC ADC3_DR // 高压交流
  171. #define NTC1_ADC ADC6_DR // NTC1
  172. #define NTC2_ADC ADC7_DR // NTC2
  173. #define IGBT_NTC_ADC ADC11_DR // IGBT
  174. #define PRE_DRIVER_RST GP04 // 驱动复位
  175. #define MAIN_RELAY GP51 // 母线主继电器
  176. // ------------------------------------------------------------------------------------------------------------------
  177. // 4.结构体定义与声明 函数声明
  178. // LED控制
  179. typedef struct
  180. {
  181. enum
  182. {
  183. LED_STATE_OFF, // 熄灭
  184. LED_STATE_ON, // 常亮
  185. LED_STATE_BLINK_FAULT, // 故障闪烁
  186. LED_STATE_POST_FAULT_DELAY // 延时等待
  187. } LedState ;
  188. uint16_t PauseDelayCnt;
  189. uint8_t BlinkCnt;
  190. } LED_Control_t;
  191. extern volatile LED_Control_t ledControl;
  192. void LED_State_Display(uint8_t Xn0);
  193. // 上电控制
  194. typedef struct
  195. {
  196. enum
  197. {
  198. DELAY_POWER_ON, // 等待上电
  199. DELAY_FREE_RST, // 等待释放复位
  200. POWER_RUN // 上电完成
  201. } PowerSate;
  202. uint16_t PowerInCnt; // 上电时间计数
  203. } Power_Control_t;
  204. extern volatile Power_Control_t powerControl;
  205. void Power_In_Control(void);
  206. // 电机控制
  207. typedef struct
  208. {
  209. // 模拟数据
  210. uint16_t DCBus;
  211. uint16_t ACBus;
  212. uint16_t IGBTTemp;
  213. // 数据处理
  214. uint16_t ACBusMax;
  215. uint16_t ACBusMin;
  216. // 速度相关
  217. enum
  218. {
  219. OPEN_MODE,
  220. CLOSE_MODE
  221. }LoopState;
  222. uint8_t LoopTime;
  223. float RampInc;
  224. float RampDec;
  225. int16_t TargetRef;
  226. float ActualRef;
  227. int16_t ActualSpeed;
  228. int16_t ActualQOutValue;
  229. int16_t QOutRef;
  230. int16_t IQRef;
  231. int16_t IDRef;
  232. int16_t ISRef;
  233. // 保护相关
  234. uint16_t BackEMF;
  235. // 测试
  236. int16_t uPhaseCurr;
  237. int16_t vPhaseCurr;
  238. } Motor_Control_t;
  239. extern Motor_Control_t motorControl;
  240. typedef struct
  241. {
  242. enum
  243. {
  244. CALIB_INIT,
  245. GET_OFFSET,
  246. OFFSET_READY
  247. } OffsetCalib;
  248. // U相
  249. int16_t IuOffset;
  250. int32_t IuOffsetSum;
  251. // V相
  252. int16_t IvOffset;
  253. int32_t IvOffsetSum;
  254. // W相
  255. int16_t IwBusOffset;
  256. int32_t IwBusOffsetSum;
  257. int16_t OffsetCount;
  258. } Curr_Offset_t;
  259. extern Curr_Offset_t xdata currOffset;
  260. // ------------------------------------------------------------------------------------------------------------------
  261. // 5.硬件初始化与调用声明
  262. void Driver_Init(void);
  263. void VREF_Config_Init(void);
  264. void ADC_Init(void);
  265. void Get_ADC_Value(void);
  266. void AMP_Init(void);
  267. void GPIO_Init(void);
  268. void PreDriver_Falut_Init(void);
  269. void Sys_Tick(void);
  270. // 串口初始化
  271. void UART1_Init(void);
  272. void UART2_Init(void);
  273. extern uint8_t xdata DebugDat[64];
  274. void Dabug_Data_Update(void);
  275. // 增量式硬件PI控制器初始化
  276. void HW_Zero_PI_Init(void);
  277. void HW_One_PI_Init(void);
  278. void HW_Two_PI_Init(void);
  279. void HW_Three_PI_Init(void);
  280. // 增量式硬件PI计算
  281. int16_t HW_Zero_Calc(int16_t Xn0);
  282. int16_t HW_One_Calc(int16_t Xn0);
  283. int16_t HW_Two_Calc(int16_t Xn0);
  284. int16_t HW_Three_Calc(int16_t Xn0);
  285. // 低通滤波计算
  286. int16_t LPF_Zero_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  287. int16_t LPF_One_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  288. int16_t LPF_Two_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  289. // ------------------------------------------------------------------------------------------------------------------
  290. // 6.函数声明 变量声明
  291. extern bool data IsTick;
  292. extern bool data isCtrlPowOn;
  293. void Tick_Task(void);
  294. void Motor_Control_State(void);
  295. void Loop_Control(void);
  296. void Ref_Ramp(void);
  297. void Get_Target_Ref(void);
  298. #endif