Motor.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. #include "MOTOR.h"
  2. #include "delay.h"
  3. #include "DS18B20.h"
  4. #include "Instrument_EEPROM_Param.h"
  5. extern unsigned char Motor_Status;
  6. unsigned char g_ucMotorXFinishFlag,g_ucMotorYFinishFlag,g_ucMotorZFinishFlag,g_ucMotor4FinishFlag,g_ucMotorZ2FinishFlag ;
  7. unsigned int g_uiAccIndex ;
  8. unsigned long int g_uliRunningPulse ;
  9. unsigned long int g_uliPulseCount ;
  10. unsigned int g_uiReagentMotorPeriod[REAGENT_MOTOR_ACC],g_uiReagentMotorPeriod2[REAGENT_MOTOR_ACC2],g_uiReagentMotorPeriod3[REAGENT_MOTOR_ACC3],g_uiReagentMotorPeriod4[REAGENT_MOTOR_ACC4];
  11. unsigned long PulseNum;
  12. unsigned char Motor1_HOME_Flag,Motor2_HOME_Flag,Motor3_HOME_Flag,Motor4_HOME_Flag,Motor5_HOME_Flag;
  13. unsigned int Moving_Speed;
  14. unsigned int Reset_Speed;
  15. struct MotorParam g_tReagentMotorParam;
  16. struct MotorParamEEPROM Motor1;
  17. struct MotorParamEEPROM Motor2;
  18. struct MotorParamEEPROM Motor3;
  19. struct MotorParamEEPROM Motor4;
  20. //struct MotorParamEEPROM Moor5;
  21. void CalculateSModelLine( unsigned int * period, unsigned int len, unsigned int fre_max, unsigned int fre_min, float flexible)
  22. {
  23. int i,ttt;
  24. float deno ;
  25. float melo ;
  26. float fre;
  27. float Fdelt = fre_max-fre_min;
  28. if(len>ACCLTH) len=ACCLTH;
  29. for(i=0; i<len; i++)
  30. {
  31. melo = flexible * (2.0*i/len-1) ;
  32. deno = 1.0 / (1 + expf(-melo));
  33. fre = Fdelt * deno + fre_min;
  34. ttt=period[i] = (unsigned short)(625000.0/fre);
  35. }
  36. }
  37. void InitMotor1PWM(void)
  38. {
  39. PMCONbits.PTBEEN=0;
  40. _RP25R = 0x12;
  41. OC1CON = 0;
  42. OC1CON = 0;
  43. OC1CONbits.OCM = 0;
  44. OC1CONbits.OCTSEL = 0x00;
  45. OC1R = 312;
  46. OC1RS =625;
  47. T2CON = 0;
  48. T2CONbits.TCKPS = 2;
  49. PR2 = 625;
  50. OC1CONbits.OCM = 4;
  51. T2CONbits.TON = 0;
  52. IEC0bits.T2IE = 0;
  53. IFS0bits.T2IF = 0;
  54. IPC0bits.OC1IP = 6;
  55. IEC0bits.OC1IE = 0;
  56. IFS0bits.OC1IF = 0;
  57. }
  58. void InitMotor2PWM(void)
  59. {
  60. PMCONbits.PTBEEN=0;
  61. _RP4R = 0x13;
  62. OC2CON=0;
  63. OC2CON = 0;
  64. OC2CONbits.OCM = 0;
  65. OC2CONbits.OCTSEL = 0x00 ;
  66. OC2R = 312;
  67. OC2RS = 625;
  68. T2CON = 0;
  69. T2CONbits.TCKPS = 2;
  70. PR2 = 625;
  71. OC2CONbits.OCM = 4;
  72. T2CONbits.TON = 0;
  73. IEC0bits.T2IE = 0;
  74. IFS0bits.T2IF = 0;
  75. IPC1bits.OC2IP = 6;
  76. IEC0bits.OC2IE = 0;
  77. IFS0bits.OC2IF = 0;
  78. }
  79. void InitMotor3PWM(void)
  80. {
  81. PMCONbits.PTBEEN=0;
  82. _RP5R = 0x14;
  83. OC3CON = 0;
  84. OC3CON = 0;
  85. OC3CONbits.OCM = 0;
  86. OC3CONbits.OCTSEL = 0x00;
  87. OC3R = 312;
  88. OC3RS = 625;
  89. T2CON = 0;
  90. T2CONbits.TCKPS = 2;
  91. PR2 = 625;
  92. OC3CONbits.OCM = 4;
  93. T2CONbits.TON = 0;
  94. IEC0bits.T2IE = 0;
  95. IFS0bits.T2IF = 0;
  96. IPC6bits.OC3IP = 6;
  97. IEC1bits.OC3IE = 0;
  98. IFS1bits.OC3IF = 0;
  99. }
  100. void InitMotor4PWM(void)
  101. {
  102. PMCONbits.PTBEEN=0;
  103. _RP13R = 0x15;
  104. OC4CON = 0;
  105. OC4CON = 0;
  106. OC4CONbits.OCM = 0;
  107. OC4CONbits.OCTSEL = 0x00;
  108. OC4R = 312;
  109. OC4RS = 625;
  110. T2CON = 0;
  111. T2CONbits.TCKPS = 2;
  112. PR2 = 625;
  113. OC4CONbits.OCM = 4;
  114. T2CONbits.TON = 0;
  115. IEC0bits.T2IE = 0;
  116. IFS0bits.T2IF = 0;
  117. IPC6bits.OC4IP = 6;
  118. IEC1bits.OC4IE = 0;
  119. IFS1bits.OC4IF = 0;
  120. }
  121. void SetMotor1(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  122. {
  123. unsigned int i;
  124. unsigned int Prd;
  125. Prd=625000/spd;
  126. *AccPLS = 0;
  127. for(i=0;i<REAGENT_MOTOR_ACC;i++)
  128. {
  129. if((Prd < g_uiReagentMotorPeriod[i])&&(Prd >= g_uiReagentMotorPeriod[i+1]))
  130. {
  131. *AccPLS=i+1;
  132. break;
  133. }
  134. else if(Prd == g_uiReagentMotorPeriod[i])
  135. {
  136. *AccPLS=i;
  137. break;
  138. }
  139. }
  140. if(Pulses >= *AccPLS + *AccPLS)
  141. {
  142. *constVPLS = Pulses- *AccPLS;
  143. }
  144. else if(Pulses < *AccPLS + *AccPLS)
  145. {
  146. *AccPLS = Pulses/2;
  147. *constVPLS = *AccPLS;
  148. }
  149. }
  150. void SetMotor2(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  151. {
  152. unsigned int i;
  153. unsigned int Prd;
  154. Prd=625000/spd;
  155. *AccPLS = 0;
  156. for(i=0;i<ACC_pulse2;i++)
  157. {
  158. if((Prd < g_uiReagentMotorPeriod2[i])&&(Prd >= g_uiReagentMotorPeriod2[i+1]))
  159. {
  160. *AccPLS=i+1;
  161. break;
  162. }
  163. else if(Prd == g_uiReagentMotorPeriod2[i])
  164. {
  165. *AccPLS=i;
  166. break;
  167. }
  168. }
  169. if(Pulses >= (*AccPLS + *AccPLS))
  170. {
  171. *constVPLS = Pulses- *AccPLS;
  172. }
  173. else if(Pulses < (*AccPLS + *AccPLS))
  174. {
  175. *AccPLS = Pulses/2;
  176. *constVPLS = *AccPLS;
  177. }
  178. }
  179. void SetMotor3(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  180. {
  181. unsigned int i;
  182. unsigned int Prd;
  183. Prd=625000/spd;
  184. *AccPLS = 0;
  185. if(spd>REAGENT_MOTOR_FREQMAX3||spd<REAGENT_MOTOR_FREQMIN3)
  186. {
  187. }
  188. else
  189. {
  190. for(i=0;i<REAGENT_MOTOR_ACC3;i++)
  191. {
  192. if((Prd < g_uiReagentMotorPeriod3[i])&&(Prd >= g_uiReagentMotorPeriod3[i+1]))
  193. {
  194. *AccPLS=i+1;
  195. break;
  196. }
  197. else if(Prd == g_uiReagentMotorPeriod3[i])
  198. {
  199. *AccPLS=i;
  200. break;
  201. }
  202. }
  203. if(Pulses >= (*AccPLS + *AccPLS))
  204. {
  205. *constVPLS = Pulses- *AccPLS;
  206. }
  207. else if(Pulses < (*AccPLS + *AccPLS))
  208. {
  209. *AccPLS = Pulses/2;
  210. *constVPLS = *AccPLS;
  211. }
  212. }
  213. }
  214. void SetMotor4(unsigned long int Pulses,unsigned int spd,unsigned long int *AccPLS,unsigned long int *constVPLS)
  215. {
  216. unsigned int i;
  217. unsigned int Prd;
  218. Prd=625000/spd;
  219. *AccPLS = 0;
  220. if(spd>REAGENT_MOTOR_FREQMAX4||spd<REAGENT_MOTOR_FREQMIN4)
  221. {
  222. }
  223. else
  224. {
  225. for(i=0;i<REAGENT_MOTOR_ACC4;i++)
  226. {
  227. if((Prd < g_uiReagentMotorPeriod4[i])&&(Prd >= g_uiReagentMotorPeriod4[i+1]))
  228. {
  229. *AccPLS=i+1;
  230. break;
  231. }
  232. else if(Prd == g_uiReagentMotorPeriod4[i])
  233. {
  234. *AccPLS=i;
  235. break;
  236. }
  237. }
  238. if(Pulses >= (*AccPLS + *AccPLS))
  239. {
  240. *constVPLS = Pulses- *AccPLS;
  241. }
  242. else if(Pulses < (*AccPLS + *AccPLS))
  243. {
  244. *AccPLS = Pulses/2;
  245. *constVPLS = *AccPLS;
  246. }
  247. }
  248. }
  249. void Motor1Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  250. {
  251. unsigned int Freq;
  252. if(g_tReagentMotorParam.ulZiPosition>Z_limitation1||g_tReagentMotorParam.ulZiPosition>Z1_limitation1)
  253. {
  254. if(X_move_protection_flag==0)
  255. {
  256. Motor_Status=13;
  257. return;
  258. }
  259. }
  260. Motor1_HOME_Flag=1;
  261. Motor2_HOME_Flag=0;
  262. Motor3_HOME_Flag=0;
  263. Motor4_HOME_Flag=0;
  264. Motor5_HOME_Flag=0;
  265. OC1CON = 0;
  266. g_tReagentMotorParam.ucXDirection = MOTOR1_FORWARD;
  267. Delay1ms(2);
  268. TMR5=0;
  269. PulseNum=0;
  270. if(MOTOR1_HOME == 1)
  271. {
  272. MOTOR1_DIR = MOTOR1_FORWARD;
  273. Freq=5000000/LEAVspd;
  274. TMR5=0;
  275. PR5=Freq;
  276. IFS1bits.T5IF = 0;
  277. T5CONbits.TON=1;
  278. while(MOTOR1_HOME)
  279. {
  280. if(PulseNum > Motor1.Limit_Pulse*2){ Motor_Status=11;break;}
  281. }
  282. T5CONbits.TON=0;
  283. MOTOR1_PWM=0;
  284. }
  285. else
  286. {
  287. MOTOR1_DIR = MOTOR1_REVERSAL;
  288. Freq=5000000/MOVEspd;
  289. TMR5=0;
  290. PR5=Freq;
  291. IFS1bits.T5IF = 0;
  292. T5CONbits.TON=1;
  293. while(PulseNum < Motor1.Limit_Pulse*2)
  294. {
  295. if(MOTOR1_HOME)
  296. break;
  297. }
  298. if(PulseNum ==Motor1.Limit_Pulse*2||PulseNum >Motor1.Limit_Pulse*2) { Motor_Status=11;}
  299. T5CONbits.TON=0;
  300. Freq=5000000/LEAVspd;
  301. TMR5=0;
  302. PR5=Freq;
  303. IFS1bits.T5IF = 0;
  304. T5CONbits.TON=1;
  305. PulseNum=0;
  306. while(PulseNum < Motor1.Overshoot_Pulse*2);
  307. T5CONbits.TON=0;
  308. Delay1ms(1000);
  309. MOTOR1_DIR = MOTOR1_FORWARD;
  310. Freq=5000000/LEAVspd;
  311. TMR5=0;
  312. PR5=Freq;
  313. IFS1bits.T5IF = 0;
  314. T5CONbits.TON=1;
  315. PulseNum=0;
  316. while(MOTOR1_HOME)
  317. {
  318. if(PulseNum >= Motor1.Limit_Pulse*2) { Motor_Status=11;break;}
  319. }
  320. T5CONbits.TON=0;
  321. MOTOR1_PWM=0;
  322. }
  323. MOTOR1_DIR = MOTOR1_FORWARD;
  324. Freq=5000000/LEAVspd;
  325. TMR5=0;
  326. PR5=Freq;
  327. IFS1bits.T5IF = 0;
  328. T5CONbits.TON=1;
  329. PulseNum=0;
  330. while(!MOTOR1_HOME)
  331. {
  332. if(PulseNum >= pulseoffset*2 ) break;
  333. }
  334. T5CONbits.TON=0;
  335. MOTOR1_PWM=0;
  336. InitMotor1PWM();
  337. g_uliRunningPulse = 0;
  338. g_tReagentMotorParam.ulXiPosition =0;
  339. }
  340. void Motor2Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  341. {
  342. unsigned int Freq;
  343. Motor1_HOME_Flag=0;
  344. Motor2_HOME_Flag=1;
  345. Motor3_HOME_Flag=0;
  346. Motor4_HOME_Flag=0;
  347. Motor5_HOME_Flag=0;
  348. OC2CON = 0;
  349. g_tReagentMotorParam.ucZDirection = MOTOR2_FORWARD;
  350. Delay1ms(2);
  351. T5CONbits.TON=0;
  352. TMR5=0;
  353. PulseNum=0;
  354. if(MOTOR2_HOME == 1)
  355. {
  356. MOTOR2_DIR =MOTOR2_FORWARD ;
  357. Freq=5000000/LEAVspd;
  358. TMR5=0;
  359. PR5=Freq;
  360. IFS1bits.T5IF = 0;
  361. T5CONbits.TON=1;
  362. while(MOTOR2_HOME)
  363. {
  364. if(PulseNum > Motor2.Limit_Pulse*2) {Motor_Status=21;break;}
  365. }
  366. T5CONbits.TON=0;
  367. MOTOR2_PWM=0;
  368. }
  369. else
  370. {
  371. MOTOR2_DIR = MOTOR2_REVERSAL;
  372. Freq=5000000/MOVEspd;
  373. TMR5=0;
  374. PR5=Freq;
  375. IFS1bits.T5IF = 0;
  376. T5CONbits.TON=1;
  377. while(PulseNum < Motor2.Limit_Pulse*2)
  378. {
  379. if(MOTOR2_HOME)
  380. break;
  381. }
  382. if(PulseNum ==Motor2.Limit_Pulse*2||PulseNum >Motor2.Limit_Pulse*2) { Motor_Status=21;}
  383. T5CONbits.TON=0;
  384. Freq=5000000/LEAVspd;
  385. TMR5=0;
  386. PR5=Freq;
  387. IFS1bits.T5IF = 0;
  388. T5CONbits.TON=1;
  389. PulseNum=0;
  390. while(PulseNum < Motor2.Overshoot_Pulse*2);
  391. T5CONbits.TON=0;
  392. Delay1ms(1000);
  393. MOTOR2_DIR = MOTOR2_FORWARD;
  394. Freq=5000000/LEAVspd;
  395. TMR5=0;
  396. PR5=Freq;
  397. IFS1bits.T5IF = 0;
  398. T5CONbits.TON=1;
  399. PulseNum=0;
  400. while(MOTOR2_HOME)
  401. {
  402. if(PulseNum > Motor2.Limit_Pulse*2) {Motor_Status=21;break;}
  403. }
  404. T5CONbits.TON=0;
  405. MOTOR2_PWM=0;
  406. }
  407. MOTOR2_DIR = MOTOR2_FORWARD;
  408. Freq=5000000/LEAVspd;
  409. TMR5=0;
  410. PR5=Freq;
  411. IFS1bits.T5IF = 0;
  412. T5CONbits.TON=1;
  413. PulseNum=0;
  414. while(!MOTOR2_HOME)
  415. {
  416. if(PulseNum >= pulseoffset*2 ) break;
  417. }
  418. T5CONbits.TON=0;
  419. MOTOR2_PWM=0;
  420. InitMotor2PWM();
  421. g_uliRunningPulse = 0;
  422. g_tReagentMotorParam.ulZiPosition =0;
  423. }
  424. void Motor3Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  425. {
  426. unsigned int Freq;
  427. Motor1_HOME_Flag=0;
  428. Motor2_HOME_Flag=0;
  429. Motor3_HOME_Flag=1;
  430. Motor4_HOME_Flag=0;
  431. Motor5_HOME_Flag=0;
  432. OC3CON = 0;
  433. g_tReagentMotorParam.ucZ1Direction = MOTOR3_FORWARD;
  434. Delay1ms(2);
  435. T5CONbits.TON=0;
  436. TMR5=0;
  437. PulseNum=0;
  438. if(MOTOR3_HOME == 1)
  439. {
  440. MOTOR3_DIR = MOTOR3_FORWARD;
  441. Freq=5000000/LEAVspd;
  442. TMR5=0;
  443. PR5=Freq;
  444. IFS1bits.T5IF = 0;
  445. T5CONbits.TON=1;
  446. while(MOTOR3_HOME)
  447. {
  448. if(PulseNum > Motor3.Limit_Pulse*2) {Motor_Status=31;break;}
  449. }
  450. T5CONbits.TON=0;
  451. MOTOR3_PWM=0;
  452. }
  453. else
  454. {
  455. MOTOR3_DIR = MOTOR3_REVERSAL;
  456. Freq=5000000/MOVEspd;
  457. TMR5=0;
  458. PR5=Freq;
  459. IFS1bits.T5IF = 0;
  460. T5CONbits.TON=1;
  461. while(PulseNum < Motor3.Limit_Pulse*2)
  462. {
  463. if(MOTOR3_HOME)
  464. break;
  465. }
  466. if(PulseNum ==Motor3.Limit_Pulse*2||PulseNum >Motor3.Limit_Pulse*2) { Motor_Status=31;}
  467. T5CONbits.TON=0;
  468. Freq=5000000/LEAVspd;
  469. TMR5=0;
  470. PR5=Freq;
  471. IFS1bits.T5IF = 0;
  472. T5CONbits.TON=1;
  473. PulseNum=0;
  474. while(PulseNum < Motor3.Overshoot_Pulse*2);
  475. T5CONbits.TON=0;
  476. Delay1ms(1000);
  477. MOTOR3_DIR = MOTOR3_FORWARD;
  478. Freq=5000000/LEAVspd;
  479. TMR5=0;
  480. PR5=Freq;
  481. IFS1bits.T5IF = 0;
  482. T5CONbits.TON=1;
  483. PulseNum=0;
  484. while(MOTOR3_HOME)
  485. {
  486. if(PulseNum > Motor3.Limit_Pulse*2) {Motor_Status=31;break;}
  487. }
  488. T5CONbits.TON=0;
  489. MOTOR3_PWM=0;
  490. }
  491. MOTOR3_DIR = MOTOR3_FORWARD;
  492. Freq=5000000/LEAVspd;
  493. TMR5=0;
  494. PR5=Freq;
  495. IFS1bits.T5IF = 0;
  496. T5CONbits.TON=1;
  497. PulseNum=0;
  498. while(!MOTOR3_HOME)
  499. {
  500. if(PulseNum >= pulseoffset*2 ) break;
  501. }
  502. T5CONbits.TON=0;
  503. MOTOR3_PWM=0;
  504. InitMotor3PWM();
  505. g_uliRunningPulse = 0;
  506. g_tReagentMotorParam.ulZ1iPosition =0;
  507. }
  508. void Motor4Home(unsigned int pulseoffset,unsigned int MOVEspd,unsigned int LEAVspd )
  509. {
  510. unsigned int Freq;
  511. Motor1_HOME_Flag=0;
  512. Motor2_HOME_Flag=0;
  513. Motor3_HOME_Flag=0;
  514. Motor4_HOME_Flag=1;
  515. Motor5_HOME_Flag=0;
  516. OC4CON = 0;
  517. g_tReagentMotorParam.uc4Direction = MOTOR4_FORWARD;
  518. Delay1ms(2);
  519. T5CONbits.TON=0;
  520. TMR5=0;
  521. PulseNum=0;
  522. if(MOTOR4_HOME == 1)
  523. {
  524. MOTOR4_DIR = MOTOR4_FORWARD;
  525. Freq=5000000/LEAVspd;
  526. TMR5=0;
  527. PR5=Freq;
  528. IFS1bits.T5IF = 0;
  529. T5CONbits.TON=1;
  530. while(MOTOR4_HOME)
  531. {
  532. if(PulseNum > Motor4.Limit_Pulse*2) {Motor_Status=41;break;}
  533. }
  534. T5CONbits.TON=0;
  535. MOTOR4_PWM=0;
  536. }
  537. else
  538. {
  539. MOTOR4_DIR = MOTOR4_REVERSAL;
  540. Freq=5000000/MOVEspd;
  541. TMR5=0;
  542. PR5=Freq;
  543. IFS1bits.T5IF = 0;
  544. T5CONbits.TON=1;
  545. while(PulseNum < Motor4.Limit_Pulse*2)
  546. {
  547. if(MOTOR4_HOME)
  548. break;
  549. }
  550. if(PulseNum ==Motor4.Limit_Pulse*2||PulseNum >Motor4.Limit_Pulse*2) { Motor_Status=41;}
  551. T5CONbits.TON=0;
  552. Freq=5000000/LEAVspd;
  553. TMR5=0;
  554. PR5=Freq;
  555. IFS1bits.T5IF = 0;
  556. T5CONbits.TON=1;
  557. PulseNum=0;
  558. while(PulseNum < Motor4.Overshoot_Pulse*2);
  559. T5CONbits.TON=0;
  560. Delay1ms(1000);
  561. MOTOR4_DIR = MOTOR4_FORWARD;
  562. Freq=5000000/LEAVspd;
  563. TMR5=0;
  564. PR5=Freq;
  565. IFS1bits.T5IF = 0;
  566. T5CONbits.TON=1;
  567. PulseNum=0;
  568. while(MOTOR4_HOME)
  569. {
  570. if(PulseNum > Motor4.Limit_Pulse*2) {Motor_Status=41;break;}
  571. }
  572. T5CONbits.TON=0;
  573. MOTOR4_PWM=0;
  574. }
  575. MOTOR4_DIR = MOTOR4_FORWARD;
  576. Freq=5000000/LEAVspd;
  577. TMR5=0;
  578. PR5=Freq;
  579. IFS1bits.T5IF = 0;
  580. T5CONbits.TON=1;
  581. PulseNum=0;
  582. while(!MOTOR4_HOME)
  583. {
  584. if(PulseNum >= pulseoffset*2 ) break;
  585. }
  586. T5CONbits.TON=0;
  587. MOTOR4_PWM=0;
  588. InitMotor4PWM();
  589. g_uliRunningPulse = 0;
  590. g_tReagentMotorParam.ul4iPosition =0;
  591. }
  592. void Motor1MoveTo(unsigned long int pulse,unsigned int spd)
  593. {
  594. unsigned long int uliMotorPulse = 0;
  595. if(pulse > g_tReagentMotorParam.ulXiPosition)
  596. {
  597. uliMotorPulse = pulse - g_tReagentMotorParam.ulXiPosition;
  598. Motor1Move(uliMotorPulse,!Motor1.Reset_Direction,spd);
  599. }
  600. else if(pulse < g_tReagentMotorParam.ulXiPosition)
  601. {
  602. uliMotorPulse = g_tReagentMotorParam.ulXiPosition - pulse;
  603. Motor1Move(uliMotorPulse,Motor1.Reset_Direction,spd);
  604. }
  605. else
  606. {
  607. }
  608. }
  609. void Motor2MoveTo_ok(unsigned long int pulse,unsigned int spd)
  610. {
  611. if(Z_di_bu_position>21000)
  612. {
  613. pulse=pulse+(Z_di_bu_position-21000);
  614. }
  615. else
  616. {
  617. pulse=pulse-(21000-Z_di_bu_position);
  618. }
  619. Motor2MoveTo(pulse,spd);
  620. }
  621. void Motor2MoveTo(unsigned long int pulse,unsigned int spd)
  622. {
  623. unsigned long int uliMotorPulse = 0;
  624. if(pulse > g_tReagentMotorParam.ulZiPosition)
  625. {
  626. uliMotorPulse = pulse - g_tReagentMotorParam.ulZiPosition;
  627. Motor2Move(uliMotorPulse,!Motor2.Reset_Direction,spd);
  628. }
  629. else if(pulse < g_tReagentMotorParam.ulZiPosition)
  630. {
  631. uliMotorPulse = g_tReagentMotorParam.ulZiPosition - pulse;
  632. Motor2Move(uliMotorPulse,Motor2.Reset_Direction,spd);
  633. }
  634. else
  635. {
  636. }
  637. }
  638. void Motor3MoveTo(unsigned long int pulse,unsigned int spd)
  639. {
  640. unsigned long int uliMotorPulse = 0;
  641. if(pulse > g_tReagentMotorParam.ulZ1iPosition)
  642. {
  643. uliMotorPulse = pulse - g_tReagentMotorParam.ulZ1iPosition;
  644. Motor3Move(uliMotorPulse,!Motor3.Reset_Direction,spd);
  645. }
  646. else if(pulse < g_tReagentMotorParam.ulZ1iPosition)
  647. {
  648. uliMotorPulse = g_tReagentMotorParam.ulZ1iPosition - pulse;
  649. Motor3Move(uliMotorPulse,Motor3.Reset_Direction,spd);
  650. }
  651. else
  652. {
  653. }
  654. }
  655. void Motor4MoveTo(unsigned long int pulse,unsigned int spd)
  656. {
  657. unsigned long int uliMotorPulse = 0;
  658. if(pulse > g_tReagentMotorParam.ul4iPosition)
  659. {
  660. uliMotorPulse = pulse - g_tReagentMotorParam.ul4iPosition;
  661. Motor4Move(uliMotorPulse,!Motor4.Reset_Direction,spd);
  662. }
  663. else if(pulse < g_tReagentMotorParam.ul4iPosition)
  664. {
  665. uliMotorPulse = g_tReagentMotorParam.ul4iPosition - pulse;
  666. Motor4Move(uliMotorPulse,Motor4.Reset_Direction,spd);
  667. }
  668. else
  669. {
  670. }
  671. }
  672. /*******************************************************************/
  673. void Motor1Move(unsigned long int pulse,unsigned char dir,unsigned int spd)
  674. {
  675. if(g_tReagentMotorParam.ulZiPosition>Z_limitation1||g_tReagentMotorParam.ulZiPosition>Z1_limitation1)
  676. {
  677. if(X_move_protection_flag==0)
  678. {
  679. Motor_Status=13;
  680. return;
  681. }
  682. }
  683. InitMotor1PWM();
  684. if(pulse ==0) return;
  685. MOTOR1_DIR = dir;
  686. if(dir==MOTOR1_FORWARD)
  687. {
  688. if(( g_tReagentMotorParam.ulXiPosition+pulse)>Motor1.Limit_Pulse)
  689. {
  690. Motor_Status=12;
  691. return;
  692. }
  693. }
  694. if(dir==MOTOR1_REVERSAL)
  695. {
  696. if( g_tReagentMotorParam.ulXiPosition<pulse)
  697. {
  698. Motor_Status=12;
  699. return;
  700. }
  701. }
  702. g_uiAccIndex=0;
  703. g_uliRunningPulse=0;
  704. g_ucMotorXFinishFlag = 0;
  705. g_uliPulseCount = pulse;
  706. g_tReagentMotorParam.ucXDirection=dir?1:0;
  707. if(spd>REAGENT_MOTOR_FREQMIN)
  708. {
  709. if(spd>REAGENT_MOTOR_FREQMAX)
  710. {
  711. spd=REAGENT_MOTOR_FREQMAX;
  712. }
  713. SetMotor1(pulse,spd,&(g_tReagentMotorParam.ulXiAccPulse),&(g_tReagentMotorParam.ulXiConstPulse));
  714. g_tReagentMotorParam.uiXSpeed_switch=0;
  715. g_tReagentMotorParam.uiXSpeed=spd;
  716. }
  717. else
  718. {
  719. g_tReagentMotorParam.uiXSpeed_switch=1;
  720. if(spd<MOTOR_FREQLIMIT_L)
  721. {
  722. spd=MOTOR_FREQLIMIT_L;
  723. }
  724. if(spd>MOTOR_FREQLIMIT_H)
  725. {
  726. spd=MOTOR_FREQLIMIT_H;
  727. }
  728. g_tReagentMotorParam.uiXSpeed=spd;
  729. g_tReagentMotorParam.uiXprd=(unsigned short)(625000.0/ g_tReagentMotorParam.uiXSpeed);
  730. }
  731. IEC0bits.OC1IE = 1;
  732. T2CONbits.TON=1;
  733. while(!g_ucMotorXFinishFlag);
  734. OC1CON=0;
  735. }
  736. void Motor2Move(unsigned long int pulse,unsigned char dir,unsigned int spd)
  737. {
  738. InitMotor2PWM();
  739. if(pulse ==0) return;
  740. MOTOR2_DIR = dir;//r
  741. g_tReagentMotorParam.ucZDirection = dir?1:0;
  742. if(dir==MOTOR2_FORWARD)
  743. {
  744. if(( g_tReagentMotorParam.ulZiPosition+pulse)>Motor2.Limit_Pulse)
  745. if( g_tReagentMotorParam.ulXiPosition<pulse)
  746. {
  747. Motor_Status=22;
  748. return;
  749. }
  750. }
  751. if(dir==MOTOR2_REVERSAL)
  752. {
  753. if( g_tReagentMotorParam.ulZiPosition<pulse)
  754. if( g_tReagentMotorParam.ulXiPosition<pulse)
  755. {
  756. Motor_Status=22;
  757. return;
  758. }
  759. }
  760. g_uiAccIndex=0;
  761. g_uliRunningPulse=0;
  762. g_ucMotorZFinishFlag = 0;
  763. g_uliPulseCount = pulse;
  764. if(spd>REAGENT_MOTOR_FREQMIN2)
  765. {
  766. if(spd>Motor2.Limit_speed)
  767. {
  768. spd=Motor2.Limit_speed;
  769. }
  770. SetMotor2(pulse,spd,&(g_tReagentMotorParam.ulZiAccPulse),&(g_tReagentMotorParam.ulZiConstPulse));
  771. g_tReagentMotorParam.uiZSpeed_switch=0;
  772. g_tReagentMotorParam.uiZSpeed=spd;
  773. }
  774. else
  775. {
  776. g_tReagentMotorParam.uiZSpeed_switch=1;
  777. if(spd<MOTOR_FREQLIMIT_L)
  778. {
  779. spd=MOTOR_FREQLIMIT_L;
  780. }
  781. if(spd>MOTOR_FREQLIMIT_H)
  782. {
  783. spd=MOTOR_FREQLIMIT_H;
  784. }
  785. g_tReagentMotorParam.uiZSpeed=spd;
  786. g_tReagentMotorParam.uiZprd=(unsigned short)(625000.0/ g_tReagentMotorParam.uiZSpeed);
  787. }
  788. IEC0bits.OC2IE = 1;
  789. T2CONbits.TON=1;
  790. while(!g_ucMotorZFinishFlag);
  791. OC2CON=0;
  792. }
  793. void Motor3Move(unsigned long int pulse,unsigned char dir,unsigned int spd)//spd --HZ
  794. {
  795. InitMotor3PWM();
  796. if(pulse ==0) return;
  797. MOTOR3_DIR = dir;
  798. if(dir==MOTOR3_FORWARD)
  799. {
  800. if(( g_tReagentMotorParam.ulZ1iPosition+pulse)>Motor3.Limit_Pulse)
  801. {
  802. Motor_Status=32;
  803. return;
  804. }
  805. }
  806. if(dir==MOTOR3_REVERSAL)
  807. {
  808. if( g_tReagentMotorParam.ulZ1iPosition<pulse)
  809. {
  810. Motor_Status=32;
  811. return;
  812. }
  813. }
  814. g_tReagentMotorParam.ucZ1Direction = dir?1:0;
  815. g_uiAccIndex=0;
  816. g_uliRunningPulse=0;
  817. g_ucMotorZ1FinishFlag = 0;
  818. g_uliPulseCount = pulse;
  819. if(spd>REAGENT_MOTOR_FREQMIN3)
  820. {
  821. if(spd>REAGENT_MOTOR_FREQMAX3)
  822. {
  823. spd=REAGENT_MOTOR_FREQMAX3;
  824. }
  825. SetMotor3(pulse,spd,&(g_tReagentMotorParam.ulZ1iAccPulse),&(g_tReagentMotorParam.ulZ1iConstPulse));
  826. g_tReagentMotorParam.uiZ1Speed_switch=0;
  827. g_tReagentMotorParam.uiZ1Speed=spd;
  828. }
  829. else
  830. {
  831. g_tReagentMotorParam.uiZ1Speed_switch=1;
  832. if(spd<MOTOR_FREQLIMIT_L)
  833. {
  834. spd=MOTOR_FREQLIMIT_L;
  835. }
  836. if(spd>MOTOR_FREQLIMIT_H)
  837. {
  838. spd=MOTOR_FREQLIMIT_H;
  839. }
  840. g_tReagentMotorParam.uiZ1Speed=spd;
  841. g_tReagentMotorParam.uiZ1prd=(unsigned short)(625000.0/ g_tReagentMotorParam.uiZ1Speed);
  842. }
  843. IEC1bits.OC3IE = 1;
  844. T2CONbits.TON=1; //
  845. while(!g_ucMotorZ1FinishFlag);
  846. OC3CON=0;
  847. }
  848. void Motor4Move(unsigned long int pulse,unsigned char dir,unsigned int spd)//spd --HZ
  849. {
  850. InitMotor4PWM();
  851. if(pulse ==0) return;
  852. MOTOR4_DIR = dir;
  853. if(dir==MOTOR4_FORWARD)
  854. {
  855. if(( g_tReagentMotorParam.ul4iPosition+pulse)>Motor4.Limit_Pulse)
  856. {
  857. Motor_Status=42;
  858. return;
  859. }
  860. }
  861. if(dir==MOTOR4_REVERSAL)
  862. {
  863. if( g_tReagentMotorParam.ul4iPosition<pulse)
  864. {
  865. Motor_Status=42;
  866. return;
  867. }
  868. }
  869. g_tReagentMotorParam.uc4Direction = dir?1:0;
  870. g_uiAccIndex=0;
  871. g_uliRunningPulse=0;
  872. g_ucMotor4FinishFlag = 0;
  873. g_uliPulseCount = pulse;
  874. if(spd>REAGENT_MOTOR_FREQMIN4)
  875. {
  876. if(spd>REAGENT_MOTOR_FREQMAX4)
  877. {
  878. spd=REAGENT_MOTOR_FREQMAX4;
  879. }
  880. SetMotor4(pulse,spd,&(g_tReagentMotorParam.ul4iAccPulse),&(g_tReagentMotorParam.ul4iConstPulse));
  881. g_tReagentMotorParam.ui4Speed_switch=0;
  882. g_tReagentMotorParam.ui4Speed=spd;
  883. }
  884. else
  885. {
  886. g_tReagentMotorParam.ui4Speed_switch=1;
  887. if(spd<MOTOR_FREQLIMIT_L)
  888. {
  889. spd=MOTOR_FREQLIMIT_L;
  890. }
  891. if(spd>MOTOR_FREQLIMIT_H)
  892. {
  893. spd=MOTOR_FREQLIMIT_H;
  894. }
  895. g_tReagentMotorParam.ui4Speed=spd;
  896. g_tReagentMotorParam.ui4prd=(unsigned short)(625000.0/ g_tReagentMotorParam.ui4Speed);
  897. }
  898. IEC1bits.OC4IE = 1;
  899. T2CONbits.TON=1; //
  900. while(!g_ucMotor4FinishFlag);
  901. OC4CON=0;
  902. }
  903. void Move_to_by_volume(unsigned int volume,unsigned int spd)//spd --HZ
  904. {
  905. unsigned int pulse;
  906. if(volume<40)
  907. {
  908. pulse=(unsigned int)(Z_di_bu_position-(volume*12.5+0));
  909. }
  910. else if(volume<400)
  911. {
  912. pulse=(unsigned int)(Z_di_bu_position-(volume*10.05139057+103.5066505));
  913. }
  914. else if(volume<1100)
  915. {
  916. pulse=(unsigned int)(Z_di_bu_position-(volume*6.833333333+1366.666667));
  917. }
  918. else
  919. {
  920. return;
  921. }
  922. Motor2MoveTo( pulse, spd);
  923. }