axis_motion.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143
  1. #include "global.h"
  2. #include "axis_motion.h"
  3. /**
  4. * 轴运动驱动是一个利用时间为单位进行步进加减速度的驱动库。由于采用了定时周期加减速度,因此可以避免步进电机与
  5. * 伺服电机因为脉冲周期问题导致加减速度不好控制以及不好确定加减脉冲的问题。另外可以通过引入多段加减速度来保证
  6. * 停止的时候与开始时候的顺滑度,保证不会因为中间段加减速度过大而过冲。
  7. *
  8. */
  9. #define AXIS_ACC_REG_TABLE(axis) axis->acc_reg_table
  10. #define AXIS_ACC_NUMBER_TABLE(axis) axis->acc_number_table
  11. #ifndef AXIS_USING_ACC_TABLE_ONLY
  12. #define AXIS_DEC_REG_TABLE(axis) axis->dec_reg_table
  13. #define AXIS_DEC_NUMBER_TABLE(axis) axis->dec_number_table
  14. #else
  15. #define AXIS_DEC_REG_TABLE(axis) axis->acc_reg_table
  16. #define AXIS_DEC_NUMBER_TABLE(axis) axis->acc_number_table
  17. #endif
  18. /**
  19. * 轴使能
  20. *
  21. * @author LXZ (121919)
  22. *
  23. * @param axis
  24. */
  25. void axis_enable(axis_object_t *axis) {
  26. axis->outputs.enable = 1;
  27. if (axis->inputs.en_reverse) {
  28. hw_pwm_set_enable(axis->axis_no,0);
  29. }
  30. else{
  31. hw_pwm_set_enable(axis->axis_no,1);
  32. }
  33. }
  34. /**
  35. * 轴禁止
  36. *
  37. * @author LXZ (121919)
  38. *
  39. * @param axis
  40. */
  41. void axis_disable(axis_object_t *axis) {
  42. axis->outputs.enable = 0;
  43. if (axis->inputs.en_reverse) {
  44. hw_pwm_set_enable(axis->axis_no,1);
  45. }
  46. else{
  47. hw_pwm_set_enable(axis->axis_no,0);
  48. }
  49. }
  50. /**
  51. * 轴正转
  52. *
  53. * @author lxz
  54. *
  55. * @param axis
  56. */
  57. void axis_cw(axis_object_t *axis) {
  58. axis->outputs.dir = 1;
  59. axis->feed = 1;
  60. if (axis->inputs.dir_reverse) {
  61. hw_pwm_set_dir(axis->axis_no,0);
  62. } else {
  63. hw_pwm_set_dir(axis->axis_no,1);
  64. }
  65. }
  66. /**
  67. * 轴反转
  68. *
  69. * @author lxz
  70. *
  71. * @param axis
  72. */
  73. void axis_ccw(axis_object_t *axis) {
  74. axis->outputs.dir = 0;
  75. axis->feed = -1;
  76. if (axis->inputs.dir_reverse) {
  77. hw_pwm_set_dir(axis->axis_no,1);
  78. } else {
  79. hw_pwm_set_dir(axis->axis_no,0);
  80. }
  81. }
  82. #define AXIS_USING_FLOAT
  83. #ifdef AXIS_USING_FLOAT
  84. /**
  85. * 以直线加减速的方式计算加减速表
  86. *
  87. * @author LXZ (021720)
  88. *
  89. * @param axis
  90. * @param target_speed
  91. */
  92. static void axis_mode0_calc_accdec(axis_object_t *axis, int target_speed) {
  93. int acc_steps = 0; //加速时间单位数
  94. int dec_steps = 0; //减速时间单位数
  95. float cur_max_accdec = 0; //当前最大加速度减速度
  96. float cur_speed = 0; //当前速度
  97. int index = 0;
  98. int clock = axis->clock;
  99. int start_speed = axis->start_speed ;
  100. int stop_speed = axis->stop_speed;
  101. float clock_ratio = axis->clock / 1000000;
  102. //周期临时值
  103. //int period;
  104. int reg_Value = 0;
  105. int last_reg_value = 0;
  106. float period;
  107. period=0.5;
  108. //period = axis->period;
  109. //限制加速度周期
  110. //if (period < 1) period = 2;
  111. //限制最小加速度时间单位
  112. acc_steps =(int)( axis->acc_time / period);
  113. if (acc_steps < 10) {
  114. acc_steps = 10;
  115. }
  116. dec_steps =(int)(axis->dec_time / period);
  117. if (dec_steps < 10) {
  118. dec_steps = 10;
  119. }
  120. //转换倍率
  121. period *= 1000;
  122. //保证加减速尽量对齐,需要保证加减数是偶数,由于计算误差,加1能让结果更加逼近设置的时间
  123. //acc_steps++;
  124. //acc_steps >>= 1;
  125. //dec_steps++;
  126. //dec_steps >>= 1;
  127. //保证是偶次数加减数,能减少计算误差
  128. //if ((min_acc_step & 0x01) != 0)
  129. // min_acc_step++;
  130. //目标速度不能比开始速度小
  131. /*if (target_speed < start_speed) {
  132. target_speed = start_speed;
  133. }*/
  134. //计算加速度会达到的最大值
  135. cur_max_accdec = (float)(target_speed - axis->start_speed) / acc_steps;
  136. //尽量保证最大加速度不会大于限制值
  137. if(cur_max_accdec>=0)
  138. {
  139. if (cur_max_accdec > axis->max_acc) {
  140. cur_max_accdec = axis->max_acc;
  141. }
  142. }
  143. else
  144. {
  145. if (-cur_max_accdec > axis->max_acc) {
  146. cur_max_accdec = -axis->max_acc;
  147. }
  148. }
  149. cur_speed = start_speed;
  150. //S加速曲线加速表计算
  151. do {
  152. //计算速度
  153. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  154. reg_Value = (int)((clock) / cur_speed);
  155. //过滤器重复的加减速点
  156. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  157. AXIS_ACC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  158. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  159. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  160. }
  161. #ifdef AXIS_USING_DEBUG
  162. axis->acc_speed_table[index] = (int)cur_speed;
  163. #endif
  164. index++;
  165. cur_speed += cur_max_accdec;
  166. } while ((((cur_max_accdec>=0)&&(cur_speed < target_speed))
  167. ||((cur_max_accdec<0)&&(cur_speed > target_speed)))
  168. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  169. //保存表大小
  170. axis->acc_table_size = index;
  171. #ifdef AXIS_USING_ACC_TABLE_ONLY
  172. if (index > 0) {
  173. index -= 1;
  174. }
  175. axis->dec_table_size = index;
  176. #else
  177. cur_max_accdec = (target_speed - axis->stop_speed) / dec_steps;
  178. //计算加速度会达到的最大值
  179. if (cur_max_accdec > axis->max_acc) {
  180. cur_max_accdec = axis->max_acc;
  181. }
  182. dec_steps = (int)((target_speed - axis->stop_speed) / cur_max_accdec);
  183. //开始减速表计算
  184. index = 0;
  185. axis->acc_position=0;
  186. last_reg_value = 0;
  187. cur_speed = stop_speed;
  188. //S加速曲线减速表计算
  189. do {
  190. //计算速度
  191. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  192. reg_Value = (int)((clock) / cur_speed);
  193. //过滤器重复的加减速点
  194. #ifdef AXIS_IGNORE_REPEAT_STEP
  195. if (last_reg_value != reg_Value) {
  196. #endif
  197. last_reg_value = reg_Value;
  198. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  199. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  200. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  201. axis->acc_position+=AXIS_ACC_NUMBER_TABLE(axis)[index];
  202. #ifdef AXIS_USING_DEBUG
  203. axis->dec_speed_table[index] = (int)cur_speed;
  204. #endif
  205. index++;
  206. #ifdef AXIS_IGNORE_REPEAT_STEP
  207. }
  208. #endif
  209. cur_speed += cur_max_accdec;
  210. } while ((cur_speed < target_speed) && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  211. //保存减速表大小
  212. axis->dec_table_size = index;
  213. //if (axis->dec_table_size > 0) {
  214. // axis->dec_table_size -= 1;
  215. // }
  216. #endif
  217. }
  218. /**
  219. * 以S曲线减速的方式计算加减速表
  220. *
  221. * @author LXZ (021720)
  222. *
  223. * @param axis 轴对象
  224. * @param target_speed 目标速度
  225. */
  226. static void axis_mode1_calc_accdec(axis_object_t *axis, int target_speed) {
  227. int acc_steps = 0; //加速时间单位数
  228. int dec_steps = 0; //减速时间单位数
  229. long cur_accdec = 0; //当前加减速度
  230. long cur_max_accdec = 0; //当前最大加速度减速度
  231. long cur_speed = 0; //当前速度
  232. long ddc_speed = 0; //减减速开始速度
  233. long cur_aacddc = 0; //当前加加速减减速
  234. long mid_speed = 0; //中间速度
  235. int index = 0;
  236. long clock = axis->clock;
  237. long clock_ratio = axis->clock / 1000000;
  238. long start_speed = axis->start_speed*10;
  239. long cur_target_speed=target_speed*10;
  240. long stop_speed=axis->stop_speed*10;
  241. //周期临时值
  242. //int period;
  243. long period;
  244. int reg_Value = 0;
  245. memset(&axis->acc_number_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  246. memset(&axis->acc_reg_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  247. memset(&axis->dec_number_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  248. memset(&axis->dec_reg_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  249. period = axis->period;
  250. //限制加速度周期
  251. //if (period < 1) period = 2;
  252. //限制最小加速度时间单位
  253. acc_steps = axis->acc_time / period;
  254. if (acc_steps < 10) {
  255. acc_steps = 10;
  256. }
  257. dec_steps = axis->dec_time / period;
  258. if (dec_steps < 10) {
  259. dec_steps = 10;
  260. }
  261. //转换倍率
  262. period *= 1000;
  263. //保证加减速尽量对齐,需要保证加减数是偶数,由于计算误差,加1能让结果更加逼近设置的时间
  264. acc_steps++;
  265. acc_steps >>= 1;
  266. dec_steps++;
  267. dec_steps >>= 1;
  268. //S加速算法需要先计算出加加速
  269. //推导过程:
  270. //S加减速度分为加加速与减加速度两个过程,假定两个过程是对称的,那么总占用时间为加速时间t的一半
  271. //因此利用积分公式(dv = sadt, vm = (vd - v0) / 2 => a = vm / (2 * (t / 2))可以得到acc = (vd - v0) * 2 / t
  272. //其中a与acc表示加速度,vd表示目标速度,v0表示开始速度t表示加速时间一半
  273. //加加速度aac与加速度关系acc为acc = aac * (t / 2)
  274. //从而公式为aac = (vd - v0) * 4 / (t ^2)
  275. //其中acc表示加速度,aac表示加加速,vd为目标速度,v0为开始速度,t为总的加速时间,在这算法里它就是加速度步数
  276. //由于加速度步数要对称就必须保持偶数,加上一些计算偏差以及为了保证电机正常运算必须限制参数
  277. //因此实际上两个过程在实际上是很难对称的,这就造成了实际加速时间肯定会比设定的长,具体长多
  278. //跟最大加加速,最大中速度,最小脉冲数,计算偏差,最小加速度步数有关
  279. cur_aacddc = ((cur_target_speed - start_speed)) / (acc_steps * acc_steps);
  280. //保证至少1以上的加减速度
  281. if (cur_aacddc >= 0 && cur_aacddc < 1) {
  282. cur_aacddc = 1;
  283. }else if (cur_aacddc < 0 && cur_aacddc > -1) {
  284. cur_aacddc = -1;
  285. }
  286. //计算加速度会达到的最大值
  287. cur_max_accdec = cur_aacddc * acc_steps;
  288. if (cur_max_accdec > 0 && cur_max_accdec > axis->max_acc*10) {
  289. cur_max_accdec = axis->max_acc*10;
  290. }else if(cur_max_accdec < 0 && -cur_max_accdec > axis->max_acc*10) {
  291. cur_max_accdec = -axis->max_acc*10;
  292. }
  293. //限制加加速度,加加速度过大会导致过冲
  294. if (cur_aacddc > 0 && cur_aacddc > axis->max_aac*10) {
  295. cur_aacddc = axis->max_aac;
  296. }else if(cur_aacddc < 0 && -cur_aacddc > axis->max_aac*10) {
  297. cur_aacddc = -axis->max_aac*10;
  298. }
  299. //由于加加速比计算结果小,所以需要更多步去达到最高速,需要重新加速步
  300. acc_steps = (int)(cur_max_accdec / cur_aacddc);
  301. if(acc_steps < 0) acc_steps = -acc_steps;
  302. //开始计算加速表
  303. mid_speed = (cur_target_speed + start_speed) / 2;
  304. ddc_speed = cur_target_speed - ((cur_aacddc * acc_steps * acc_steps) / 2);
  305. if (ddc_speed < mid_speed) {
  306. ddc_speed = mid_speed;
  307. }
  308. cur_speed = start_speed;
  309. axis->acc_position=0;
  310. //S加速曲线加速表计算
  311. do {
  312. //正常的情况下,速度达到目标速度一半后,加速度肯定达到最大值,如果没有达到,必须强制达到
  313. //否则会造成波形中间下移,导致减加速流程过长
  314. if (cur_aacddc > 0 && cur_speed >= mid_speed) {
  315. cur_accdec = cur_max_accdec;
  316. //保证只能进一次
  317. mid_speed *= 3;
  318. }else if(cur_aacddc < 0 && cur_speed <= mid_speed){
  319. cur_accdec = -cur_max_accdec;
  320. //保证只能进一次
  321. mid_speed = 0;
  322. }
  323. if (cur_aacddc > 0 && cur_speed >= ddc_speed) { //减减速需要提前判断,尽量保证流程对称
  324. //减加速流程
  325. if (cur_accdec > cur_aacddc) {
  326. cur_accdec -= cur_aacddc;
  327. }
  328. else {
  329. //保证至少有这个加加速,让速度有机会达到目标速度
  330. cur_accdec = cur_aacddc;
  331. }
  332. }else if(cur_aacddc < 0 && cur_speed <= ddc_speed) { //减减速需要提前判断,尽量保证流程对称
  333. //减加速流程
  334. if (-cur_accdec < cur_aacddc) {
  335. cur_accdec += cur_aacddc;
  336. }
  337. else {
  338. //保证至少有这个加加速,让速度有机会达到目标速度
  339. cur_accdec = -cur_aacddc;
  340. }
  341. }
  342. //计算速度
  343. if(cur_aacddc>0)
  344. cur_speed += cur_accdec;
  345. else
  346. cur_speed -= cur_accdec;
  347. if (cur_aacddc >= 0 && cur_speed > cur_target_speed) cur_speed = cur_target_speed;
  348. if (cur_aacddc < 0 && cur_speed < cur_target_speed) cur_speed = cur_target_speed;
  349. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  350. reg_Value = (int)((clock*10+5) / cur_speed);
  351. //过滤器重复的加减速点
  352. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  353. AXIS_ACC_NUMBER_TABLE(axis)[index] = (int)((period + AXIS_CALC_ROUND(reg_Value)) * clock_ratio / reg_Value);
  354. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  355. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  356. }
  357. axis->acc_position+=AXIS_ACC_NUMBER_TABLE(axis)[index];
  358. #ifdef AXIS_USING_DEBUG
  359. axis->acc_speed_table[index] = (int)cur_speed/10;
  360. #endif
  361. index++;
  362. if (cur_aacddc > 0 && cur_speed < ddc_speed) { //还没到开始减速的速度
  363. //加加速流程
  364. cur_accdec += cur_aacddc;
  365. if (cur_accdec >= cur_max_accdec) {
  366. cur_accdec = cur_max_accdec;
  367. }
  368. }else if (cur_aacddc < 0 && cur_speed > ddc_speed) { //还没到开始减速的速度
  369. //加加速流程
  370. cur_accdec -= cur_aacddc;
  371. if (-cur_accdec <= cur_max_accdec) {
  372. cur_accdec = -cur_max_accdec;
  373. }
  374. }
  375. } while (((cur_aacddc > 0 && cur_speed < cur_target_speed)
  376. ||(cur_aacddc < 0 && cur_speed > cur_target_speed))
  377. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  378. //保存表大小
  379. axis->acc_table_size = index;
  380. #ifdef AXIS_USING_ACC_TABLE_ONLY
  381. if (index > 0) {
  382. index -= 1;
  383. }
  384. axis->dec_table_size = index;
  385. #else
  386. //if (dec_steps != acc_steps) {
  387. cur_aacddc = ((cur_target_speed - stop_speed)) / (dec_steps * dec_steps);
  388. //保证至少1以上的加减速度
  389. if (cur_aacddc >= 0 && cur_aacddc < 1) {
  390. cur_aacddc = 1;
  391. }else if (cur_aacddc < 0 && cur_aacddc > -1) {
  392. cur_aacddc = -1;
  393. }
  394. //计算加速度会达到的最大值
  395. cur_max_accdec = cur_aacddc * acc_steps;
  396. if (cur_max_accdec > 0 && cur_max_accdec > axis->max_acc*10) {
  397. cur_max_accdec = axis->max_acc*10;
  398. }else if(cur_max_accdec < 0 && -cur_max_accdec > axis->max_acc*10) {
  399. cur_max_accdec = -axis->max_acc*10;
  400. }
  401. //限制加加速度,加加速度过大会导致过冲
  402. if (cur_aacddc > 0 && cur_aacddc > axis->max_aac*10) {
  403. cur_aacddc = axis->max_aac*10;
  404. }else if(cur_aacddc < 0 && -cur_aacddc > axis->max_aac*10) {
  405. cur_aacddc = -axis->max_aac*10;
  406. }
  407. //由于加加速比计算结果小,所以需要更多步去达到最高速,需要重新加速步
  408. dec_steps = (int)(cur_max_accdec / cur_aacddc);
  409. //curMaxAccDec = curAacDdc * decSteps;
  410. //if (cur_aacddc == 0) cur_aacddc = 1;
  411. // }
  412. //如果减速时间一样,就不重复计算表了
  413. /* if (dec_steps == acc_steps) {
  414. int j = 0;
  415. while (j < index) {
  416. AXIS_DEC_REG_TABLE(axis)[j] = AXIS_ACC_REG_TABLE(axis)[j];
  417. AXIS_DEC_NUMBER_TABLE(axis)[j] = AXIS_ACC_NUMBER_TABLE(axis)[j];
  418. j++;
  419. }
  420. axis->dec_table_size = index;
  421. if (axis->dec_table_size > 0) {
  422. axis->dec_table_size -= 1;
  423. }
  424. return;
  425. }*/
  426. //开始减速表计算
  427. index = 0;
  428. mid_speed = (cur_target_speed + stop_speed) / 2;
  429. ddc_speed = cur_target_speed - ((cur_aacddc * dec_steps * dec_steps) / 2);
  430. if (ddc_speed < mid_speed) {
  431. ddc_speed = mid_speed;
  432. }
  433. cur_speed = stop_speed;
  434. //S加速曲线减速表计算
  435. do {
  436. //正常的情况下,速度达到目标速度一半后,加速度肯定达到最大值,如果没有达到,必须强制达到
  437. //否则会造成波形中间下移,导致减加速流程过长
  438. if (cur_aacddc > 0 && cur_speed >= mid_speed) {
  439. cur_accdec = cur_max_accdec;
  440. //保证只能进一次
  441. mid_speed *= 3;
  442. }else if(cur_aacddc < 0 && cur_speed <= mid_speed){
  443. cur_accdec = -cur_max_accdec;
  444. //保证只能进一次
  445. mid_speed = 0;
  446. }
  447. if (cur_aacddc > 0 && cur_speed >= ddc_speed) { //减减速需要提前判断,尽量保证流程对称
  448. //减加速流程
  449. if (cur_accdec > cur_aacddc) {
  450. cur_accdec -= cur_aacddc;
  451. }
  452. else {
  453. //保证至少有这个加加速,让速度有机会达到目标速度
  454. cur_accdec = cur_aacddc;
  455. }
  456. }else if(cur_aacddc < 0 && cur_speed <= ddc_speed) { //减减速需要提前判断,尽量保证流程对称
  457. //减加速流程
  458. if (-cur_accdec < cur_aacddc) {
  459. cur_accdec += cur_aacddc;
  460. }
  461. else {
  462. //保证至少有这个加加速,让速度有机会达到目标速度
  463. cur_accdec = -cur_aacddc;
  464. }
  465. }
  466. //计算速度
  467. if(cur_aacddc>0)
  468. cur_speed += cur_accdec;
  469. else
  470. cur_speed -= cur_accdec;
  471. if (cur_speed > cur_target_speed) cur_speed = cur_target_speed;
  472. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  473. reg_Value = (int)((clock*10+5) / cur_speed);
  474. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  475. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + AXIS_CALC_ROUND(reg_Value)) * clock_ratio / reg_Value);
  476. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  477. AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  478. }
  479. #ifdef AXIS_USING_DEBUG
  480. axis->dec_speed_table[index] = (int)cur_speed;
  481. #endif
  482. index++;
  483. if (cur_aacddc > 0 && cur_speed < ddc_speed) { //还没到开始减速的速度
  484. //加加速流程
  485. cur_accdec += cur_aacddc;
  486. if (cur_accdec >= cur_max_accdec) {
  487. cur_accdec = cur_max_accdec;
  488. }
  489. }else if (cur_aacddc < 0 && cur_speed > ddc_speed) { //还没到开始减速的速度
  490. //加加速流程
  491. cur_accdec -= cur_aacddc;
  492. if (-cur_accdec <= cur_max_accdec) {
  493. cur_accdec = -cur_max_accdec;
  494. }
  495. }
  496. } while (((cur_aacddc > 0 && cur_speed < cur_target_speed)
  497. ||(cur_aacddc < 0 && cur_speed > cur_target_speed))
  498. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  499. //保存减速表大小
  500. axis->dec_table_size = index;
  501. //if (axis->dec_table_size > 0) {
  502. // axis->dec_table_size -= 1;
  503. //}
  504. #endif
  505. }
  506. /**
  507. * 以两段直线加减速的方式计算加减速表
  508. *
  509. * @author LXZ (021720)
  510. *
  511. * @param axis
  512. * @param target_speed
  513. */
  514. static void axis_mode2_calc_accdec(axis_object_t *axis, int target_speed) {
  515. int acc_steps = 0; //加速时间单位数
  516. int dec_steps = 0; //减速时间单位数
  517. float cur_max_accdec = 0; //当前最大加速度减速度
  518. float cur_speed = 0; //当前速度
  519. int index = 0;
  520. int clock = axis->clock;
  521. int start_speed = axis->start_speed ;
  522. int stop_speed = axis->stop_speed;
  523. float clock_ratio = axis->clock / 1000000;
  524. //周期临时值
  525. //int period;
  526. int reg_Value = 0;
  527. int last_reg_value = 0;
  528. float period;
  529. period=0.5;
  530. //两段直线加减速参数
  531. int acc_steps1,acc_steps2,dec_steps1,dec_steps2;
  532. int target_speed1,target_speed2;
  533. //period = axis->period;
  534. //限制加速度周期
  535. //if (period < 1) period = 2;
  536. //限制最小加速度时间单位
  537. acc_steps = (int)(axis->acc_time / period);
  538. if (acc_steps < 10) {
  539. acc_steps = 10;
  540. }
  541. dec_steps = (int)(axis->dec_time / period);
  542. if (dec_steps < 10) {
  543. dec_steps = 10;
  544. }
  545. //计算第一段直线加速的步数和目标速度
  546. if(target_speed >= axis->start_speed)
  547. {
  548. //第一段直线以1/10的时间加速到1/4的速度
  549. acc_steps1=acc_steps / 10;
  550. target_speed1=((target_speed-axis->start_speed) / 4)+axis->start_speed;
  551. }
  552. else
  553. {
  554. acc_steps1=acc_steps * 9 / 10;
  555. target_speed1=((target_speed-axis->start_speed) * 3 / 4)+axis->start_speed;
  556. }
  557. //计算第二段直线加速的步数和目标速度
  558. acc_steps2=acc_steps-acc_steps1;
  559. target_speed2=target_speed;
  560. //转换倍率
  561. period *= 1000;
  562. //保证加减速尽量对齐,需要保证加减数是偶数,由于计算误差,加1能让结果更加逼近设置的时间
  563. //acc_steps++;
  564. //acc_steps >>= 1;
  565. //dec_steps++;
  566. //dec_steps >>= 1;
  567. //保证是偶次数加减数,能减少计算误差
  568. //if ((min_acc_step & 0x01) != 0)
  569. // min_acc_step++;
  570. //目标速度不能比开始速度小
  571. /*if (target_speed < start_speed) {
  572. target_speed = start_speed;
  573. }*/
  574. //计算加速度会达到的最大值
  575. cur_max_accdec = (float)(target_speed1 - axis->start_speed) / acc_steps1;
  576. //尽量保证最大加速度不会大于限制值
  577. if(cur_max_accdec>=0)
  578. {
  579. if (cur_max_accdec > axis->max_acc) {
  580. cur_max_accdec = axis->max_acc;
  581. }
  582. }
  583. else
  584. {
  585. if (-cur_max_accdec > axis->max_acc) {
  586. cur_max_accdec = -axis->max_acc;
  587. }
  588. }
  589. cur_speed = start_speed;
  590. //S加速曲线加速表计算
  591. do {
  592. //计算速度
  593. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  594. reg_Value = (int)((clock) / cur_speed);
  595. //过滤器重复的加减速点
  596. #ifdef AXIS_IGNORE_REPEAT_STEP
  597. if (last_reg_value != reg_Value) {
  598. #endif
  599. last_reg_value = reg_Value;
  600. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  601. AXIS_ACC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  602. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  603. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  604. }
  605. #ifdef AXIS_USING_DEBUG
  606. axis->acc_speed_table[index] = (int)cur_speed;
  607. #endif
  608. index++;
  609. #ifdef AXIS_IGNORE_REPEAT_STEP
  610. }
  611. #endif
  612. cur_speed += cur_max_accdec;
  613. } while ((((cur_max_accdec>=0)&&(cur_speed < target_speed1))
  614. ||((cur_max_accdec<0)&&(cur_speed > target_speed1)))
  615. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  616. //第二段加速直线
  617. //计算加速度会达到的最大值
  618. cur_max_accdec = (float)(target_speed2 - target_speed1) / acc_steps2;
  619. //尽量保证最大加速度不会大于限制值
  620. if(cur_max_accdec>=0)
  621. {
  622. if (cur_max_accdec > axis->max_acc) {
  623. cur_max_accdec = axis->max_acc;
  624. }
  625. }
  626. else
  627. {
  628. if (-cur_max_accdec > axis->max_acc) {
  629. cur_max_accdec = -axis->max_acc;
  630. }
  631. }
  632. //cur_speed = start_speed;
  633. //S加速曲线加速表计算
  634. do {
  635. //计算速度
  636. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  637. reg_Value = (int)((clock) / cur_speed);
  638. //过滤器重复的加减速点
  639. #ifdef AXIS_IGNORE_REPEAT_STEP
  640. if (last_reg_value != reg_Value) {
  641. #endif
  642. last_reg_value = reg_Value;
  643. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  644. AXIS_ACC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  645. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  646. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  647. }
  648. #ifdef AXIS_USING_DEBUG
  649. axis->acc_speed_table[index] = (int)cur_speed;
  650. #endif
  651. index++;
  652. #ifdef AXIS_IGNORE_REPEAT_STEP
  653. }
  654. #endif
  655. cur_speed += cur_max_accdec;
  656. } while ((((cur_max_accdec>=0)&&(cur_speed < target_speed2))
  657. ||((cur_max_accdec<0)&&(cur_speed > target_speed2)))
  658. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  659. //保存表大小
  660. axis->acc_table_size = index;
  661. #ifdef AXIS_USING_ACC_TABLE_ONLY
  662. if (index > 0) {
  663. index -= 1;
  664. }
  665. axis->dec_table_size = index;
  666. #else
  667. //开始减速表计算
  668. //计算第一第二段直线减速的步数
  669. dec_steps1=dec_steps / 10;
  670. dec_steps2=dec_steps-dec_steps1;
  671. if(axis->stop_speed > target_speed) axis->stop_speed = target_speed;
  672. target_speed1=((target_speed-axis->stop_speed) / 4)+axis->stop_speed;
  673. target_speed2=target_speed;
  674. //第一段减速直线
  675. cur_max_accdec = (target_speed1 - axis->stop_speed) / dec_steps1;
  676. //计算加速度会达到的最大值
  677. if (cur_max_accdec > axis->max_acc) {
  678. cur_max_accdec = axis->max_acc;
  679. }
  680. //dec_steps1 = (int)((target_speed1 - axis->stop_speed) / cur_max_accdec);
  681. //开始减速表计算
  682. index = 0;
  683. last_reg_value = 0;
  684. cur_speed = stop_speed;
  685. //S加速曲线减速表计算
  686. do {
  687. //计算速度
  688. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  689. reg_Value = (int)((clock) / cur_speed);
  690. //过滤器重复的加减速点
  691. #ifdef AXIS_IGNORE_REPEAT_STEP
  692. if (last_reg_value != reg_Value) {
  693. #endif
  694. last_reg_value = reg_Value;
  695. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  696. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  697. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  698. #ifdef AXIS_USING_DEBUG
  699. axis->dec_speed_table[index] = (int)cur_speed;
  700. #endif
  701. index++;
  702. #ifdef AXIS_IGNORE_REPEAT_STEP
  703. }
  704. #endif
  705. cur_speed += cur_max_accdec;
  706. } while ((cur_speed < target_speed1) && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  707. //第二段减速直线
  708. cur_max_accdec = (target_speed2 - target_speed1) / dec_steps2;
  709. //计算加速度会达到的最大值
  710. if (cur_max_accdec > axis->max_acc) {
  711. cur_max_accdec = axis->max_acc;
  712. }
  713. //dec_steps2 = (int)((target_speed1 - axis->stop_speed) / cur_max_accdec);
  714. //开始减速表计算
  715. //index = 0;
  716. //last_reg_value = 0;
  717. //cur_speed = stop_speed;
  718. //S加速曲线减速表计算
  719. do {
  720. //计算速度
  721. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  722. reg_Value = (int)((clock) / cur_speed);
  723. //过滤器重复的加减速点
  724. #ifdef AXIS_IGNORE_REPEAT_STEP
  725. if (last_reg_value != reg_Value) {
  726. #endif
  727. last_reg_value = reg_Value;
  728. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  729. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  730. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  731. #ifdef AXIS_USING_DEBUG
  732. axis->dec_speed_table[index] = (int)cur_speed;
  733. #endif
  734. index++;
  735. #ifdef AXIS_IGNORE_REPEAT_STEP
  736. }
  737. #endif
  738. cur_speed += cur_max_accdec;
  739. } while ((cur_speed < target_speed2) && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  740. //保存减速表大小
  741. axis->dec_table_size = index;
  742. //if (axis->dec_table_size > 0) {
  743. // axis->dec_table_size -= 1;
  744. //}
  745. #endif
  746. }
  747. /**
  748. * 以固定斜率直线加减速的方式计算加减速表,加减速时间设置自动失效
  749. *
  750. * @author LXZ (021720)
  751. *
  752. * @param axis
  753. * @param target_speed
  754. */
  755. static void axis_mode3_calc_accdec(axis_object_t *axis, int target_speed) {
  756. int acc_steps = 0; //加速时间单位数
  757. int dec_steps = 0; //减速时间单位数
  758. float cur_max_accdec = 0; //当前最大加速度减速度
  759. float cur_speed = 0; //当前速度
  760. int index = 0;
  761. int clock = axis->clock;
  762. int start_speed = axis->start_speed ;
  763. int stop_speed = axis->stop_speed;
  764. float clock_ratio = axis->clock / 1000000;
  765. //周期临时值
  766. //int period;
  767. int reg_Value = 0;
  768. int last_reg_value = 0;
  769. float period;
  770. period=0.5;
  771. //period = axis->period;
  772. //限制加速度周期
  773. //if (period < 1) period = 2;
  774. //限制最小加速度时间单位
  775. //acc_steps = axis->acc_time / period;
  776. //固定加减速斜率
  777. int accdec_value;
  778. if(target_speed > axis->start_speed) accdec_value=200;
  779. else accdec_value=-200;
  780. /*acc_steps = (target_speed - axis->start_speed) / accdec_value;
  781. if (acc_steps < 10) {
  782. acc_steps = 10;
  783. }*/
  784. //转换倍率
  785. period *= 1000;
  786. //保证加减速尽量对齐,需要保证加减数是偶数,由于计算误差,加1能让结果更加逼近设置的时间
  787. //acc_steps++;
  788. //acc_steps >>= 1;
  789. //dec_steps++;
  790. //dec_steps >>= 1;
  791. //保证是偶次数加减数,能减少计算误差
  792. //if ((min_acc_step & 0x01) != 0)
  793. // min_acc_step++;
  794. //目标速度不能比开始速度小
  795. /*if (target_speed < start_speed) {
  796. target_speed = start_speed;
  797. }*/
  798. /*
  799. //计算加速度会达到的最大值
  800. cur_max_accdec = (float)(target_speed - axis->start_speed) / acc_steps;
  801. //尽量保证最大加速度不会大于限制值
  802. if(cur_max_accdec>=0)
  803. {
  804. if (cur_max_accdec > axis->max_acc) {
  805. cur_max_accdec = axis->max_acc;
  806. }
  807. }
  808. else
  809. {
  810. if (-cur_max_accdec > axis->max_acc) {
  811. cur_max_accdec = -axis->max_acc;
  812. }
  813. }
  814. */
  815. cur_speed = start_speed;
  816. //S加速曲线加速表计算
  817. do {
  818. //计算速度
  819. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  820. reg_Value = (int)((clock) / cur_speed);
  821. //过滤器重复的加减速点
  822. #ifdef AXIS_IGNORE_REPEAT_STEP
  823. if (last_reg_value != reg_Value) {
  824. #endif
  825. last_reg_value = reg_Value;
  826. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  827. AXIS_ACC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  828. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  829. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  830. }
  831. #ifdef AXIS_USING_DEBUG
  832. axis->acc_speed_table[index] = (int)cur_speed;
  833. #endif
  834. index++;
  835. #ifdef AXIS_IGNORE_REPEAT_STEP
  836. }
  837. #endif
  838. cur_speed += accdec_value;
  839. } while ((((accdec_value>=0)&&(cur_speed < target_speed))
  840. ||((accdec_value<0)&&(cur_speed > target_speed)))
  841. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  842. //保存表大小
  843. axis->acc_table_size = index;
  844. #ifdef AXIS_USING_ACC_TABLE_ONLY
  845. if (index > 0) {
  846. index -= 1;
  847. }
  848. axis->dec_table_size = index;
  849. #else
  850. //cur_max_accdec = (target_speed - axis->stop_speed) / dec_steps;
  851. /*
  852. //计算加速度会达到的最大值
  853. if (cur_max_accdec > axis->max_acc) {
  854. cur_max_accdec = axis->max_acc;
  855. }
  856. dec_steps = (int)((target_speed - axis->stop_speed) / cur_max_accdec);
  857. */
  858. //开始减速表计算
  859. index = 0;
  860. last_reg_value = 0;
  861. //固定斜率
  862. accdec_value=200;
  863. if(target_speed < stop_speed)
  864. stop_speed = target_speed;
  865. cur_speed = stop_speed;
  866. //S加速曲线减速表计算
  867. do {
  868. //计算速度
  869. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  870. reg_Value = (int)((clock) / cur_speed);
  871. //过滤器重复的加减速点
  872. #ifdef AXIS_IGNORE_REPEAT_STEP
  873. if (last_reg_value != reg_Value) {
  874. #endif
  875. last_reg_value = reg_Value;
  876. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  877. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + (reg_Value >> 1)) / reg_Value * clock_ratio);
  878. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  879. #ifdef AXIS_USING_DEBUG
  880. axis->dec_speed_table[index] = (int)cur_speed;
  881. #endif
  882. index++;
  883. #ifdef AXIS_IGNORE_REPEAT_STEP
  884. }
  885. #endif
  886. cur_speed += accdec_value;
  887. } while ((cur_speed < target_speed) && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  888. //保存减速表大小
  889. axis->dec_table_size = index;
  890. //if (axis->dec_table_size > 0) {
  891. // axis->dec_table_size -= 1;
  892. //}
  893. #endif
  894. }
  895. /**
  896. * 以指数曲线减速的方式计算加减速表
  897. *
  898. * @author LAZ (024608)
  899. *
  900. * @param axis 轴对象
  901. * @param target_speed 目标速度
  902. */
  903. static void axis_mode4_calc_accdec(axis_object_t *axis, int target_speed) {
  904. int acc_steps = 0; //加速时间单位数
  905. int dec_steps = 0; //减速时间单位数
  906. int cur_accdec = 0; //当前加减速度
  907. int cur_max_accdec = 0; //当前最大加速度减速度
  908. long cur_speed = 0; //当前速度
  909. // long ddc_speed = 0; //减减速开始速度
  910. long cur_aacddc = 0; //当前加加速减减速
  911. int index = 0;
  912. int i;
  913. long clock = axis->clock;
  914. long clock_ratio = axis->clock / 1000000;
  915. long start_speed = (long)(axis->start_speed*10);
  916. long cur_target_speed=(long)(target_speed*10);
  917. long stop_speed;
  918. //周期临时值
  919. long period;
  920. int reg_Value = 0;
  921. memset(&axis->acc_number_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  922. memset(&axis->acc_reg_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  923. memset(&axis->dec_number_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  924. memset(&axis->dec_reg_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  925. period = axis->period;
  926. //限制加速度周期
  927. //if (period < 1) period = 2;
  928. //限制最小加速度时间单位
  929. acc_steps = axis->acc_time / period;
  930. if (acc_steps < 10) {
  931. acc_steps = 10;
  932. }
  933. dec_steps = axis->dec_time / period;
  934. if (dec_steps < 10) {
  935. dec_steps = 10;
  936. }
  937. //转换倍率
  938. period *= 1000;
  939. cur_aacddc = abs(cur_target_speed - start_speed);
  940. cur_aacddc =cur_aacddc*2/(acc_steps * acc_steps);
  941. //速度放大10倍,保证至少0.1以上的加减速度,这样加速时间可延时到300ms
  942. if (cur_aacddc < 1)cur_aacddc = 1;
  943. //计算加速度会达到的最大值
  944. cur_max_accdec = cur_aacddc * acc_steps;
  945. if (cur_max_accdec > axis->max_acc*10)cur_max_accdec = axis->max_acc*10;
  946. //限制加加速度,加加速度过大会导致过冲
  947. if (cur_aacddc > axis->max_aac*10)cur_aacddc = axis->max_aac*10;
  948. //由于加加速比计算结果小,所以需要更多步去达到最高速,需要重新加速步
  949. acc_steps = cur_max_accdec / cur_aacddc;
  950. //目标速度大于起始速度,加速
  951. if(target_speed>=axis->start_speed)
  952. {
  953. cur_speed = (long)(axis->start_speed*10);
  954. cur_target_speed=(long)(target_speed*10);
  955. axis->acc_position=0;
  956. cur_accdec=0;
  957. index=0;
  958. }
  959. else//减速
  960. {
  961. cur_speed=target_speed*10;
  962. cur_target_speed=axis->start_speed*10;
  963. axis->acc_position=0;
  964. cur_accdec=0;
  965. index=0;
  966. }
  967. //指数加速曲线加速表计算
  968. do{
  969. cur_speed += cur_accdec;//计算速度
  970. if (cur_speed > cur_target_speed) cur_speed = cur_target_speed;
  971. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  972. reg_Value = (clock*10+5) / cur_speed;
  973. if(target_speed>=axis->start_speed){ //加速
  974. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  975. AXIS_ACC_NUMBER_TABLE(axis)[index] = (int)((period + AXIS_CALC_ROUND(reg_Value)) * clock_ratio / reg_Value);
  976. if(AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  977. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  978. }
  979. axis->acc_position+=AXIS_ACC_NUMBER_TABLE(axis)[index];
  980. #ifdef AXIS_USING_DEBUG
  981. axis->acc_speed_table[index] = (int)cur_speed/10;
  982. #endif
  983. }
  984. else{ //减速,
  985. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  986. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + AXIS_CALC_ROUND(reg_Value)) * clock_ratio / reg_Value);
  987. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  988. AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  989. }
  990. #ifdef AXIS_USING_DEBUG
  991. axis->dec_speed_table[index] = (int)cur_speed/10;
  992. #endif
  993. }
  994. index++;
  995. if (cur_speed < cur_target_speed) { //还没到开始减速的速度
  996. //加加速流程
  997. cur_accdec += cur_aacddc;
  998. if (cur_accdec >= cur_max_accdec) {
  999. cur_accdec = cur_max_accdec;
  1000. }
  1001. }
  1002. } while ((cur_aacddc > 0 && cur_speed < cur_target_speed)
  1003. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  1004. if(target_speed<axis->start_speed){//减速,调换减速顺序。
  1005. for(i=0;i<index;i++)
  1006. {
  1007. AXIS_ACC_REG_TABLE(axis)[i] = AXIS_DEC_REG_TABLE(axis)[index-1-i];
  1008. AXIS_ACC_NUMBER_TABLE(axis)[i] = AXIS_DEC_NUMBER_TABLE(axis)[index-1-i];
  1009. #ifdef AXIS_USING_DEBUG
  1010. axis->acc_speed_table[i] = axis->dec_speed_table[index-1-i] ;
  1011. #endif
  1012. }
  1013. }
  1014. //保存表大小
  1015. axis->acc_table_size = index;
  1016. #ifdef AXIS_USING_ACC_TABLE_ONLY
  1017. if (index > 0) {
  1018. index -= 1;
  1019. }
  1020. axis->dec_table_size = index;
  1021. #else
  1022. memset(&axis->dec_number_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  1023. memset(&axis->dec_reg_table[0],0,2*AXIS_MAX_ACCDEC_STEP);
  1024. //if (dec_steps != acc_steps) {
  1025. stop_speed=axis->stop_speed*10;
  1026. cur_target_speed=target_speed*10;
  1027. if(stop_speed>cur_target_speed)stop_speed=cur_target_speed;
  1028. cur_aacddc = ((cur_target_speed - stop_speed))*2 / (dec_steps * dec_steps);
  1029. //保证至少1以上的加减速度
  1030. if (cur_aacddc < 1) cur_aacddc = 1;
  1031. //计算加速度会达到的最大值
  1032. cur_max_accdec = cur_aacddc * dec_steps;
  1033. if ( cur_max_accdec > axis->max_acc*10)cur_max_accdec = axis->max_acc*10;
  1034. //限制加加速度,加加速度过大会导致过冲
  1035. if ( cur_aacddc > axis->max_aac*10) {
  1036. cur_aacddc = axis->max_aac*10;
  1037. }
  1038. //由于加加速比计算结果小,所以需要更多步去达到最高速,需要重新加速步
  1039. dec_steps = cur_max_accdec / cur_aacddc;
  1040. //开始减速表计算
  1041. index = 0;
  1042. cur_accdec=0;
  1043. cur_speed = stop_speed;
  1044. //指数加速曲线减速表计算
  1045. do {
  1046. //计算速度
  1047. cur_speed += cur_accdec;
  1048. if (cur_speed > cur_target_speed) cur_speed = cur_target_speed;
  1049. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  1050. reg_Value = ((clock*10+5) / cur_speed);
  1051. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  1052. AXIS_DEC_NUMBER_TABLE(axis)[index] = (int)((period + AXIS_CALC_ROUND(reg_Value)) * clock_ratio / reg_Value);
  1053. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  1054. AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  1055. }
  1056. #ifdef AXIS_USING_DEBUG
  1057. axis->dec_speed_table[index] = cur_speed/10;
  1058. #endif
  1059. index++;
  1060. if (cur_speed < cur_target_speed) { //还没到开始减速的速度
  1061. //加加速流程
  1062. cur_accdec += cur_aacddc;
  1063. if (cur_accdec >= cur_max_accdec) {
  1064. cur_accdec = cur_max_accdec;
  1065. }
  1066. }
  1067. } while (( cur_speed < cur_target_speed)
  1068. && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  1069. //保存减速表大小
  1070. axis->dec_table_size = index;
  1071. //if (axis->dec_table_size > 0) {
  1072. // axis->dec_table_size -= 1;
  1073. //}
  1074. #endif
  1075. }
  1076. #else
  1077. /**
  1078. * 以直线加减速的方式计算加减速表
  1079. *
  1080. * @author LXZ (021720)
  1081. *
  1082. * @param axis
  1083. * @param target_speed
  1084. */
  1085. static void axis_mode0_calc_accdec(axis_object_t *axis, int target_speed) {
  1086. int acc_steps = 0; //加速时间单位数
  1087. int dec_steps = 0; //减速时间单位数
  1088. int cur_max_accdec = 0; //当前最大加速度减速度
  1089. int cur_speed = 0; //当前速度
  1090. int index = 0;
  1091. int step = 0;
  1092. int clock = axis->clock;
  1093. int clock_ratio = axis->clock / 1000000;
  1094. int start_speed = axis->start_speed;
  1095. //周期临时值
  1096. int period;
  1097. int reg_Value = 0;
  1098. int last_reg_value = 0;
  1099. period = axis->period;
  1100. //限制加速度周期
  1101. if (period < 1) period = 2;
  1102. //限制最小加速度时间单位
  1103. acc_steps = axis->acc_time / period;
  1104. if (acc_steps < 10) {
  1105. acc_steps = 10;
  1106. }
  1107. dec_steps = axis->dec_time / period;
  1108. if (dec_steps < 10) {
  1109. dec_steps = 10;
  1110. }
  1111. //转换倍率
  1112. period *= 1000;
  1113. //保证加减速尽量对齐,需要保证加减数是偶数,由于计算误差,加1能让结果更加逼近设置的时间
  1114. acc_steps++;
  1115. acc_steps >>= 1;
  1116. dec_steps++;
  1117. dec_steps >>= 1;
  1118. //保证是偶次数加减数,能减少计算误差
  1119. //if ((min_acc_step & 0x01) != 0)
  1120. // min_acc_step++;
  1121. //目标速度不能比开始速度小
  1122. if (target_speed < start_speed) {
  1123. target_speed = start_speed;
  1124. }
  1125. //计算加速度会达到的最大值
  1126. cur_max_accdec = (target_speed - axis->start_speed) / acc_steps;
  1127. //尽量保证最大加速度不会大于限制值
  1128. if (cur_max_accdec > axis->max_acc) {
  1129. cur_max_accdec = axis->max_acc;
  1130. }
  1131. acc_steps = (target_speed - axis->start_speed) / cur_max_accdec;
  1132. //S加速曲线加速表计算
  1133. do {
  1134. //计算速度
  1135. cur_speed = ((target_speed - start_speed) * step) / acc_steps + start_speed;
  1136. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  1137. reg_Value = ((clock) / cur_speed);
  1138. //过滤器重复的加减速点
  1139. #ifdef AXIS_IGNORE_REPEAT_STEP
  1140. if (last_reg_value != reg_Value) {
  1141. #endif
  1142. last_reg_value = reg_Value;
  1143. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  1144. AXIS_ACC_NUMBER_TABLE(axis)[index] = ((period + (reg_Value >> 1)) / reg_Value) * clock_ratio;
  1145. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  1146. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  1147. }
  1148. #ifdef AXIS_USING_DEBUG
  1149. axis->acc_speed_table[index] = cur_speed;
  1150. #endif
  1151. index++;
  1152. #ifdef AXIS_IGNORE_REPEAT_STEP
  1153. }
  1154. #endif
  1155. step++;
  1156. } while (cur_speed < target_speed && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  1157. //保存表大小
  1158. axis->acc_table_size = index;
  1159. #ifdef AXIS_USING_ACC_TABLE_ONLY
  1160. if (index > 0) {
  1161. index -= 1;
  1162. }
  1163. axis->dec_table_size = index;
  1164. #else
  1165. cur_max_accdec = (target_speed - axis->start_speed) / dec_steps;
  1166. //计算加速度会达到的最大值
  1167. if (cur_max_accdec > axis->max_acc) {
  1168. cur_max_accdec = axis->max_acc;
  1169. }
  1170. dec_steps = (target_speed - axis->start_speed) / cur_max_accdec;
  1171. //开始减速表计算
  1172. index = 0;
  1173. last_reg_value = 0;
  1174. step = 0;
  1175. //S加速曲线减速表计算
  1176. do {
  1177. //计算速度
  1178. cur_speed = (target_speed - start_speed) * step / dec_steps + start_speed;
  1179. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  1180. reg_Value = ((clock) / cur_speed);
  1181. //过滤器重复的加减速点
  1182. #ifdef AXIS_IGNORE_REPEAT_STEP
  1183. if (last_reg_value != reg_Value) {
  1184. #endif
  1185. last_reg_value = reg_Value;
  1186. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  1187. AXIS_DEC_NUMBER_TABLE(axis)[index] = ((period + (reg_Value >> 1)) / reg_Value) * clock_ratio;
  1188. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  1189. #ifdef AXIS_USING_DEBUG
  1190. axis->dec_speed_table[index] = cur_speed;
  1191. #endif
  1192. index++;
  1193. #ifdef AXIS_IGNORE_REPEAT_STEP
  1194. }
  1195. #endif
  1196. step++;
  1197. } while (cur_speed < target_speed && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  1198. //保存减速表大小
  1199. axis->dec_table_size = index;
  1200. if (axis->dec_table_size > 0) {
  1201. axis->dec_table_size -= 1;
  1202. }
  1203. #endif
  1204. }
  1205. /**
  1206. * 以S曲线减速的方式计算加减速表
  1207. *
  1208. * @author LXZ (021720)
  1209. *
  1210. * @param axis 轴对象
  1211. * @param target_speed 目标速度
  1212. */
  1213. static void axis_mode1_calc_accdec(axis_object_t *axis, int target_speed) {
  1214. int acc_steps = 0; //加速时间单位数
  1215. int dec_steps = 0; //减速时间单位数
  1216. int cur_accdec = 0; //当前加减速度
  1217. int cur_max_accdec = 0; //当前最大加速度减速度
  1218. int cur_speed = 0; //当前速度
  1219. int ddc_speed = 0; //减减速开始速度
  1220. int cur_aacddc = 0; //当前加加速减减速
  1221. int mid_speed = 0; //中间速度
  1222. int index = 0;
  1223. int clock = axis->clock;
  1224. int clock_ratio = axis->clock / 1000000;
  1225. int start_speed = axis->start_speed;
  1226. //周期临时值
  1227. int period;
  1228. int reg_Value = 0;
  1229. int last_reg_value = 0;
  1230. period = axis->period;
  1231. //限制加速度周期
  1232. if (period < 1) period = 2;
  1233. //限制最小加速度时间单位
  1234. acc_steps = axis->acc_time / period;
  1235. if (acc_steps < 10) {
  1236. acc_steps = 10;
  1237. }
  1238. dec_steps = axis->dec_time / period;
  1239. if (dec_steps < 10) {
  1240. dec_steps = 10;
  1241. }
  1242. //转换倍率
  1243. period *= 1000;
  1244. //保证加减速尽量对齐,需要保证加减数是偶数,由于计算误差,加1能让结果更加逼近设置的时间
  1245. acc_steps++;
  1246. acc_steps >>= 1;
  1247. dec_steps++;
  1248. dec_steps >>= 1;
  1249. //保证是偶次数加减数,能减少计算误差
  1250. //if ((min_acc_step & 0x01) != 0)
  1251. // min_acc_step++;
  1252. //目标速度不能比开始速度小
  1253. if (target_speed < start_speed) {
  1254. target_speed = start_speed;
  1255. }
  1256. //S加速算法需要先计算出加加速
  1257. //推导过程:
  1258. //S加减速度分为加加速与减加速度两个过程,假定两个过程是对称的,那么总占用时间为加速时间t的一半
  1259. //因此利用积分公式(dv = sadt, vm = (vd - v0) / 2 => a = vm / (2 * (t / 2))可以得到acc = (vd - v0) * 2 / t
  1260. //其中a与acc表示加速度,vd表示目标速度,v0表示开始速度t表示加速时间一半
  1261. //加加速度aac与加速度关系acc为acc = aac * (t / 2)
  1262. //从而公式为aac = (vd - v0) * 4 / (t ^2)
  1263. //其中acc表示加速度,aac表示加加速,vd为目标速度,v0为开始速度,t为总的加速时间,在这算法里它就是加速度步数
  1264. //由于加速度步数要对称就必须保持偶数,加上一些计算偏差以及为了保证电机正常运算必须限制参数
  1265. //因此实际上两个过程在实际上是很难对称的,这就造成了实际加速时间肯定会比设定的长,具体长多
  1266. //跟最大加加速,最大中速度,最小脉冲数,计算偏差,最小加速度步数有关
  1267. cur_aacddc = ((target_speed - axis->start_speed)) / (acc_steps * acc_steps);
  1268. //计算加速度会达到的最大值
  1269. cur_max_accdec = cur_aacddc * acc_steps;
  1270. if (cur_max_accdec > axis->max_acc) {
  1271. cur_max_accdec = axis->max_acc;
  1272. }
  1273. //限制加加速度,加加速度过大会导致过冲
  1274. if (cur_aacddc > axis->max_aac) {
  1275. cur_aacddc = axis->max_aac;
  1276. }
  1277. //由于加加速比计算结果小,所以需要更多步去达到最高速,需要重新加速步
  1278. acc_steps = cur_max_accdec / cur_aacddc;
  1279. if (cur_aacddc == 0) cur_aacddc = 1;
  1280. //开始计算加速表
  1281. mid_speed = (target_speed + axis->start_speed) >> 1;
  1282. ddc_speed = target_speed - ((cur_aacddc * acc_steps * acc_steps) >> 1);
  1283. if (ddc_speed < mid_speed) {
  1284. ddc_speed = mid_speed;
  1285. }
  1286. cur_speed = axis->start_speed;
  1287. //S加速曲线加速表计算
  1288. do {
  1289. //正常的情况下,速度达到目标速度一半后,加速度肯定达到最大值,如果没有达到,必须强制达到
  1290. //否则会造成波形中间下移,导致减加速流程过长
  1291. if (cur_speed >= mid_speed) {
  1292. cur_accdec = cur_max_accdec;
  1293. //保证只能进一次
  1294. mid_speed *= 3;
  1295. }
  1296. if (cur_speed >= ddc_speed) { //减减速需要提前判断,尽量保证流程对称
  1297. //减加速流程
  1298. if (cur_accdec > cur_aacddc) {
  1299. cur_accdec -= cur_aacddc;
  1300. } else {
  1301. //保证至少有这个加加速,让速度有机会达到目标速度
  1302. cur_accdec = cur_aacddc;
  1303. }
  1304. }
  1305. //计算速度
  1306. cur_speed += cur_accdec;
  1307. if (cur_speed > target_speed) cur_speed = target_speed;
  1308. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  1309. reg_Value = ((clock) / cur_speed);
  1310. //过滤器重复的加减速点
  1311. #ifdef AXIS_IGNORE_REPEAT_STEP
  1312. if (last_reg_value != reg_Value) {
  1313. #endif
  1314. last_reg_value = reg_Value;
  1315. AXIS_ACC_REG_TABLE(axis)[index] = reg_Value;
  1316. AXIS_ACC_NUMBER_TABLE(axis)[index] = ((period + (reg_Value >> 1)) / reg_Value) * clock_ratio;
  1317. if (AXIS_ACC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  1318. AXIS_ACC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  1319. }
  1320. #ifdef AXIS_USING_DEBUG
  1321. axis->acc_speed_table[index] = cur_speed;
  1322. #endif
  1323. index++;
  1324. #ifdef AXIS_IGNORE_REPEAT_STEP
  1325. }
  1326. #endif
  1327. if (cur_speed < ddc_speed) { //还没到开始减速的速度
  1328. //加加速流程
  1329. cur_accdec += cur_aacddc;
  1330. if (cur_accdec >= cur_max_accdec) {
  1331. cur_accdec = cur_max_accdec;
  1332. }
  1333. }
  1334. } while (cur_speed < target_speed && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  1335. //保存表大小
  1336. axis->acc_table_size = index;
  1337. #ifdef AXIS_USING_ACC_TABLE_ONLY
  1338. if (index > 0) {
  1339. index -= 1;
  1340. }
  1341. axis->dec_table_size = index;
  1342. #else
  1343. if (dec_steps != acc_steps) {
  1344. cur_aacddc = ((target_speed - axis->start_speed)) / (dec_steps * dec_steps);
  1345. //计算加速度会达到的最大值
  1346. cur_max_accdec = cur_aacddc * dec_steps;
  1347. if (cur_max_accdec > axis->max_acc) {
  1348. cur_max_accdec = axis->max_acc;
  1349. }
  1350. //限制加加速度,加加速度过大会导致过冲
  1351. if (cur_aacddc > axis->max_aac) {
  1352. cur_aacddc = axis->max_aac;
  1353. }
  1354. //由于加加速比计算结果小,所以需要更多步去达到最高速,需要重新加速步
  1355. dec_steps = cur_max_accdec / cur_aacddc;
  1356. //curMaxAccDec = curAacDdc * decSteps;
  1357. if (cur_aacddc == 0) cur_aacddc = 1;
  1358. }
  1359. //如果减速时间一样,就不重复计算表了
  1360. if (dec_steps == acc_steps) {
  1361. int j = 0;
  1362. while (j < index) {
  1363. AXIS_DEC_REG_TABLE(axis)[j] = AXIS_ACC_REG_TABLE(axis)[j];
  1364. AXIS_DEC_NUMBER_TABLE(axis)[j] = AXIS_ACC_NUMBER_TABLE(axis)[j];
  1365. j++;
  1366. }
  1367. axis->dec_table_size = index;
  1368. if (axis->dec_table_size > 0) {
  1369. axis->dec_table_size -= 1;
  1370. }
  1371. return;
  1372. }
  1373. //开始减速表计算
  1374. index = 0;
  1375. last_reg_value = 0;
  1376. mid_speed = (target_speed + axis->start_speed) >> 1;
  1377. ddc_speed = target_speed - ((cur_aacddc * dec_steps * dec_steps) >> 1);
  1378. if (ddc_speed < mid_speed) {
  1379. ddc_speed = mid_speed;
  1380. }
  1381. cur_speed = axis->start_speed;
  1382. //S加速曲线减速表计算
  1383. do {
  1384. //正常的情况下,速度达到目标速度一半后,加速度肯定达到最大值,如果没有达到,必须强制达到
  1385. //否则会造成波形中间下移,导致减加速流程过长
  1386. if (cur_speed >= mid_speed) {
  1387. cur_accdec = cur_max_accdec;
  1388. //保证只可能进一次
  1389. mid_speed *= 3;
  1390. }
  1391. if (cur_speed >= ddc_speed) { //减减速需要提前判断,尽量保证流程对称
  1392. //减加速流程
  1393. if (cur_accdec > cur_aacddc) {
  1394. cur_accdec -= cur_aacddc;
  1395. } else {
  1396. //保证至少有这个加加速,让速度有机会达到目标速度
  1397. cur_accdec = cur_aacddc;
  1398. }
  1399. }
  1400. //计算速度
  1401. cur_speed += cur_accdec;
  1402. if (cur_speed > target_speed) cur_speed = target_speed;
  1403. //计算该速度对应的寄存器值与以及当前频率下需要满足加速周期的脉冲速
  1404. reg_Value = ((clock) / cur_speed);
  1405. //过滤器重复的加减速点
  1406. #ifdef AXIS_IGNORE_REPEAT_STEP
  1407. if (last_reg_value != reg_Value) {
  1408. #endif
  1409. last_reg_value = reg_Value;
  1410. AXIS_DEC_REG_TABLE(axis)[index] = reg_Value;
  1411. AXIS_DEC_NUMBER_TABLE(axis)[index] = ((period + (reg_Value >> 1)) / reg_Value) * clock_ratio;
  1412. if (AXIS_DEC_NUMBER_TABLE(axis)[index] < axis->min_steps) {
  1413. AXIS_DEC_NUMBER_TABLE(axis)[index] = axis->min_steps;
  1414. }
  1415. #ifdef AXIS_USING_DEBUG
  1416. axis->dec_speed_table[index] = cur_speed;
  1417. #endif
  1418. index++;
  1419. #ifdef AXIS_IGNORE_REPEAT_STEP
  1420. }
  1421. #endif
  1422. if (cur_speed < ddc_speed) { //还没到开始减速的速度
  1423. //加加速流程
  1424. cur_accdec += cur_aacddc;
  1425. if (cur_accdec >= cur_max_accdec) {
  1426. cur_accdec = cur_max_accdec;
  1427. }
  1428. }
  1429. } while (cur_speed < target_speed && (index + 1) < AXIS_MAX_ACCDEC_STEP);
  1430. //保存减速表大小
  1431. axis->dec_table_size = index;
  1432. if (axis->dec_table_size > 0) {
  1433. axis->dec_table_size -= 1;
  1434. }
  1435. #endif
  1436. }
  1437. #endif
  1438. /**
  1439. * 计算加减速表的接口
  1440. *
  1441. * @author LXZ (021720)
  1442. *
  1443. * @param axis
  1444. * @param target_speed
  1445. */
  1446. static void axis_calc_speed_table(axis_object_t *axis, int target_speed) {
  1447. switch(axis->accdec_mode)
  1448. {
  1449. case 0:
  1450. axis_mode0_calc_accdec(axis, target_speed);
  1451. break;
  1452. case 1:
  1453. axis_mode1_calc_accdec(axis, target_speed);
  1454. break;
  1455. case 2:
  1456. axis_mode2_calc_accdec(axis, target_speed);
  1457. break;
  1458. case 3:
  1459. axis_mode3_calc_accdec(axis, target_speed);
  1460. break;
  1461. case 4:
  1462. axis_mode4_calc_accdec(axis, target_speed);
  1463. break;
  1464. default:
  1465. axis_mode0_calc_accdec(axis, target_speed);
  1466. break;
  1467. }
  1468. }
  1469. /*
  1470. *脉冲细分分割函数,
  1471. *希望是每个脉冲产生后,改变一次脉冲频率,尽量使脉冲平滑过渡
  1472. */
  1473. void axis_acc_pwm_divide(axis_object_t *axis)
  1474. {
  1475. unsigned short reg_value = 0;
  1476. if(axis->cur_speed_index+1 <=axis->acc_speed_index){//加速索引小于总的索引
  1477. if(axis->cur_reg_value>AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index+1]){//加速时
  1478. reg_value=axis->cur_reg_value-AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index+1];
  1479. if(axis->accdec_count>reg_value){//如果脉冲数大于,频率的差值
  1480. reg_value=1;
  1481. }
  1482. else{//如果脉冲数小于频率的差值
  1483. reg_value>>=1;
  1484. }
  1485. axis->cur_reg_value-=reg_value;
  1486. }
  1487. else if(axis->cur_reg_value<AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index+1]){//减速时
  1488. reg_value=AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index+1]-axis->cur_reg_value;
  1489. if(axis->accdec_count>reg_value){//如果脉冲数大于,频率的差值
  1490. reg_value=1;
  1491. }
  1492. else{//如果脉冲数小于频率的差值
  1493. reg_value>>=1;
  1494. }
  1495. axis->cur_reg_value+=reg_value;
  1496. }
  1497. }
  1498. }
  1499. void axis_dec_pwm_divide(axis_object_t *axis)
  1500. {
  1501. unsigned short reg_value = 0;
  1502. if(axis->cur_speed_index-1 >=0){//加速索引小于总的索引
  1503. if(axis->cur_reg_value>AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index-1]){//加速时
  1504. reg_value=axis->cur_reg_value-AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index-1];
  1505. if(axis->accdec_count>reg_value){//如果脉冲数大于,频率的差值
  1506. reg_value=1;
  1507. }
  1508. else{//如果脉冲数小于频率的差值
  1509. reg_value>>=1;
  1510. }
  1511. axis->cur_reg_value-=reg_value;
  1512. }
  1513. else if(axis->cur_reg_value<AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index-1]){//减速时
  1514. reg_value=AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index-1]-axis->cur_reg_value;
  1515. if(axis->accdec_count>reg_value){//如果脉冲数大于,频率的差值
  1516. reg_value=1;
  1517. }
  1518. else{//如果脉冲数小于频率的差值
  1519. reg_value>>=1;
  1520. }
  1521. axis->cur_reg_value+=reg_value;
  1522. }
  1523. }
  1524. }
  1525. /**
  1526. * 轴运动中断函数
  1527. *
  1528. * @author lxz
  1529. *
  1530. * @param handle
  1531. */
  1532. void axis_it_handle(void *handle) {
  1533. axis_object_t *axis = (axis_object_t *)handle;
  1534. unsigned short reg_value = 0;
  1535. EXIO_AXIS_DW_Filter(axis->axis_no);
  1536. //if(axis->axis_no==X_AXIS)MXC_SBCHECK_PROC();
  1537. axis->position += axis->feed;
  1538. switch (axis->run_mode) {
  1539. case AXIS_MODE_PP:
  1540. // if (axis->feed > 0) {
  1541. if ((axis->position - axis->dec_position) * axis->feed >= 0) { //开始减速的条件
  1542. if (axis->cur_accdec_status != AXIS_ACCDEC_STATUS_DEC) {
  1543. axis->cur_accdec_status = AXIS_ACCDEC_STATUS_DEC;
  1544. axis->cur_speed_index = axis->dec_speed_index;
  1545. axis->accdec_count = AXIS_DEC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1546. axis->cur_reg_value = AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index];
  1547. }
  1548. else if (axis->accdec_count >1) { //减速脉冲计数
  1549. axis->accdec_count--;
  1550. axis_dec_pwm_divide(axis);
  1551. }
  1552. else if (axis->cur_speed_index > 0) { //切换速度
  1553. axis->cur_speed_index--;
  1554. axis->accdec_count = AXIS_DEC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1555. axis->cur_reg_value=AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index];
  1556. }
  1557. }
  1558. else {
  1559. axis->cur_accdec_status = AXIS_ACCDEC_STATUS_ACC;
  1560. if (axis->accdec_count >1) { //加速脉冲计数
  1561. axis->accdec_count--;
  1562. axis_acc_pwm_divide(axis);
  1563. } else if (axis->cur_speed_index < axis->acc_speed_index) { //切换速度
  1564. axis->cur_speed_index++;
  1565. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1566. axis->cur_reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1567. }
  1568. }
  1569. //到达位置立刻停止
  1570. if ((axis->position - axis->target_position) * axis->feed >= 0) {
  1571. if((axis->PP_Stop_Select) == 0)
  1572. AXIS_OFF(axis);
  1573. }
  1574. //更新寄存器值
  1575. AXIS_SET_PERION(axis, axis->cur_reg_value);
  1576. break;
  1577. #ifdef AXIS_TASK_NUMBER
  1578. case AXIS_MODE_PP_TASKS:
  1579. if ((axis->position - axis->tasks[axis->cur_task_index].dec_position) * axis->feed >= 0) {
  1580. if (axis->cur_task_index + 1 < axis->task_number) {
  1581. if (axis->cur_accdec_status != 2) {
  1582. axis->cur_accdec_status = 2;
  1583. axis->cur_speed_index = axis->tasks[axis->cur_task_index].dec_speed_index;
  1584. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1585. } else if (axis->accdec_count > 1) {
  1586. axis->accdec_count--;
  1587. } else if (axis->cur_speed_index > axis->tasks[axis->cur_task_index + 1].dst_speed_index) {
  1588. axis->cur_speed_index--;
  1589. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1590. }
  1591. reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1592. } else {
  1593. if (axis->cur_accdec_status != 2) {
  1594. axis->cur_accdec_status = 2;
  1595. axis->cur_speed_index = axis->tasks[axis->cur_task_index].dec_speed_index;
  1596. axis->accdec_count = AXIS_DEC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1597. } else if (axis->accdec_count > 1) {
  1598. axis->accdec_count--;
  1599. } else if (axis->cur_speed_index > 0) {
  1600. axis->cur_speed_index--;
  1601. axis->accdec_count = AXIS_DEC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1602. }
  1603. reg_value = AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index];
  1604. }
  1605. } else {
  1606. axis->cur_accdec_status = 0;
  1607. if (axis->accdec_count > 1) { //加速脉冲计数
  1608. axis->accdec_count--;
  1609. } else if (axis->cur_speed_index < axis->tasks[axis->cur_task_index].dst_speed_index) { //切换速度
  1610. axis->cur_speed_index++;
  1611. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1612. }
  1613. if (axis->cur_speed_index == axis->tasks[axis->cur_task_index].dst_speed_index &&
  1614. axis->accdec_count == 0) {
  1615. reg_value = axis->tasks[axis->cur_task_index].reg_value;
  1616. } else {
  1617. reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1618. }
  1619. }
  1620. if ((axis->position - axis->tasks[axis->cur_task_index].target_position) * axis->feed >= 0) {
  1621. if(axis->cur_task_index < axis->task_number) axis->cur_task_index++;
  1622. axis->cur_accdec_status = 0;
  1623. if (axis->cur_task_index >= axis->task_number) {
  1624. if((axis->PP_Stop_Select) == 0)
  1625. AXIS_OFF(axis);
  1626. }
  1627. }
  1628. //更新寄存器值
  1629. AXIS_SET_PERION(axis, reg_value);
  1630. axis->cur_speed=axis->clock/reg_value;
  1631. break;
  1632. #endif
  1633. case AXIS_MODE_PV:
  1634. axis->cur_accdec_status = AXIS_ACCDEC_STATUS_ACC;
  1635. if (axis->accdec_count > 1) {
  1636. axis->accdec_count--;
  1637. axis_acc_pwm_divide(axis);
  1638. }
  1639. else if (axis->cur_speed_index < axis->acc_speed_index) {
  1640. axis->cur_speed_index++;
  1641. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1642. axis->cur_reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1643. }
  1644. else if (axis->cur_speed_index > axis->acc_speed_index) {
  1645. axis->cur_speed_index--;
  1646. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1647. axis->cur_reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1648. }
  1649. else {
  1650. axis->accdec_count = 0;
  1651. }
  1652. if (axis->accdec_count == 0 && axis->cur_speed_index == axis->acc_speed_index) {
  1653. axis->cur_reg_value = axis->target_speed_reg;
  1654. }
  1655. //更新寄存器值
  1656. AXIS_SET_PERION(axis,axis->cur_reg_value);
  1657. break;
  1658. case AXIS_MODE_STOP:
  1659. //由于允许加减速表不对称,因此需要保护不要溢出
  1660. if (axis->cur_accdec_status != AXIS_ACCDEC_STATUS_DEC) {
  1661. axis->cur_accdec_status = AXIS_ACCDEC_STATUS_DEC;
  1662. axis->cur_speed_index = axis->dec_speed_index;
  1663. axis->accdec_count = AXIS_DEC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1664. } else if (axis->accdec_count > 1) {
  1665. axis->accdec_count--;
  1666. } else {
  1667. //计数一定脉冲后切换速度
  1668. if (axis->cur_speed_index > 0) axis->cur_speed_index--;
  1669. else AXIS_OFF(axis);
  1670. axis->accdec_count = AXIS_DEC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1671. }
  1672. axis->cur_reg_value = AXIS_DEC_REG_TABLE(axis)[axis->cur_speed_index];
  1673. //更新寄存器值
  1674. AXIS_SET_PERION(axis, axis->cur_reg_value);
  1675. break;
  1676. case AXIS_MODE_EMGSTOP:
  1677. axis->home_step = 0;
  1678. AXIS_OFF(axis);
  1679. axis->cur_speed=0;
  1680. break;
  1681. case AXIS_MODE_HOLD:
  1682. break;
  1683. }
  1684. if (axis->handle != 0) {
  1685. axis->handle();
  1686. }
  1687. }
  1688. /**
  1689. * 以目标速度移动到指定位置
  1690. *
  1691. * @author LXZ (021720)
  1692. *
  1693. * @param axis 轴对象
  1694. * @param rel
  1695. * 位置参考系,0表示相对坐标,1表进绝对值坐标
  1696. * @param position 目标位置
  1697. * @param target_speed 目标速度
  1698. */
  1699. void axis_pp(axis_object_t *axis, int rel, int position, int target_speed,long slowpos) {
  1700. //允许加速表可执行到的位置
  1701. int acc_index = 0;
  1702. //当前减速表可执行到的位置
  1703. int dec_index = 0;
  1704. //开始减速的位置
  1705. int dec_position = 0;
  1706. axis_calc_speed_table(axis, target_speed * axis->speed_unit);
  1707. //计算目标停止位置
  1708. axis->target_position = position;
  1709. if (rel == 0) {
  1710. axis->target_position += axis->position;
  1711. } else {
  1712. position -= axis->position;
  1713. SetStopSelect(X_AXIS,PP_Stop); //绝对位置时要停止
  1714. }
  1715. //计算进给方向
  1716. if (axis->target_position > axis->position) {
  1717. axis_cw(axis);
  1718. } else {
  1719. axis_ccw(axis);
  1720. }
  1721. //换算成绝对值
  1722. if (position < 0) {
  1723. position = -position;
  1724. }
  1725. if(position>slowpos){
  1726. position -= slowpos;
  1727. }
  1728. dec_index = 0;
  1729. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  1730. do {
  1731. //加速区域判断
  1732. //当当前的加速时速度比当前的减速时速度相等或者大的时候,可以进行一次减速
  1733. //这是为了保证加减速度差在可以接受的范围内
  1734. //这样也就可以让加减时间更加的灵活
  1735. if (AXIS_ACC_REG_TABLE(axis)[acc_index] < AXIS_DEC_REG_TABLE(axis)[dec_index] &&
  1736. dec_index < axis->dec_table_size - 1) {
  1737. //如果不能进行减加速,退出
  1738. if (position < AXIS_DEC_NUMBER_TABLE(axis)[dec_index]) break;
  1739. dec_index++;
  1740. position -= AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  1741. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  1742. }
  1743. else if (acc_index < (axis->acc_table_size - 1)) {
  1744. //如果已经不能加速就退出
  1745. if (position < AXIS_ACC_NUMBER_TABLE(axis)[acc_index]) break;
  1746. position -= AXIS_ACC_NUMBER_TABLE(axis)[acc_index];
  1747. acc_index++;
  1748. }
  1749. else {
  1750. break;
  1751. }
  1752. } while (position > 0);
  1753. axis->acc_speed_index = acc_index;
  1754. axis->dec_speed_index = dec_index;
  1755. axis->dec_position = axis->target_position - (dec_position * axis->feed) -(slowpos* axis->feed);
  1756. //让中断切换到定位模式
  1757. axis->run_mode = AXIS_MODE_PP;
  1758. axis_start(axis);
  1759. }
  1760. /**
  1761. * pp模式中途改变速度
  1762. *
  1763. * @author LXZ (021720)
  1764. *
  1765. * @param axis 轴对象
  1766. * @param target_speed 目标速度
  1767. * @param slowpos 最后低速运行距离
  1768. */
  1769. void axis_pp_change_speed (axis_object_t *axis, int rel,
  1770. int position, int target_speed,long slowpos) {
  1771. //允许加速表可执行到的位置
  1772. int acc_index = 0;
  1773. //当前减速表可执行到的位置
  1774. int dec_index = 0;
  1775. //开始减速的位置
  1776. int dec_position = 0;
  1777. //开始重新计算参数前,先进入轴保持模式
  1778. axis->run_mode = AXIS_MODE_HOLD;
  1779. //位置
  1780. axis->target_position = position;
  1781. if (rel == 0) {
  1782. axis->target_position += axis->position;
  1783. } else {
  1784. position -= axis->position;
  1785. SetStopSelect(X_AXIS,PP_Stop); //绝对位置时要停止
  1786. }
  1787. //计算加减速表
  1788. axis_calc_speed_table(axis, target_speed * axis->speed_unit);
  1789. //换算成绝对值
  1790. if (position < 0) {
  1791. position = -position;
  1792. }
  1793. //减掉最后低速运行距离
  1794. if(position>slowpos){
  1795. position -= slowpos;
  1796. }
  1797. dec_index = 0;
  1798. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  1799. do {
  1800. //加速区域判断
  1801. //当当前的加速时速度比当前的减速时速度相等或者大的时候,可以进行一次减速
  1802. //这是为了保证加减速度差在可以接受的范围内
  1803. //这样也就可以让加减时间更加的灵活
  1804. if (AXIS_ACC_REG_TABLE(axis)[acc_index] < AXIS_DEC_REG_TABLE(axis)[dec_index] &&
  1805. dec_index < axis->dec_table_size - 1) {
  1806. //如果不能进行减加速,退出
  1807. if (position < AXIS_DEC_NUMBER_TABLE(axis)[dec_index]) break;
  1808. dec_index++;
  1809. position -= AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  1810. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  1811. } else if (acc_index < (axis->acc_table_size - 1)) {
  1812. //如果已经不能加速就退出
  1813. if (position < AXIS_ACC_NUMBER_TABLE(axis)[acc_index]) break;
  1814. position -= AXIS_ACC_NUMBER_TABLE(axis)[acc_index];
  1815. acc_index++;
  1816. } else {
  1817. break;
  1818. }
  1819. } while (position > 0);
  1820. axis->acc_speed_index = acc_index;
  1821. axis->dec_speed_index = dec_index;
  1822. axis->dec_position = axis->target_position - (dec_position * axis->feed) -(slowpos* axis->feed);
  1823. axis->cur_speed_index = 0;
  1824. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1825. axis->cur_accdec_status = 0;
  1826. axis->cur_reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1827. AXIS_SET_PERION(axis, AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index]);
  1828. //计算完成,切换回PP模式
  1829. axis->run_mode = AXIS_MODE_PP;
  1830. }
  1831. /**
  1832. * 以目标速度执行连续运动
  1833. *
  1834. * @author LXZ (021720)
  1835. *
  1836. * @param axis 轴对象
  1837. * @param dir 运动方向,0为反转,1为正转
  1838. * @param target_speed 目标速度
  1839. */
  1840. void axis_pv(axis_object_t *axis, int dir, int target_speed) {
  1841. if (dir == 0) axis_ccw(axis);
  1842. else axis_cw(axis);
  1843. axis_calc_speed_table(axis, target_speed * axis->speed_unit);
  1844. axis->acc_speed_index = axis->acc_table_size - 1;
  1845. axis->target_speed_reg = AXIS_ACC_REG_TABLE(axis)[axis->acc_speed_index];
  1846. //让中断切换到连续运动模式
  1847. axis->run_mode = AXIS_MODE_PV;
  1848. axis_start(axis);
  1849. }
  1850. /**
  1851. * 以目标速度执行连续运动-中途改变目标速度
  1852. *
  1853. * @author LXZ (021720)
  1854. *
  1855. * @param axis 轴对象
  1856. * @param target_speed 目标速度
  1857. */
  1858. void axis_pv_change_speed_table(axis_object_t *axis, int target_speed) {
  1859. //开始计算前,先切换到保持模式
  1860. axis->run_mode = AXIS_MODE_HOLD;
  1861. //计算加减速表
  1862. axis_calc_speed_table(axis, target_speed * axis->speed_unit);
  1863. axis->acc_speed_index = axis->acc_table_size - 1;
  1864. axis->target_speed_reg = AXIS_ACC_REG_TABLE(axis)[axis->acc_speed_index];
  1865. axis->cur_speed_index = 0;
  1866. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1867. axis->cur_accdec_status = 0;
  1868. AXIS_SET_PERION(axis, AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index]);
  1869. //让中断切换到连续运动模式
  1870. axis->run_mode = AXIS_MODE_PV;
  1871. }
  1872. /**
  1873. * 直接变速不经过加减速过程,支持PP、PV模式调用,调用后切换到连续运动PV模式
  1874. *
  1875. * @author LXZ (021720)
  1876. *
  1877. * @param axis 轴对象
  1878. * @param target_speed 目标速度
  1879. */
  1880. void axis_change_speed_direct(axis_object_t *axis, int target_speed) {
  1881. unsigned short reg_value = 0;
  1882. int speed=target_speed * axis->speed_unit;
  1883. //开始计算前,先切换到保持模式
  1884. axis->run_mode = AXIS_MODE_HOLD;
  1885. //直接更新寄存器值
  1886. reg_value = axis->clock / speed;
  1887. axis->cur_reg_value=reg_value;
  1888. AXIS_SET_PERION(axis, reg_value);
  1889. axis->cur_speed=speed;
  1890. axis->start_speed = axis->cur_speed;
  1891. //计算加减速表
  1892. axis_calc_speed_table(axis, target_speed * axis->speed_unit);
  1893. axis->acc_speed_index = axis->acc_table_size - 1;
  1894. axis->target_speed_reg = AXIS_ACC_REG_TABLE(axis)[axis->acc_speed_index];
  1895. axis->cur_speed_index = 0;
  1896. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  1897. axis->cur_accdec_status = 0;
  1898. axis->cur_reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  1899. AXIS_SET_PERION(axis, AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index]);
  1900. //让中断切换到连续运动模式
  1901. axis->run_mode = AXIS_MODE_PV;
  1902. }
  1903. #ifdef AXIS_TASK_NUMBER
  1904. /**
  1905. * 初始化轴任务缓冲,并设置此时的方向
  1906. *
  1907. * @author LXZ (031420)
  1908. *
  1909. * @param axis 轴对象
  1910. * @param dir 期望运动的方向
  1911. */
  1912. void axis_init_task(axis_object_t *axis, int dir) {
  1913. axis->task_number = 0;
  1914. memset(axis->tasks, 0, sizeof(axis->tasks));
  1915. if (dir) {
  1916. axis_cw(axis);
  1917. } else {
  1918. axis_ccw(axis);
  1919. }
  1920. }
  1921. /**
  1922. * 添加进给值,该进给值都是绝对进给值,与方向与当前坐标无关,方向在初始化任务缓冲的时候就已经设置了
  1923. *
  1924. * @author LXZ (031420)
  1925. *
  1926. * @param axis 轴对象
  1927. * @param feed 进给值
  1928. * @param speed 期望速度
  1929. */
  1930. void axis_add_pp_task(axis_object_t *axis, int feed, int speed) {
  1931. if (feed <= 0) return;
  1932. if (axis->task_number < AXIS_TASK_NUMBER && feed > 0) {
  1933. axis->tasks[axis->task_number].position = feed;
  1934. axis->tasks[axis->task_number].speed = speed;
  1935. axis->task_number++;
  1936. }
  1937. }
  1938. /**
  1939. * 启动多段速定位运动模式
  1940. *
  1941. * @author LXZ (031420)
  1942. *
  1943. * @param axis
  1944. */
  1945. void axis_start_pp_task(axis_object_t *axis) {
  1946. int speed;
  1947. int i;
  1948. int posi1; //上一个位置
  1949. int posi2; //当前计算位置
  1950. int feed = 0; //进给量
  1951. int cur_speed_index = 0; //当前速度
  1952. int dst_speed_index = 0; //目标速度
  1953. int dec_speed_index = 0; //
  1954. int dec_position = 0; //开始减速的位置
  1955. if (axis->task_number == 0) return;
  1956. //计算最高速度
  1957. for (i = 0, speed = 0; i < axis->task_number; i++) {
  1958. if (speed < axis->tasks[i].speed) speed = axis->tasks[i].speed;
  1959. }
  1960. speed *= axis->speed_unit;
  1961. if (speed < axis->start_speed) {
  1962. speed = axis->start_speed;
  1963. }
  1964. axis_calc_speed_table(axis, speed);
  1965. posi1 = 0;
  1966. for (i = 0; i < axis->task_number; i++) {
  1967. speed = axis->tasks[i].speed * axis->speed_unit;
  1968. if (speed < axis->start_speed) {
  1969. speed = axis->start_speed;
  1970. }
  1971. axis->tasks[i].reg_value = axis->clock / speed;
  1972. dst_speed_index = 0;
  1973. do {
  1974. if (axis->acc_reg_table[dst_speed_index] > axis->tasks[i].reg_value) {
  1975. dst_speed_index++;
  1976. } else if (axis->acc_reg_table[dst_speed_index] < axis->tasks[i].reg_value) {
  1977. if (dst_speed_index > 0) dst_speed_index--;
  1978. break;
  1979. } else {
  1980. break;
  1981. }
  1982. } while ((dst_speed_index + 1) < axis->acc_table_size);
  1983. axis->tasks[i].dst_speed_index = dst_speed_index;
  1984. //计算目标位置
  1985. posi1 += axis->tasks[i].position;
  1986. axis->tasks[i].target_position = posi1 * axis->feed + axis->position;
  1987. }
  1988. posi2 = axis->tasks[i].target_position;
  1989. //进行减速位置安排
  1990. dst_speed_index = 0;
  1991. for (i = 0; i < axis->task_number; i++) {
  1992. //计算进给量
  1993. posi1 = posi2;
  1994. posi2 = axis->tasks[i].target_position;
  1995. //cur_speed_index = dst_speed_index;
  1996. dst_speed_index = axis->tasks[i].dst_speed_index;
  1997. feed = axis->tasks[i].position;
  1998. if (i + 1 == axis->task_number) {
  1999. //最后一个流程需要涉及到减停操作
  2000. //由上一个速度计算减速位置
  2001. dec_speed_index = 0;
  2002. dec_position = 0;
  2003. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_speed_index];
  2004. do {
  2005. //加速区域判断
  2006. //当当前的加速时速度比当前的减速时速度相等或者大的时候,可以进行一次减速
  2007. //这是为了保证加减速度差在可以接受的范围内
  2008. //这样也就可以让加减时间更加的灵活
  2009. if (cur_speed_index > dst_speed_index) { //如果当前速度比期望速度大,需要先降速再打算,
  2010. // 然而这样的机会在前面运算正常的情况下是很少出现的
  2011. if (feed < AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index]) break;
  2012. feed -= AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index];
  2013. cur_speed_index--;
  2014. } else if (AXIS_ACC_REG_TABLE(axis)[cur_speed_index] < AXIS_DEC_REG_TABLE(axis)[dec_speed_index] &&
  2015. dec_speed_index < axis->dec_table_size - 1) { //当前速度在期望速度以下先保证减速位置
  2016. //如果不能进行减加速,退出
  2017. if (feed < AXIS_DEC_NUMBER_TABLE(axis)[dec_speed_index]) break;
  2018. dec_speed_index++;
  2019. feed -= AXIS_DEC_NUMBER_TABLE(axis)[dec_speed_index];
  2020. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_speed_index];
  2021. } else if (cur_speed_index < dst_speed_index) { //如果当前速度是在期望速度以下,就需要有加速环节
  2022. //如果已经不能加速就退出
  2023. if (feed < AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index]) break;
  2024. feed -= AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index];
  2025. cur_speed_index++;
  2026. } else {
  2027. break;
  2028. }
  2029. } while (feed > 0);
  2030. //设置停止位置
  2031. axis->tasks[i].dec_position = posi2 - axis->feed * dec_position;
  2032. axis->tasks[i].dec_speed_index = dec_speed_index;
  2033. } else {
  2034. //当前段的减速其实就是下一段的匀速,这样才能保证进入下一段的时候速度能保证是期望的速度
  2035. dec_speed_index = axis->tasks[i + 1].dst_speed_index;
  2036. dec_position = 0;
  2037. if (dec_speed_index < dst_speed_index) {
  2038. //可以完全忽视减速部分,因此减速位置为0
  2039. dec_position += AXIS_ACC_NUMBER_TABLE(axis)[dec_speed_index];
  2040. }
  2041. do {
  2042. //加速区域判断
  2043. //当当前的加速时速度比当前的减速时速度相等或者大的时候,可以进行一次减速
  2044. //这是为了保证加减速度差在可以接受的范围内
  2045. //这样也就可以让加减时间更加的灵活
  2046. //为了保证效果,当下一阶段速比当前阶段高的时候,减速段不进行操作,因为本来就是以
  2047. //保证阶段速度不能高于指定速度的目的来做这个功能的
  2048. if (cur_speed_index > dst_speed_index) { //如果当前速度比期望速度大,需要先降速再打算,
  2049. // 然而这样的机会在前面运算正常的情况下是很少出现的
  2050. if (feed < AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index]) break;
  2051. feed -= AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index];
  2052. cur_speed_index--;
  2053. dec_speed_index = dst_speed_index;
  2054. } else if (cur_speed_index > dec_speed_index && dec_speed_index < dst_speed_index) {
  2055. //如果当前速度比减速大,需要减速过程
  2056. if (feed < AXIS_ACC_NUMBER_TABLE(axis)[dec_speed_index]) break;
  2057. dec_speed_index++;
  2058. feed -= AXIS_ACC_NUMBER_TABLE(axis)[dec_speed_index];
  2059. dec_position += AXIS_ACC_NUMBER_TABLE(axis)[dec_speed_index];
  2060. } else if (cur_speed_index < dst_speed_index) { //如果当前速度比期望速度小,需要进行加速
  2061. if (feed < AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index]) break;
  2062. feed -= AXIS_ACC_NUMBER_TABLE(axis)[cur_speed_index];
  2063. cur_speed_index++;
  2064. } else {
  2065. break;
  2066. }
  2067. } while (feed > 0);
  2068. if (dec_speed_index > dst_speed_index) {
  2069. //可以完全忽视减速部分,因此减速位置为0
  2070. dec_speed_index = cur_speed_index;
  2071. }
  2072. //设置停止位置
  2073. axis->tasks[i].dec_position = posi2 - axis->feed * dec_position;
  2074. axis->tasks[i].dec_speed_index = dec_speed_index;
  2075. //修正下次计算的开始速度,如果下次的速度比这次小,减速后也应该是进入的速度
  2076. if (dst_speed_index > axis->tasks[i + 1].dst_speed_index) {
  2077. cur_speed_index = axis->tasks[i + 1].dst_speed_index;
  2078. }
  2079. }
  2080. }
  2081. axis->cur_task_index = 0;
  2082. axis->run_mode = AXIS_MODE_PP_TASKS;
  2083. axis_start(axis);
  2084. }
  2085. #endif //#ifdef AXIS_TASK_NUMBER
  2086. /**
  2087. * 减速停止
  2088. *
  2089. * @author LXZ (021720)
  2090. *
  2091. * @param axis
  2092. */
  2093. void axis_stop(axis_object_t *axis) {
  2094. if (axis->run_mode != AXIS_MODE_STOP) {
  2095. if (axis->cur_accdec_status != AXIS_ACCDEC_STATUS_DEC) {
  2096. int cur_acc_index = axis->cur_speed_index;
  2097. int star_dec_index = 0;
  2098. //查找开始减速的点
  2099. unsigned short number = AXIS_ACC_NUMBER_TABLE(axis)[cur_acc_index];
  2100. do {
  2101. //当当前开始减速的点比当前开始加速的点速度大或者等于的时候,说明已经找到点了
  2102. if (AXIS_DEC_NUMBER_TABLE(axis)[star_dec_index] >= number) break;
  2103. star_dec_index++;
  2104. } while (star_dec_index < (axis->dec_table_size - 1));
  2105. //减速点开始位置必须速度比当前速度低,因此需要减一,但是如果是最高速,却是不需要的
  2106. if (star_dec_index > 0 && star_dec_index != ((axis->dec_table_size - 1))) star_dec_index--;
  2107. axis->dec_speed_index = star_dec_index;
  2108. //让中断切换到减速停止模式
  2109. axis->run_mode = AXIS_MODE_STOP;
  2110. }
  2111. else
  2112. {
  2113. //到位不停模式下,直接停
  2114. if(axis->PP_Stop_Select != 0)
  2115. axis->run_mode = AXIS_MODE_EMGSTOP;
  2116. }
  2117. }
  2118. }
  2119. /**
  2120. * 非停止的运行模式下保持当前方向运行到指定位置停止
  2121. * 支持PV=>PP与PP=>PP
  2122. * 当位置不够减速停止时会执行减速停止,所以需要保证有足够位置提前调用
  2123. *
  2124. * @author LXZ (021720)
  2125. *
  2126. * @param axis 轴对象
  2127. * @param position
  2128. */
  2129. void axis_stop_at(axis_object_t *axis, int position) {
  2130. int cur_position = axis->position;
  2131. int acc_index = 0;
  2132. int dec_index = 0;
  2133. //开始减速的位置
  2134. int dec_position = 0;
  2135. //停止状态下调用该函数无效
  2136. if (AXIS_IS_RUNNING(axis) == 0) return;
  2137. //如果要停的位置已经超过了,直接减速停止
  2138. if (axis->feed > 0) {
  2139. if (cur_position > position) {
  2140. axis_stop(axis);
  2141. return;
  2142. }
  2143. } else if (cur_position <= position) {
  2144. axis_stop(axis);
  2145. return;
  2146. }
  2147. //设置要停止的目标位置
  2148. axis->target_position = position;
  2149. //计算要
  2150. position -= axis->position;
  2151. //计算减速位置与开始减速的减速表位置
  2152. if (axis->cur_accdec_status == 2) { //正在减速状态下,先查找相匹配的加速点
  2153. acc_index = 0;
  2154. dec_index = axis->cur_speed_index;
  2155. while (AXIS_ACC_REG_TABLE(axis)[acc_index] < AXIS_DEC_REG_TABLE(axis)[dec_index]) {
  2156. acc_index++;
  2157. }
  2158. } else { //直接获取当前加速点
  2159. acc_index = axis->cur_speed_index;
  2160. }
  2161. dec_index = 0;
  2162. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  2163. do {
  2164. if (AXIS_ACC_REG_TABLE(axis)[acc_index] > AXIS_DEC_REG_TABLE(axis)[dec_index] &&
  2165. dec_index < (axis->dec_table_size - 1)) { //计算指定速度下减速表开始位置
  2166. if (position < AXIS_DEC_NUMBER_TABLE(axis)[dec_index]) //剩余脉冲数不足
  2167. break;
  2168. //计算剩余脉冲与减速点
  2169. dec_index++;
  2170. position -= AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  2171. dec_position += AXIS_DEC_NUMBER_TABLE(axis)[dec_index];
  2172. } else if (acc_index < (axis->acc_table_size - 1)) { //允许继续加速
  2173. if (position < AXIS_ACC_NUMBER_TABLE(axis)[acc_index]) //剩余脉冲不足
  2174. break;
  2175. //计算剩余脉冲
  2176. position -= AXIS_ACC_NUMBER_TABLE(axis)[acc_index];
  2177. acc_index++;
  2178. } else {
  2179. break;
  2180. }
  2181. } while (position > 0);
  2182. //开始减速的减速表位置
  2183. axis->dec_speed_index = dec_index;
  2184. //计算减速位置
  2185. axis->dec_position = axis->target_position - dec_position * axis->feed;
  2186. if (axis->cur_accdec_status == 2) {
  2187. if (dec_index < axis->cur_speed_index) {
  2188. //由于当前要求的减速开始速度比当前速度要小,需要触发速度跳变
  2189. axis->cur_accdec_status = 1;
  2190. }
  2191. }
  2192. //切换运行模式
  2193. axis->run_mode = AXIS_MODE_PP;
  2194. }
  2195. /**
  2196. * PV模式允许在设定的目标速度范围内进行变速
  2197. * 该接口只有在PV模式下才有用
  2198. *
  2199. * @author LXZ (021820)
  2200. *
  2201. * @param axis 轴对象
  2202. * @param speed 要变化的速度
  2203. */
  2204. void axis_pv_change_speed(axis_object_t *axis, int speed) {
  2205. unsigned short reg;
  2206. unsigned short index = 0;
  2207. speed *= axis->speed_unit;
  2208. if (speed < axis->start_speed) {
  2209. speed = axis->start_speed;
  2210. }
  2211. reg = axis->clock / speed;
  2212. while (index < axis->acc_table_size) {
  2213. if (reg <= AXIS_ACC_REG_TABLE(axis)[index]) {
  2214. index++;
  2215. } else {
  2216. break;
  2217. }
  2218. }
  2219. axis->acc_speed_index = index;
  2220. axis->target_speed_reg = reg;
  2221. }
  2222. /**
  2223. * 减速停止所有轴的动作,包括停止回零的操作
  2224. *
  2225. * @author lxz (2019/6/16/周日)
  2226. *
  2227. * @param axis
  2228. */
  2229. void axis_stop_all(axis_object_t *axis) {
  2230. axis_stop(axis);
  2231. axis->home_step = 0;
  2232. }
  2233. /**
  2234. * 以急停的方式去停止
  2235. *
  2236. * @author lxz
  2237. *
  2238. * @param axis
  2239. */
  2240. void axis_emgstop(axis_object_t *axis) {
  2241. axis->home_step = 0;
  2242. axis->run_mode = AXIS_MODE_EMGSTOP;
  2243. axis->outputs.on = 0;
  2244. AXIS_OFF(axis);
  2245. }
  2246. /**
  2247. * 轴启动
  2248. *
  2249. * @author lxz
  2250. *
  2251. * @param axis
  2252. */
  2253. void axis_start(axis_object_t *axis) {
  2254. axis->cur_speed_index = 0;
  2255. axis->accdec_count = AXIS_ACC_NUMBER_TABLE(axis)[axis->cur_speed_index];
  2256. axis->cur_accdec_status = 0;
  2257. axis->cur_reg_value = AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index];
  2258. AXIS_SET_PERION(axis, AXIS_ACC_REG_TABLE(axis)[axis->cur_speed_index]);
  2259. AXIS_ON(axis);
  2260. axis->outputs.on = AXIS_IS_ON(axis) != 0;
  2261. }
  2262. /**
  2263. * 设置轴的参数
  2264. *
  2265. * @author LXZ (031420)
  2266. *
  2267. * @param axis 轴对象
  2268. * @param cmd 设置的对象
  2269. * @param argv 设置的参数
  2270. *
  2271. */
  2272. void axis_set_parameter(axis_object_t *axis, axis_paramter_t cmd, int argv) {
  2273. switch (cmd) {
  2274. case AXIS_AXIS_NO:
  2275. axis->axis_no = argv;
  2276. break;
  2277. case AXIS_POSITION:
  2278. axis->position = argv;
  2279. break;
  2280. case AXIS_CLOCK:
  2281. axis->clock = argv;
  2282. break;
  2283. case AXIS_PERIOD:
  2284. axis->period = argv;
  2285. break;
  2286. case AXIS_MIN_STEP:
  2287. axis->min_steps = argv;
  2288. break;
  2289. case AXIS_SPEED_UNIT:
  2290. axis->speed_unit = argv;
  2291. break;
  2292. case AXIS_ACCDEC_MODE:
  2293. axis->accdec_mode = argv;
  2294. break;
  2295. case AXIS_ACC_TIME:
  2296. axis->acc_time = argv;
  2297. break;
  2298. case AXIS_DEC_TIME:
  2299. axis->dec_time = argv;
  2300. break;
  2301. case AXIS_AAC_LMT:
  2302. axis->max_aac = argv;
  2303. break;
  2304. case AXIS_ACC_LMT:
  2305. axis->max_acc = argv;
  2306. break;
  2307. case AXIS_START_SPEED:
  2308. axis->start_speed = argv;
  2309. break;
  2310. case AXIS_HOME_SPACE:
  2311. axis->home_space = argv;
  2312. break;
  2313. case AXIS_HOME_OFFSET:
  2314. axis->home_offset = argv;
  2315. break;
  2316. case AXIS_HOME_FIND_SPEED:
  2317. axis->home_find_speed = argv;
  2318. break;
  2319. case AXIS_HOME_HIGH_SPEED:
  2320. axis->home_high_speed = argv;
  2321. break;
  2322. case AXIS_HOME_LOW_SPEED:
  2323. axis->home_low_speed = argv;
  2324. break;
  2325. case AXIS_PP_STOP_SELECT:
  2326. axis->PP_Stop_Select = argv;
  2327. break;
  2328. case AXIS_DIR_REVERSE:
  2329. axis->inputs.dir_reverse = argv;
  2330. break;
  2331. case AXIS_EN_REVERSE:
  2332. axis->inputs.en_reverse = argv;
  2333. break;
  2334. case AXIS_ALARM_REVERSE:
  2335. axis->inputs.alarm_reverse = argv;
  2336. break;
  2337. case AXIS_USE_ENCODE:
  2338. axis->use_encode = argv;
  2339. break;
  2340. }
  2341. }
  2342. /**
  2343. * 设置轴的参数
  2344. *
  2345. * @author LXZ (031420)
  2346. *
  2347. * @param axis 轴对象
  2348. * @param cmd 设置的对象
  2349. * @param argv 设置的参数
  2350. *
  2351. */
  2352. int axis_get_parameter(axis_object_t *axis, axis_paramter_t cmd) {
  2353. int argv = 0;
  2354. switch (cmd) {
  2355. case AXIS_AXIS_NO:
  2356. argv = axis->axis_no;
  2357. break;
  2358. case AXIS_POSITION:
  2359. argv = axis->position;
  2360. break;
  2361. case AXIS_CLOCK:
  2362. argv = axis->clock;
  2363. break;
  2364. case AXIS_PERIOD:
  2365. argv = axis->period;
  2366. break;
  2367. case AXIS_MIN_STEP:
  2368. argv = axis->min_steps;
  2369. break;
  2370. case AXIS_SPEED_UNIT:
  2371. argv = axis->speed_unit;
  2372. break;
  2373. case AXIS_ACCDEC_MODE:
  2374. argv = axis->accdec_mode;
  2375. break;
  2376. case AXIS_ACC_TIME:
  2377. argv = axis->acc_time;
  2378. break;
  2379. case AXIS_DEC_TIME:
  2380. argv = axis->dec_time;
  2381. break;
  2382. case AXIS_AAC_LMT:
  2383. argv = axis->max_aac;
  2384. break;
  2385. case AXIS_ACC_LMT:
  2386. argv = axis->max_acc;
  2387. break;
  2388. case AXIS_START_SPEED:
  2389. argv = axis->start_speed;
  2390. break;
  2391. case AXIS_HOME_SPACE:
  2392. argv = axis->home_space;
  2393. break;
  2394. case AXIS_HOME_OFFSET:
  2395. argv = axis->home_offset;
  2396. break;
  2397. case AXIS_HOME_FIND_SPEED:
  2398. argv = axis->home_find_speed;
  2399. break;
  2400. case AXIS_HOME_HIGH_SPEED:
  2401. argv = axis->home_high_speed;
  2402. break;
  2403. case AXIS_HOME_LOW_SPEED:
  2404. argv = axis->home_low_speed;
  2405. break;
  2406. }
  2407. return argv;
  2408. }
  2409. /**
  2410. * 回零方式1
  2411. *
  2412. * @author lxz (2019/6/10/周一)
  2413. *
  2414. * @param axis
  2415. */
  2416. void axis_home_method1(axis_object_t *axis) {
  2417. switch (axis->home_step) {
  2418. case 1:
  2419. //负限位或者零点有信号 ,需要向前运动
  2420. if (axis->inputs.hw_home_sgn) {
  2421. //先是离开一定的距离
  2422. axis->home_step = 4;
  2423. } else {
  2424. if (axis->inputs.hw_negative_lmt) {
  2425. axis_pv(axis, 0, axis->home_low_speed);
  2426. axis->home_step = 3;
  2427. } else if (axis->inputs.hw_slow_lmt == 0 && axis->inputs.home_high_speed_enable) {
  2428. //回零减速信号位有信号,速度要采用低速回零
  2429. axis_pv(axis, 1, axis->home_high_speed);
  2430. axis->home_step = 2;
  2431. } else {
  2432. //以回零高速查找零点信号
  2433. axis_pv(axis, 0, axis->home_low_speed);
  2434. axis->home_step = 3;
  2435. }
  2436. }
  2437. break;
  2438. case 2:
  2439. if (axis->inputs.hw_slow_lmt) {
  2440. axis_pv_change_speed(axis, axis->home_low_speed);
  2441. axis->home_step++;
  2442. }
  2443. break;
  2444. case 3:
  2445. if (axis->inputs.hw_home_sgn) {
  2446. //查到了零点, 先减速停止
  2447. axis_stop(axis);
  2448. axis->home_step++;
  2449. }
  2450. break;
  2451. case 4:
  2452. if (axis->outputs.on == 0) {
  2453. //离开原点一定距离
  2454. axis_pp(axis, 0, axis->home_space, axis->home_low_speed,0);
  2455. axis->home_step++;
  2456. }
  2457. break;
  2458. case 5:
  2459. if (axis->outputs.on == 0) {
  2460. if (axis->inputs.hw_home_sgn) {
  2461. axis->home_step = 4;
  2462. } else {
  2463. //以低速回退来查找原点
  2464. axis_pv(axis, 0, axis->home_find_speed);
  2465. axis->home_step++;
  2466. }
  2467. }
  2468. break;
  2469. case 6:
  2470. if (axis->inputs.hw_home_sgn) {
  2471. AXIS_OFF(axis);
  2472. axis->home_step++;
  2473. }
  2474. break;
  2475. case 7:
  2476. if (axis->outputs.on == 0) {
  2477. //查到零点,停止
  2478. axis->home_step++;
  2479. }
  2480. break;
  2481. case 8:
  2482. if (axis->outputs.on == 0) {
  2483. //再走一段设置距离
  2484. axis_pp(axis, 0, axis->home_offset, axis->home_low_speed,0);
  2485. axis->home_step++;
  2486. }
  2487. break;
  2488. case 9:
  2489. if (axis->outputs.on == 0) {
  2490. //回零完毕
  2491. axis->home_step = 0;
  2492. axis->position = 0;
  2493. axis->encoder = 0;
  2494. }
  2495. break;
  2496. }
  2497. }
  2498. /**
  2499. * 回零方式1
  2500. *
  2501. * @author lxz (2019/6/10/周一)
  2502. *
  2503. * @param axis
  2504. */
  2505. void axis_home_method0(axis_object_t *axis) {
  2506. switch (axis->home_step) {
  2507. case 1:
  2508. //负限位或者零点有信号 ,需要向前运动
  2509. if (axis->inputs.hw_home_sgn) {
  2510. //先是离开一定的距离
  2511. axis->home_step = 4;
  2512. } else {
  2513. if (axis->inputs.hw_negative_lmt) {
  2514. axis_pv(axis, 1, axis->home_low_speed);
  2515. axis->home_step = 3;
  2516. } else if (axis->inputs.hw_slow_lmt == 0 && axis->inputs.home_high_speed_enable) {
  2517. //回零减速信号位有信号,速度要采用低速回零
  2518. axis_pv(axis, 0, axis->home_high_speed);
  2519. axis->home_step = 2;
  2520. } else {
  2521. //以回零高速查找零点信号
  2522. axis_pv(axis, 0, axis->home_low_speed);
  2523. axis->home_step = 3;
  2524. }
  2525. }
  2526. break;
  2527. case 2:
  2528. if (axis->inputs.hw_slow_lmt) {
  2529. axis_pv_change_speed(axis, axis->home_low_speed);
  2530. axis->home_step++;
  2531. }
  2532. break;
  2533. case 3:
  2534. if (axis->inputs.hw_home_sgn) {
  2535. //查到了零点, 先减速停止
  2536. axis_stop(axis);
  2537. axis->home_step++;
  2538. }
  2539. break;
  2540. case 4:
  2541. if (axis->outputs.on == 0) {
  2542. //离开原点一定距离
  2543. axis_pp(axis, 0, axis->home_space, axis->home_low_speed,0);
  2544. axis->home_step++;
  2545. }
  2546. break;
  2547. case 5:
  2548. if (axis->outputs.on == 0) {
  2549. if (axis->inputs.hw_home_sgn) {
  2550. axis->home_step = 4;
  2551. } else {
  2552. //以低速回退来查找原点
  2553. axis_pv(axis, 0, axis->home_find_speed);
  2554. axis->home_step++;
  2555. }
  2556. }
  2557. break;
  2558. case 6:
  2559. if (axis->inputs.hw_home_sgn) {
  2560. AXIS_OFF(axis);
  2561. axis->home_step++;
  2562. }
  2563. break;
  2564. case 7:
  2565. if (axis->outputs.on == 0) {
  2566. //查到零点,停止
  2567. axis->home_step++;
  2568. }
  2569. break;
  2570. case 8:
  2571. if (axis->outputs.on == 0) {
  2572. //再走一段设置距离
  2573. axis_pp(axis, 0, axis->home_offset, axis->home_low_speed,0);
  2574. axis->home_step++;
  2575. }
  2576. break;
  2577. case 9:
  2578. if (axis->outputs.on == 0 && axis->inputs.fin_sgn == 0) {
  2579. //回零完毕
  2580. axis->home_step = 0;
  2581. axis->position = 0;
  2582. axis->encoder = 0;
  2583. }
  2584. break;
  2585. }
  2586. }
  2587. /**
  2588. * 回零方式2
  2589. *
  2590. * @author LXZ (010820)
  2591. *
  2592. * @param axis
  2593. */
  2594. void axis_home_method2(axis_object_t *axis) {
  2595. switch (axis->home_step) {
  2596. case 1:
  2597. //不在零点时,不需要重复检测
  2598. if (!axis->inputs.hw_home_sgn) {
  2599. //先是离开一定的距离
  2600. axis->home_step++;
  2601. }
  2602. axis->home_step++;
  2603. axis_pv(axis, 1, axis->home_low_speed);
  2604. break;
  2605. case 2:
  2606. if (!axis->inputs.hw_home_sgn) {
  2607. axis->home_step++;
  2608. }
  2609. break;
  2610. case 3:
  2611. if (axis->inputs.hw_home_sgn) {
  2612. axis_stop(axis);
  2613. axis->home_step++;
  2614. }
  2615. break;
  2616. case 4:
  2617. if (axis->outputs.on == 0) {
  2618. //离开原点一定距离
  2619. axis_pp(axis, 0, axis->home_space, axis->home_low_speed,0);
  2620. axis->home_step++;
  2621. }
  2622. break;
  2623. case 5:
  2624. if (axis->outputs.on == 0) {
  2625. axis->home_step = 0;
  2626. axis->position = 0;
  2627. axis->encoder = 0;
  2628. }
  2629. }
  2630. }
  2631. /**
  2632. * 开始回零动作
  2633. *
  2634. * @author LXZ (031420)
  2635. *
  2636. * @param axis 轴对象
  2637. * @param method 回零方式
  2638. * @param home_speed 回零速度
  2639. * @param find_speed 查找零点速度
  2640. */
  2641. void axis_home(axis_object_t *axis, int method, int home_speed, int find_speed) {
  2642. axis->home_method = method;
  2643. axis->home_step = 1;
  2644. axis->home_find_speed = find_speed;
  2645. axis->home_low_speed = home_speed;
  2646. }
  2647. /**
  2648. * 使能高速回原点的功能
  2649. * 当回零系统中存在减速开关的时候,使能高速功能允许执行二段速回零,当没有减速信号时采用
  2650. * 高速动作,检测到开关后会变为低速返回零点从而更加有效的加零。
  2651. *
  2652. * @author LXZ (031420)
  2653. *
  2654. * @param axis
  2655. * @param high_speed
  2656. */
  2657. void axis_home_enable_high_speed(axis_object_t *axis, int high_speed) {
  2658. axis->inputs.home_high_speed_enable = 1;
  2659. axis->home_high_speed = high_speed;
  2660. }
  2661. /**
  2662. * 禁止高速回原点的功能
  2663. *
  2664. * @author LXZ (031420)
  2665. *
  2666. * @param axis
  2667. */
  2668. void axis_home_disable_high_speed(axis_object_t *axis) {
  2669. axis->inputs.home_high_speed_enable = 0;
  2670. }
  2671. /**
  2672. * 执行回零动作
  2673. *
  2674. * @author lxz (2019/6/10/周一)
  2675. *
  2676. * @param axis
  2677. */
  2678. void axis_home_run(axis_object_t *axis) {
  2679. switch (axis->home_method) {
  2680. case 2:
  2681. axis_home_method2(axis);
  2682. break;
  2683. case 1:
  2684. axis_home_method1(axis);
  2685. break;
  2686. default:
  2687. axis_home_method0(axis);
  2688. break;
  2689. }
  2690. }
  2691. /**
  2692. * 对轴的一些输入输出信号进行集合处理,包括限位保护等
  2693. *
  2694. * @author lxz
  2695. *
  2696. * @param axis
  2697. */
  2698. void axis_run(axis_object_t *axis) {
  2699. axis->outputs.error_alarm = axis->inputs.alarm_sgn;
  2700. axis_home_run(axis);
  2701. if (axis->outputs.error_alarm) {
  2702. axis_emgstop(axis);
  2703. }
  2704. //软件限位保护
  2705. if (axis->inputs.sw_lmt_enable && axis->outputs.on) {
  2706. //正软件限位保护
  2707. if (axis->outputs.dir && axis->position >= axis->sw_positive_limit) {
  2708. axis_emgstop(axis);
  2709. axis->outputs.sw_positive_alarm = 1;
  2710. } else {
  2711. axis->outputs.sw_positive_alarm = 0;
  2712. }
  2713. //负软件限位保护
  2714. if (axis->outputs.dir == 0 && axis->position <= axis->sw_negative_limit) {
  2715. axis_emgstop(axis);
  2716. axis->outputs.sw_negative_alarm = 1;
  2717. } else {
  2718. axis->outputs.sw_negative_alarm = 0;
  2719. }
  2720. }
  2721. //正硬件限位保护
  2722. if (axis->outputs.dir && axis->inputs.hw_positive_lmt) {
  2723. axis_emgstop(axis);
  2724. axis->outputs.hw_positive_alarm = 1;
  2725. } else {
  2726. axis->outputs.hw_positive_alarm = 0;
  2727. }
  2728. //负硬件限位保护
  2729. if (axis->outputs.dir == 0 && axis->inputs.hw_negative_lmt) {
  2730. axis_emgstop(axis);
  2731. axis->outputs.hw_negative_alarm = 1;
  2732. } else {
  2733. axis->outputs.hw_negative_alarm = 0;
  2734. }
  2735. /*
  2736. if (axis->outputs.enable) {
  2737. AXIS_SET_EN(axis, 1);
  2738. }
  2739. else
  2740. {
  2741. AXIS_SET_EN(axis, 0);
  2742. }
  2743. */
  2744. //当回零步不为0的时候,说明正在回零,这时候一些操作就要注意不要冲突
  2745. axis->outputs.homing = axis->home_step != 0;
  2746. axis->outputs.on = AXIS_IS_ON(axis) != 0;
  2747. }
  2748. /**
  2749. * 操作伺服对应IO
  2750. *
  2751. * @author xuzhenglim:276137500 (2019-12-5)
  2752. *
  2753. * @param axis
  2754. * @param type
  2755. * @param value
  2756. */
  2757. void axis_set_ouput(axis_object_t *axis, axis_output_name type, int value) {
  2758. switch (type) {
  2759. case AXIS_OUT_ENABLE:
  2760. axis->outputs.enable = value;
  2761. break;
  2762. case AXIS_OUT_ERRCLEAR:
  2763. axis->outputs.error_clear = value;
  2764. break;
  2765. }
  2766. }
  2767. /**
  2768. * 获取报警值
  2769. *
  2770. * @author LXZ (121919)
  2771. *
  2772. * @param axis 轴对象
  2773. *
  2774. * @return int 0 没有报警
  2775. * 1 伺服报警
  2776. * 2 软件正限位
  2777. * 3 软件负限位
  2778. * 4 硬件正限位
  2779. * 5 硬件负限位
  2780. */
  2781. axis_alarm_code_t axis_alarm_code(axis_object_t *axis) {
  2782. if (axis->outputs.error_alarm) {
  2783. return AXIS_ALARM_ERROR;
  2784. } else if (axis->outputs.sw_positive_alarm) {
  2785. return AXIS_ALARM_SW_POSITIVE;
  2786. } else if (axis->outputs.sw_negative_alarm) {
  2787. return AXIS_ALARM_SW_NEGATIVE;
  2788. } else if (axis->outputs.hw_positive_alarm) {
  2789. return AXIS_ALARM_HW_POSITIVE;
  2790. } else if (axis->outputs.hw_negative_alarm) {
  2791. return AXIS_ALARM_HW_NEGATIVE;
  2792. } else return AXIS_ALARM_NONE;
  2793. }
  2794. /**
  2795. * 判断是否能正转
  2796. *
  2797. * @author LXZ (010120)
  2798. *
  2799. * @param axis
  2800. *
  2801. * @return int
  2802. */
  2803. int axis_cw_lmt(axis_object_t *axis) {
  2804. return (axis->outputs.on == 0 && axis->outputs.error_alarm == 0 &&
  2805. axis->outputs.hw_positive_alarm == 0 &&
  2806. axis->outputs.sw_positive_alarm == 0);
  2807. }
  2808. /**
  2809. * 判断是否能反转
  2810. *
  2811. * @author LXZ (010120)
  2812. *
  2813. * @param axis
  2814. *
  2815. * @return int
  2816. */
  2817. int axis_ccw_lmt(axis_object_t *axis) {
  2818. return (axis->outputs.on == 0 && axis->outputs.error_alarm == 0 &&
  2819. axis->outputs.hw_negative_alarm == 0 &&
  2820. axis->outputs.sw_negative_alarm == 0);
  2821. }
  2822. /**
  2823. * 使能软件限位
  2824. *
  2825. * @author LXZ (031420)
  2826. *
  2827. * @param axis 轴对象
  2828. * @param positive 软件正限位
  2829. * @param negative 软件负限位
  2830. */
  2831. void axis_enable_software_lmt(axis_object_t *axis, int positive, int negative) {
  2832. axis->inputs.sw_lmt_enable = 1;
  2833. axis->sw_positive_limit = positive;
  2834. axis->sw_negative_limit = negative;
  2835. }
  2836. /**
  2837. * 关闭软件限位
  2838. *
  2839. * @author LXZ (031420)
  2840. *
  2841. * @param axis 轴对象
  2842. * @param positive 软件正限位
  2843. * @param negative 软件负限位
  2844. */
  2845. void axis_disable_software_lmt(axis_object_t *axis) {
  2846. axis->inputs.sw_lmt_enable = 0;
  2847. }
  2848. /**
  2849. * 设置加减速模式,包括时间
  2850. *
  2851. * @author LXZ (031420)
  2852. *
  2853. * @param axis 轴对象
  2854. * @param mode
  2855. * 加减模式,0是直线加减速,1是S型加减速
  2856. * @param acc_time 加速度时间
  2857. * @param dec_time 减速度时间
  2858. */
  2859. void axis_set_accdec(axis_object_t *axis, int mode, int acc_time, int dec_time) {
  2860. axis->accdec_mode = mode;
  2861. axis->acc_time = acc_time;
  2862. axis->dec_time = dec_time;
  2863. }
  2864. /**
  2865. * 使能调中回调函数
  2866. *
  2867. * @author LXZ (031420)
  2868. *
  2869. * @param axis 轴对象
  2870. * @param handle 回调函数入口
  2871. */
  2872. void axis_enable_irq_callback(axis_object_t *axis, void (*handle)()) {
  2873. axis->handle = handle;
  2874. }
  2875. /**
  2876. * 禁止中断回调
  2877. *
  2878. * @author LXZ (031420)
  2879. *
  2880. * @param axis
  2881. */
  2882. void axis_disable_irq_callback(axis_object_t *axis) {
  2883. axis->handle = (void *)0;
  2884. }