AllSeroDrv.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. #include "global.h"
  2. #include "AllServoDrv.h"
  3. axis_object_t *axis_x;
  4. axis_object_t *axis_y;
  5. axis_object_t *axis_z;
  6. float XGearRatio = 1,YGearRatio = 1,ZGearRatio = 1;
  7. static int axis_x_ctrl_mode=0,axis_y_ctrl_mode=0,axis_z_ctrl_mode=0;
  8. unsigned char axis_x_com_alarm=0,axis_y_com_alarm=0,axis_z_com_alarm=0;
  9. void SetDir(unsigned short axis, unsigned short dir)
  10. {
  11. switch(axis)
  12. {
  13. case X_AXIS:
  14. if(dir)
  15. axis_cw(axis_x);
  16. else
  17. axis_ccw(axis_x);
  18. break;
  19. case Y_AXIS:
  20. if(dir)
  21. axis_cw(axis_y);
  22. else
  23. axis_ccw(axis_y);
  24. break;
  25. case Z_AXIS:
  26. if(dir)
  27. axis_cw(axis_z);
  28. else
  29. axis_ccw(axis_z);
  30. break;
  31. default:;
  32. }
  33. }
  34. void SetDirReverse(unsigned short axis, int value)
  35. {
  36. switch(axis)
  37. {
  38. case X_AXIS:
  39. if(value)
  40. axis_set_parameter (axis_x,AXIS_DIR_REVERSE,1);
  41. else
  42. axis_set_parameter (axis_x,AXIS_DIR_REVERSE,0);
  43. break;
  44. case Y_AXIS:
  45. if(value)
  46. axis_set_parameter (axis_y,AXIS_DIR_REVERSE,1);
  47. else
  48. axis_set_parameter (axis_y,AXIS_DIR_REVERSE,0);
  49. break;
  50. case Z_AXIS:
  51. if(value)
  52. axis_set_parameter (axis_z,AXIS_DIR_REVERSE,1);
  53. else
  54. axis_set_parameter (axis_z,AXIS_DIR_REVERSE,0);
  55. break;
  56. default:;
  57. }
  58. }
  59. unsigned char GetDir(unsigned short axis)
  60. {
  61. switch(axis)
  62. {
  63. case X_AXIS: return axis_x->outputs.dir;break;
  64. case Y_AXIS: return axis_y->outputs.dir;break;
  65. case Z_AXIS: return axis_z->outputs.dir;break;
  66. default:return axis_x->outputs.dir;
  67. }
  68. }
  69. //设置使能
  70. void SetEn(unsigned short axis, unsigned short ena)
  71. {
  72. switch(axis)
  73. {
  74. case X_AXIS:
  75. if(ena)
  76. axis_enable(axis_x);
  77. else
  78. axis_disable(axis_x);
  79. break;
  80. case Y_AXIS:
  81. if(ena)
  82. axis_enable(axis_y);
  83. else
  84. axis_disable(axis_y);
  85. break;
  86. case Z_AXIS:
  87. if(ena)
  88. axis_enable(axis_z);
  89. else
  90. axis_disable(axis_z);
  91. break;
  92. default:;
  93. }
  94. }
  95. //设置使能电平
  96. void SetEnReverse(unsigned short axis, int value)
  97. {
  98. switch(axis)
  99. {
  100. case X_AXIS:
  101. if(value)
  102. axis_set_parameter (axis_x,AXIS_EN_REVERSE,1);
  103. else
  104. axis_set_parameter (axis_x,AXIS_EN_REVERSE,0);
  105. break;
  106. case Y_AXIS:
  107. if(value)
  108. axis_set_parameter (axis_y,AXIS_EN_REVERSE,1);
  109. else
  110. axis_set_parameter (axis_y,AXIS_EN_REVERSE,0);
  111. break;
  112. case Z_AXIS:
  113. if(value)
  114. axis_set_parameter (axis_z,AXIS_EN_REVERSE,1);
  115. else
  116. axis_set_parameter (axis_z,AXIS_EN_REVERSE,0);
  117. break;
  118. default:;
  119. }
  120. }
  121. //设置伺服告警的极性
  122. void SetAlarmReverse(unsigned short axis, int value)
  123. {
  124. switch(axis)
  125. {
  126. case X_AXIS:
  127. if(value){
  128. axis_set_parameter (axis_x,AXIS_ALARM_REVERSE,1);
  129. set_com_alarm_reverse(axis,1);
  130. }
  131. else{
  132. axis_set_parameter (axis_x,AXIS_ALARM_REVERSE,0);
  133. set_com_alarm_reverse(axis,0);
  134. }
  135. break;
  136. case Y_AXIS:
  137. if(value){
  138. axis_set_parameter (axis_y,AXIS_ALARM_REVERSE,1);
  139. set_com_alarm_reverse(axis,1);
  140. }
  141. else{
  142. axis_set_parameter (axis_y,AXIS_ALARM_REVERSE,0);
  143. set_com_alarm_reverse(axis,0);
  144. }
  145. break;
  146. case Z_AXIS:
  147. if(value){
  148. axis_set_parameter (axis_z,AXIS_ALARM_REVERSE,1);
  149. set_com_alarm_reverse(axis,0);
  150. }
  151. else{
  152. axis_set_parameter (axis_z,AXIS_ALARM_REVERSE,0);
  153. set_com_alarm_reverse(axis,0);
  154. }
  155. break;
  156. default:;
  157. }
  158. }
  159. unsigned char GetEn(unsigned short axis)
  160. {
  161. switch(axis)
  162. {
  163. case X_AXIS:return (axis_x->outputs.enable);break;
  164. case Y_AXIS:return (axis_y->outputs.enable);break;
  165. case Z_AXIS:return (axis_z->outputs.enable);break;
  166. default:return (axis_x->outputs.enable);
  167. }
  168. }
  169. unsigned char GetAlarm(unsigned short axis)
  170. {
  171. switch(axis)
  172. {
  173. case X_AXIS:
  174. if(axis_x_ctrl_mode){//通讯模式
  175. return axis_x_com_alarm;
  176. }
  177. else {//IO模式
  178. if(axis_x->inputs.alarm_reverse){
  179. if(hw_pwm_get_alr(axis_x->axis_no))return 1;else return 0;
  180. }
  181. else
  182. if(hw_pwm_get_alr(axis_x->axis_no))return 0;else return 1;
  183. }
  184. break;
  185. case Y_AXIS:
  186. if(axis_y_ctrl_mode){//通讯模式
  187. return axis_y_com_alarm;
  188. }
  189. else {//IO模式
  190. if(axis_y->inputs.alarm_reverse){
  191. if(hw_pwm_get_alr(axis_y->axis_no))return 1;else return 0;
  192. }
  193. else
  194. if(hw_pwm_get_alr(axis_y->axis_no))return 0;else return 1;
  195. }
  196. break;
  197. case Z_AXIS:
  198. if(axis_z_ctrl_mode){//通讯模式
  199. return axis_z_com_alarm;
  200. }
  201. else {//IO模式
  202. if(axis_z->inputs.alarm_reverse){
  203. if(hw_pwm_get_alr(axis_z->axis_no))return 1;else return 0;
  204. }
  205. else
  206. if(hw_pwm_get_alr(axis_z->axis_no))return 0;else return 1;
  207. }
  208. break;
  209. default:return hw_pwm_get_alr(axis_x->axis_no);
  210. }
  211. }
  212. void SetClr(unsigned short axis, unsigned short clr)
  213. {
  214. switch(axis)
  215. {
  216. case X_AXIS:
  217. if(axis_x_ctrl_mode){//通讯模式
  218. if(clr)clr_com_servo_alarm(axis);
  219. }
  220. else{//IO模式
  221. hw_pwm_set_clr(axis_x->axis_no, clr);
  222. }
  223. break;
  224. case Y_AXIS:
  225. if(axis_y_ctrl_mode){//通讯模式
  226. if(clr)clr_com_servo_alarm(axis);
  227. }
  228. else{//IO模式
  229. hw_pwm_set_clr(axis_y->axis_no, clr);
  230. }
  231. break;
  232. case Z_AXIS:
  233. if(axis_y_ctrl_mode){//通讯模式
  234. if(clr)clr_com_servo_alarm(axis);
  235. }
  236. else{//IO模式
  237. hw_pwm_set_clr(axis_z->axis_no, clr);
  238. }
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. unsigned char GetTarr(unsigned short axis)
  245. {
  246. switch(axis)
  247. {
  248. case X_AXIS:return hw_pwm_get_tarr(axis_x->axis_no);break;
  249. case Y_AXIS:return hw_pwm_get_tarr(axis_y->axis_no);break;
  250. case Z_AXIS:return hw_pwm_get_tarr(axis_z->axis_no);break;
  251. default:return hw_pwm_get_tarr(axis_x->axis_no);
  252. }
  253. }
  254. /**
  255. * 切换控制模式切换,转矩+位置模式
  256. *
  257. * @author LAZ (121919)
  258. *
  259. * @param axis
  260. * @param mode,0为IO转矩模式,1为位置模式
  261. */
  262. void Set_Ctrlmode_trans(unsigned short axis, unsigned short mode)
  263. {
  264. switch(axis)
  265. {
  266. case X_AXIS:
  267. hw_pwm_set_tmode(axis_x->axis_no, mode);
  268. break;
  269. case Y_AXIS:
  270. hw_pwm_set_tmode(axis_y->axis_no, mode);
  271. break;
  272. case Z_AXIS:
  273. hw_pwm_set_tmode(axis_z->axis_no, mode);
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. /**
  280. * 设置伺服的运行模式
  281. *
  282. * @author LAZ (121919)
  283. *
  284. * @param axis
  285. * @param mode,0为IO模式,1为通讯模式
  286. */
  287. void Set_Servo_Runmode(unsigned short axis, unsigned short mode)
  288. {
  289. switch(axis)
  290. {
  291. case X_AXIS:
  292. axis_x_ctrl_mode=mode;
  293. break;
  294. case Y_AXIS:
  295. axis_y_ctrl_mode=mode;
  296. break;
  297. case Z_AXIS:
  298. axis_z_ctrl_mode=mode;
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. //脉冲转化为距离
  305. long PulseToPos(unsigned short axis, long pulse)
  306. {
  307. float pulse_buff;
  308. pulse_buff = pulse;
  309. switch(axis)
  310. {
  311. case X_AXIS: return (long)(pulse_buff/XGearRatio);break;
  312. case Y_AXIS: return (long)(pulse_buff/YGearRatio);break;
  313. case Z_AXIS: return (long)(pulse_buff/ZGearRatio);break;
  314. default:return (long)(pulse_buff/XGearRatio);;
  315. }
  316. }
  317. //距离转化为脉冲
  318. long PosToPulse(unsigned short axis, long pos)
  319. {
  320. float pos_buff;
  321. pos_buff = pos;
  322. switch(axis)
  323. {
  324. case X_AXIS: return (long)(pos_buff*XGearRatio);break;
  325. case Y_AXIS: return (long)(pos_buff*YGearRatio);break;
  326. case Z_AXIS: return (long)(pos_buff*ZGearRatio);break;
  327. default:return (long)(pos_buff*XGearRatio);
  328. }
  329. }
  330. //设置真实位置
  331. void SetPos(unsigned short axis, long pos)
  332. {
  333. switch(axis)
  334. {
  335. case X_AXIS:
  336. //if(axis_x_ctrl_mode){//通讯模式
  337. // axis_set_com_pos(axis,pos );
  338. //}
  339. //else {
  340. axis_set_parameter(axis_x, AXIS_POSITION, PosToPulse(axis,pos));
  341. //}
  342. break;
  343. case Y_AXIS:
  344. if(axis_y_ctrl_mode){//通讯模式
  345. axis_set_com_pos(axis,pos );
  346. }
  347. else {
  348. axis_set_parameter(axis_y, AXIS_POSITION, PosToPulse(axis,pos));
  349. }
  350. break;
  351. case Z_AXIS:
  352. //if(axis_z_ctrl_mode){//通讯模式
  353. // axis_set_com_pos(axis,pos );
  354. //}
  355. // else {
  356. axis_set_parameter(axis_z, AXIS_POSITION, PosToPulse(axis,pos));
  357. //}
  358. break;
  359. default:;
  360. }
  361. }
  362. //获取真实位置
  363. long GetPos(unsigned short axis)
  364. {
  365. switch(axis)
  366. {
  367. case X_AXIS:
  368. {
  369. // if(axis_x_ctrl_mode){//通讯模式
  370. // return get_encode_value(axis);
  371. // }
  372. // else {//IO模式
  373. return PulseToPos(axis, axis_get_parameter(axis_x, AXIS_POSITION));
  374. // }
  375. }break;
  376. case Y_AXIS:
  377. {
  378. // if(axis_y_ctrl_mode){//通讯模式
  379. // return get_encode_value(axis);
  380. // }
  381. // else {//IO模式
  382. return PulseToPos(axis, axis_get_parameter(axis_y, AXIS_POSITION));
  383. // }
  384. }break;
  385. case Z_AXIS:
  386. {
  387. if(axis_z_ctrl_mode){//通讯模式
  388. return get_encode_value(axis);
  389. }
  390. else {//IO模式
  391. return PulseToPos(axis, axis_get_parameter(axis_z, AXIS_POSITION));
  392. }
  393. }break;
  394. default:;
  395. }
  396. return 0;
  397. }
  398. //获取当前速度
  399. unsigned long GetCurSpeed(unsigned short axis)
  400. {
  401. switch(axis)
  402. {
  403. case X_AXIS:
  404. axis_x->cur_speed=axis_x->clock/axis_x->cur_reg_value;
  405. return axis_x->cur_speed; break;
  406. case Y_AXIS:
  407. axis_y->cur_speed=axis_y->clock/axis_y->cur_reg_value;
  408. return axis_y->cur_speed; break;
  409. case Z_AXIS:
  410. axis_z->cur_speed=axis_z->clock/axis_z->cur_reg_value;
  411. return axis_z->cur_speed; break;
  412. default:
  413. axis_x->cur_speed=axis_x->clock/axis_x->cur_reg_value;
  414. return axis_x->cur_speed; break;
  415. }
  416. // return 0;
  417. }
  418. //获取设置速度
  419. unsigned long GetSetSpeed(unsigned short axis)
  420. {
  421. return 0;
  422. }
  423. //获取脉冲状态
  424. unsigned long GetState(unsigned short axis)
  425. {
  426. return 0;
  427. }
  428. //获取脉冲状态
  429. unsigned long SetState(unsigned short axis,unsigned short state)
  430. {
  431. return 0;
  432. }
  433. //获取剩余脉冲数
  434. unsigned long GetRemainPulse(unsigned short axis)
  435. {
  436. return 0;
  437. }
  438. //设置脉冲数
  439. unsigned long SetPulse(unsigned short axis,unsigned long pulse)
  440. {
  441. return 0;
  442. }
  443. //设置起动速度
  444. void SetStartSpeed(unsigned short axis,unsigned short speed)
  445. {
  446. switch(axis)
  447. {
  448. case X_AXIS:axis_set_parameter(axis_x, AXIS_START_SPEED,speed);break;
  449. case Y_AXIS:axis_set_parameter(axis_y, AXIS_START_SPEED,speed);break;
  450. case Z_AXIS:axis_set_parameter(axis_z, AXIS_START_SPEED,speed);break;
  451. default:;
  452. }
  453. }
  454. //设置最低速度
  455. void SetMinSpeed(unsigned short axis,unsigned short speed)
  456. {
  457. }
  458. void SetAccTime(unsigned short axis,unsigned short acc_time)
  459. {
  460. switch(axis)
  461. {
  462. case X_AXIS:axis_set_parameter(axis_x, AXIS_ACC_TIME,acc_time);break;
  463. case Y_AXIS:axis_set_parameter(axis_y, AXIS_ACC_TIME,acc_time);break;
  464. case Z_AXIS:axis_set_parameter(axis_z, AXIS_ACC_TIME,acc_time);break;
  465. default:;
  466. }
  467. }
  468. void SetDecTime(unsigned short axis,unsigned short dec_time)
  469. {
  470. switch(axis)
  471. {
  472. case X_AXIS:axis_set_parameter(axis_x, AXIS_DEC_TIME,dec_time);break;
  473. case Y_AXIS:axis_set_parameter(axis_y, AXIS_DEC_TIME,dec_time);break;
  474. case Z_AXIS:axis_set_parameter(axis_z, AXIS_DEC_TIME,dec_time);break;
  475. default:;
  476. }
  477. }
  478. void SetStopSelect(unsigned short axis,unsigned short Select_Flag)
  479. {
  480. switch(axis)
  481. {
  482. case X_AXIS:axis_set_parameter(axis_x, AXIS_PP_STOP_SELECT,Select_Flag);break;
  483. case Y_AXIS:axis_set_parameter(axis_y, AXIS_PP_STOP_SELECT,Select_Flag);break;
  484. case Z_AXIS:axis_set_parameter(axis_z, AXIS_PP_STOP_SELECT,Select_Flag);break;
  485. default:;
  486. }
  487. }
  488. //运动当中改变速度
  489. void AxisChangeSpeed(unsigned short axis,unsigned short speed)
  490. {
  491. switch(axis)
  492. {
  493. case X_AXIS:
  494. axis_pv_change_speed(axis_x,speed);
  495. break;
  496. case Y_AXIS:
  497. axis_pv_change_speed(axis_y,speed);
  498. break;
  499. case Z_AXIS:
  500. axis_pv_change_speed(axis_z,speed);
  501. break;
  502. default:;
  503. }
  504. }
  505. //固定减速距离停止
  506. void AxisDecStopPos(unsigned short axis,unsigned long pos)
  507. {
  508. switch(axis)
  509. {
  510. case X_AXIS: axis_stop_at(axis_x,PosToPulse(X_AXIS,pos));break;
  511. case Y_AXIS: axis_stop_at(axis_y,PosToPulse(Y_AXIS,pos));break;
  512. case Z_AXIS: axis_stop_at(axis_z,PosToPulse(Z_AXIS,pos));break;
  513. default:;
  514. }
  515. }
  516. //减速停
  517. void AxisDecStop(unsigned short axis)
  518. {
  519. switch(axis)
  520. {
  521. case X_AXIS:
  522. axis_stop(axis_x);break;
  523. case Y_AXIS:
  524. axis_stop(axis_y);break;
  525. case Z_AXIS:
  526. axis_stop(axis_z);break;
  527. default:
  528. break;
  529. }
  530. }
  531. //急停
  532. void AxisEgmStop(unsigned short axis)
  533. {
  534. switch(axis)
  535. {
  536. case X_AXIS:
  537. {
  538. axis_emgstop(axis_x);
  539. }break;
  540. case Y_AXIS:
  541. {
  542. axis_emgstop(axis_y);
  543. }break;
  544. case Z_AXIS:
  545. {
  546. axis_emgstop(axis_z);
  547. }break;
  548. default:
  549. break;
  550. }
  551. }
  552. //移动绝对位置
  553. void AxisMovePoint(unsigned short axis,unsigned short speed,long pos)
  554. {
  555. switch(axis)
  556. {
  557. case X_AXIS: axis_pp(axis_x, 1, PosToPulse(X_AXIS,pos),speed,0);break;
  558. case Y_AXIS: axis_pp(axis_y, 1, PosToPulse(Y_AXIS,pos),speed,0);break;
  559. case Z_AXIS: axis_pp(axis_z, 1, PosToPulse(Z_AXIS,pos),speed,0);break;
  560. default:;
  561. }
  562. }
  563. //移动绝对位置带加减速
  564. void AxisMovePointAccDec(unsigned short axis,unsigned short speed,long pos,
  565. unsigned short start_speed,unsigned short stop_speed,
  566. unsigned short acc_pulse,unsigned short dec_pulse)
  567. {
  568. switch(axis)
  569. {
  570. case X_AXIS:
  571. axis_x->start_speed = start_speed * axis_x->speed_unit;
  572. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  573. axis_x->acc_time = acc_pulse;
  574. axis_x->dec_time = dec_pulse;
  575. SetStopSelect(X_AXIS,PP_Stop);
  576. axis_pp(axis_x, 1, PosToPulse(X_AXIS,pos),speed,0);break;
  577. case Y_AXIS:
  578. axis_y->start_speed = start_speed * axis_y->speed_unit;
  579. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  580. axis_y->acc_time = acc_pulse;
  581. axis_y->dec_time = dec_pulse;
  582. SetStopSelect(Y_AXIS,PP_Stop);
  583. axis_pp(axis_y, 1, PosToPulse(Y_AXIS,pos),speed,0);break;
  584. case Z_AXIS:
  585. axis_z->start_speed = start_speed * axis_z->speed_unit;
  586. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  587. axis_z->acc_time = acc_pulse;
  588. axis_z->dec_time = dec_pulse;
  589. SetStopSelect(Z_AXIS,PP_Stop);
  590. axis_pp(axis_z, 1, PosToPulse(Z_AXIS,pos),speed,0);break;
  591. default:;
  592. }
  593. }
  594. //移动距离
  595. void AxisMovePos(unsigned short axis,unsigned short speed,long pos)
  596. {
  597. switch(axis)
  598. {
  599. case X_AXIS:
  600. {
  601. SetStopSelect(X_AXIS,PP_Stop);
  602. axis_pp(axis_x, 0, PosToPulse(X_AXIS,pos),speed,0);
  603. }break;
  604. case Y_AXIS:
  605. {
  606. axis_pp(axis_y, 0, PosToPulse(Y_AXIS,pos),speed,0);
  607. }break;
  608. case Z_AXIS:
  609. {
  610. SetStopSelect(Y_AXIS,PP_Stop);
  611. axis_pp(axis_z, 0, PosToPulse(Z_AXIS,pos),speed,0);
  612. }break;
  613. default:;
  614. }
  615. }
  616. //移动距离
  617. /* 如果运动过程中调用,则start_speed参数无效,使用当前速度作为起始速度*/
  618. void AxisMovePosAccDec(unsigned short axis,unsigned short speed,long pos,
  619. unsigned short start_speed,unsigned short stop_speed,
  620. unsigned short acc_pulse,unsigned short dec_pulse,long slowpos)
  621. {
  622. if(pos==0)return;
  623. switch(axis)
  624. {
  625. case X_AXIS:
  626. {
  627. if(axis_x->outputs.on)
  628. {
  629. axis_x->start_speed = GetCurSpeed(axis);
  630. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  631. axis_x->acc_time = acc_pulse;
  632. axis_x->dec_time = dec_pulse;
  633. axis_pp_change_speed(axis_x, 0, PosToPulse(X_AXIS,pos),speed,slowpos);
  634. SetStopSelect(X_AXIS,PP_Stop);
  635. }
  636. else
  637. {
  638. axis_x->start_speed = start_speed * axis_x->speed_unit;
  639. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  640. axis_x->acc_time = acc_pulse;
  641. axis_x->dec_time = dec_pulse;
  642. axis_pp(axis_x, 0, PosToPulse(X_AXIS,pos),speed,slowpos);
  643. SetStopSelect(X_AXIS,PP_Stop);
  644. }
  645. }
  646. break;
  647. case Y_AXIS:
  648. {
  649. if(axis_y->outputs.on)
  650. {
  651. axis_y->start_speed = GetCurSpeed(axis);
  652. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  653. axis_y->acc_time = acc_pulse;
  654. axis_y->dec_time = dec_pulse;
  655. axis_pp_change_speed(axis_y, 0, PosToPulse(Y_AXIS,pos),speed,slowpos);
  656. SetStopSelect(Y_AXIS,PP_Stop);
  657. }
  658. else
  659. {
  660. axis_y->start_speed = start_speed * axis_y->speed_unit;
  661. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  662. axis_y->acc_time = acc_pulse;
  663. axis_y->dec_time = dec_pulse;
  664. axis_pp(axis_y, 0, PosToPulse(Y_AXIS,pos),speed,slowpos);
  665. SetStopSelect(Y_AXIS,PP_Stop);
  666. }
  667. }break;
  668. case Z_AXIS:
  669. {
  670. if(axis_z->outputs.on)
  671. {
  672. axis_z->start_speed = GetCurSpeed(axis);
  673. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  674. axis_z->acc_time = acc_pulse;
  675. axis_z->dec_time = dec_pulse;
  676. axis_pp_change_speed(axis_z, 0, PosToPulse(Z_AXIS,pos),speed,slowpos);
  677. SetStopSelect(Z_AXIS,PP_Stop);
  678. }
  679. else
  680. {
  681. axis_z->start_speed = start_speed * axis_z->speed_unit;
  682. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  683. axis_z->acc_time = acc_pulse;
  684. axis_z->dec_time = dec_pulse;
  685. axis_pp(axis_z, 0, PosToPulse(Z_AXIS,pos),speed,slowpos);
  686. SetStopSelect(Z_AXIS,PP_Stop);
  687. }
  688. }
  689. break;
  690. default:;
  691. }
  692. }
  693. //移动距离不停止
  694. /*
  695. 参数
  696. axis 轴选择
  697. speed 最高速度位置
  698. pos 移动位置
  699. start_speed 启动速度,如果运动过程中调用,则start_speed参数无效,使用当前速度作为起始速度
  700. acc_pulse 加速度
  701. dec_pulse 减速度
  702. lastspeed 数控最后速度
  703. slowpos
  704. */
  705. void AxisMovePosAccDecNotStop(unsigned short axis,unsigned short speed,long pos,
  706. unsigned short start_speed,unsigned short lastspeed,
  707. unsigned short acc_pulse,unsigned short dec_pulse,long slowpos)
  708. {
  709. if(pos==0)return;
  710. switch(axis)
  711. {
  712. case X_AXIS:
  713. {
  714. if(axis_x->outputs.on)
  715. {
  716. SetStopSelect(X_AXIS,PP_NoStop);
  717. axis_x->start_speed = GetCurSpeed(axis);;
  718. axis_x->stop_speed = lastspeed * axis_x->speed_unit;
  719. axis_x->acc_time = acc_pulse;
  720. axis_x->dec_time = dec_pulse;
  721. axis_pp_change_speed(axis_x, 0, PosToPulse(X_AXIS,pos),speed,slowpos);
  722. }
  723. else
  724. {
  725. SetStopSelect(X_AXIS,PP_NoStop);
  726. axis_x->start_speed = start_speed * axis_x->speed_unit;
  727. axis_x->stop_speed = lastspeed * axis_x->speed_unit;
  728. axis_x->acc_time = acc_pulse;
  729. axis_x->dec_time = dec_pulse;
  730. axis_pp(axis_x, 0, PosToPulse(X_AXIS,pos),speed,slowpos);
  731. }
  732. }
  733. break;
  734. case Y_AXIS:
  735. {
  736. if(axis_y->outputs.on)
  737. {
  738. SetStopSelect(Y_AXIS,PP_NoStop);
  739. axis_y->start_speed = GetCurSpeed(axis);;
  740. axis_y->stop_speed = lastspeed * axis_y->speed_unit;
  741. axis_y->acc_time = acc_pulse;
  742. axis_y->dec_time = dec_pulse;
  743. axis_pp_change_speed(axis_y, 0, PosToPulse(Y_AXIS,pos),speed,slowpos);
  744. }
  745. else
  746. {
  747. SetStopSelect(Y_AXIS,PP_NoStop);
  748. axis_y->start_speed = start_speed * axis_y->speed_unit;
  749. axis_y->stop_speed = lastspeed * axis_y->speed_unit;
  750. axis_y->acc_time = acc_pulse;
  751. axis_y->dec_time = dec_pulse;
  752. axis_pp(axis_y, 0, PosToPulse(Y_AXIS,pos),speed,slowpos);
  753. }
  754. }break;
  755. case Z_AXIS:
  756. {
  757. if(axis_z->outputs.on)
  758. {
  759. SetStopSelect(Z_AXIS,PP_NoStop);
  760. axis_z->start_speed = GetCurSpeed(axis);
  761. axis_z->stop_speed = lastspeed * axis_z->speed_unit;
  762. axis_z->acc_time = acc_pulse;
  763. axis_z->dec_time = dec_pulse;
  764. axis_pp_change_speed(axis_z, 0, PosToPulse(Z_AXIS,pos),speed,slowpos);
  765. }
  766. else
  767. {
  768. SetStopSelect(Z_AXIS,PP_NoStop);
  769. axis_z->start_speed = start_speed * axis_z->speed_unit;
  770. axis_z->stop_speed = lastspeed * axis_z->speed_unit;
  771. axis_z->acc_time = acc_pulse;
  772. axis_z->dec_time = dec_pulse;
  773. axis_pp(axis_z, 0, PosToPulse(Z_AXIS,pos),speed,slowpos);
  774. }
  775. }
  776. break;
  777. default:;
  778. }
  779. }
  780. //连续运动
  781. void AxisContinueMove(unsigned short axis,unsigned short speed,unsigned char dir)
  782. {
  783. switch(axis)
  784. {
  785. case X_AXIS:
  786. {
  787. axis_pv(axis_x,dir,speed);
  788. }break;
  789. case Y_AXIS:
  790. {
  791. axis_pv(axis_y,dir,speed);
  792. }break;
  793. case Z_AXIS:
  794. {
  795. axis_pv(axis_z,dir,speed);
  796. }break;
  797. default:;
  798. }
  799. }
  800. //连续运动带加减速参数
  801. void AxisContinueMoveAcc(unsigned short axis,unsigned short speed,unsigned char dir,
  802. unsigned short start_speed,unsigned short stop_speed,
  803. unsigned short acc,unsigned short dec)
  804. {
  805. switch(axis)
  806. {
  807. case X_AXIS:
  808. {
  809. axis_x->start_speed = start_speed * axis_x->speed_unit;
  810. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  811. axis_x->acc_time = acc;
  812. axis_x->dec_time = dec;
  813. axis_pv(axis_x,dir,speed);
  814. }break;
  815. case Y_AXIS:
  816. {
  817. axis_y->start_speed = start_speed * axis_y->speed_unit;
  818. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  819. axis_y->acc_time = acc;
  820. axis_y->dec_time = dec;
  821. axis_pv(axis_y,dir,speed);
  822. }break;
  823. case Z_AXIS:
  824. {
  825. axis_z->start_speed = start_speed * axis_z->speed_unit;
  826. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  827. axis_z->acc_time = acc;
  828. axis_z->dec_time = dec;
  829. axis_pv(axis_z,dir,speed);
  830. }break;
  831. default:;
  832. }
  833. }
  834. //连续运动带加减速参数--中途改变目标速度
  835. void AxisContinueMoveChangeSpeed(unsigned short axis,unsigned short speed,unsigned short stop_speed,
  836. unsigned short acc,unsigned short dec)
  837. {
  838. GetCurSpeed(axis);
  839. switch(axis)
  840. {
  841. case X_AXIS:
  842. {
  843. if(axis_x->outputs.on)
  844. {
  845. axis_x->start_speed = GetCurSpeed(axis);;
  846. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  847. axis_x->acc_time = acc;
  848. axis_x->dec_time = dec;
  849. axis_pv_change_speed_table(axis_x,speed);
  850. }
  851. }break;
  852. case Y_AXIS:
  853. {
  854. if(axis_y->outputs.on)
  855. {
  856. axis_y->start_speed = GetCurSpeed(axis);;
  857. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  858. axis_y->acc_time = acc;
  859. axis_y->dec_time = dec;
  860. axis_pv_change_speed_table(axis_y,speed);
  861. }
  862. }break;
  863. case Z_AXIS:
  864. {
  865. if(axis_z->outputs.on)
  866. {
  867. axis_z->start_speed = GetCurSpeed(axis);;
  868. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  869. axis_z->acc_time = acc;
  870. axis_z->dec_time = dec;
  871. axis_pv_change_speed_table(axis_z,speed);
  872. }
  873. }break;
  874. default:;
  875. }
  876. }
  877. //直接变速不经过加减速过程,支持PP、PV模式调用,调用后切换到连续运动PV模式
  878. void AxisChangeSpeedDirect(unsigned short axis,unsigned short speed)
  879. {
  880. switch(axis)
  881. {
  882. case X_AXIS:
  883. {
  884. if(axis_x->outputs.on)
  885. {
  886. axis_change_speed_direct(axis_x,speed);
  887. }
  888. }break;
  889. case Y_AXIS:
  890. {
  891. if(axis_y->outputs.on)
  892. {
  893. axis_change_speed_direct(axis_y,speed);
  894. }
  895. }break;
  896. case Z_AXIS:
  897. {
  898. if(axis_z->outputs.on)
  899. {
  900. axis_change_speed_direct(axis_z,speed);
  901. }
  902. }break;
  903. default:;
  904. }
  905. }
  906. //两段速度移动距离
  907. void AxisMoveTwoPos(unsigned short axis,unsigned short speed1,long pos1,
  908. unsigned short speed2,long pos2,int dir,
  909. unsigned short start_speed,unsigned short stop_speed,
  910. unsigned short acc_pulse,unsigned short dec_pulse)
  911. {
  912. switch(axis)
  913. {
  914. case X_AXIS:
  915. {
  916. axis_init_task(axis_x,dir);
  917. SetStopSelect(X_AXIS,PP_Stop);
  918. axis_x->start_speed = start_speed * axis_x->speed_unit;
  919. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  920. axis_x->acc_time = acc_pulse;
  921. axis_x->dec_time = dec_pulse;
  922. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos1), speed1);
  923. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos2), speed2);
  924. axis_start_pp_task(axis_x);
  925. }
  926. break;
  927. case Y_AXIS:
  928. {
  929. SetStopSelect(Y_AXIS,PP_Stop);
  930. axis_init_task(axis_y,dir);
  931. axis_y->start_speed = start_speed * axis_y->speed_unit;
  932. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  933. axis_y->acc_time = acc_pulse;
  934. axis_y->dec_time = dec_pulse;
  935. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos1), speed1);
  936. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos2), speed2);
  937. axis_start_pp_task(axis_y);
  938. }
  939. break;
  940. case Z_AXIS:
  941. {
  942. SetStopSelect(Z_AXIS,PP_Stop);
  943. axis_init_task(axis_z,dir);
  944. axis_z->start_speed = start_speed * axis_z->speed_unit;
  945. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  946. axis_z->acc_time = acc_pulse;
  947. axis_z->dec_time = dec_pulse;
  948. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos1), speed1);
  949. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos2), speed2);
  950. axis_start_pp_task(axis_z);
  951. }
  952. break;
  953. default:
  954. break;
  955. }
  956. }
  957. void AxisMoveTwoPosNoStop(unsigned short axis,unsigned short speed1,long pos1,
  958. unsigned short speed2,long pos2,int dir,
  959. unsigned short start_speed,unsigned short stop_speed,
  960. unsigned short acc_pulse,unsigned short dec_pulse)
  961. {
  962. switch(axis)
  963. {
  964. case X_AXIS:
  965. {
  966. axis_init_task(axis_x,dir);
  967. SetStopSelect(X_AXIS,PP_NoStop);
  968. axis_x->start_speed = start_speed * axis_x->speed_unit;
  969. axis_x->stop_speed = stop_speed * axis_x->speed_unit;
  970. axis_x->acc_time = acc_pulse;
  971. axis_x->dec_time = dec_pulse;
  972. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos1), speed1);
  973. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos2), speed2);
  974. axis_start_pp_task(axis_x);
  975. }
  976. break;
  977. case Y_AXIS:
  978. {
  979. SetStopSelect(Y_AXIS,PP_NoStop);
  980. axis_init_task(axis_y,dir);
  981. axis_y->start_speed = start_speed * axis_y->speed_unit;
  982. axis_y->stop_speed = stop_speed * axis_y->speed_unit;
  983. axis_y->acc_time = acc_pulse;
  984. axis_y->dec_time = dec_pulse;
  985. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos1), speed1);
  986. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos2), speed2);
  987. axis_start_pp_task(axis_y);
  988. }
  989. break;
  990. case Z_AXIS:
  991. {
  992. SetStopSelect(Z_AXIS,PP_NoStop);
  993. axis_init_task(axis_z,dir);
  994. axis_z->start_speed = start_speed * axis_z->speed_unit;
  995. axis_z->stop_speed = stop_speed * axis_z->speed_unit;
  996. axis_z->acc_time = acc_pulse;
  997. axis_z->dec_time = dec_pulse;
  998. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos1), speed1);
  999. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos2), speed2);
  1000. axis_start_pp_task(axis_z);
  1001. }
  1002. break;
  1003. default:
  1004. break;
  1005. }
  1006. }
  1007. //三段速度移动距离
  1008. void AxisMoveThreePos(unsigned short axis,unsigned short speed1,long pos1,unsigned short speed2,long pos2,unsigned short speed3,long pos3,int dir)
  1009. {
  1010. switch(axis)
  1011. {
  1012. case X_AXIS:
  1013. {
  1014. axis_init_task(axis_x,dir);
  1015. SetStopSelect(X_AXIS,PP_Stop);
  1016. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos1), speed1);
  1017. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos2), speed2);
  1018. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos3), speed3);
  1019. axis_start_pp_task(axis_x);
  1020. }break;
  1021. case Y_AXIS:
  1022. {
  1023. axis_init_task(axis_y,dir);
  1024. SetStopSelect(Y_AXIS,PP_Stop);
  1025. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos1), speed1);
  1026. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos2), speed2);
  1027. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos3), speed3);
  1028. axis_start_pp_task(axis_y);
  1029. }
  1030. break;
  1031. case Z_AXIS:
  1032. {
  1033. axis_init_task(axis_z,dir);
  1034. SetStopSelect(Z_AXIS,PP_Stop);
  1035. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos1), speed1);
  1036. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos2), speed2);
  1037. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos3), speed3);
  1038. axis_start_pp_task(axis_z);
  1039. }
  1040. break;
  1041. default:;
  1042. }
  1043. }
  1044. //三段速度移动距离不停止
  1045. void AxisMoveThreePosNoStop(unsigned short axis,unsigned short speed1,long pos1,unsigned short speed2,long pos2,unsigned short speed3,long pos3,int dir)
  1046. {
  1047. switch(axis)
  1048. {
  1049. case X_AXIS:
  1050. {
  1051. axis_init_task(axis_x,dir);
  1052. SetStopSelect(X_AXIS,PP_NoStop);
  1053. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos1), speed1);
  1054. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos2), speed2);
  1055. axis_add_pp_task(axis_x, PosToPulse(X_AXIS,pos3), speed3);
  1056. axis_start_pp_task(axis_x);
  1057. }break;
  1058. case Y_AXIS:
  1059. {
  1060. axis_init_task(axis_y,dir);
  1061. SetStopSelect(Y_AXIS,PP_NoStop);
  1062. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos1), speed1);
  1063. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos2), speed2);
  1064. axis_add_pp_task(axis_y, PosToPulse(Y_AXIS,pos3), speed3);
  1065. axis_start_pp_task(axis_y);
  1066. }break;
  1067. case Z_AXIS:
  1068. {
  1069. axis_init_task(axis_z,dir);
  1070. SetStopSelect(Z_AXIS,PP_NoStop);
  1071. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos1), speed1);
  1072. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos2), speed2);
  1073. axis_add_pp_task(axis_z, PosToPulse(Z_AXIS,pos3), speed3);
  1074. axis_start_pp_task(axis_z);
  1075. }break;
  1076. default:;
  1077. }
  1078. }