JiaoYaDinCun.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #include "global.h"
  2. #if JIN_HONG_MACHINE
  3. long RealPosBuff = 0; //记录齿数
  4. unsigned long ToothNumBuff = 0;
  5. unsigned long RemainKongWeiToothNum = 0; // 空位剩余齿数
  6. unsigned char cFirstFlg = 0;
  7. unsigned short SecondToothNum = 0;
  8. unsigned char SingOneFlg = 0;
  9. void StopProc(void);
  10. void StartProc(void);
  11. unsigned long CalEncodePulse(void);
  12. void JYDC_DelayAction(void);
  13. void JYDC_InitAction(void);
  14. void JYDC_ExtiActionX30(void);
  15. void JYDC_Action(void);
  16. void ModeSelect(void);
  17. void JYDC_ChuiQi(void);
  18. void JYDC_XiaChong(void);
  19. void JYDC_SetAlarmCode(unsigned alarm_code);
  20. void JYDC_StartStopAction(void);
  21. void JYDC_AutoRunStep(void);
  22. unsigned long GetToothNum(unsigned long zipper_length);
  23. /*
  24. #define DATA_SIZE 100
  25. #define MAX_FREQ 20000
  26. #define MIN_FREQ 600
  27. #define MAX_SPEED 99
  28. #define FLEXIBLE 3 //2~15数值越大中间越陡刚启动和越接近最高速越平滑
  29. #define ACC_TIME 200
  30. #define DEC_TIME 200
  31. unsigned short FreqTab[DATA_SIZE];
  32. unsigned char AccTab[DATA_SIZE];
  33. unsigned char DecTab[DATA_SIZE];
  34. unsigned short CALFREQTab[DATA_SIZE];
  35. void CalculateSModelLine(unsigned short *fre,float len, float fre_max, float fre_min, float flexible)
  36. {
  37. int i=0;
  38. float deno ;
  39. float melo ;
  40. float delt = fre_max-fre_min;
  41. float freq_buff;
  42. float acc_time_buff,dec_time_buff;
  43. float pulse_buff;
  44. acc_time_buff = ACC_TIME;
  45. acc_time_buff = acc_time_buff / (len+1);
  46. dec_time_buff = DEC_TIME;
  47. dec_time_buff = dec_time_buff / (len+1);
  48. for(i=0; i<len; i++)
  49. {
  50. melo = flexible * (i-len/2) / (len/2);
  51. deno = 1.0 / (1 + expf(-melo)); //expf is a library function of exponential(e)
  52. freq_buff = delt * deno + fre_min;
  53. *(fre+i) = 11059200 / (unsigned short)freq_buff;
  54. CALFREQTab[i] = freq_buff;
  55. }
  56. for(i = 0;i<(DATA_SIZE /2);i++)
  57. {
  58. deno = i*acc_time_buff;
  59. AccTab[i] = (unsigned char)deno;
  60. deno = (DATA_SIZE /2 - i)*acc_time_buff;
  61. AccTab[(DATA_SIZE/2)+i] = (unsigned char)deno;
  62. deno = i*dec_time_buff;
  63. DecTab[i] = (unsigned char)deno;
  64. deno = (DATA_SIZE /2 - i)*dec_time_buff;
  65. DecTab[(DATA_SIZE/2)+i] = (unsigned char)deno;
  66. }
  67. for(i=(int)len;i<DATA_SIZE;i++)
  68. {
  69. *(fre+i) = *(fre+(unsigned short)(len - 1));
  70. AccTab[i] = 0;
  71. DecTab[i] = 0;
  72. }
  73. }
  74. */
  75. void JYDC_SetAlarmCode(unsigned alarm_code)
  76. {
  77. SetAlarmCode(JYDC_ALARM_ADDR,alarm_code);
  78. JYDC_bAlarmStop = 1;
  79. }
  80. void JYDC_DelayAction(void)
  81. {
  82. }
  83. void JYDC_InitAction(void)
  84. {
  85. // float pulse_buff,length_buff;
  86. // JYDC_Param = (JYDC_ParamStrct *)&user_datas[0];
  87. /* pulse_buff = JYDC_Param->MotorCyclePulse;
  88. length_buff = JYDC_Param->MotorCyclePulse;
  89. XGearRatio = length_buff/pulse_buff;
  90. if(XGearRatio == 0)XGearRatio = 1;
  91. SetAccTime(X_AXIS,JYDC_Param->MotorAccTime);
  92. SetDecTime(X_AXIS,JYDC_Param->MotorDecTime);
  93. JYDC_SetAlarmCode(JYDC_NO_ALARM);
  94. SetPos(X_AXIS,0);
  95. */
  96. if(MODE_SELECT > 3)MODE_SELECT = 0;
  97. ModeSelect();
  98. CUR_LEN_JY = 0;
  99. }
  100. void SetZipperLength(unsigned short addr,unsigned long length)
  101. {
  102. user_datas[addr + 1] = length >> 16;
  103. user_datas[addr] = length & 0x00FF;
  104. }
  105. unsigned char test_buff[14]={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
  106. //手动动作
  107. void JYDC_ManualAction(void)
  108. {
  109. //警告跳出
  110. if(bClearTotal)
  111. {
  112. bClearTotal = 0;
  113. ClrcToTal(JYDC_TOTAL_ADDR);
  114. }
  115. if(!JYDC_bRunning)
  116. {
  117. if(JYDC_bAddTooth)
  118. {
  119. JYDC_bAddTooth = 0;
  120. if(JYDC_PARAM_REVICHISHU < 65535)JYDC_PARAM_REVICHISHU++;
  121. }
  122. if(JYDC_bDecTooth)
  123. {
  124. JYDC_bDecTooth = 0;
  125. if(JYDC_PARAM_REVICHISHU != 0)JYDC_PARAM_REVICHISHU--;
  126. }
  127. if(JYDC_bManXiaChong || JYDC_ManXiaChong_UP)
  128. {
  129. JYDC_bManXiaChong = 0;
  130. if(JYDC_XiaChongStep == 0)//if(!JYDC_bRunning && !STOP_IN && !X_DRV && (JYDC_XiaChongStep == 0))
  131. {
  132. JYDC_XiaChongStep = 1;
  133. }
  134. }
  135. if(JYDC_bManSuoChi)
  136. {
  137. JYDC_bManSuoChi = 0;
  138. JYDC_SuoChi_VAVLE = ~JYDC_SuoChi_VAVLE;
  139. }
  140. if(JYDC_bManChuiQi)
  141. {
  142. JYDC_bManChuiQi = 0;
  143. JYDC_ChuiQi_VAVLE = 1;
  144. JYDC_ChuiQiDelay = dwTickCount + JYDC_PARAM_CHUIQI_TIME * 10;
  145. }
  146. if(JYDC_bChangeLength)
  147. {
  148. JYDC_bChangeLength = 0;
  149. ModeSelect();
  150. SetZipperLength(31,CalInch(SET_ZIPPER_LENGTH));
  151. JYDC_PARAM_REVICHISHU = GetToothNum(SET_ZIPPER_LENGTH);
  152. }
  153. if(JYDC_bSetInch)
  154. {
  155. JYDC_bSetInch = 0;
  156. SetZipperLength(6,CalLengthCM(SET_ZIPPER_LENGTH_INCH));
  157. JYDC_bChangeLength = 1;
  158. }
  159. }
  160. }
  161. //高速输入X30/X17外部中断
  162. void JYDC_ExtiActionX30(void)
  163. {
  164. CUR_LEN_JY++;
  165. DISPLAY_DATA0++;
  166. if((CUR_LEN_JY >= ToothNumBuff) && JYDC_bRunning)
  167. {
  168. AxisEgmStop(X_AXIS);
  169. if(JYDC_bTest)
  170. {
  171. JYDC_bRunning = 0;
  172. JYDC_AutoStep = 0;
  173. JYDC_SuoChi_VAVLE = 1;
  174. }
  175. }
  176. }
  177. //胶牙定寸动作
  178. void JYDC_Action(void)
  179. {
  180. // DISPLAY_DATA0 = cNoPulseStopTime;
  181. // DISPLAY_DATA1 = GetCurSpeed(X_AXIS);
  182. // DISPLAY_DATA2 = GetSetSpeed(X_AXIS);
  183. // DISPLAY_DATA3 = GetState(X_AXIS);
  184. JYDC_ManualAction();
  185. JYDC_ChuiQi();
  186. JYDC_XiaChong();
  187. JYDC_StartStopAction();
  188. JYDC_AutoRunStep();
  189. }
  190. void ModeSelect(void)
  191. {
  192. switch(MODE_SELECT) //模具选择
  193. {
  194. case 0: //3#模具
  195. JY_JIZHUN_CISHU = JY_JIZHUN_CISHU1;//基准长度对应齿数
  196. JY_JIZHUN_LEN = JY_JIZHUN_LEN1;//基准长度
  197. SET_DAOKOU_JY = SET_DAOKOU_JY1; //机械模具刀口
  198. break;
  199. case 1: //5#模具
  200. JY_JIZHUN_CISHU = JY_JIZHUN_CISHU2;//基准长度对应齿数
  201. JY_JIZHUN_LEN = JY_JIZHUN_LEN2;//基准长度
  202. SET_DAOKOU_JY = SET_DAOKOU_JY2; //机械模具刀口
  203. break;
  204. case 2: //8#模具
  205. JY_JIZHUN_CISHU = JY_JIZHUN_CISHU3;//基准长度对应齿数
  206. JY_JIZHUN_LEN = JY_JIZHUN_LEN3;//基准长度
  207. SET_DAOKOU_JY = SET_DAOKOU_JY3; //机械模具刀口
  208. break;
  209. default: //特殊模具
  210. JY_JIZHUN_CISHU = JY_JIZHUN_CISHU4;//基准长度对应齿数
  211. JY_JIZHUN_LEN = JY_JIZHUN_LEN4;//基准长度
  212. SET_DAOKOU_JY = SET_DAOKOU_JY4; //机械模具刀口
  213. break;
  214. }
  215. if((JY_JIZHUN_CISHU == 0) || (JY_JIZHUN_LEN == 0))
  216. {
  217. JY_JIZHUN_CISHU = 49;
  218. JY_JIZHUN_LEN = 1250;
  219. SET_DAOKOU_JY = 10;
  220. }
  221. }
  222. //输入拉链长度返回所输入拉链长度所对应的齿数
  223. unsigned long GetToothNum(unsigned long zipper_length)
  224. {
  225. float length_buff1,tooth_buff,length_buff2;
  226. length_buff1 = zipper_length;
  227. tooth_buff = JY_JIZHUN_CISHU;
  228. length_buff2 = JY_JIZHUN_LEN;
  229. return (unsigned long)round(length_buff1 * tooth_buff / length_buff2);
  230. }
  231. //吹气动作
  232. void JYDC_ChuiQi(void)
  233. {
  234. if(JYDC_XiaChong_DW)
  235. {
  236. JYDC_ChuiQi_VAVLE = 1;
  237. JYDC_ChuiQiDelay = dwTickCount + JYDC_PARAM_CHUIQI_TIME * 10;
  238. }
  239. if(JYDC_ChuiQi_VAVLE)
  240. {
  241. if(JYDC_ChuiQiDelay < dwTickCount)JYDC_ChuiQi_VAVLE = 0;
  242. }
  243. if(JYDC_XiChiChuiQi_VAVLE)
  244. {
  245. if(JYDC_XiChiChuiQiDelay < dwTickCount)JYDC_XiChiChuiQi_VAVLE = 0;
  246. }
  247. }
  248. //下冲动作
  249. void JYDC_XiaChong(void)
  250. {
  251. if(JYDC_XiaChongStep == 1)
  252. {
  253. JYDC_ShaChe_VAVLE = 0;
  254. JYDC_SuoChi_VAVLE = 1; //手动下冲不锁齿
  255. JYDC_XiaChongDelay = dwTickCount + XIACONG_DELAY_TIME*10;
  256. JYDC_XiaChongStep = 2;
  257. }
  258. else if((JYDC_XiaChongStep == 2) && (dwTickCount >= JYDC_XiaChongDelay))
  259. {
  260. JYDC_XiaChong_MOTOR = 1;
  261. JYDC_XiaChongDelay = dwTickCount + JYDC_PARAM_XC_ALARM_TIME * 10;
  262. JYDC_XiaChongStep = 3;
  263. }
  264. else if(JYDC_XiaChongStep == 3)
  265. {
  266. if(JYDC_XiaChong_IN_UP)
  267. {
  268. JYDC_XiaChong_MOTOR = 0;
  269. JYDC_XiaChongDelay = dwTickCount + JYDC_PARAM_SHACHE_DELAY_TIME * 10;
  270. JYDC_XiaChongStep = 4;
  271. }
  272. else if(dwTickCount >= JYDC_XiaChongDelay)
  273. {
  274. JYDC_SetAlarmCode(JYDC_XIACHONG_ARRIVE_ALARM);
  275. JYDC_XiaChong_MOTOR = 0;
  276. JYDC_ShaChe_VAVLE = 0;
  277. }
  278. }
  279. else if((JYDC_XiaChongStep == 4) && (dwTickCount >= JYDC_XiaChongDelay))
  280. {
  281. JYDC_ShaChe_VAVLE = 1;
  282. JYDC_XiaChongStep = 5;
  283. }
  284. else if(JYDC_XiaChongStep == 5)
  285. {
  286. JYDC_XiaChongStep = 0;
  287. }
  288. }
  289. //启动停止故障停止动作
  290. void JYDC_StartStopAction(void)
  291. {
  292. if((START_IN_UP || JYDC_bStart) && !JYDC_bRunning && (JYDC_AutoStep ==0))
  293. {
  294. if(!JYDC_XiaChong_IN)JYDC_SetAlarmCode(JYDC_START_XIACHONG_ALARM);
  295. else
  296. {
  297. JYDC_AutoStep = 1;
  298. JYDC_bRunning = 1;
  299. JYDC_AutoDelay = 0;
  300. }
  301. JYDC_bStart = 0;
  302. }
  303. if(JYDC_KaDai_IN && (GetAlarmCode(100) == 0))
  304. {
  305. JYDC_SetAlarmCode(JYDC_KADAI_ALARM);
  306. }
  307. if(STOP_IN_UP || JYDC_bStop)
  308. {
  309. JYDC_bStop = 0;
  310. if(JYDC_bRunning)
  311. {
  312. SingOneFlg = 1;
  313. /* JYDC_ChuiQi_VAVLE = 1;
  314. JYDC_ChuiQiDelay = dwTickCount + JYDC_PARAM_CHUIQI_TIME * 10;
  315. JYDC_SuoChi_VAVLE = 1;
  316. JYDC_bRunning = 0;
  317. JYDC_AutoStep = 0;
  318. AxisEgmStop(X_AXIS);*/
  319. }
  320. else
  321. {
  322. CUR_LEN_JY = 0;
  323. JYDC_ChuiQi_VAVLE = 1;
  324. JYDC_ChuiQiDelay = dwTickCount + JYDC_PARAM_CHUIQI_TIME * 10;
  325. JYDC_SuoChi_VAVLE = 0;
  326. JYDC_bRunning = 0;
  327. JYDC_AutoStep = 0;
  328. AxisEgmStop(X_AXIS);
  329. DISPLAY_DATA0 = 0;
  330. }
  331. }
  332. if(JYDC_bAlarmStop)
  333. {
  334. JYDC_bAlarmStop = 0;
  335. AxisEgmStop(X_AXIS);
  336. JYDC_AutoStep = 0;
  337. if(JYDC_bRunning)JYDC_SuoChi_VAVLE = 1;
  338. JYDC_bRunning = 0;
  339. }
  340. }
  341. unsigned long ToothTransPulse(unsigned long tooth)
  342. {
  343. return(JY_JIZHUN_LEN * tooth * 2000 / 250 / JY_JIZHUN_CISHU);
  344. }
  345. //自动运行动作
  346. void JYDC_AutoRunStep(void)
  347. {
  348. // DISPLAY_DATA0 = JYDC_AutoStep;
  349. // DISPLAY_DATA1 = ToothNumBuff;
  350. // DISPLAY_DATA2 = SET_TOTAL;
  351. // DISPLAY_DATA3 = GetTotal(JYDC_TOTAL_ADDR);
  352. if(JYDC_bRunning)
  353. {
  354. if(JYDC_AutoStep == 1)
  355. {
  356. JYDC_AutoStep = 2;
  357. }
  358. else if((JYDC_AutoStep == 2))
  359. {
  360. cFirstFlg = 1;
  361. if(RemainKongWeiToothNum == 0)
  362. {
  363. RemainKongWeiToothNum = SET_KOUWEI_JY;
  364. }
  365. else
  366. {
  367. }
  368. JYDC_AutoStep = 3;
  369. }
  370. else if(JYDC_AutoStep == 3)
  371. {
  372. if(RemainKongWeiToothNum >= SET_DAOKOU_JY)
  373. {
  374. if(cFirstFlg)
  375. {
  376. if(JYDC_PARAM_REVICHISHU <= SET_KOUWEI_JY)
  377. {
  378. ToothNumBuff = JYDC_PARAM_REVICHISHU + SET_DAOKOU_JY;
  379. RemainKongWeiToothNum = 0;
  380. }
  381. else
  382. {
  383. ToothNumBuff = JYDC_PARAM_REVICHISHU + (RemainKongWeiToothNum-SET_DAOKOU_JY) + SET_DAOKOU_JY;
  384. RemainKongWeiToothNum -= SET_DAOKOU_JY;
  385. }
  386. }
  387. else
  388. {
  389. ToothNumBuff = ToothNumBuff + SET_DAOKOU_JY;
  390. RemainKongWeiToothNum -= SET_DAOKOU_JY;
  391. }
  392. }
  393. else
  394. {
  395. if(cFirstFlg)
  396. {
  397. ToothNumBuff = JYDC_PARAM_REVICHISHU - RemainKongWeiToothNum + SET_DAOKOU_JY;
  398. }
  399. else
  400. {
  401. ToothNumBuff = ToothNumBuff + RemainKongWeiToothNum;
  402. }
  403. RemainKongWeiToothNum = 0;
  404. }
  405. JYDC_SuoChi_VAVLE = 0;
  406. // JYDC_AutoDelay = dwTickCount + 50;
  407. JYDC_AutoStep = 4;
  408. }
  409. else if((JYDC_AutoStep == 4) && (dwTickCount >= JYDC_AutoDelay))
  410. {
  411. if((ToothNumBuff - CUR_LEN_JY) > JYDC_PARAM_LOW_LENGTH)
  412. {
  413. AxisContinueMove(X_AXIS,JYDC_PARAM_SET_SPEED,0);
  414. JYDC_AutoDelay = dwTickCount + NO_TOOTHCOUT_TIME * 10;
  415. JYDC_XiChiChuiQi_VAVLE = 1;
  416. JYDC_XiChiChuiQiDelay = dwTickCount + 200;
  417. JYDC_AutoStep = 5;
  418. }
  419. else
  420. {
  421. AxisContinueMove(X_AXIS,JYDC_PARAM_LOW_SPEED,0);
  422. JYDC_AutoDelay = dwTickCount + NO_TOOTHCOUT_TIME * 10;
  423. JYDC_XiChiChuiQi_VAVLE = 1;
  424. JYDC_XiChiChuiQiDelay = dwTickCount + 200;
  425. JYDC_AutoStep = 6;
  426. }
  427. /* DISPLAY_DATA6 = ToothTransPulse(ToothNumBuff);
  428. DISPLAY_DATA7 = ToothTransPulse(JYDC_PARAM_LOW_LENGTH);
  429. AxisMoveTwoPos(X_AXIS,JYDC_PARAM_SET_SPEED,ToothTransPulse(ToothNumBuff - JYDC_PARAM_LOW_LENGTH),JYDC_PARAM_LOW_SPEED,ToothTransPulse(JYDC_PARAM_LOW_LENGTH+5),0);
  430. JYDC_AutoStep = 6;
  431. JYDC_AutoDelay = dwTickCount + NO_TOOTHCOUT_TIME;*/
  432. }
  433. else if(JYDC_AutoStep == 5)
  434. {
  435. if(cFirstFlg)
  436. {
  437. if((ToothNumBuff - CUR_LEN_JY) <= (JYDC_PARAM_LOW_LENGTH))
  438. {
  439. AxisChangeSpeed(X_AXIS,JYDC_PARAM_LOW_SPEED);
  440. cFirstFlg = 0;
  441. JYDC_AutoStep = 6;
  442. }
  443. else if(dwTickCount >= JYDC_AutoDelay)
  444. {
  445. JYDC_SetAlarmCode(JYDC_COUNT_ALARM);
  446. }
  447. }
  448. else
  449. {
  450. if((ToothNumBuff - CUR_LEN_JY) <= JYDC_PARAM_LOW_LENGTH)
  451. {
  452. AxisChangeSpeed(X_AXIS,JYDC_PARAM_LOW_SPEED);
  453. JYDC_AutoStep = 6;
  454. }
  455. else if(dwTickCount >= JYDC_AutoDelay)
  456. {
  457. JYDC_SetAlarmCode(JYDC_COUNT_ALARM);
  458. }
  459. }
  460. }
  461. else if(JYDC_AutoStep == 6)
  462. {
  463. if(!X_DRV)
  464. {
  465. JYDC_XiaChongStep = 1;
  466. JYDC_AutoStep = 7;
  467. }
  468. else if(dwTickCount >= JYDC_AutoDelay)
  469. {
  470. JYDC_SetAlarmCode(JYDC_COUNT_ALARM);
  471. }
  472. }
  473. else if(JYDC_AutoStep == 7)
  474. {
  475. if(JYDC_XiaChongStep == 0)
  476. {
  477. if(RemainKongWeiToothNum == 0)
  478. {
  479. JYDC_AutoStep = 8;
  480. }
  481. else
  482. {
  483. JYDC_AutoStep = 3;
  484. }
  485. }
  486. }
  487. else if((JYDC_AutoStep == 8) && (JYDCRunFlg == 1))
  488. {
  489. AddToTal(JYDC_TOTAL_ADDR);
  490. CUR_LEN_JY = 0;
  491. if((GetTotal(JYDC_TOTAL_ADDR) >= SET_TOTAL) || JYDC_bStop || SingOneFlg)
  492. {
  493. if(JYDC_bStop || SingOneFlg)
  494. {
  495. JYDC_bStop = 0;
  496. JYDC_AutoStep = 0;
  497. JYDC_bRunning = 0;
  498. SingOneFlg = 0;
  499. }
  500. else
  501. {
  502. JYDC_SetAlarmCode(JYDC_TOTAL_ALARM);
  503. }
  504. }
  505. else
  506. {
  507. JYDC_AutoStep = 1;
  508. JYDC_AutoDelay = JYDC_PARAM_CYCLE_DELAY * 10 + dwTickCount;
  509. CalProSP(JYDC_PROSPEED_ADDR);
  510. }
  511. }
  512. }
  513. }
  514. #endif