it.c 9.3 KB

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