can.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "include.h"
  2. u8 CAN_DATA[8];
  3. u8 CAN_DATA1[8];
  4. u8 CAN_DATA2[8];
  5. u8 CAN_DATA3[8];
  6. u8 CAN_DATA4[8];
  7. u8 CAN_DATA5[8];
  8. u8 CAN_DATA6[8];
  9. u8 CAN_DATA_CNT;
  10. u32 Init_Flash_Data = 0x12345678;
  11. u8 CAN_RecData[CAN_RecDataMax];
  12. u8 CAN_RecData_i;
  13. u8 CAN_RecData_Toggle;
  14. u8 CAN_RecData_TID;
  15. void CAN_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef* GPIOx, u16 GPIO_Pin, u16 GPIO_PinSource, u8 GPIO_AF)
  16. {
  17. RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn, ENABLE);
  18. //初始化GPIO
  19. //引脚复用映射配置
  20. GPIO_PinAFConfig(GPIOx,GPIO_PinSource,GPIO_AF);//PA11复用为CAN1
  21. GPIO_InitTypeDef GPIO_InitStructure;
  22. GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
  23. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
  24. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  26. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
  27. GPIO_Init(GPIOx,&GPIO_InitStructure); //初始化PD0,PD1
  28. }
  29. void CAN_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
  30. {
  31. NVIC_InitTypeDef NVIC_InitStructure;
  32. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
  33. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; // 主优先级为1
  34. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; // 次优先级为0
  35. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  36. NVIC_Init(&NVIC_InitStructure);
  37. }
  38. void CAN_Mod_Config(u32 RCC_APB1Periph, CAN_TypeDef* CANx, u32 EXTID, u32 MaskID)
  39. {
  40. //u32 EXTID1 = 0X014E550A;
  41. //u32 MaskId_ID = 0XFFFF0000;
  42. RCC_APB1PeriphClockCmd(RCC_APB1Periph, ENABLE);
  43. CAN_InitTypeDef CAN_InitStructure;
  44. CAN_InitStructure.CAN_TTCM = DISABLE; //非时间触发通信模式
  45. CAN_InitStructure.CAN_ABOM = DISABLE; //软件自动离线管理
  46. CAN_InitStructure.CAN_AWUM = DISABLE; //睡眠模式通过软件唤醒
  47. CAN_InitStructure.CAN_NART = ENABLE; //禁止报文自动传送
  48. CAN_InitStructure.CAN_RFLM = DISABLE; //报文不锁定,新的覆盖旧的
  49. CAN_InitStructure.CAN_TXFP = DISABLE; //优先级由报文标识符决定
  50. CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //模式设置 CAN_Mode_Normal
  51. CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //重新同步跳跃宽度为个时间单位
  52. CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq; //时间段1占用8个时间单位
  53. CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; //时间段2占用5个时间单位
  54. CAN_InitStructure.CAN_Prescaler = 6; //分频系数(Fdiv)6 500K
  55. CAN_Init(CANx, &CAN_InitStructure); //初始化CAN1
  56. /*
  57. CAN_FilterInitTypeDef CAN_FilterInitStructure;
  58. CAN_FilterInitStructure.CAN_FilterNumber = 0;//过滤器0
  59. CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;// CAN_FilterMode_IdMask
  60. CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;//32位
  61. CAN_FilterInitStructure.CAN_FilterIdHigh = ((EXTID<<3)>>16) & 0xffff;//32位ID
  62. CAN_FilterInitStructure.CAN_FilterIdLow = ((EXTID<<3)& 0xffff) | CAN_ID_EXT;
  63. CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ((MaskID<<3)>>16) & 0xffff;//32位ID
  64. CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((MaskID<<3)& 0xffff) | CAN_ID_EXT;
  65. CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//FIFO0
  66. CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;//激活过滤器0
  67. CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
  68. */
  69. CAN_FilterInitTypeDef CAN_FilterInitStructure;
  70. CAN_FilterInitStructure.CAN_FilterNumber = 0;//过滤器0
  71. CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;// CAN_FilterMode_IdMask
  72. CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;//32位
  73. CAN_FilterInitStructure.CAN_FilterIdHigh = ((EXTID<<3)>>16) & 0xffff;//32位ID
  74. CAN_FilterInitStructure.CAN_FilterIdLow = ((EXTID<<3)& 0xffff) | CAN_ID_EXT;
  75. CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ((MaskID<<3)>>16) & 0xffff;//32位ID
  76. CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((MaskID<<3)& 0xffff) | CAN_ID_EXT;
  77. CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//FIFO0
  78. CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;//激活过滤器0
  79. CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
  80. CAN_ITConfig(CANx,CAN_IT_FMP0,ENABLE);//FIFO0 允许 消息挂号中断.
  81. }
  82. u8 CAN1_Send_Msg(u8* msg,u32 Can_ID,u8 len)
  83. {
  84. u8 mbox;
  85. u16 i = 0;
  86. CanTxMsg TxMessage;
  87. TxMessage.StdId = 0; // 标准标识符为0
  88. TxMessage.ExtId = Can_ID;//0x1f4e5301 0x1f4e5701 // 设置扩展标示符(29位)
  89. TxMessage.IDE = CAN_ID_EXT; // 使用扩展标识符
  90. TxMessage.RTR = CAN_RTR_DATA; // 消息类型为数据帧,一帧8位
  91. TxMessage.DLC = len; // 发送两帧信息
  92. for(i=0;i<len;i++)
  93. {
  94. TxMessage.Data[i] = msg[i]; // 第一帧信息
  95. }
  96. mbox = CAN_Transmit(CAN1,&TxMessage);
  97. i=0;
  98. while((CAN_TransmitStatus(CAN1,mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))
  99. {
  100. i++;//等待发送结束
  101. }
  102. if(i>=0XFFF)return 1;
  103. return 0;
  104. }
  105. u8 CAN1_Receive_Msg(u8 *buf, u32 *id)
  106. {
  107. u32 i;
  108. CanRxMsg RxMessage;
  109. if(CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //没有接收到数据,直接退出
  110. CAN_Receive(CAN1, CAN_FIFO0,&RxMessage); //读取数据
  111. *id = RxMessage.ExtId;
  112. for(i=0;i<RxMessage.DLC;i++)
  113. {
  114. buf[i]=RxMessage.Data[i];
  115. }
  116. return RxMessage.DLC;
  117. }
  118. //Flash参数发送
  119. void Send_Flash_Set(u8 num, u8 flag, float value)
  120. {
  121. int temp;
  122. CAN_DATA[0] = num; //参数标号
  123. CAN_DATA[1] = flag;//命令编号
  124. temp = (int)value;//00000123.00
  125. /*
  126. CAN_DATA[2] = (temp/10000000)<<4 | (temp%10000000)/1000000;
  127. temp = temp%1000000;//000123.00
  128. CAN_DATA[3] = (temp/100000)<<4 | (temp%100000) /10000;
  129. temp = temp%10000;//0123.00
  130. CAN_DATA[4] = (temp/1000)<<4 | (temp%1000) /100;
  131. temp = temp%100;//23.00
  132. CAN_DATA[5] = (temp/10)<<4 | (temp%10);
  133. temp = (value-(int)value)*100;
  134. CAN_DATA[6] = ((int)temp/10)<<4 | ((int)temp%10);
  135. */
  136. //12345678
  137. CAN_DATA[2] = (temp/1000000);
  138. temp = temp%1000000;
  139. CAN_DATA[3] = (temp/10000);
  140. temp = temp%10000;
  141. CAN_DATA[4] = (temp/100);
  142. temp = temp%100;
  143. CAN_DATA[5] = temp;
  144. temp = (value-(int)value)*100;
  145. CAN_DATA[6] = temp;
  146. CAN_DATA[7] = 0xC0 + CAN_DATA_CNT++;
  147. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  148. CAN1_Send_Msg(CAN_DATA, CAN_Send_Flash_ID, 8);
  149. }
  150. float Rec_Flash_Set(u8 data[])
  151. {
  152. float value = 0;
  153. value = (data[2]/16)*10000000 +
  154. (data[2]%16)*1000000 +
  155. (data[3]/16)*100000 +
  156. (data[3]%16)*10000 +
  157. (data[4]/16)*1000 +
  158. (data[4]%16)*100 +
  159. (data[5]/16)*10 +
  160. (data[5]%16) +
  161. (data[6]/16)*0.1 +
  162. (data[6]%16)*0.01 ;
  163. return value;
  164. }
  165. void Send_CAN_DATA1(void)
  166. {
  167. CAN_DATA1[0] = (u8)right_thruster.usart_data.rpm;
  168. CAN_DATA1[1] = (u8)((u16)(right_thruster.usart_data.rpm)>>8);
  169. CAN_DATA1[2] = (u8)left_thruster.usart_data.rpm;
  170. CAN_DATA1[3] = (u8)((u16)(left_thruster.usart_data.rpm)>>8);
  171. // CAN_DATA1[0] = 100;
  172. // CAN_DATA1[1] = 0;
  173. // CAN_DATA1[2] = 100;
  174. // CAN_DATA1[3] = 0;
  175. CAN_DATA1[4] = 0;
  176. CAN_DATA1[5] = 0;
  177. CAN_DATA1[6] = 0;
  178. CAN_DATA1[7] = 0xC0 + CAN_DATA_CNT++;
  179. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  180. CAN1_Send_Msg(CAN_DATA1, CAN_Send_Speed_ID, 8);
  181. }
  182. void Send_CAN_DATA2(void)
  183. {
  184. CAN_DATA2[0] = (u8)left_thruster.pid_data.dshot_pwm;
  185. CAN_DATA2[1] = (u8)(left_thruster.pid_data.dshot_pwm>>8);
  186. CAN_DATA2[2] = (u8)right_thruster.pid_data.dshot_pwm;
  187. CAN_DATA2[3] = (u8)(right_thruster.pid_data.dshot_pwm>>8);
  188. CAN_DATA2[4] = (u8)left_track_motor.pid_data.dshot_pwm;
  189. CAN_DATA2[5] = (u8)(left_track_motor.pid_data.dshot_pwm>>8);
  190. CAN_DATA2[6] = 3;
  191. CAN_DATA2[7] = 0xC0 + CAN_DATA_CNT++;
  192. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  193. CAN1_Send_Msg(CAN_DATA2, CAN_Send_Speed_ID, 8);
  194. }
  195. void Send_CAN_DATA3(void)
  196. {
  197. CAN_DATA3[0] = (u8)right_track_motor.pid_data.dshot_pwm;
  198. CAN_DATA3[1] = (u8)(right_track_motor.pid_data.dshot_pwm>>8);
  199. CAN_DATA3[2] = 0;
  200. CAN_DATA3[3] = 0;
  201. CAN_DATA3[4] = (u8)(left_track_motor.usart_data.rpm/18);//18 rear
  202. CAN_DATA3[5] = (u8)((u16)(left_track_motor.usart_data.rpm/18)>>8);//18 rear
  203. CAN_DATA3[6] = 6;
  204. CAN_DATA3[7] = 0xC0 + CAN_DATA_CNT++;
  205. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  206. CAN1_Send_Msg(CAN_DATA3, CAN_Send_Speed_ID, 8);
  207. }
  208. void Send_CAN_DATA4(void)
  209. {
  210. CAN_DATA4[0] = (u8)(right_track_motor.usart_data.rpm/18);//18 rear
  211. CAN_DATA4[1] = (u8)((u16)(right_track_motor.usart_data.rpm/18)>>8);//18 rear
  212. CAN_DATA4[2] = 0;
  213. CAN_DATA4[3] = 0;
  214. CAN_DATA4[4] = 0;
  215. CAN_DATA4[5] = 0;
  216. CAN_DATA4[6] = 9;
  217. CAN_DATA4[7] = 0xC0 + CAN_DATA_CNT++;
  218. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  219. CAN1_Send_Msg(CAN_DATA4, CAN_Send_Speed_ID, 8);
  220. }
  221. void Send_CAN_DATA5(void)
  222. {
  223. CAN_DATA5[0] = (u8)(Voltage*10);
  224. CAN_DATA5[1] = (u8)((u16)(Voltage*10)>>8);
  225. CAN_DATA5[2] = (u8)(temperature*10);
  226. CAN_DATA5[3] = (u8)((u16)(temperature*10)>>8);
  227. CAN_DATA5[4] = Motor_err_flag;
  228. CAN_DATA5[5] = Motor_err_flag>>8;
  229. CAN_DATA5[6] = 12;
  230. CAN_DATA5[7] = 0xC0 + CAN_DATA_CNT++;
  231. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  232. CAN1_Send_Msg(CAN_DATA5, CAN_Send_Speed_ID, 8);
  233. }
  234. void Send_CAN_DATA6(void)
  235. {
  236. CAN_DATA6[0] = (u8)light_pwm;
  237. CAN_DATA6[1] = (u8)(light_pwm>>8);
  238. CAN_DATA6[2] = usbl_opendown;
  239. CAN_DATA6[3] = 0;
  240. if(light_err>0) CAN_DATA6[4] = 1;
  241. else CAN_DATA6[4] = 0;
  242. CAN_DATA6[5] = humidity_err;
  243. CAN_DATA6[6] = 15;
  244. CAN_DATA6[7] = 0xC0 + CAN_DATA_CNT++;
  245. if(CAN_DATA_CNT>=32) CAN_DATA_CNT = 0;
  246. CAN1_Send_Msg(CAN_DATA6, CAN_Send_Speed_ID, 8);
  247. }
  248. void Send_CAN_MOTOR1DATA(void)
  249. {
  250. static u8 tail1 = 0;
  251. hv_motor1_data[7] = tail1 + 0xC0;
  252. tail1++;
  253. if(tail1>31) tail1 = 0;
  254. CAN1_Send_Msg(hv_motor1_data, 0x014E650B, 8);
  255. }
  256. void Send_CAN_MOTOR2DATA(void)
  257. {
  258. static u8 tail2 = 0;
  259. hv_motor2_data[7] = tail2 + 0xC0;
  260. tail2++;
  261. if(tail2>31) tail2 = 0;
  262. CAN1_Send_Msg(hv_motor2_data, 0x014E660C, 8);
  263. }
  264. void Send_CAN_MOTOR3DATA(void)
  265. {
  266. static u8 tail3 = 0;
  267. hv_motor3_data[7] = tail3 + 0xC0;
  268. tail3++;
  269. if(tail3>31) tail3 = 0;
  270. CAN1_Send_Msg(hv_motor3_data, 0x014E670D, 8);
  271. }