main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. #define __GLOBALS__
  2. #include "include.h"
  3. int main(void)
  4. {
  5. //电机结构体初始化
  6. All_motor_Config();
  7. //中断优先组分配
  8. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
  9. //时钟初始化
  10. cycleCounterInit();
  11. //读取flash数据
  12. Flash_ReadInf();
  13. //Dshot输出初始化
  14. Dshot_Config();
  15. //为初始化电机初始化延时
  16. //ad采集电流初始化
  17. ADC_Config();
  18. //adc采集校准初始化
  19. //Motor_AD_correct();
  20. //CAN通讯初始化
  21. CAN_Config();
  22. //串口初始化
  23. USART_Config();
  24. //开关量初始化
  25. LED_Config();
  26. //USBL_Config();
  27. //Light_Config();
  28. Overcurrent_Detection_Config();
  29. delay_ms(3000);
  30. //看门狗初始化
  31. IWDG_Config(4,1000);
  32. while(1)
  33. {
  34. //Motor_Stall_check();//堵转检测
  35. SysTick_1ms_Task();
  36. SysTick_10ms_Task();
  37. SysTick_20ms_Task();
  38. SysTick_100ms_Task();
  39. SysTick_500ms_Task();
  40. Other_Task();
  41. }
  42. }
  43. u32 cntLight = 0;
  44. u32 cntUSBL = 0;
  45. u32 cntVIDEO = 0;
  46. void SysTick_1ms_Task()//1000hz
  47. {
  48. static u32 last_sysTickUptime = 0;
  49. static u32 cntADC = 0;
  50. static u32 adc_value=0;
  51. if((sysTickUptime - last_sysTickUptime) >= 1)
  52. {
  53. last_sysTickUptime = sysTickUptime;
  54. //灯过流保护
  55. if(!Read_LIGHT)
  56. {
  57. if(++cntLight>2)
  58. {
  59. CUT_LIGHT;
  60. light_err = 10;
  61. light_pwm = 0;
  62. }
  63. }
  64. else
  65. {
  66. cntLight = 0;
  67. }
  68. //USBL过流保护
  69. if(!Read_USBL)
  70. {
  71. if(++cntUSBL>2)
  72. {
  73. CUT_USBL;
  74. usbl_err = 1;
  75. }
  76. }
  77. else
  78. {
  79. cntUSBL = 0;
  80. }
  81. //摄像头过流保护
  82. if(!Read_VIDEO)
  83. {
  84. if(++cntVIDEO>2)
  85. {
  86. CUT_VIDEO;
  87. }
  88. }
  89. else
  90. {
  91. cntVIDEO = 0;
  92. }
  93. if(++cntADC<=10)
  94. {
  95. adc_value += Get_SOFT_ADC(ADC1, ADC_Channel_14);
  96. }
  97. else
  98. {
  99. cntADC = 0;
  100. humidity = adc_value/10;
  101. if(humidity > 1500)
  102. {
  103. humidity_err = 1;
  104. }
  105. else
  106. {
  107. humidity_err = 0;
  108. }
  109. adc_value = 0;
  110. }
  111. }
  112. }
  113. void SysTick_10ms_Task()//100hz
  114. {
  115. static u32 last_sysTickUptime = 0;
  116. if((sysTickUptime - last_sysTickUptime) >= 10)
  117. {
  118. last_sysTickUptime = sysTickUptime;
  119. //任务列表
  120. //1.发送参数反馈
  121. Send_Pixhawk_requre();
  122. //2.
  123. }
  124. }
  125. void SysTick_20ms_Task()//50hz
  126. {
  127. static u32 last_sysTickUptime = 0;
  128. if((sysTickUptime - last_sysTickUptime) >= 20)
  129. {
  130. last_sysTickUptime = sysTickUptime;
  131. //任务列表
  132. //1.左右履带PID控制
  133. Motor_Control(&left_track_motor, &track_PID, 0);
  134. Motor_Control(&right_track_motor, &track_PID, 0);
  135. //2.
  136. }
  137. }
  138. void SysTick_100ms_Task()//10hz
  139. {
  140. static u32 last_sysTickUptime = 0;
  141. if((sysTickUptime - last_sysTickUptime) >= 20)//100
  142. {
  143. last_sysTickUptime = sysTickUptime;
  144. //任务列表
  145. //1.发送履带推进器反馈数据
  146. static u8 send_pixhawk_cnt = 0;
  147. if(send_pixhawk_cnt++ >= 3) send_pixhawk_cnt = 0;
  148. switch(send_pixhawk_cnt)
  149. {
  150. case 0:
  151. Send_CAN_DATA1();
  152. Send_CAN_DATA2();
  153. break;
  154. case 1:
  155. Send_CAN_DATA3();
  156. Send_CAN_DATA4();
  157. break;
  158. case 2:
  159. Send_CAN_DATA5();
  160. Send_CAN_DATA6();
  161. break;
  162. /*
  163. case 3:
  164. Send_CAN_MOTOR1DATA();
  165. Send_CAN_MOTOR2DATA();
  166. Send_CAN_MOTOR3DATA();
  167. */
  168. default:break;
  169. }
  170. }
  171. }
  172. void SysTick_500ms_Task()//2hz
  173. {
  174. static u32 last_sysTickUptime = 0;
  175. if((sysTickUptime - last_sysTickUptime) >= 500)
  176. {
  177. last_sysTickUptime = sysTickUptime;
  178. //任务列表
  179. //1.闪烁led
  180. LED_TURN;
  181. //测试默认开
  182. // light_pwm = 1;
  183. // usbl_opendown = 1;
  184. if(light_pwm && light_err == 0)
  185. LIGHT_ON;
  186. else
  187. LIGHT_OFF;
  188. //USBL启动
  189. if(usbl_opendown && usbl_err == 0)
  190. USBL_ON;
  191. else
  192. USBL_OFF;
  193. }
  194. }
  195. void Other_Task(void)
  196. {
  197. //1.反馈flash
  198. if(Updata_requre > 0)
  199. {
  200. Send_Updata_requre();
  201. }
  202. }
  203. void Overcurrent_Detection_Config(void)
  204. {
  205. //过流检测
  206. GPIO_IN_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_2);
  207. GPIO_IN_Config(RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_8|GPIO_Pin_9);
  208. //通断控制
  209. GPIO_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_1|GPIO_Pin_15);
  210. GPIO_Config(RCC_AHB1Periph_GPIOF, GPIOF, GPIO_Pin_15);
  211. //相机默认开
  212. GPIO_SetBits(GPIOA, GPIO_Pin_1);
  213. //灯,USBL默认关
  214. GPIO_ResetBits(GPIOA, GPIO_Pin_15);
  215. GPIO_ResetBits(GPIOF, GPIO_Pin_15);
  216. //测试 默认开
  217. //GPIO_SetBits(GPIOA, GPIO_Pin_15);
  218. //GPIO_SetBits(GPIOF, GPIO_Pin_15);
  219. }
  220. void Stall_check(MOTOR *Motor)// void(*PWMoutput)(TIM_TypeDef*,uint32_t)
  221. {
  222. float Magnification = 2;
  223. if((*Motor).usart_data.rpm >= protect_parameters.dividi_rpm)
  224. {
  225. Magnification = protect_parameters.high_speed_mag;
  226. }
  227. else
  228. {
  229. Magnification = protect_parameters.low_speed_mag;
  230. }
  231. if((*Motor).numble < 9 && (*Motor).stal_data.stop_flag == 0)
  232. {
  233. //1.推进器缠绕保护
  234. if(((*Motor).twin_data.current - (*Motor).twin_data.current_correct)
  235. > ((*Motor).twin_data.STcurrent*Magnification + 0.5f))
  236. {
  237. ++(*Motor).twin_data.twin_cnt;
  238. }
  239. else
  240. {
  241. (*Motor).twin_data.twin_cnt = 0;
  242. }
  243. //出现缠绕,削弱输出
  244. if((*Motor).twin_data.twin_cnt >= 80)//2S 1000
  245. {
  246. (*Motor).twin_data.current = 0;
  247. (*Motor).twin_data.twin_cnt = 0;
  248. if((*Motor).twin_data.speed_lim>0)
  249. {
  250. (*Motor).twin_data.speed_lim -= 1;
  251. if((*Motor).twin_data.speed_lim<1)
  252. {
  253. (*Motor).twin_data.speed_lim = 0;
  254. }
  255. }
  256. else
  257. {
  258. (*Motor).twin_data.speed_lim = 0;
  259. }
  260. }
  261. //3.启动保护 3s无法旋转判定启动堵塞
  262. // if(((*Motor).pid_data.aim_speed>200 || (*Motor).pid_data.aim_speed<-200) && (*Motor).usart_data.rpm<20)
  263. // {
  264. // (*Motor).stal_data.unormal_cnt++;
  265. // if((*Motor).stal_data.unormal_cnt>=120) (*Motor).stal_data.stop_flag = -1;
  266. // }
  267. // else
  268. // {
  269. // if((*Motor).stal_data.unormal_cnt>0) (*Motor).stal_data.unormal_cnt--;
  270. // }
  271. //2.推进器瞬时过流保护 25ms采样值超过设定值(STALL_Param2.MaxCurrent)
  272. if((*Motor).twin_data.current >= protect_parameters.max_current_lime)
  273. {
  274. (*Motor).stal_data.stop_flag = -1;//停转至重启
  275. }
  276. }
  277. else if((*Motor).numble >= 9 && (*Motor).stal_data.stop_flag == 0)
  278. {
  279. //减速电机保护
  280. //1.履带电机过功率保护
  281. if((*Motor).twin_data.current * ((*Motor).usart_data.voltage) > 700)
  282. {
  283. //过功率时间累计 以及 防止计数溢出
  284. if(++(*Motor).stal_data.stall_cnt>=65535)
  285. (*Motor).stal_data.stall_cnt = (int16_t)(protect_parameters.time_lime/25) + 1;
  286. }
  287. //2.履带瞬时过流保护 25ms采样值超过设定值(STALL_Param2.MaxCurrent)
  288. if((*Motor).twin_data.current >= protect_parameters.max_current_lime-5)
  289. {
  290. (*Motor).stal_data.stop_flag = 120;//-1;//停转至重启
  291. Motor_err_flag |= 1<<((*Motor).numble-1);
  292. }
  293. }
  294. //打嗝自恢复中
  295. if((*Motor).stal_data.stop_flag != 0)
  296. {
  297. if((*Motor).stal_data.stop_flag > 0)
  298. {
  299. (*Motor).stal_data.stop_flag--;
  300. if((*Motor).stal_data.stop_flag == 0)
  301. {
  302. Motor_err_flag &= ~(1<<((*Motor).numble-1));
  303. (*Motor).stal_data.stall_err = 0;
  304. }
  305. }
  306. else if((*Motor).stal_data.stop_flag<0)
  307. {
  308. (*Motor).stal_data.stall_err = 1;
  309. Motor_err_flag |= (*Motor).stal_data.stall_err<<((*Motor).numble-1);
  310. }
  311. //停转
  312. (*Motor).pid_data.aim_speed = 0;
  313. (*Motor).pid_data.dshot_pwm = 0;
  314. (*Motor).pid_data.pwm =0;
  315. }
  316. }
  317. void Motor_Stall_check(void)
  318. {
  319. if(DMA400Finishi == 1)// 堵转保护 25ms
  320. {
  321. DMA400Finishi = 0;
  322. //get_run_time();
  323. Stall_check(&left_thruster);
  324. Stall_check(&right_thruster);
  325. Stall_check(&left_track_motor);
  326. Stall_check(&right_track_motor);
  327. }
  328. }
  329. void Single_Motor_Config(MOTOR *motor,u8 num)
  330. {
  331. (*motor).numble = num;
  332. (*motor).pid_data.aim_speed = 0;
  333. (*motor).twin_data.speed_lim = 10;
  334. (*motor).twin_data.block_err = 0;
  335. (*motor).twin_data.block_cnt = 0;
  336. (*motor).twin_data.twin_cnt = 0;
  337. (*motor).twin_data.current_correct = 0;
  338. (*motor).stal_data.overload = 2;
  339. (*motor).stal_data.stall_cnt = 0;
  340. (*motor).stal_data.stop_flag = 0;
  341. (*motor).stal_data.stall_err = 0;
  342. }
  343. void All_motor_Config(void)
  344. {
  345. track_PID.P = 0.6;
  346. track_PID.I = 0.3;
  347. track_PID.D = 0;
  348. protect_parameters.max_current_lime = 15;
  349. protect_parameters.time_lime = 300;
  350. protect_parameters.low_speed_mag = 2.5;
  351. protect_parameters.high_speed_mag = 3.5;
  352. protect_parameters.dividi_rpm = 70;
  353. Single_Motor_Config(&left_thruster,1);
  354. Single_Motor_Config(&right_thruster,2);
  355. Single_Motor_Config(&left_track_motor,9);
  356. Single_Motor_Config(&right_track_motor,10);
  357. //履带方向
  358. left_trank_direction = 1;
  359. right_trank_direction = 1;
  360. }
  361. void LED_Config(void)
  362. {
  363. GPIO_Config(RCC_AHB1Periph_GPIOF, GPIOF, GPIO_Pin_0);
  364. }
  365. void USBL_Config(void)
  366. {
  367. GPIO_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_15);
  368. }
  369. void Light_Config(void)
  370. {
  371. GPIO_Config(RCC_AHB1Periph_GPIOF, GPIOF, GPIO_Pin_15);
  372. }
  373. void CAN_Config(void)
  374. {
  375. CAN_GPIO_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_11, GPIO_PinSource11, GPIO_AF_CAN1);
  376. CAN_GPIO_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_12, GPIO_PinSource12, GPIO_AF_CAN1);
  377. CAN_NVIC_Config(CAN1_RX0_IRQn, 6, 1);
  378. CAN_Mod_Config(RCC_APB1Periph_CAN1, CAN1, 0X014E550A, 0X014E570A);//0X014E580A, 0X014E590A);
  379. }
  380. void TIM_Config(void)
  381. {
  382. TIM_Mod_Config(RCC_APB2PeriphClockCmd, RCC_APB2Periph_TIM1, TIM1, 84-1, 400-1, 1, 0);
  383. TIM_NVIC_Config(TIM1_UP_TIM10_IRQn, 1, 0);
  384. }
  385. void ADC_Config(void)
  386. {
  387. //u8 ADC_Channel[ADC_CHANNEL] = {ADC_Channel_7, ADC_Channel_8, ADC_Channel_14, ADC_Channel_15, ADC_Channel_12, ADC_Channel_13};
  388. u8 ADC_Channel[ADC_CHANNEL] = {ADC_Channel_12, ADC_Channel_13};
  389. //电流
  390. ADC_GPIO_Config(RCC_AHB1Periph_GPIOF, GPIOF, GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_9|GPIO_Pin_10);
  391. //电压
  392. ADC_GPIO_Config(RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_2);
  393. //温度
  394. ADC_GPIO_Config(RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_3);
  395. //漏水检测
  396. ADC_GPIO_Config(RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_4);
  397. ADC_SOFT_Mode_Config(RCC_APB2Periph_ADC1, ADC1, 1);
  398. ADC_DMA_Mode_Config(RCC_APB2Periph_ADC3, ADC_ExternalTrigConv_T5_CC1, ADC3, ADC_Channel, ADC_CHANNEL);
  399. ADC_NVIC_Config(DMA2_Stream0_IRQn, 0, 0);
  400. ADC_DMA_Config(RCC_AHB1Periph_DMA2, DMA_Channel_2, ADC3, DMA2_Stream0);
  401. ADC_TIM_Config(TIM5, 84-1, 20, RCC_APB1PeriphClockCmd, RCC_APB1Periph_TIM5, TIM_OC1Init, TIM_OC1PreloadConfig);
  402. }
  403. void Dshot_Config(void)
  404. {
  405. //PA5
  406. Dshot_GPIO_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_5, GPIO_PinSource5, GPIO_AF_TIM2);
  407. Dshot_TIM_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_TIM2, TIM2, 4-1, 70-1, TIM_OC1Init, TIM_OC1PreloadConfig);
  408. Dshot_NVIC_Config(DMA1_Stream5_IRQn, 0, 0);
  409. //PA7
  410. Dshot_GPIO_Config(RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_7, GPIO_PinSource7, GPIO_AF_TIM3);
  411. Dshot_TIM_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_TIM3, TIM3, 4-1, 70-1, TIM_OC2Init, TIM_OC2PreloadConfig);
  412. Dshot_NVIC_Config(DMA1_Stream5_IRQn, 0, 0);
  413. //PB7
  414. Dshot_GPIO_Config(RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_7, GPIO_PinSource7, GPIO_AF_TIM4);
  415. Dshot_TIM_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_TIM4, TIM4, 4-1, 70-1, TIM_OC2Init, TIM_OC2PreloadConfig);
  416. Dshot_NVIC_Config(DMA1_Stream3_IRQn, 0, 0);
  417. //PB6
  418. Dshot_GPIO_Config(RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_6, GPIO_PinSource6, GPIO_AF_TIM4);
  419. Dshot_TIM_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_TIM4, TIM4, 4-1,70-1, TIM_OC1Init, TIM_OC1PreloadConfig);
  420. Dshot_NVIC_Config(DMA1_Stream0_IRQn, 0, 0);
  421. //定时器初始化
  422. TIM_Mod_Config(RCC_APB2PeriphClockCmd, RCC_APB2Periph_TIM1, TIM1, 84-1, 400-1, 1, 0);
  423. TIM_NVIC_Config(TIM1_UP_TIM10_IRQn, 1, 0);
  424. }
  425. void Flash_WriteInf()
  426. {
  427. __disable_irq();
  428. STMFLASH_Write(FLASH_ADDR1(FLASH_INIT_FLAG),&Init_Flash_Data,1);
  429. IWDG_Feed();
  430. STMFLASH_Write(FLASH_ADDR1(TRACK_P_ADDR),(u32*)&track_PID.P,1);
  431. STMFLASH_Write(FLASH_ADDR1(TRACK_I_ADDR),(u32*)&track_PID.I,1);
  432. STMFLASH_Write(FLASH_ADDR1(TRACK_D_ADDR),(u32*)&track_PID.D,1);
  433. IWDG_Feed();
  434. STMFLASH_Write(FLASH_ADDR1(STALL_LMag),(u32*)&protect_parameters.low_speed_mag,1);
  435. STMFLASH_Write(FLASH_ADDR1(STALL_HMag),(u32*)&protect_parameters.high_speed_mag,1);
  436. STMFLASH_Write(FLASH_ADDR1(STALL_RPM), (u32*)&protect_parameters.dividi_rpm, 1);
  437. IWDG_Feed();
  438. STMFLASH_Write(FLASH_ADDR1(STALL_MaxCurrent),(u32*)&protect_parameters.max_current_lime,1);
  439. STMFLASH_Write(FLASH_ADDR1(STALL_Time), (u32*)&protect_parameters.time_lime, 1);
  440. IWDG_Feed();
  441. STMFLASH_Write(FLASH_ADDR1(LEFT_TRACK_DIR),(u32*)&left_trank_direction,1);
  442. STMFLASH_Write(FLASH_ADDR1(RIGHT_TRACK_DIR), (u32*)&right_trank_direction, 1);
  443. __enable_irq();
  444. }
  445. void Flash_ReadInf()
  446. {
  447. if(STMFLASH_ReadWord(FLASH_ADDR1(FLASH_INIT_FLAG))!=0x12345678)//该单片机未写入数据
  448. {
  449. Flash_WriteInf();//初始化数据
  450. }
  451. track_PID.P = ReadFloatWord(FLASH_ADDR1(TRACK_P_ADDR));
  452. track_PID.I = ReadFloatWord(FLASH_ADDR1(TRACK_I_ADDR));
  453. track_PID.D = ReadFloatWord(FLASH_ADDR1(TRACK_D_ADDR));
  454. protect_parameters.low_speed_mag = ReadFloatWord(FLASH_ADDR1(STALL_LMag));
  455. protect_parameters.high_speed_mag = ReadFloatWord(FLASH_ADDR1(STALL_HMag));
  456. protect_parameters.dividi_rpm = ReadFloatWord(FLASH_ADDR1(STALL_RPM));
  457. protect_parameters.max_current_lime = ReadFloatWord(FLASH_ADDR1(STALL_MaxCurrent));
  458. protect_parameters.time_lime = ReadFloatWord(FLASH_ADDR1(STALL_Time));
  459. left_trank_direction = STMFLASH_ReadWord(FLASH_ADDR1(LEFT_TRACK_DIR));
  460. right_trank_direction = STMFLASH_ReadWord(FLASH_ADDR1(RIGHT_TRACK_DIR));
  461. }
  462. void Flash_ResetInf(void)
  463. {
  464. Flash_WriteInf();
  465. Flash_ReadInf();
  466. }
  467. //void Single_AD_correct(MOTOR *Motor)
  468. //{
  469. // (*Motor).twin_data.current_correct += (*Motor).twin_data.current*0.01;
  470. //}
  471. //void Motor_AD_correct(void)
  472. //{
  473. // u8 i = 0;
  474. // while(i<100)
  475. // {
  476. // if(DMA400Finishi == 1)
  477. // {
  478. // i++;
  479. // DMA400Finishi = 0;
  480. // Single_AD_correct(&motorRT);
  481. // Single_AD_correct(&motorLT);
  482. // Single_AD_correct(&motorRB);
  483. // Single_AD_correct(&motorLB);
  484. // }
  485. // }
  486. //}
  487. void USART_Config(void)
  488. {
  489. //PA3 usart2
  490. USART_GPIO_Config(RCC_AHB1Periph_GPIOA, GPIO_AF_USART2, GPIOA, GPIO_Pin_3, GPIO_PinSource3);
  491. USART_NVIC_Config(USART2_IRQn, 3, 0);
  492. USART_MOD_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_USART2, USART2, 115200);
  493. //PB11 usart3
  494. USART_GPIO_Config(RCC_AHB1Periph_GPIOB, GPIO_AF_USART3, GPIOB, GPIO_Pin_11, GPIO_PinSource11);
  495. USART_NVIC_Config(USART3_IRQn, 3, 0);
  496. USART_MOD_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_USART3, USART3, 115200);
  497. //PD2 uart5
  498. USART_GPIO_Config(RCC_AHB1Periph_GPIOD, GPIO_AF_UART5, GPIOD, GPIO_Pin_2, GPIO_PinSource2);
  499. USART_NVIC_Config(UART5_IRQn, 3, 0);
  500. USART_MOD_Config(RCC_APB1PeriphClockCmd, RCC_APB1Periph_UART5, UART5, 115200);
  501. //PG9 usart6
  502. USART_GPIO_Config(RCC_AHB1Periph_GPIOG, GPIO_AF_USART6, GPIOG, GPIO_Pin_9, GPIO_PinSource9);
  503. USART_NVIC_Config(USART6_IRQn, 3, 0);
  504. USART_MOD_Config(RCC_APB2PeriphClockCmd, RCC_APB2Periph_USART6, USART6, 115200);
  505. }
  506. float A = 0.072811;
  507. float B = -0.006079;
  508. float C = 0.000193;
  509. void Equation(MOTOR* Motor)
  510. {
  511. (*Motor).twin_data.STcurrent = A + B*((*Motor).usart_data.rpm) + C*((*Motor).usart_data.rpm)*((*Motor).usart_data.rpm);
  512. }
  513. uint8_t update_crc8(uint8_t crc, uint8_t crc_seed)
  514. {
  515. uint8_t crc_u, i;
  516. crc_u = crc;
  517. crc_u ^= crc_seed;
  518. for ( i=0; i<8; i++)
  519. {
  520. crc_u = ( crc_u & 0x80 ) ? 0x7 ^ ( crc_u << 1 ) : ( crc_u << 1 );
  521. }
  522. return (crc_u);
  523. }
  524. uint8_t get_crc8(uint8_t *Buf, uint8_t BufLen)
  525. {
  526. uint8_t crc = 0, i;
  527. for( i=0; i<BufLen; i++)
  528. {
  529. crc = update_crc8(Buf[i], crc);
  530. }
  531. return (crc);
  532. }
  533. void GetMotorData(USART_TypeDef* USARTx, MOTOR *Motor)
  534. {
  535. u8 byte = USART_ReceiveData(USARTx);
  536. if((*Motor).usart_data.buff_cnt<10)
  537. (*Motor).usart_data.buff[(*Motor).usart_data.buff_cnt] = byte;
  538. (*Motor).usart_data.buff_cnt++;
  539. }
  540. void AnaMotorData(MOTOR *Motor)
  541. {
  542. if((*Motor).usart_data.buff_cnt == 10)
  543. {
  544. if(get_crc8((*Motor).usart_data.buff,9) == (*Motor).usart_data.buff[9])//若数据有效
  545. {
  546. (*Motor).usart_data.temperature = (*Motor).usart_data.buff[0];
  547. (*Motor).usart_data.voltage = (float)((*Motor).usart_data.buff[1]<<8
  548. |(*Motor).usart_data.buff[2])/100;
  549. (*Motor).usart_data.consumption = (*Motor).usart_data.buff[5]<<8
  550. |(*Motor).usart_data.buff[6];
  551. (*Motor).usart_data.rpm = ((*Motor).usart_data.buff[7]<<8
  552. |(*Motor).usart_data.buff[8]);//*100/7
  553. if((*Motor).numble ==9 || (*Motor).numble == 10) (*Motor).usart_data.rpm = (*Motor).usart_data.rpm*0.743;
  554. Equation(Motor);
  555. }
  556. }
  557. (*Motor).usart_data.buff_cnt = 0;
  558. }
  559. u16 GetDShotValue(int16_t value, int8_t lime)
  560. {
  561. u16 DShotValue = 0;
  562. //lime = 10;//关闭缠绕限功率
  563. if(value >= 0)
  564. {
  565. DShotValue = (int16_t)(value*lime*0.1047);
  566. if(DShotValue > 942) DShotValue = 942;
  567. }
  568. else
  569. {
  570. DShotValue = 1047 - (int16_t)(value*lime*0.1);//1047 - (-300*0.001*1*1000)
  571. if(DShotValue > 1900) DShotValue = 1900;
  572. }
  573. if(lime == 0) DShotValue = 0;
  574. return DShotValue;
  575. }
  576. void Send_Pixhawk_requre(void)
  577. {
  578. switch(Pixhawk_requre)
  579. {
  580. case 4:Send_Flash_Set(TRACK_P,RESPOND,track_PID.P);break;
  581. case 5:Send_Flash_Set(TRACK_I,RESPOND,track_PID.I);break;
  582. case 6:Send_Flash_Set(TRACK_D,RESPOND,track_PID.D);break;
  583. case 7:Send_Flash_Set(LOWSPEED,RESPOND,protect_parameters.low_speed_mag);break;
  584. case 8:Send_Flash_Set(HIGHSPEED,RESPOND,protect_parameters.high_speed_mag);break;
  585. case 9:Send_Flash_Set(DIVIDI_RPM,RESPOND,protect_parameters.dividi_rpm);break;
  586. case 10:Send_Flash_Set(MAXCURRENT,RESPOND,protect_parameters.max_current_lime);break;
  587. case 11:Send_Flash_Set(MAXTIME,RESPOND,protect_parameters.time_lime);break;
  588. case 12:Send_Flash_Set(LEFT_TRACK,RESPOND,left_trank_direction);break;
  589. case 13:Send_Flash_Set(RIGHT_TRACK,RESPOND,right_trank_direction);break;
  590. default:break;
  591. }
  592. if(Pixhawk_requre>0) Pixhawk_requre--;
  593. }
  594. void Send_Updata_requre(void)
  595. {
  596. Flash_WriteInf();
  597. switch (Updata_requre)
  598. {
  599. case TRACK_P:
  600. Send_Flash_Set(TRACK_P,RESPOND,track_PID.P);break;
  601. case TRACK_I:
  602. Send_Flash_Set(TRACK_I,RESPOND,track_PID.I);break;
  603. case TRACK_D:
  604. Send_Flash_Set(TRACK_D,RESPOND,track_PID.D);break;
  605. case LOWSPEED:
  606. Send_Flash_Set(LOWSPEED,RESPOND,protect_parameters.low_speed_mag);break;
  607. case HIGHSPEED:
  608. Send_Flash_Set(HIGHSPEED,RESPOND,protect_parameters.high_speed_mag);break;
  609. case DIVIDI_RPM:
  610. Send_Flash_Set(DIVIDI_RPM,RESPOND,protect_parameters.dividi_rpm);break;
  611. case MAXCURRENT:
  612. Send_Flash_Set(MAXCURRENT,RESPOND,protect_parameters.max_current_lime);break;
  613. case MAXTIME:
  614. Send_Flash_Set(MAXTIME,RESPOND,protect_parameters.time_lime);break;
  615. case LEFT_TRACK:
  616. Send_Flash_Set(LEFT_TRACK,RESPOND,left_trank_direction);break;
  617. case RIGHT_TRACK:
  618. Send_Flash_Set(RIGHT_TRACK,RESPOND,right_trank_direction);break;
  619. default:break;
  620. }
  621. Updata_requre = 0;
  622. }
  623. void Motor_Control(MOTOR* Motor,PID_PARAMETERS* MotorPID, u8 usePID)
  624. {
  625. int16_t Add = 0;//PID增量
  626. //int8_t Iseparat = 1;//积分分离标志位
  627. if(usePID) //使用PID
  628. {
  629. (*Motor).pid_data.new_speed = (*Motor).usart_data.rpm;//获取转速
  630. //若当前目标速度与上次的目标速度的方向不一致(即需要反转)
  631. if((*Motor).pid_data.aim_speed*(*Motor).pid_data.last_aim<0 && (*Motor).pid_data.new_speed>0)
  632. {
  633. (*Motor).pid_data.aim_speed = 0;
  634. (*Motor).pid_data.err = 0;
  635. (*Motor).pid_data.last_err = 0;
  636. (*Motor).pid_data.last_aim = 0;
  637. }
  638. else
  639. (*Motor).pid_data.last_aim = (*Motor).pid_data.aim_speed;
  640. //手动将测速反转
  641. if((*Motor).pid_data.aim_speed>0) (*Motor).pid_data.err = (*Motor).pid_data.aim_speed - (*Motor).pid_data.new_speed;
  642. else (*Motor).pid_data.err = (*Motor).pid_data.aim_speed + (*Motor).pid_data.new_speed;
  643. //if((*Motor).pid.Err > 50) Iseparat = 0;
  644. //else Iseparat = 1;
  645. //增量式PID + 积分分离
  646. Add = (*MotorPID).P*((*Motor).pid_data.err - (*Motor).pid_data.last_err) +
  647. (*MotorPID).I* (*Motor).pid_data.err +
  648. (*MotorPID).D*((*Motor).pid_data.err - (*Motor).pid_data.last_err*2 + (*Motor).pid_data.pret_err);
  649. (*Motor).pid_data.pwm += Add;
  650. //防反转 若aim为正则不输出负PWM,反之亦然
  651. if((*Motor).pid_data.aim_speed>=0) (*Motor).pid_data.pwm = max((*Motor).pid_data.pwm,0);
  652. else (*Motor).pid_data.pwm = min((*Motor).pid_data.pwm,0);
  653. //死区
  654. if((*Motor).pid_data.aim_speed<20 && (*Motor).pid_data.aim_speed > -20) //&& (*Motor).pid_data.new_speed < 30
  655. {
  656. (*Motor).pid_data.pwm = 0;
  657. (*Motor).pid_data.err = 0;
  658. (*Motor).pid_data.last_err = 0;
  659. (*Motor).pid_data.pret_err = 0;
  660. }
  661. //限幅
  662. (*Motor).pid_data.pwm = min((*Motor).pid_data.pwm, 1047);
  663. (*Motor).pid_data.pwm = max((*Motor).pid_data.pwm,-1000);
  664. if((*Motor).pid_data.pwm < 0) (*Motor).pid_data.dshot_pwm = 1047 - (*Motor).pid_data.pwm;
  665. else (*Motor).pid_data.dshot_pwm = (*Motor).pid_data.pwm;
  666. (*Motor).pid_data.pret_err = (*Motor).pid_data.last_err;
  667. (*Motor).pid_data.last_err = (*Motor).pid_data.err;
  668. }
  669. else //不使用PID
  670. {
  671. //limit Dshort by speed
  672. if((*Motor).pid_data.aim_speed >= 0)
  673. {
  674. u16 Dshort_limit = (u16)(((u32)3845*(*Motor).usart_data.rpm +(u32)1867200)/10000);
  675. if(Dshort_limit>850 ) Dshort_limit = 850;
  676. else if(Dshort_limit<320) Dshort_limit = 320;
  677. (*Motor).pid_data.dshot_limit = Dshort_limit;
  678. (*Motor).pid_data.dshot_pwm = (*Motor).pid_data.aim_speed;
  679. // if((*Motor).pid_data.dshot_pwm>=1047) (*Motor).pid_data.dshot_pwm = 1047;
  680. if((*Motor).pid_data.dshot_pwm>=Dshort_limit) (*Motor).pid_data.dshot_pwm = Dshort_limit;
  681. }
  682. else
  683. {
  684. u16 Dshort_limit = (u16)(((u32)3832*(*Motor).usart_data.rpm +(u32)1377600)/10000);//1377600
  685. if(Dshort_limit>800) Dshort_limit = 800;
  686. else if(Dshort_limit<280) Dshort_limit = 280;
  687. (*Motor).pid_data.dshot_limit = Dshort_limit;
  688. (*Motor).pid_data.dshot_pwm = 1048 - (*Motor).pid_data.aim_speed;
  689. //if((*Motor).pid_data.dshot_pwm>=(1048)) (*Motor).pid_data.dshot_pwm = 1048;
  690. if((*Motor).pid_data.dshot_pwm>=(1048+Dshort_limit)) (*Motor).pid_data.dshot_pwm = 1048+Dshort_limit;
  691. }
  692. }
  693. }
  694. void Motor_AD_correct(void)
  695. {
  696. u8 i = 0;
  697. while(i<100)
  698. {
  699. if(DMA400Finishi == 1)
  700. {
  701. i++;
  702. DMA400Finishi = 0;
  703. left_thruster.twin_data.current_correct += left_thruster.twin_data.current*0.01;
  704. right_thruster.twin_data.current_correct += right_thruster.twin_data.current*0.01;
  705. left_track_motor.twin_data.current_correct += left_track_motor.twin_data.current*0.01;
  706. right_track_motor.twin_data.current_correct += right_track_motor.twin_data.current*0.01;
  707. }
  708. }
  709. }
  710. void SoftReset(void)
  711. {
  712. __set_FAULTMASK(1); // 关闭所有中断
  713. NVIC_SystemReset(); // 复位
  714. }