UserCan.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * UserCan.c
  3. *
  4. * Created on: 2023年1月16日
  5. * Author: senco
  6. */
  7. #include "var.h" // DSP2803x Examples Include File
  8. #include "UserCan.h" // DSP2803x Headerfile Include File
  9. //#include "DSP2803x_Device.h" // DSP28 Headerfile Include File
  10. #include "DSP2803x_Examples.h" // DSP28 Examples Include File
  11. #define Reverse 0
  12. #define Normal 1
  13. // Global variable for this example
  14. #define TX_PERIOD 20 //ms
  15. //绘制曲线
  16. #pragma DATA_SECTION(CanCurveData,"MemoryFile");
  17. int16 CanCurveData[0x400];
  18. int16 CanCurveFlag = 0;
  19. int16 CanDebugFlag = 1;
  20. struct CANMBOX_USER_REGS CanMboxUserRegsT; //用户CAN数据发送结构体
  21. struct CANMBOX_USER_REGS CanMboxUserRegsT2; //用户CAN数据发送结构体
  22. struct CANMBOX_USER_REGS CanMboxUserRegsR; //用户CAN接收数据结构体1
  23. struct CANMBOX_USER_REGS CanMboxUserRegsR2; //用户CAN接收数据结构体2
  24. struct CANMBOX_USER_REGS CanMboxUserRegsR3; //用户CAN接收数据结构体2
  25. #ifdef MOTORLEFT
  26. #define ID_MOTOR_TIMERBACK 0x014E6909
  27. #define ID_MOTOR_PARBACK 0X014E6709
  28. #else
  29. #define ID_MOTOR_TIMERBACK 0x014E6A08
  30. #define ID_MOTOR_PARBACK 0X014E6808
  31. #endif
  32. #define ID_MOTOR1_COMMAND 0x014E540A
  33. #define ID_MOTOR2_COMMAND 0x014E560A
  34. #define ID_MOTOR3_COMMAND 0x014E5A0A
  35. #define ID_FAULT_CLEAR 0x014E510A
  36. #define ID_MOTOR1_SET 0x014E5C0A
  37. Uint32 motor_command[]={ID_MOTOR1_COMMAND,ID_MOTOR2_COMMAND,ID_MOTOR3_COMMAND};
  38. void CanVar(void){
  39. CanMboxUserRegsR.MboxRxFlg = 0;
  40. CanMboxUserRegsR.MboxNum = 0;
  41. CanMboxUserRegsR.MboxDLC = 0;
  42. CanMboxUserRegsR.MboxID = 0;
  43. CanMboxUserRegsR.MboxWord1 = 0;
  44. CanMboxUserRegsR.MboxWord2 = 0;
  45. CanMboxUserRegsR.MboxWord3 = 0;
  46. CanMboxUserRegsR.MboxWord4 = 0;
  47. CanMboxUserRegsR2.MboxRxFlg = 0;
  48. CanMboxUserRegsR2.MboxNum = 0;
  49. CanMboxUserRegsR2.MboxDLC = 0;
  50. CanMboxUserRegsR2.MboxID = 0;
  51. CanMboxUserRegsR2.MboxWord1 = 0;
  52. CanMboxUserRegsR2.MboxWord2 = 0;
  53. CanMboxUserRegsR2.MboxWord3 = 0;
  54. CanMboxUserRegsR2.MboxWord4 = 0;
  55. CanMboxUserRegsR3.MboxRxFlg = 0;
  56. CanMboxUserRegsR3.MboxNum = 0;
  57. CanMboxUserRegsR3.MboxDLC = 0;
  58. CanMboxUserRegsR3.MboxID = 0;
  59. CanMboxUserRegsR3.MboxWord1 = 0;
  60. CanMboxUserRegsR3.MboxWord2 = 0;
  61. CanMboxUserRegsR3.MboxWord3 = 0;
  62. CanMboxUserRegsR3.MboxWord4 = 0;
  63. CanMboxUserRegsT.MboxRxFlg = 0;
  64. CanMboxUserRegsT.MboxNum = 0;
  65. CanMboxUserRegsT.MboxDLC = 0;
  66. CanMboxUserRegsT.MboxID = 0;
  67. CanMboxUserRegsT.MboxWord1 = 0;
  68. CanMboxUserRegsT.MboxWord2 = 0;
  69. CanMboxUserRegsT.MboxWord3 = 0;
  70. CanMboxUserRegsT.MboxWord4 = 0;
  71. }
  72. void InitCan(void){
  73. struct ECAN_REGS ECanaShadow;
  74. InitECanaGpio();//引脚初始化
  75. //设置波特率
  76. InitECana(); // Initialize eCAN-A module
  77. // Mailboxes can be written to 16-bits or 32-bits at a time
  78. // Write to the MSGID field of TRANSMIT mailboxes MBOX0 - 15
  79. ECanaMboxes.MBOX0.MSGID.all = ID_MOTOR_TIMERBACK|EXTFRAME;//定时上传
  80. ECanaMboxes.MBOX1.MSGID.all = ID_MOTOR_TIMERBACK|EXTFRAME;//定时上传
  81. ECanaMboxes.MBOX2.MSGID.all = ID_MOTOR_TIMERBACK|EXTFRAME;//定时上传
  82. ECanaMboxes.MBOX3.MSGID.all = ID_MOTOR_PARBACK|EXTFRAME;//飞控参数返回
  83. ECanaMboxes.MBOX4.MSGID.all = ID_MOTOR_PARBACK|EXTFRAME;//飞控参数返回
  84. ECanaMboxes.MBOX5.MSGID.all = 0x9555AAA5;//调试参数返回1 给定速度 目标速度 反馈速度 正反转
  85. ECanaMboxes.MBOX6.MSGID.all = 0x9555AAA6;//调试参数返回2 速度PID输出 运行占空比
  86. ECanaMboxes.MBOX7.MSGID.all = 0x9555AAA7;//调试参数返回3
  87. ECanaMboxes.MBOX8.MSGID.all = 0x9555AAA8;//调试参数返回4
  88. ECanaMboxes.MBOX9.MSGID.all = 0x9555AAA9;//调试参数返回5
  89. ECanaMboxes.MBOX10.MSGID.all = 0x9555AAAA;
  90. ECanaMboxes.MBOX11.MSGID.all = 0x9555AAAB;
  91. ECanaMboxes.MBOX12.MSGID.all = 0x9555AAAC;
  92. ECanaMboxes.MBOX13.MSGID.all = 0x9555AAAD;
  93. ECanaMboxes.MBOX14.MSGID.all = 0x9555AAAE;
  94. ECanaMboxes.MBOX15.MSGID.all = 0x9555AAAF;
  95. // Write to the MSGID field of RECEIVE mailboxes MBOX16 - 31
  96. ECanaMboxes.MBOX16.MSGID.all = 0xC0000000;//014E540A esc1 esc2
  97. ECanaMboxes.MBOX17.MSGID.all = 0x814E540A;
  98. ECanaMboxes.MBOX18.MSGID.all = 0x814E560A;
  99. ECanaMboxes.MBOX19.MSGID.all = 0x814E5A0A;
  100. ECanaMboxes.MBOX20.MSGID.all = 0x814E5C0A;
  101. ECanaMboxes.MBOX21.MSGID.all = 0x814E510A;//故障消除
  102. ECanaMboxes.MBOX22.MSGID.all = 0xC0000000;
  103. ECanaMboxes.MBOX23.MSGID.all = 0xC0000000;
  104. ECanaMboxes.MBOX24.MSGID.all = 0x9555AAA8;
  105. ECanaMboxes.MBOX25.MSGID.all = 0x9555AAA9;
  106. ECanaMboxes.MBOX26.MSGID.all = 0x9555AAAA;
  107. ECanaMboxes.MBOX27.MSGID.all = 0x9555AAAB;
  108. ECanaMboxes.MBOX28.MSGID.all = 0x9555AAAC;
  109. ECanaMboxes.MBOX29.MSGID.all = 0x9555AAAD;
  110. ECanaMboxes.MBOX30.MSGID.all = 0x9555AAAE;
  111. ECanaMboxes.MBOX31.MSGID.all = 0x9555AAAF;
  112. // Configure Mailboxes 0-15 as Tx, 16-31 as Rx
  113. // Since this write is to the entire register (instead of a bit
  114. // field) a shadow register is not required.
  115. ECanaRegs.CANMD.all = 0xFFFF0000;// 0 transmit or 1 receive
  116. // Enable all Mailboxes
  117. // Since this write is to the entire register (instead of a bit
  118. // field) a shadow register is not required.
  119. /* ECanaLAMRegs.LAM20.all = 0x9FFFFFFF;//20号邮箱不在乎ID号
  120. // ECanaLAMRegs.LAM21.all = 0x9FFFFFFF;//21号邮箱不在乎ID号,当没有设置优先级的时候高位邮箱优先接收
  121. //ECanaRegs.CANME.all = 0x000FC3FF;//16-19号接收邮箱必须完全等于ID才能接收
  122. ECanaRegs.CANME.all = 0x001FC3FF;//由于设置20号邮箱不在乎ID,接收信息优先进入20号邮箱*/
  123. ECanaLAMRegs.LAM16.all = 0x9FFFFFFF;//16号邮箱不在乎ID号 不在乎ID的放在低位
  124. //20 19-16 15-11 11-8 8-4 3-0
  125. //11 1111 1100 0011 1111 1111
  126. ECanaRegs.CANME.all = 0x003FC3FF;//1 Mailbox enable bits 0-4 飞控协议 5-9用于发送调试上位机代码 14-15 用于发送绘图数据
  127. //17-20 用于飞控格式接收 16 用于调试上位机数据接收
  128. //ECanaRegs.CANME.all = 0x0001C7E0;//屏蔽飞控相关传输
  129. // Specify that 8 bits will be sent/received
  130. ECanaMboxes.MBOX0.MSGCTRL.bit.DLC = 8;
  131. ECanaMboxes.MBOX1.MSGCTRL.bit.DLC = 8;
  132. ECanaMboxes.MBOX2.MSGCTRL.bit.DLC = 8;
  133. ECanaMboxes.MBOX3.MSGCTRL.bit.DLC = 8;
  134. ECanaMboxes.MBOX4.MSGCTRL.bit.DLC = 8;
  135. ECanaMboxes.MBOX5.MSGCTRL.bit.DLC = 8;
  136. ECanaMboxes.MBOX6.MSGCTRL.bit.DLC = 8;
  137. ECanaMboxes.MBOX7.MSGCTRL.bit.DLC = 8;
  138. ECanaMboxes.MBOX8.MSGCTRL.bit.DLC = 8;
  139. ECanaMboxes.MBOX9.MSGCTRL.bit.DLC = 8;
  140. ECanaMboxes.MBOX10.MSGCTRL.bit.DLC = 8;
  141. ECanaMboxes.MBOX11.MSGCTRL.bit.DLC = 8;
  142. ECanaMboxes.MBOX12.MSGCTRL.bit.DLC = 8;
  143. ECanaMboxes.MBOX13.MSGCTRL.bit.DLC = 8;
  144. ECanaMboxes.MBOX14.MSGCTRL.bit.DLC = 8;
  145. ECanaMboxes.MBOX15.MSGCTRL.bit.DLC = 8;
  146. ECanaMboxes.MBOX16.MSGCTRL.all = 0x00000008;
  147. ECanaMboxes.MBOX17.MSGCTRL.all = 0x00000008;
  148. ECanaMboxes.MBOX18.MSGCTRL.all = 0x00000008;
  149. ECanaMboxes.MBOX19.MSGCTRL.all = 0x00000008;
  150. ECanaMboxes.MBOX20.MSGCTRL.all = 0x00000008;
  151. ECanaMboxes.MBOX21.MSGCTRL.all = 0x00000008;
  152. // Write to the mailbox RAM field of MBOX0 - 15
  153. // Message Data Registers MBOX0.MDL低字节和MBOX0.MDH高字节
  154. // Since this write is to the entire register (instead of a bit
  155. // field) a shadow register is not required.
  156. EALLOW;
  157. ECanaRegs.CANMIM.all = 0xFFFFFFFF;//中断使能
  158. // Configure the eCAN for self test mode
  159. // Enable the enhanced features of the eCAN.
  160. EALLOW;
  161. ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
  162. ECanaShadow.CANMC.bit.STM = 0; // Configure CAN for self-test mode //1回环模式
  163. ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
  164. EDIS;
  165. CanVar();//数据初始化
  166. }
  167. void ECanMboxSend(struct CANMBOX_USER_REGS *pRegs)
  168. {// Configure Mailboxes 0-15 as Tx, 16-31 as Rx
  169. struct ECAN_REGS ECanaShadow;
  170. volatile struct MBOX *Mailbox;
  171. ECanaShadow.CANME.all = ECanaRegs.CANME.all;
  172. ECanaShadow.CANME.all &= ~((Uint32)1 << pRegs->MboxNum);//DISABLE
  173. ECanaRegs.CANME.all = ECanaShadow.CANME.all;
  174. Mailbox = &ECanaMboxes.MBOX0 + pRegs->MboxNum;
  175. Mailbox->MSGID.all = pRegs->MboxID | 0x80000000; //扩展帧
  176. // Mailbox->MSGID.all = pRegs->MboxID << 18; //标准帧
  177. Mailbox->MDL.word.LOW_WORD = pRegs->MboxWord1;
  178. Mailbox->MDL.word.HI_WORD = pRegs->MboxWord2;
  179. Mailbox->MDH.word.LOW_WORD = pRegs->MboxWord3;
  180. Mailbox->MDH.word.HI_WORD = pRegs->MboxWord4;
  181. Mailbox->MSGCTRL.all = pRegs->MboxDLC;
  182. ECanaShadow.CANME.all = ECanaRegs.CANME.all;
  183. ECanaShadow.CANME.all |= (Uint32)1 << pRegs->MboxNum;//ENABLE
  184. ECanaRegs.CANME.all = ECanaShadow.CANME.all;
  185. ECanaRegs.CANTRS.all = (Uint32)1 << pRegs->MboxNum;
  186. ECanaRegs.CANTA.all = (Uint32)1 << pRegs->MboxNum;
  187. }
  188. // MBXnbr 16-31
  189. struct CANMBOX_USER_REGS ECanMboxRead(int16 MBXnbr)
  190. {//Mailboxes 0-15 as Tx, 16-31 as Rx
  191. struct CANMBOX_USER_REGS CanMboxUserRegs;
  192. volatile struct MBOX *Mailbox;
  193. Mailbox = &ECanaMboxes.MBOX0 + MBXnbr;
  194. if ((ECanaRegs.CANRMP.all & ((Uint32)1<<MBXnbr))!=0 ) //if (ECanaRegs.CANRMP.bit.RMP20 ==1 )
  195. {
  196. CanMboxUserRegs.MboxRxFlg = TRUE;
  197. CanMboxUserRegs.MboxNum = MBXnbr;
  198. CanMboxUserRegs.MboxID = Mailbox->MSGID.all & 0x1FFFFFFF; //扩展帧
  199. CanMboxUserRegs.MboxDLC = Mailbox->MSGCTRL.bit.DLC;
  200. CanMboxUserRegs.MboxWord1 = Mailbox->MDL.word.LOW_WORD;
  201. CanMboxUserRegs.MboxWord2 = Mailbox->MDL.word.HI_WORD;
  202. CanMboxUserRegs.MboxWord3 = Mailbox->MDH.word.LOW_WORD;
  203. CanMboxUserRegs.MboxWord4 = Mailbox->MDH.word.HI_WORD;
  204. ECanaRegs.CANRMP.all = ECanaRegs.CANRMP.all|((Uint32)1<<MBXnbr);//写1 clear
  205. }
  206. else
  207. {
  208. CanMboxUserRegs.MboxRxFlg = FALSE;
  209. }
  210. return CanMboxUserRegs;
  211. }
  212. void CanDebugTx(void)
  213. {// Configure Mailboxes 0-15 as Tx, 16-31 as Rx
  214. static int16 TxCnt = 0;
  215. static int16 TxPeriod = 0;
  216. static int16 step = 0;
  217. #ifdef DEBUG_CAN
  218. static int16 step2 = 0;
  219. #endif
  220. TxCnt++;
  221. if (TxCnt >= TxPeriod)
  222. {
  223. TxPeriod = TX_PERIOD*MAINLOOPRATE;// =TX_PERIOD/1000/(1/mainloop) mainloop = 8k
  224. TxCnt = 0;
  225. step++;
  226. #ifdef DEBUG_CAN
  227. step2++;
  228. #endif
  229. #ifdef PIXHAWK
  230. static Uint16 tail=0;
  231. if(step ==1){
  232. // //ID Byte0 Byte1 Byte2 Byte3 Byte4 Byte5 Byte6 Tail
  233. // 电调ID 0X30|电调编号 母线电压L 母线电压H 当前速度L 当前速度H 0 0 见附表
  234. CanMboxUserRegsT.MboxID = ID_MOTOR_TIMERBACK;
  235. CanMboxUserRegsT.MboxNum = 0;
  236. CanMboxUserRegsT.MboxDLC = 8;
  237. CanMboxUserRegsT.MboxWord1 = ((int16)0x30|ESC_NUMBER ) + (((int16)DCbus_voltage & 0x00FF)<<8);
  238. CanMboxUserRegsT.MboxWord2 = (((int16)DCbus_voltage & 0xFF00)>>8) + (((int16)speed1.SpeedRpm& 0x00FF)<<8);
  239. CanMboxUserRegsT.MboxWord3 = ((int16)speed1.SpeedRpm & 0xFF00)>>8;
  240. tail = (tail++ >=31)? 0 : tail;
  241. CanMboxUserRegsT.MboxWord4 = ((int16)(0xC0+tail)&0x00FF)<<8;
  242. ECanMboxSend(&CanMboxUserRegsT);
  243. }else if(step ==2){
  244. // Byte0 Byte1 Byte2 Byte3 Byte4 Byte5 Byte6 Tail
  245. // 0X50|电调编号 故障 故障 故障 故障 给定速度L 给定速度H 见附表
  246. CanMboxUserRegsT.MboxID = ID_MOTOR_TIMERBACK;
  247. CanMboxUserRegsT.MboxNum = 1;
  248. CanMboxUserRegsT.MboxDLC = 8;
  249. CanMboxUserRegsT.MboxWord1 = ((int16)0x50|ESC_NUMBER) + (((int16)FaultFlag.all & 0x00FF)<<8);
  250. CanMboxUserRegsT.MboxWord2 = (((int16)FaultFlag.all & 0xFF00)>>8) + 0;
  251. CanMboxUserRegsT.MboxWord3 = (int16)0 + (((int16)(_IQtoIQ12(SpeedRef)*speed1.BaseRpm>>12 )& 0x00FF)<<8);
  252. tail = (tail++ >=31)? 0 : tail;
  253. CanMboxUserRegsT.MboxWord4 = (((int16)(_IQtoIQ12(SpeedRef)*speed1.BaseRpm>>12 ) & 0xFF00)>>8)+ (((int16)(0xC0 + tail)&0x00FF)<<8);
  254. ECanMboxSend(&CanMboxUserRegsT);
  255. }else if(step ==3){
  256. step = 0;
  257. // Byte0 Byte1 Byte2 Byte3 Byte4 Byte5 Byte6 Tail
  258. // 0XA0|电调编号 扭矩电流L 扭矩电流H 电调温度L 电调温度H 电机温度L 电机温度H 见附表
  259. CanMboxUserRegsT.MboxID = ID_MOTOR_TIMERBACK;
  260. CanMboxUserRegsT.MboxNum = 2;
  261. CanMboxUserRegsT.MboxDLC = 8;
  262. CanMboxUserRegsT.MboxWord1 = (((int16)0xA0|ESC_NUMBER)) + (((int16)((DC_current_filter_avr*1000)>>12)&0x00FF)<<8);
  263. CanMboxUserRegsT.MboxWord2 = (((int16)((DC_current_filter_avr*1000)>>12)&0xFF00)>>8) + ((Tvot&0x00FF)<<8);
  264. CanMboxUserRegsT.MboxWord3 = (((int16)Tvot&0xFF00)>>8) + (((int16)Tmotor&0x00FF)<<8);//
  265. tail = (tail++ >=31)? 0 : tail;
  266. CanMboxUserRegsT.MboxWord4 = (((int16)Tmotor&0xFF00)>>8) + (((int16)(0xC0+tail)&0x00FF)<<8);
  267. ECanMboxSend(&CanMboxUserRegsT);
  268. }
  269. /* //参数设置---------------------------
  270. CanMboxUserRegsT.MboxID = 0x014E5D0B;
  271. CanMboxUserRegsT.MboxNum = 3;
  272. CanMboxUserRegsT.MboxDLC = 8;
  273. CanMboxUserRegsT.MboxWord1 = 0;
  274. CanMboxUserRegsT.MboxWord2 = 0;
  275. CanMboxUserRegsT.MboxWord3 = 0;
  276. tail = (tail++ >=31)? 0 : tail;
  277. CanMboxUserRegsT.MboxWord4 = 0 + tail<<4;
  278. ECanMboxSend(&CanMboxUserRegsT);
  279. //参数设置---------------------------
  280. CanMboxUserRegsT.MboxID = 0x014E5D0B;
  281. CanMboxUserRegsT.MboxNum = 4;
  282. CanMboxUserRegsT.MboxDLC = 8;
  283. CanMboxUserRegsT.MboxWord1 = 0;
  284. CanMboxUserRegsT.MboxWord2 = 0;
  285. CanMboxUserRegsT.MboxWord3 = 0;
  286. tail = (tail++ >=31)? 0 : tail;
  287. CanMboxUserRegsT.MboxWord4 = 0 + tail<<4;
  288. ECanMboxSend(&CanMboxUserRegsT);*/
  289. #endif
  290. #ifdef DEBUG_CAN
  291. if(step2 ==1){
  292. //给定速度 目标速度 反馈速度 正反转
  293. CanMboxUserRegsT.MboxID = 0x11111303;
  294. CanMboxUserRegsT.MboxNum = 5;
  295. CanMboxUserRegsT.MboxDLC = 8;
  296. CanMboxUserRegsT.MboxWord1 = _IQtoIQ15(rc1.TargetValue);//Q15主控给的速度
  297. CanMboxUserRegsT.MboxWord2 = _IQtoIQ15(pid1_spd.Ref);//Q15 pid给定的速度
  298. CanMboxUserRegsT.MboxWord3 = _IQtoIQ15(pid1_spd.Fbk);//Q15 pid反馈速度
  299. CanMboxUserRegsT.MboxWord4 = _IQtoIQ12(pid1_spd.Ref)*speed1.BaseRpm>>12;//rpm PID给定速度
  300. ECanMboxSend(&CanMboxUserRegsT);
  301. }else if(step2 ==2){
  302. //速度PID输出 运行占空比 速度闭环 相位
  303. CanMboxUserRegsT.MboxID = 0x11111304;
  304. CanMboxUserRegsT.MboxNum = 6;
  305. CanMboxUserRegsT.MboxDLC = 8;
  306. CanMboxUserRegsT.MboxWord1 = speed1.SpeedRpm;//pid反馈速度 rpm
  307. CanMboxUserRegsT.MboxWord2 = _IQtoIQ15(pid1_spd.Out);//PID输出
  308. CanMboxUserRegsT.MboxWord3 = pwm1.DutyFunc;//占空比
  309. CanMboxUserRegsT.MboxWord4 = startuptimer;//0-5
  310. ECanMboxSend(&CanMboxUserRegsT);
  311. }else if(step2 ==3){
  312. //是否触发过零 速度闭环标志 运行方向 0
  313. CanMboxUserRegsT.MboxID = 0x11111305;
  314. CanMboxUserRegsT.MboxNum = 7;
  315. CanMboxUserRegsT.MboxDLC = 8;
  316. CanMboxUserRegsT.MboxWord1 = pwmlimit;//过零点
  317. CanMboxUserRegsT.MboxWord2 = IsrTime;
  318. CanMboxUserRegsT.MboxWord3 = PwmSet;
  319. CanMboxUserRegsT.MboxWord4 =0;
  320. ECanMboxSend(&CanMboxUserRegsT);
  321. }else if(step2 ==4){
  322. //数据内容:UA UB UC IDC
  323. CanMboxUserRegsT.MboxID = 0x11111306;
  324. CanMboxUserRegsT.MboxNum = 8;
  325. CanMboxUserRegsT.MboxDLC = 8;
  326. CanMboxUserRegsT.MboxWord1 = hall1.HallGpioAccepted;//放大10倍
  327. CanMboxUserRegsT.MboxWord2 = test;
  328. CanMboxUserRegsT.MboxWord3 = test3;
  329. CanMboxUserRegsT.MboxWord4 = test5;
  330. ECanMboxSend(&CanMboxUserRegsT);
  331. }else if(step2 ==5){
  332. //速度PI 电流PI
  333. CanMboxUserRegsT.MboxID = 0x11111307;
  334. CanMboxUserRegsT.MboxNum = 9;
  335. CanMboxUserRegsT.MboxDLC = 8;
  336. CanMboxUserRegsT.MboxWord1 = (int16)(pid1_spd.Kp);
  337. CanMboxUserRegsT.MboxWord2 = (int16)(pid1_spd.Ki);
  338. CanMboxUserRegsT.MboxWord3 = (int16)(pid1_idc.Kp);
  339. CanMboxUserRegsT.MboxWord4 = (int16)(pid1_idc.Ki);
  340. ECanMboxSend(&CanMboxUserRegsT);
  341. }else if(step2 ==6){
  342. //电流给定 电流反馈 平均电流 滤波后的平均电流
  343. CanMboxUserRegsT.MboxID = 0x11111308;
  344. CanMboxUserRegsT.MboxNum = 10;
  345. CanMboxUserRegsT.MboxDLC = 8;
  346. CanMboxUserRegsT.MboxWord1 = _IQtoIQ15(pid1_idc.Ref);//电流给定
  347. CanMboxUserRegsT.MboxWord2 = _IQtoIQ15(pid1_idc.Fbk);//电流反馈pid1_idc.Fbk
  348. CanMboxUserRegsT.MboxWord3 = (DC_current_filter_avr*1000)>>12;
  349. CanMboxUserRegsT.MboxWord4 = (DC_current_filter_avr*1000)>>12;
  350. ECanMboxSend(&CanMboxUserRegsT);
  351. }else if(step2 ==7){
  352. step2 = 0;
  353. //
  354. CanMboxUserRegsT.MboxID = 0x11111309;
  355. CanMboxUserRegsT.MboxNum = 11;
  356. CanMboxUserRegsT.MboxDLC = 8;
  357. CanMboxUserRegsT.MboxWord1 = DCbus_voltage;//直流电压
  358. CanMboxUserRegsT.MboxWord2 = Tmotor;//电机温度
  359. CanMboxUserRegsT.MboxWord3 = Tvot;//电调温度
  360. CanMboxUserRegsT.MboxWord4 = FaultFlag.all;//故障
  361. ECanMboxSend(&CanMboxUserRegsT);
  362. }
  363. #endif
  364. }
  365. }
  366. #ifdef DEBUG_CAN
  367. void carveData1(){
  368. //当发送标志位为0时,采集并记录模拟数据,但不发送
  369. if (CanCurveFlag == 0)
  370. {
  371. DebugData1(0,test2);
  372. DebugData1(1,speed1.SpeedRpm);
  373. DebugData1(2,0);
  374. DebugData1(3,0);
  375. }
  376. }
  377. void CanCurve(void)
  378. {// Configure Mailboxes 0-15 as Tx, 16-31 as Rx
  379. static int16 i = 0;
  380. static int16 DubugCnt = 0;
  381. static int16 DubugPer = 0;
  382. //当数据大于发送间隔时,发送绘图数据
  383. DubugCnt++;
  384. if (DubugCnt >= DubugPer)
  385. {
  386. //发送间隔数据
  387. DubugPer = TX_PERIOD*MAINLOOPRATE;
  388. DubugCnt = 0;
  389. //当发送标志位为1时,发送绘图波形数据
  390. if (CanCurveFlag == 1)
  391. {
  392. CanMboxUserRegsT.MboxID = 0x1111130E;
  393. CanMboxUserRegsT.MboxNum = 14;
  394. CanMboxUserRegsT.MboxDLC = 8;
  395. CanMboxUserRegsT.MboxWord1 = CanCurveData[i];
  396. CanMboxUserRegsT.MboxWord2 = CanCurveData[i+0x100];
  397. CanMboxUserRegsT.MboxWord3 = CanCurveData[i+0x200];
  398. CanMboxUserRegsT.MboxWord4 = CanCurveData[i+0x300];
  399. ECanMboxSend(&CanMboxUserRegsT);
  400. i++;
  401. if (i >= 0x100)
  402. {
  403. i = 0;
  404. CanCurveFlag = 0;
  405. }
  406. }
  407. }
  408. }
  409. #endif
  410. /**********采集并存储绘图数据1**********/
  411. void DebugData1(int16 num,int16 data)
  412. {
  413. static int16 cnt[4] ={0,0,0,0};
  414. CanCurveData[cnt[num]+ 0x100*num] = data;
  415. cnt[num]++;
  416. if (cnt[num] >= 0x100)
  417. {
  418. cnt[num]=0;
  419. CanCurveFlag = 1;
  420. }
  421. }
  422. void CanReceive(void){
  423. struct CANMBOX_USER_REGS CanMboxTempRegs;
  424. //飞控上位机
  425. CanMboxTempRegs = ECanMboxRead(19);
  426. if (CanMboxTempRegs.MboxRxFlg == TRUE)
  427. {
  428. if (CanMboxTempRegs.MboxID == ID_MOTOR3_COMMAND )
  429. {
  430. CanMboxUserRegsR = CanMboxTempRegs;
  431. }
  432. }
  433. CanMboxTempRegs = ECanMboxRead(21);
  434. if (CanMboxTempRegs.MboxRxFlg == TRUE)
  435. {
  436. if (CanMboxTempRegs.MboxID == ID_FAULT_CLEAR )
  437. {
  438. CanMboxUserRegsR3 = CanMboxTempRegs;
  439. }
  440. }
  441. #ifdef DEBUG_CAN
  442. //------------------------------调试上位机----------------------
  443. CanMboxTempRegs = ECanMboxRead(16);
  444. if (CanMboxTempRegs.MboxRxFlg == TRUE)
  445. {
  446. if (CanMboxTempRegs.MboxID == ID_DMC_DEBUG)
  447. {//握手 可以不握手
  448. if (CanMboxTempRegs.MboxWord1 == 0x5A5A && CanMboxTempRegs.MboxWord2 == 0x2013 &&
  449. CanMboxTempRegs.MboxWord3 == 0x0218 && CanMboxTempRegs.MboxWord4 == 0xA5A5)
  450. {
  451. CanDebugFlag = TRUE;
  452. }
  453. else
  454. {
  455. CanDebugFlag = TRUE;//FALSE;
  456. }
  457. }
  458. else if (CanMboxTempRegs.MboxID == ID_DMC_SET || CanMboxTempRegs.MboxID == ID_DMC_CLEAR)
  459. {
  460. if (CanDebugFlag == TRUE)
  461. {
  462. CanMboxUserRegsR2 = CanMboxTempRegs;//
  463. }
  464. }
  465. else if (CanMboxTempRegs.MboxID == ID_DMC_SET1 || CanMboxTempRegs.MboxID == ID_DMC_SET2\
  466. ||CanMboxTempRegs.MboxID == ID_DMC_SET3 || CanMboxTempRegs.MboxID == ID_DMC_SET4|| CanMboxTempRegs.MboxID == ID_DMC_SET5)
  467. {
  468. if (CanDebugFlag == TRUE)
  469. {
  470. CanMboxUserRegsR3 = CanMboxTempRegs;
  471. }
  472. }
  473. }
  474. #endif
  475. }
  476. void CanMaster(void){
  477. CanReceive();
  478. CanDebugTx();
  479. }
  480. void UserCANprocess(void){
  481. #ifdef PIXHAWK
  482. if (CanMboxUserRegsR.MboxID == ID_MOTOR3_COMMAND)
  483. {
  484. #ifdef SPEEDCLOSED
  485. int16 speedrecieve = 0;
  486. if(ESC_NUMBER == 0x07){
  487. speedrecieve = ((CanMboxUserRegsR.MboxWord1 & 0xff00) >>8) + ((CanMboxUserRegsR.MboxWord2 & 0x00ff)<<8);
  488. SpeedRef = _IQdiv(speedrecieve,speed1.BaseRpm);//速度给定
  489. }
  490. else if(ESC_NUMBER == 0x08){
  491. speedrecieve = ((CanMboxUserRegsR.MboxWord2 & 0xff00) >>8) + ((CanMboxUserRegsR.MboxWord3 & 0x00ff)<<8);
  492. SpeedRef = _IQdiv(speedrecieve,speed1.BaseRpm);//速度给定
  493. }
  494. #else
  495. if(ESC_NUMBER == 0x07){
  496. PwmSet = ((CanMboxUserRegsR.MboxWord1 & 0xff00) >>8) + ((CanMboxUserRegsR.MboxWord2 & 0x00ff)<<8);
  497. }
  498. else if(ESC_NUMBER == 0x08){
  499. PwmSet = ((CanMboxUserRegsR.MboxWord2 & 0xff00) >>8) + ((CanMboxUserRegsR.MboxWord3 & 0x00ff)<<8);
  500. }
  501. #endif //SPEEDCLOSED
  502. }
  503. if(CanMboxUserRegsR3.MboxID == ID_FAULT_CLEAR){
  504. Fault_clear = 1;
  505. CanMboxUserRegsR3.MboxID = 0;
  506. }
  507. #ifdef SPEEDCLOSED
  508. if(_IQabs(SpeedRef)>_IQ(0.1)){
  509. EnableFlag = 1;
  510. }else{
  511. if(_IQabs(speed1.SpeedRpm)<350){//现在烧写的程序还没有这一句
  512. EnableFlag = 0;
  513. }
  514. }
  515. #else
  516. if(_IQabs(PwmSet)>500){
  517. EnableFlag = 1;
  518. }else{
  519. EnableFlag = 0;
  520. }
  521. #endif //SPEEDCLOSED
  522. #endif //PIXHAWK
  523. #ifdef DEBUG_CAN
  524. if (CanMboxUserRegsR2.MboxID == ID_DMC_SET)
  525. {
  526. EnableFlag = 1;
  527. if (CanMboxUserRegsR3.MboxID == ID_DMC_SET1)
  528. {
  529. SpeedRef = _IQdiv(CanMboxUserRegsR3.MboxWord1,speed1.BaseRpm);//速度给定
  530. }
  531. else if (CanMboxUserRegsR3.MboxID == ID_DMC_SET2)
  532. {
  533. // CurrentSet = _IQ((float)CanMboxUserRegsR3.MboxWord1/1000);
  534. test = CanMboxUserRegsR3.MboxWord1;
  535. }
  536. else if (CanMboxUserRegsR3.MboxID == ID_DMC_SET3)
  537. {
  538. }
  539. }
  540. else if (CanMboxUserRegsR2.MboxID == ID_DMC_CLEAR)
  541. {
  542. SpeedRef = _IQabs(0.0);
  543. CurrentSet = _IQabs(0.0);
  544. EnableFlag = 0;
  545. if (CanMboxUserRegsR3.MboxID == ID_DMC_SET4)
  546. {
  547. pid1_spd.Kp = (int32)CanMboxUserRegsR3.MboxWord1;
  548. pid1_spd.Ki = (int32)(CanMboxUserRegsR3.MboxWord2);
  549. pid1_idc.Kp = (int32)(CanMboxUserRegsR3.MboxWord3);
  550. pid1_idc.Ki = (int32)(CanMboxUserRegsR3.MboxWord4);
  551. }
  552. }
  553. if (CanMboxUserRegsR3.MboxID == ID_DMC_SET5)
  554. {
  555. //
  556. Fault_clear = 1;
  557. CanMboxUserRegsR3.MboxID = 0;
  558. }
  559. #endif
  560. }