UserCan.c 18 KB

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