Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "config.h"
  3. #include "FocheggiatoreLib.h"
  4. #include <TMC2130Stepper.h>
  5.  
  6. /*
  7. * CONSTRUCTOR
  8. */
  9. Focheggiatore::Focheggiatore(int MOT_driverEnable, int MOT_dirPin, int MOT_stepPin, int MOT_selectPin, int MOT_switchFocPri, int MOT_switchFocSec, int MOT_switchHomeFoc, int MOT_MOSI, int MOT_MISO, int MOT_SCK )
  10. {
  11. this->driverEnable = MOT_driverEnable;
  12. this->dirPin = MOT_dirPin;
  13. this->stepPin = MOT_stepPin;
  14. this->selectPin = MOT_selectPin;
  15. this->switchFocPri = MOT_switchFocPri;
  16. this->switchFocSec = MOT_switchFocSec;
  17. this->switchHome = MOT_switchHomeFoc;
  18. this->mosi = MOT_MOSI;
  19. this->miso = MOT_MISO;
  20. this->sck = MOT_SCK;
  21. }
  22.  
  23.  
  24.  
  25.  
  26. /*
  27. * INIZIALIZZAZIONE
  28. */
  29. void Focheggiatore::begin()
  30. {
  31. // Ottengo i dati dalla eeprom
  32. getEeprom();
  33.  
  34. // Inizializzo le variabili di servizio
  35. appo = 0;
  36. count = 0;
  37. timeH = 0;
  38. timeL = 0;
  39. timeDelay = 50;
  40. limit = false;
  41. backlashCounter = backlash;
  42. oldDir = 0;
  43. infinity = 0;
  44. currentMicros = 0;
  45. currentMillis = 0;
  46. rampTimeMicro = 0;
  47. rampTimeRatio = 0;
  48. targetStep = 0;
  49. oldError = 1;
  50. statusError = 1;
  51. checkErrorPrec = 0;
  52. status = STATUS_STOP;
  53.  
  54. TMC2130Stepper driver = TMC2130Stepper(driverEnable, dirPin, stepPin, selectPin, PIN_MOSI, PIN_MISO, PIN_SCK);
  55.  
  56. // Inizializzo i PIN
  57. pinMode(switchFocPri, INPUT);
  58. pinMode(switchFocSec, INPUT);
  59. pinMode(switchHome, INPUT);
  60.  
  61. // Inizializzo il Driver Amis
  62. driver.begin();
  63. driver.reset();
  64. driver.rms_current( CONF_FOC_CURRENT_LIMIT );
  65. driver.stealthChop(1);
  66. driver.microsteps( 1 );
  67.  
  68. digitalWrite(driverEnable, LOW);
  69.  
  70. Serial.print("DRV_STATUS=0b");
  71. Serial.println(driver.DRV_STATUS(), BIN);
  72. }
  73.  
  74.  
  75.  
  76. /*
  77. * MOVIMENTAZIONE MOTORE
  78. */
  79. void Focheggiatore::move( char dirLoc, long int stepOut, int inf = 0 )
  80. {
  81.  
  82. initRampData();
  83.  
  84. if( dirLoc == KEY_FORWARD){
  85. homePosition = false;
  86. status = KEY_FORWARD;
  87. setStepDirection(1);
  88. dir = 1;
  89. }
  90. else if ( dirLoc == KEY_BACKWARD){
  91. homePosition = false;
  92. status = KEY_BACKWARD;
  93. setStepDirection(0);
  94. dir = 0;
  95. }
  96. else if( dirLoc == KEY_HOME ){
  97. homePosition = true;
  98. status = KEY_BACKWARD;
  99. infinity = 1;
  100. setStepDirection(1);
  101. dir = 1;
  102. Serial.println( "target step: " );
  103. Serial.println( targetStep );
  104. targetStep = 1000 * CONF_FOC_MICROSTEP;
  105. return;
  106. }
  107.  
  108. if( inf ){
  109. infinity = 1;
  110. }else{
  111. infinity = 0;
  112. }
  113. Serial.println( "target step: " );
  114. Serial.println( targetStep );
  115. targetStep = CONF_FOC_MICROSTEP * stepOut;
  116.  
  117. }
  118.  
  119. /*
  120. * MOTOR STOP
  121. */
  122. void Focheggiatore::stop()
  123. {
  124. limit = false;
  125. oldDir = dir;
  126. targetStep = 0;
  127. status = STATUS_STOP;
  128. infinity = 0;
  129. digitalWrite(stepPin, LOW);
  130. Serial.println( "stop" );
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. /*
  138. * MOVIMENTAZIONE STEP
  139. */
  140. void Focheggiatore::singleSteps ()
  141. {
  142. currentMillis = millis();
  143. currentMicros = micros();
  144. // Se sono fermo non faccio nulla
  145. if( status == STATUS_STOP ){
  146. return;
  147. }
  148.  
  149.  
  150. // Gestisco la fine del movimento. Se non infinto mi fermo altrimenti rigenero gli step traget
  151. if( targetStep == 0 && infinity ){
  152. targetStep = 1000 * CONF_FOC_MICROSTEP;
  153. return;
  154. }else if( targetStep == 0 && !infinity ){
  155. stop();
  156. }
  157.  
  158.  
  159. // Controllo il driver
  160. if( timeH == 0 && timeL == 0){
  161. //sbi(PORTB, stepPin);
  162. digitalWrite(stepPin, HIGH);
  163. timeH = currentMicros;
  164. timeL = 0;
  165. return;
  166. }
  167. if( timeH!=0 && currentMicros - timeH > CONF_FOC_LOW_MICRO_TIME ){
  168. //cbi(PORTB, stepPin);
  169. digitalWrite(stepPin, LOW);
  170. timeH = 0;
  171. timeL = currentMicros;
  172. return;
  173. }
  174. if( timeL!=0 && (currentMicros - timeL > speedDelay ) ){
  175. timeH = 0;
  176. timeL = 0;
  177. targetStep--;
  178. appo++;
  179. if(appo == CONF_FOC_MICROSTEP){
  180.  
  181. if( dir == 1 ){
  182. steps--;
  183. }
  184. else{
  185. steps++;
  186. }
  187.  
  188. appo = 0;
  189.  
  190.  
  191. return;
  192. }
  193.  
  194. }
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. /*
  203. * SET DIRECTION
  204. */
  205. void Focheggiatore::setStepDirection( bool dirLoc )
  206. {
  207. Serial.println( "direzione: " );
  208. Serial.println( dirLoc );
  209. driver.shaft_dir(dirLoc);
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement