it.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "include.h"
  2. u8 hv_motor1_data[8];
  3. u8 hv_motor2_data[8];
  4. u8 hv_motor3_data[8];
  5. void SysTick_Handler(void)
  6. {
  7. sysTickUptime++;
  8. }
  9. void TIM1_UP_TIM10_IRQHandler(void)
  10. {
  11. if(TIM_GetITStatus(TIM1, TIM_IT_Update)) //判断发生update事件中断
  12. {
  13. TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除update事件中断标志
  14. //放Dshot函数
  15. static u8 count = 0;
  16. if(count++>=4) count = 0;
  17. switch(count)
  18. {
  19. case 0:
  20. RThruster_DSHOT_DMA_Config();
  21. pwmWriteDigital(right_thruster.pid_data.dshot_data, right_thruster.pid_data.dshot_pwm, DMA1_Stream0);
  22. break;
  23. case 1:
  24. LThruster_DSHOT_DMA_Config();
  25. pwmWriteDigital(left_thruster.pid_data.dshot_data, left_thruster.pid_data.dshot_pwm, DMA1_Stream5);
  26. break;
  27. case 2:
  28. RTrack_DSHOT_DMA_Config();
  29. pwmWriteDigital(right_track_motor.pid_data.dshot_data, right_track_motor.pid_data.dshot_pwm, DMA1_Stream3);
  30. break;
  31. case 3:
  32. LTrack_DSHOT_DMA_Config();
  33. pwmWriteDigital(left_track_motor.pid_data.dshot_data, left_track_motor.pid_data.dshot_pwm, DMA1_Stream5);
  34. break;
  35. default:break;
  36. }
  37. }
  38. }
  39. void ResearchProtection(MOTOR* motor, s16 aimSpeed)
  40. {
  41. if((*motor).pid_data.last_aim<0 && aimSpeed>=0)
  42. {
  43. (*motor).pid_data.aim_speed += 2;
  44. }
  45. else
  46. {
  47. (*motor).pid_data.aim_speed = aimSpeed;
  48. }
  49. (*motor).pid_data.last_aim = (*motor).pid_data.aim_speed;
  50. }
  51. int maxcnt = 0;
  52. void CAN1_RX0_IRQHandler(void)
  53. {
  54. u32 id = 0;
  55. s16 value = 0;
  56. u8 RecData[8];
  57. u8 RecNum;
  58. u8 i=0;
  59. RecNum = CAN1_Receive_Msg(RecData,&id);
  60. if(id == 0X014E570A)//4E59
  61. {
  62. if(RecData[1]==PIXHAWK_REQURE)
  63. {
  64. Pixhawk_requre = 13;
  65. }
  66. else if(RecData[1]==SET_PARME)
  67. {
  68. switch(RecData[0])
  69. {
  70. case TRACK_P: track_PID.P = Rec_Flash_Set(RecData);break;
  71. case TRACK_I: track_PID.I = Rec_Flash_Set(RecData);break;
  72. case TRACK_D: track_PID.D = Rec_Flash_Set(RecData);break;
  73. case LOWSPEED: protect_parameters.low_speed_mag = Rec_Flash_Set(RecData);break;
  74. case HIGHSPEED: protect_parameters.high_speed_mag = Rec_Flash_Set(RecData);break;
  75. case DIVIDI_RPM: protect_parameters.dividi_rpm = Rec_Flash_Set(RecData);break;
  76. case MAXCURRENT: protect_parameters.max_current_lime = Rec_Flash_Set(RecData);break;
  77. case MAXTIME: protect_parameters.time_lime = Rec_Flash_Set(RecData);break;
  78. case REST_STM32: SoftReset();break;
  79. case LEFT_TRACK: left_trank_direction = Rec_Flash_Set(RecData);break;
  80. case RIGHT_TRACK: right_trank_direction = Rec_Flash_Set(RecData);break;
  81. // Single_Motor_Config(&left_thruster,1);
  82. // Single_Motor_Config(&right_thruster,2);
  83. // Single_Motor_Config(&left_track_motor,9);
  84. // Single_Motor_Config(&right_track_motor,10);
  85. break;
  86. default:break;
  87. }
  88. Updata_requre = RecData[0];
  89. }
  90. }
  91. else if(id == 0X014E550A)
  92. {
  93. //分段数据包首帧
  94. if((RecData[RecNum-1]>>7)&1)
  95. {
  96. i = 2;
  97. CAN_RecData_i = 0;
  98. CAN_RecData_Toggle = 0;
  99. CAN_RecData_TID = RecData[RecNum-1]&0x1F;
  100. }
  101. if(((RecData[RecNum-1]&0x1F) == CAN_RecData_TID) && //TransferID正确
  102. (((RecData[RecNum-1]>>5)&1) == CAN_RecData_Toggle) ) //反转位正确
  103. {
  104. CAN_RecData_Toggle = (CAN_RecData_Toggle==0)?1:0;
  105. for(;i<RecNum-1;i++)
  106. {
  107. if(CAN_RecData_i>maxcnt) maxcnt = CAN_RecData_i;
  108. if(CAN_RecData_i > CAN_RecDataMax)
  109. {
  110. //出现错误,舍弃该包
  111. RecData[RecNum-1] = 0;
  112. CAN_RecData_i = 0;
  113. CAN_RecData_Toggle = 0;
  114. break;
  115. }
  116. CAN_RecData[CAN_RecData_i++] = RecData[i];
  117. }
  118. }
  119. //分段数据包尾帧
  120. if(((RecData[RecNum-1]>>6)&1)==1 && RecNum == 8)
  121. {
  122. if(CAN_RecData_i == CAN_RecDataMax) //成功接收全部数据,赋值
  123. {
  124. IWDG_Feed();
  125. //左履带
  126. if(left_trank_direction == 0)
  127. value = (CAN_RecData[5]<<8|CAN_RecData[4]);
  128. else
  129. value = -1*(CAN_RecData[5]<<8|CAN_RecData[4]);
  130. //赋值
  131. if(left_track_motor.stal_data.stop_flag == 0)
  132. {
  133. ResearchProtection(&left_track_motor, value);
  134. }
  135. //右履带
  136. if(right_trank_direction == 0)
  137. value = (CAN_RecData[7]<<8|CAN_RecData[6]);
  138. else
  139. value = -1*(CAN_RecData[7]<<8|CAN_RecData[6]);
  140. //赋值
  141. if(right_track_motor.stal_data.stop_flag == 0)
  142. {
  143. ResearchProtection(&right_track_motor, value);
  144. }
  145. //2#
  146. value = CAN_RecData[3]<<8|CAN_RecData[2];
  147. if(value<=1000 && value>=-1000 && left_thruster.stal_data.stop_flag == 0) //motor2.pid_data.aim_speed = value;
  148. {
  149. ResearchProtection(&left_thruster, value);
  150. left_thruster.pid_data.dshot_pwm = GetDShotValue(left_thruster.pid_data.aim_speed, left_thruster.twin_data.speed_lim);
  151. }
  152. //1#
  153. value = CAN_RecData[1]<<8|CAN_RecData[0];
  154. if(value<=1000 && value>=-1000 && right_thruster.stal_data.stop_flag == 0)// motor1.pid_data.aim_speed = value;
  155. {
  156. ResearchProtection(&right_thruster, value);
  157. right_thruster.pid_data.dshot_pwm = GetDShotValue(right_thruster.pid_data.aim_speed, right_thruster.twin_data.speed_lim);
  158. }
  159. light_pwm = (CAN_RecData[9]<<8)|CAN_RecData[8];//灯 PWM
  160. if(light_err>0){
  161. if(( Read_LIGHT) && light_pwm == 0)
  162. {
  163. light_err--;
  164. light_pwm = 0;
  165. }
  166. }
  167. usbl_opendown = (CAN_RecData[11]<<8)|CAN_RecData[10];//usbl
  168. //照明灯启动
  169. //light_pwm = 1;
  170. }
  171. CAN_RecData_i = 0;
  172. CAN_RecData_Toggle = 0;
  173. }
  174. }
  175. /*
  176. 转发比弗迪电调信息
  177. else if(id == 0x014E600B)
  178. {
  179. for(j=0; j<8; j++)
  180. {
  181. hv_motor1_data[j] = RecData[j];
  182. }
  183. }
  184. else if(id == 0x014E610C)
  185. {
  186. for(j=0; j<8; j++)
  187. {
  188. hv_motor2_data[j] = RecData[j];
  189. }
  190. }
  191. else if(id == 0x014E620D)
  192. {
  193. for(j=0; j<8; j++)
  194. {
  195. hv_motor3_data[j] = RecData[j];
  196. }
  197. }
  198. */
  199. }
  200. //左履带
  201. void USART2_IRQHandler(void)
  202. {
  203. if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
  204. {
  205. GetMotorData(USART2, &left_track_motor);
  206. USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  207. }
  208. else if(USART_GetITStatus(USART2,USART_IT_IDLE)!=RESET)
  209. {
  210. USART2->SR;
  211. USART2->DR;
  212. AnaMotorData(&left_track_motor);
  213. }
  214. }
  215. //左推进器
  216. void USART3_IRQHandler(void)
  217. {
  218. if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)
  219. {
  220. GetMotorData(USART3, &left_thruster);
  221. USART_ClearITPendingBit(USART3, USART_IT_RXNE);
  222. }
  223. else if(USART_GetITStatus(USART3,USART_IT_IDLE)!=RESET)
  224. {
  225. USART3->SR;
  226. USART3->DR;
  227. AnaMotorData(&left_thruster);
  228. }
  229. }
  230. //右推进器
  231. void UART5_IRQHandler(void)
  232. {
  233. if(USART_GetITStatus(UART5,USART_IT_RXNE)!=RESET)
  234. {
  235. GetMotorData(UART5, &right_thruster);
  236. USART_ClearITPendingBit(UART5, USART_IT_RXNE);
  237. }
  238. else if(USART_GetITStatus(UART5,USART_IT_IDLE)!=RESET)
  239. {
  240. UART5->SR;
  241. UART5->DR;
  242. AnaMotorData(&right_thruster);
  243. }
  244. }
  245. //右履带
  246. void USART6_IRQHandler(void)
  247. {
  248. if(USART_GetITStatus(USART6,USART_IT_RXNE)!=RESET)
  249. {
  250. GetMotorData(USART6, &right_track_motor);
  251. USART_ClearITPendingBit(USART6, USART_IT_RXNE);
  252. }
  253. else if(USART_GetITStatus(USART6,USART_IT_IDLE)!=RESET)
  254. {
  255. USART6->SR;
  256. USART6->DR;
  257. AnaMotorData(&right_track_motor);
  258. }
  259. }
  260. void DMA1_Stream0_IRQHandler(void)
  261. {
  262. if(DMA_GetITStatus(DMA1_Stream0, DMA_IT_TCIF0)) //判断DMA传输完成中断
  263. {
  264. DMA_ClearITPendingBit(DMA1_Stream0, DMA_IT_TCIF0);
  265. DMA_Cmd(DMA1_Stream0, DISABLE);
  266. }
  267. }
  268. void DMA1_Stream3_IRQHandler(void)
  269. {
  270. if(DMA_GetITStatus(DMA1_Stream3, DMA_IT_TCIF3)) //判断DMA传输完成中断
  271. {
  272. DMA_ClearITPendingBit(DMA1_Stream3, DMA_IT_TCIF3);
  273. DMA_Cmd(DMA1_Stream3, DISABLE);
  274. }
  275. }
  276. void DMA1_Stream5_IRQHandler(void)
  277. {
  278. if(DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5)) //判断DMA传输完成中断
  279. {
  280. DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
  281. DMA_Cmd(DMA1_Stream5, DISABLE);
  282. }
  283. }
  284. void DMA2_Stream0_IRQHandler(void)
  285. {
  286. static u16 AVG_i=0;
  287. static u16 CurrentBuffPtr[6];
  288. if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0)) //判断DMA传输完成中断
  289. {
  290. DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
  291. //DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA,连续模式下不需要该使能
  292. //数据转移程序
  293. if(DMA_GetCurrentMemoryTarget(DMA2_Stream0) == DMA_Memory_0)
  294. {
  295. CurrentBuffPtr[0] = uAD_Buff_A[0];
  296. CurrentBuffPtr[1] = uAD_Buff_A[1];
  297. //CurrentBuffPtr[2] = uAD_Buff_A[2];
  298. /*
  299. CurrentBuffPtr[3] = uAD_Buff_A[3];
  300. CurrentBuffPtr[4] = uAD_Buff_A[4];
  301. CurrentBuffPtr[5] = uAD_Buff_A[5];
  302. */
  303. }
  304. else
  305. {
  306. CurrentBuffPtr[0] = uAD_Buff_B[0];
  307. CurrentBuffPtr[1] = uAD_Buff_B[1];
  308. //CurrentBuffPtr[2] = uAD_Buff_B[2];
  309. /*
  310. CurrentBuffPtr[3] = uAD_Buff_B[3];
  311. CurrentBuffPtr[4] = uAD_Buff_B[4];
  312. CurrentBuffPtr[5] = uAD_Buff_B[5];
  313. */
  314. }
  315. AVG_i++;
  316. //CurrentBuffPtr[0] = 4095;
  317. /*
  318. left_thruster.twin_data.avg_sum += CurrentBuffPtr[1];//0
  319. right_thruster.twin_data.avg_sum += CurrentBuffPtr[3];//1
  320. left_track_motor.twin_data.avg_sum += CurrentBuffPtr[0];//2
  321. right_track_motor.twin_data.avg_sum += CurrentBuffPtr[2];//3
  322. */
  323. Voltage_sum += CurrentBuffPtr[0];
  324. NTC_ADC_VALUE_SUM += CurrentBuffPtr[1];
  325. //humidity_sum += CurrentBuffPtr[2];
  326. if(AVG_i >= 400)
  327. {
  328. DMA400Finishi = 1;
  329. AVG_i = 0;
  330. /*
  331. left_thruster.twin_data.current = (float)left_thruster.twin_data.avg_sum*0.00004f;
  332. right_thruster.twin_data.current = (float)right_thruster.twin_data.avg_sum*0.00004f;
  333. left_track_motor.twin_data.current = (float)left_track_motor.twin_data.avg_sum*0.00004f;
  334. right_track_motor.twin_data.current = (float)right_track_motor.twin_data.avg_sum*0.00004f;
  335. left_thruster.twin_data.avg_sum = 0;
  336. right_thruster.twin_data.avg_sum = 0;
  337. left_track_motor.twin_data.avg_sum = 0;
  338. right_track_motor.twin_data.avg_sum = 0;
  339. */
  340. Voltage = ((float)Voltage_sum)*3.27856/102400;
  341. Voltage_sum = 0;
  342. temperature = Get_Temperature(NTC_ADC_VALUE_SUM/400);
  343. NTC_ADC_VALUE_SUM = 0;
  344. // humidity = humidity_sum/400;
  345. // humidity_sum = 0;
  346. // if(humidity > 3000)
  347. // {
  348. // humidity_err = 1;
  349. // }
  350. // else
  351. // {
  352. // humidity_err = 0;
  353. // }
  354. }
  355. }
  356. }
  357. void HardFault_Handler(void)
  358. {
  359. /* Go to infinite loop when Hard Fault exception occurs */
  360. while (1)
  361. {
  362. }
  363. }