Definition.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 UART_MODE (0xA0)
  65. #define NONE_MODE (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. #define IFFDebugg (0)
  164. // ------------------------------------------------------------------------------------------------------------------
  165. // 3.其他宏定义
  166. #define LED_PIN (GP03)
  167. #define LED_ON (LED_PIN = 0) // LED 点亮
  168. #define LED_OFF (LED_PIN = 1) // LED 熄灭
  169. #define LED_OFF_STATE (LED_PIN == 1) // LED 熄灭状态
  170. #define LED_TOGGLE (LED_PIN = ~LED_PIN) // LED 翻转
  171. #define VDCBUS_ADC ADC2_DR // 高压直流
  172. #define VACBUS_ADC ADC3_DR // 高压交流
  173. #define NTC1_ADC ADC6_DR // NTC1
  174. #define NTC2_ADC ADC7_DR // NTC2
  175. #define IGBT_NTC_ADC ADC11_DR // IGBT
  176. #define PRE_DRIVER_RST GP04 // 驱动复位
  177. #define MAIN_RELAY GP51 // 母线主继电器
  178. // 串口通讯协议定义 << 匿名协议
  179. #define FRAME_HEADER 0xAB // 帧头
  180. #define MAX_FRAME_LEN 0x40 // 最大数据帧长度
  181. #define DATA_OFFSET 0x06 // 头(1B) + 地址(2B) + 功能码(1B) + 长度(2B)
  182. #define CRC_OFFSET 0x02 // 校验字(2B)
  183. // ------------------------------------------------------------------------------------------------------------------
  184. // 4.结构体定义与声明 函数声明
  185. // LED控制
  186. typedef struct
  187. {
  188. enum
  189. {
  190. LED_STATE_OFF, // 熄灭
  191. LED_STATE_ON, // 常亮
  192. LED_STATE_BLINK_FAULT, // 故障闪烁
  193. LED_STATE_POST_FAULT_DELAY // 延时等待
  194. } LedState ;
  195. uint16_t PauseDelayCnt;
  196. uint8_t BlinkCnt;
  197. } LED_Control_t;
  198. extern volatile LED_Control_t ledControl;
  199. void LED_State_Display(uint8_t Xn0);
  200. // 上电控制
  201. typedef struct
  202. {
  203. enum
  204. {
  205. DELAY_POWER_ON, // 等待上电
  206. DELAY_FREE_RST, // 等待释放复位
  207. POWER_RUN // 上电完成
  208. } PowerSate;
  209. uint16_t PowerInCnt; // 上电时间计数
  210. } Power_Control_t;
  211. extern volatile Power_Control_t powerControl;
  212. void Power_In_Control(void);
  213. // 电机控制
  214. typedef struct
  215. {
  216. // 模拟数据
  217. uint16_t DCBus;
  218. uint16_t ACBus;
  219. uint16_t IGBTTemp;
  220. // 数据处理
  221. uint16_t ACBusMax;
  222. uint16_t ACBusMin;
  223. // 预充电状态
  224. enum
  225. {
  226. CHARGE_INIT,
  227. U_BRIDGE,
  228. V_BRIDGE,
  229. W_BRIDGE,
  230. CHARGE_END
  231. } ChargeState;
  232. // 预定位状态
  233. enum
  234. {
  235. ALIGN_INIT,
  236. ALIGN_CURR_RAMP,
  237. ALIGN_FIRST_ANGLE,
  238. ALIGN_SECOND_ANGLE
  239. } AlignState;
  240. // 闭环状态
  241. enum
  242. {
  243. OPEN_MODE,
  244. CLOSE_MODE
  245. } LoopState;
  246. uint8_t LoopTime;
  247. float RampInc;
  248. float RampDec;
  249. int16_t TargetRef;
  250. float ActualRef;
  251. int16_t ActualSpeed;
  252. int16_t ActualQOutValue;
  253. int16_t QOutRef;
  254. int16_t IQRef;
  255. int16_t IDRef;
  256. int16_t ISRef;
  257. // 保护相关
  258. uint16_t BackEMF;
  259. } Motor_Control_t;
  260. extern Motor_Control_t motorControl;
  261. typedef struct
  262. {
  263. enum
  264. {
  265. CALIB_INIT,
  266. GET_OFFSET,
  267. OFFSET_READY
  268. } OffsetCalib;
  269. // U相
  270. int16_t IuOffset;
  271. int32_t IuOffsetSum;
  272. // V相
  273. int16_t IvOffset;
  274. int32_t IvOffsetSum;
  275. // W相
  276. int16_t IwBusOffset;
  277. int32_t IwBusOffsetSum;
  278. int16_t OffsetCount;
  279. } Curr_Offset_t;
  280. extern Curr_Offset_t xdata currOffset;
  281. // ------------------------------------------------------------------------------------------------------------------
  282. // 5.硬件初始化与调用声明
  283. void Driver_Init(void);
  284. void VREF_Config_Init(void);
  285. void ADC_Init(void);
  286. void Get_ADC_Value(void);
  287. void AMP_Init(void);
  288. void GPIO_Init(void);
  289. void PreDriver_Falut_Init(void);
  290. void Sys_Tick(void);
  291. // 串口初始化
  292. void UART1_Init(void);
  293. void UART2_Init(void);
  294. extern bool RecMessageFalg;
  295. extern uint8_t xdata DebugDat[MAX_FRAME_LEN];
  296. extern uint8_t RecvBuffer[MAX_FRAME_LEN];
  297. void Dabug_Data_Update(void);
  298. // 增量式硬件PI控制器初始化
  299. void HW_Zero_PI_Init(void);
  300. void HW_One_PI_Init(void);
  301. void HW_Two_PI_Init(void);
  302. void HW_Three_PI_Init(void);
  303. // 增量式硬件PI计算
  304. int16_t HW_Zero_Calc(int16_t Xn0);
  305. int16_t HW_One_Calc(int16_t Xn0);
  306. int16_t HW_Two_Calc(int16_t Xn0);
  307. int16_t HW_Three_Calc(int16_t Xn0);
  308. // 低通滤波计算
  309. int16_t LPF_Zero_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  310. int16_t LPF_One_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  311. int16_t LPF_Two_Update(int16_t Xn1, int16_t Xn0, int16_t K);
  312. // ------------------------------------------------------------------------------------------------------------------
  313. // 6.函数声明 变量声明
  314. extern bool data IsTick;
  315. extern bool data isCtrlPowOn;
  316. void Tick_Task(void);
  317. void Motor_Control_State(void);
  318. void Loop_Control(void);
  319. void Ref_Ramp(void);
  320. void Get_Target_Ref(void);
  321. #endif