JiaoYaDinCun.c 16 KB

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