dcmotor.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*********************************************************************
  2. *
  3. * DC motor library for Fraise pic18f device
  4. *
  5. *
  6. *********************************************************************
  7. * Author Date Comment
  8. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. * Antoine Rousseau 2014 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 "dcmotor.h"
  27. static long incDeltaT, lastIncTime, deltaT, actualSpeed, error;
  28. static int pos,deltaPos;
  29. int dcmotor_v,dcmotor_vabs;
  30. t_dcmotorVars dcmotorVars;
  31. t_dcmotorVolVars dcmotorVolVars;
  32. static t_dcmotorSetting S;
  33. void dcmotorCompute(t_dcmotor *mot)
  34. {
  35. #define MAXSPEED ((unsigned long)(1UL<<24))
  36. //done by macro
  37. /*__critical {
  38. //memcpy(&dcmotorVolVars,&(mot->VolVars),sizeof(dcmotorVolVars));
  39. dcmotorVolVars.lastIncTime = mot->VolVars.lastIncTime;
  40. dcmotorVolVars.IncDeltaT = mot->VolVars.IncDeltaT;
  41. dcmotorVolVars.Position = mot->VolVars.Position;
  42. dcmotorVolVars.flags = mot->VolVars.flags;
  43. mot->VolVars.IncDeltaT = 0;
  44. }*/
  45. memcpy(&dcmotorVars,&(mot->Vars),sizeof(dcmotorVars));
  46. memcpy(&S,&(mot->Setting),sizeof(S));
  47. if((S.Mode == 1) || (S.Mode == 3)) {
  48. deltaPos = dcmotorVolVars.Position - dcmotorVars.lastPosition;
  49. dcmotorVars.lastPosition = dcmotorVolVars.Position;
  50. if(!deltaPos) {
  51. deltaT = elapsed(dcmotorVolVars.lastIncTime);
  52. if(!dcmotorVars.stalled) {
  53. if((deltaT>>18) > S.StallTime) dcmotorVars.stalled = 1; // more than (8<<18)/2 us = 1 sec -> speed=0
  54. }
  55. if(!dcmotorVars.stalled) {
  56. if(deltaT < dcmotorVars.lastIncDeltaT) deltaT = dcmotorVars.lastIncDeltaT; //
  57. deltaPos = dcmotorVolVars.direction ? 1 : -1; //make like a new step was occuring now, at the same rate the previous one.
  58. }
  59. }
  60. else {
  61. dcmotorVars.stalled = 0;
  62. deltaT = dcmotorVolVars.IncDeltaT;
  63. dcmotorVars.lastIncDeltaT = deltaT;
  64. }
  65. if(deltaT <= 0) actualSpeed = 0;
  66. else actualSpeed = deltaPos * (MAXSPEED/((unsigned long)(deltaT)));
  67. }
  68. if((S.Mode >= 2) && (dcmotorVolVars.homed)) {
  69. rampCompute(&mot->PosRamp);
  70. error = (long)(mot->PosRamp.currentPos>>(RAMP_UINCPOW)) - (dcmotorVolVars.Position) ;
  71. if((error < 0) && (error >= -S.PosWindow)) error = 0;
  72. if((error > 0) && (error <= S.PosWindow)) error = 0;
  73. error <<= S.PosErrorGain; //7;
  74. if(error >= (1L<<15)) error = (1L<<15)-1;
  75. else if(error < -(1L<<15)) error = -(1L<<15);
  76. pidCompute(&mot->PosPID,(int)error);
  77. if(S.Mode == 3) dcmotorVars.SpeedConsign = mot->PosPID.Out >> 8;
  78. else dcmotorVars.PWMConsign = mot->PosPID.Out >> 8;
  79. }
  80. if((S.Mode == 1) || (S.Mode == 3)) {
  81. error = (long)(dcmotorVars.SpeedConsign) - (long)actualSpeed;
  82. if(error >= (1L<<15)) error = (1L<<15)-1;
  83. else if(error < -(1L<<15)) error = -(1L<<15);
  84. pidCompute(&mot->SpeedPID, (int)error);
  85. dcmotorVars.PWMConsign = mot->SpeedPID.Out>>8;
  86. }
  87. memcpy(&(mot->Vars),&dcmotorVars,sizeof(dcmotorVars));
  88. }
  89. void dcmotorInput(t_dcmotor *mot)
  90. {
  91. unsigned char c,c2;
  92. unsigned int i = 0;
  93. c=fraiseGetChar();
  94. if(c == 254) {
  95. fraiseSendCopy();
  96. c2=fraiseGetChar();
  97. switch(c2) {
  98. GETPARAM(3, mot->Vars.SpeedConsign, i);
  99. GETPARAM(4, mot->Vars.PWMConsign, i);
  100. GETPARAM(5, (char)mot->Setting.reversed, i);
  101. }
  102. printf("%d %d\n", c2, i);
  103. }
  104. else switch(c) {
  105. case 0 : rampInput(&mot->PosRamp); mot->Setting.Mode = 2; break;
  106. case 1 : pidInput(&mot->PosPID); break;
  107. case 2 : pidInput(&mot->SpeedPID); break;
  108. PARAM_INT(3, mot->Vars.SpeedConsign); mot->Setting.Mode = 1; break;
  109. PARAM_INT(4, mot->Vars.PWMConsign); mot->Setting.Mode = 0; break;
  110. PARAM_CHAR(5, mot->Setting.reversed); break;
  111. PARAM_CHAR(6, c2); mot->VolVars.homed = (c2!=0); if(c2==1) {
  112. mot->VolVars.Position = 0;
  113. rampSetPos(&mot->PosRamp,0);
  114. }
  115. break;
  116. }
  117. }
  118. void dcmotorDeclareEE(t_dcmotor *mot)
  119. {
  120. EEdeclareChar(&mot->Setting.flags);
  121. rampDeclareEE(&mot->PosRamp);
  122. pidDeclareEE(&mot->PosPID);
  123. pidDeclareEE(&mot->SpeedPID);
  124. }