Advertisement
thorium90

StepperMotor.cpp

Sep 29th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. /*
  2. ##########################################################################
  3. ##                          ARDUINO SOURCES                             ##
  4. ##########################################################################
  5. */
  6. /*
  7.    ___|  _)  | _)       _)                         ___|                    
  8.  \___ \   |  |  |   __|  |  |   |  __ `__ \       |       _ \    __|  __ \  
  9.        |  |  |  |  (     |  |   |  |   |   |      |      (   |  |     |   |
  10.  _____/  _| _| _| \___| _| \__,_| _|  _|  _|     \____| \___/  _|     .__/  
  11.                                                                      _|  
  12. Code source libre de droit. Si vous utilisez mon code, merci à vous d'en préciser la source.
  13.  
  14. @Date : 28/09/2011
  15. @Auteur : Silicium Corp
  16. */
  17. #include "StepperMotor.h"
  18.  
  19. //===============================================================
  20. StepperMotor::StepperMotor(int pps, int* pinArduino)
  21. {
  22.     for (int i = 0; i < 4; i++)
  23.     {
  24.         pinMotor[i] = pinArduino[i];
  25.     }
  26.     for (int j = 0; j < 4; j++)
  27.     {
  28.         pinMode(pinMotor[j], OUTPUT);
  29.     }
  30.     setMotorSpeed(pps);
  31.     init();
  32. }
  33. //===============================================================
  34. void StepperMotor::init()
  35. {
  36.     elapsedPulse = 0;
  37.     stopMarker = 0;
  38.     workInProgress = false;
  39.     isLaunch = false;
  40.     way = FORWARD;
  41.     nextPulse = B00001000;
  42.     timing.setCycleTime(coreSpeed);
  43.     timing.run();
  44. }
  45. //===============================================================
  46. void StepperMotor::pulse()
  47. {
  48.     if (workInProgress)
  49.     {
  50.         if (isLaunch)
  51.         {
  52.             if(timing.isOutOfDate())
  53.             {
  54.                 electrikChoc();
  55.                 elapsedPulse++;
  56.             }
  57.         }
  58.         else
  59.         {
  60.             if (elapsedPulse <= stopMarker)
  61.             {
  62.                 if(timing.isOutOfDate())
  63.                 {
  64.                     electrikChoc();
  65.                     elapsedPulse++;
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 workInProgress = false;
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. //===============================================================
  77. void StepperMotor::electrikChoc()
  78. {
  79.     for (int i = 0; i < 4; i++)
  80.     {
  81.         digitalWrite(pinMotor[i], (nextPulse>>(3 - i))&0x01);
  82.     }
  83.     updateNextPulse();
  84. }
  85. //===============================================================
  86. void StepperMotor::updateNextPulse()
  87. {
  88.     if (way == FORWARD)
  89.     {
  90.         nextPulse >>= 1;
  91.     }
  92.     else
  93.     {
  94.         nextPulse <<= 1;
  95.     }
  96.  
  97.     if (nextPulse == B00000000)
  98.     {
  99.         nextPulse = B00001000;
  100.     }
  101.     else if (nextPulse == B00010000)
  102.     {
  103.         nextPulse = B00000001;
  104.     }
  105. }
  106. //===============================================================
  107. void StepperMotor::launch()
  108. {
  109.     isLaunch = true;
  110.     workInProgress = true;
  111. }
  112. //===============================================================
  113. void StepperMotor::stopNow()
  114. {
  115.     workInProgress = false;
  116.     isLaunch = false;
  117. }
  118. //===============================================================
  119. void StepperMotor::setMotorSpeed(int pps)
  120. {
  121.     motorSpeed = pps;
  122.     coreSpeed = 1000 / pps;
  123.     timing.setCycleTime(coreSpeed);
  124. }
  125. //===============================================================
  126. void StepperMotor::setStepOrder(int nbStep)
  127. {
  128.     stopMarker = elapsedPulse + nbStep;
  129.     workInProgress = true;
  130.     isLaunch = false;
  131. }
  132. //===============================================================
  133. void StepperMotor::setWay(boolean sense)
  134. {
  135.     way = sense;
  136. }
  137. //===============================================================
  138. boolean StepperMotor::getWay()
  139. {
  140.     return(way);
  141. }
  142. //===============================================================
  143. boolean StepperMotor::getIsLauch()
  144. {
  145.     return(isLaunch);
  146. }
  147. //===============================================================
  148. boolean StepperMotor::getWorking()
  149. {
  150.     return(workInProgress);
  151. }
  152. //===============================================================
  153. int StepperMotor::getMotorSpeed()
  154. {
  155.     return(motorSpeed);
  156. }
  157. //===============================================================
  158. int StepperMotor::getElapsedPulse()
  159. {
  160.     return(elapsedPulse);
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement