ramp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*********************************************************************
  2. *
  3. * Ramp library for Fraise pic18f device
  4. *
  5. * Ramp generator with destination position, maxspeed/acceleration
  6. *********************************************************************
  7. * Author Date Comment
  8. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. * Antoine Rousseau dec 2012 Original.
  10. ********************************************************************/
  11. /*
  12. # This program is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU General Public License
  14. # as published by the Free Software Foundation; either version 2
  15. # of the License, or (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24. # MA 02110-1301, USA.
  25. */
  26. /*#include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>*/
  29. /*#include <core.h>
  30. #include <fraisedevice.h>
  31. #include <eeparams.h>*/
  32. //#include <config.h>
  33. #include "ramp.h"
  34. void rampInit(t_ramp *Ramp)
  35. {
  36. Ramp->destPos=0;
  37. Ramp->currentPos=0;
  38. Ramp->speed=0;
  39. }
  40. void rampSetPos(t_ramp *Ramp,int pos)
  41. {
  42. Ramp->destPos=pos;
  43. Ramp->currentPos=((long)pos)<<RAMP_UINCPOW;
  44. Ramp->speed=0;
  45. }
  46. void rampGoto(t_ramp *Ramp,int pos)
  47. {
  48. Ramp->destPos=pos;
  49. }
  50. void rampCompute(t_ramp *Ramp)
  51. {
  52. static t_ramp M;
  53. int ds,absspeed;
  54. long d;
  55. long int absspeed_l,maxspeed_l;
  56. //int intconsignpos;
  57. memcpy(&M,Ramp,sizeof(M));
  58. absspeed_l=M.speed;if(absspeed_l<0) absspeed_l=-absspeed_l;
  59. d=((long)M.destPos-(long)(M.currentPos>>RAMP_UINCPOW));
  60. if((d>-RAMP_MAXERROR)&&(d<RAMP_MAXERROR)&&((absspeed_l>>1)<=M.maxDecel)) { //target proximity
  61. M.currentPos=((long int)M.destPos)<<RAMP_UINCPOW;
  62. M.speed=0;
  63. }
  64. else {
  65. absspeed=absspeed_l>>RAMP_UINCPOW;
  66. ds=(((long)(absspeed>>1)*absspeed)/(M.maxDecel)); //stop distance = speed²/2d
  67. //ds=ds+ds>>1; //over evaluate ds...
  68. maxspeed_l=((long)M.maxSpeed)<<RAMP_UINCPOW;
  69. #if 1
  70. if(M.speed>=0) {
  71. if(d>ds) {
  72. if(M.speed<maxspeed_l) {
  73. M.speed+=M.maxAccel;
  74. if(M.maxDecel<M.maxAccel){
  75. //verify :
  76. absspeed=M.speed>>RAMP_UINCPOW;
  77. ds=(((long)(absspeed>>1)*absspeed)/(M.maxDecel)); //stop distance = speed²/2d
  78. //ds=ds+ds>>1;//over evaluate ds...
  79. if(d<ds) M.speed-=M.maxAccel-M.maxDecel;
  80. }
  81. if(M.speed>maxspeed_l) M.speed=maxspeed_l;
  82. }
  83. else if(M.speed>maxspeed_l) M.speed-=M.maxDecel;
  84. }
  85. else M.speed-=((long)M.maxDecel)+(long)M.maxAccel;//M.maxDecel<<1; //really don't know why i have to apply twice decel, to avoid overshoot...
  86. }
  87. else {
  88. ds=-ds;
  89. if(d<ds) {
  90. if(M.speed>-maxspeed_l) {
  91. M.speed-=M.maxAccel;
  92. if(M.maxDecel<M.maxAccel){
  93. //verify :
  94. absspeed=-(M.speed>>RAMP_UINCPOW);
  95. ds=-(((long)(absspeed>>1)*absspeed)/(M.maxDecel)); //stop distance = speed²/2d
  96. //ds=ds+ds>>1;//over evaluate ds...
  97. if(d>ds) M.speed+=M.maxAccel-M.maxDecel;
  98. }
  99. if(M.speed<-maxspeed_l) M.speed=-maxspeed_l;
  100. }
  101. else if(M.speed<-maxspeed_l) M.speed+=M.maxDecel;
  102. }
  103. else M.speed+=(long)M.maxDecel+(long)M.maxAccel;//M.maxDecel<<1; //really don't why i have to apply twice decel, to avoid overshoot...
  104. }
  105. #else
  106. if(M.speed<0) ds=-ds;
  107. if(d>ds) M.speed+=M.maxDecel;
  108. else M.speed-=M.maxDecel;
  109. /*if(M.speed<0) ds=-ds;
  110. if(d>ds) M.speed+=M.maxAccel;
  111. else M.speed-=M.maxAccel;*/
  112. if(M.speed>maxspeed_l) M.speed=maxspeed_l;
  113. else if((-M.speed)>maxspeed_l) M.speed=-maxspeed_l;
  114. #endif
  115. M.currentPos+=(M.speed>>RAMP_UINCPOW);
  116. }
  117. memcpy(Ramp,&M,sizeof(M));
  118. }
  119. void rampInput(t_ramp *Ramp)
  120. {
  121. unsigned char c,c2;
  122. int i=0;
  123. c=fraiseGetChar();
  124. if(c == 254) {
  125. fraiseSendCopy();
  126. c2=fraiseGetChar();
  127. switch(c2) {
  128. GETPARAM(0, Ramp->maxSpeed, i);
  129. GETPARAM(1, Ramp->maxAccel, i);
  130. GETPARAM(2, Ramp->maxDecel, i);
  131. GETPARAM(10, Ramp->destPos, i);
  132. }
  133. printf("%d %d\n", c2, i);
  134. } else switch(c) {
  135. PARAM_INT(0, Ramp->maxSpeed); break;
  136. PARAM_INT(1, Ramp->maxAccel); break;
  137. PARAM_INT(2, Ramp->maxDecel); break;
  138. PARAM_INT(10, i);rampGoto(Ramp,i);break;
  139. }
  140. }
  141. void rampDeclareEE(t_ramp *Ramp)
  142. {
  143. EEdeclareInt(&Ramp->maxSpeed);
  144. EEdeclareInt(&Ramp->maxAccel);
  145. EEdeclareInt(&Ramp->maxDecel);
  146. }