Advertisement
Guest User

calibrate.c

a guest
Jun 26th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. /**
  2. * Polargraph Server for ATMEGA1280+
  3. * Written by Sandy Noble
  4. * Released under GNU License version 3.
  5. * http://www.polargraph.co.uk
  6. * http://code.google.com/p/polargraph/
  7.  
  8. Specific features for Polarshield / arduino mega.
  9. Calibration.
  10.  
  11. Experimental calibration routine. It is designed to work with
  12. limit switches, triggered by the gondola being a fixed (known)
  13. distance from the sprocket.
  14.  
  15. */
  16.  
  17. /* =============================================================================
  18. Here is the calibration routines
  19. =============================================================================*/
  20.  
  21. extern AF_Stepper afMotorB;
  22.  
  23. void calibrate_doCalibration()
  24. {
  25. Serial.println("Doing calibration.");
  26. Serial.println("Lifting Pen.");
  27. penlift_movePen(isPenUp ? upPosition : downPosition, upPosition, penLiftSpeed);
  28.  
  29. Serial.print("machineWidth: "); Serial.print(machineWidth);
  30. Serial.print(",stepsPerMM: "); Serial.print(stepsPerMM);
  31. Serial.print(",pageWidth: "); Serial.println(pageWidth);
  32.  
  33. motorA.setAcceleration(200000);
  34. motorB.setAcceleration(200000);
  35.  
  36. releaseMotors();
  37. afMotorB.release(); // release doesn't actually release
  38.  
  39. motorA.enableOutputs();
  40. motorA.setCurrentPosition(0);
  41.  
  42. // get a baseline from the endstop detector in case it's already at the stop
  43. byte endStopSignal = digitalRead(ENDSTOP_X_MIN);
  44. Serial.print("Reading X Endstop:");
  45. Serial.println(endStopSignal);
  46.  
  47. //motorA.setAcceleration(10000);
  48. if (endStopSignal == 0)
  49. {
  50. // it's already there! God what a mess!
  51. // what we'll do is wind forwards until it changes back
  52. Serial.println("Already at X Endstop, winding motorA forward");
  53. while (endStopSignal == 0)
  54. {
  55. motorA.move(stepMultiplier);
  56. while (motorA.distanceToGo() != 0)
  57. motorA.run();
  58. endStopSignal = digitalRead(ENDSTOP_X_MIN);
  59. Serial.println(endStopSignal);
  60. }
  61. Serial.println("motorA: at endstop, jumping a bit more");
  62. motorA.move(stepMultiplier * motorStepsPerRev); // then jump a bit more - one rev
  63. while (motorA.distanceToGo() != 0) motorA.run();
  64. }
  65. delay(400);
  66.  
  67. Serial.println("motorA: wind backwards until we hit endstop X");
  68. // so wind backwards until hitting the stop.
  69. while (endStopSignal != 0)
  70. {
  71. motorA.move(-1);
  72. while (motorA.distanceToGo() != 0)
  73. motorA.run();
  74. endStopSignal = digitalRead(ENDSTOP_X_MIN);
  75. }
  76. Serial.print("motorA: at endstop X, saving position for motorA, resting at ");
  77. motorARestPoint = abs(motorA.currentPosition()) + (ENDSTOP_X_MIN_POSITION * stepsPerMM);
  78. Serial.println(motorARestPoint);
  79. Serial.print(", current position ");
  80. motorA.setCurrentPosition(ENDSTOP_X_MIN_POSITION * stepsPerMM);
  81. Serial.println(motorA.currentPosition());
  82.  
  83. delay(1000);
  84. reportPosition();
  85.  
  86. //Serial.print("moving motorA to ");
  87. //Serial.println(pageWidth/20);
  88. //motorA.moveTo(pageWidth/20);
  89. //motorA.setAcceleration(currentAcceleration);
  90. //while (motorA.distanceToGo() != 0)
  91. // motorA.run();
  92.  
  93. Serial.println("repeating process for motorB, moving motorA with it");
  94. // now do above for motorB
  95. motorB.enableOutputs();
  96. motorB.setCurrentPosition(0);
  97. //motorB.setAcceleration(10000);
  98.  
  99. Serial.println("motorB: wind backwards until we hit endstop Y, move motorA with it");
  100. endStopSignal = digitalRead(ENDSTOP_Y_MIN); // get baseline read from endstop in case we're already there
  101. delay(400);
  102. while (endStopSignal != 0)
  103. {
  104. motorB.move(-1);
  105. motorA.move(1);
  106. while (motorB.distanceToGo() != 0) {
  107. motorB.run();
  108. motorA.run();
  109. }
  110. endStopSignal = digitalRead(ENDSTOP_Y_MIN);
  111. }
  112. Serial.print("motorB: at endstop Y, saving position for motorB, resting at ");
  113. motorBRestPoint = abs(motorB.currentPosition()) + (ENDSTOP_Y_MIN_POSITION * stepsPerMM);
  114. Serial.print(motorBRestPoint);
  115. Serial.print(", current position ");
  116. motorB.setCurrentPosition(ENDSTOP_Y_MIN_POSITION * stepsPerMM);
  117. Serial.println(motorB.currentPosition());
  118. reportPosition();
  119.  
  120. //motorA.setMaxSpeed(currentMaxSpeed);
  121. // motorB.setMaxSpeed(currentMaxSpeed);
  122. //motorA.setAcceleration(currentAcceleration);
  123. //motorB.setAcceleration(currentAcceleration);
  124.  
  125. float h = 100;
  126. float fudge = 7.28; // substitude penWidth for debugging
  127. Serial.print("using fudge: "); Serial.println(fudge);
  128.  
  129.  
  130. int d = sqrt(h*h+((float)machineWidth/2)*((float)machineWidth/2))-fudge ;
  131. int ds = d*16;
  132.  
  133. Serial.print("move motorA and motorB to ");
  134. Serial.print(d);
  135. Serial.print(",");
  136. Serial.println(d);
  137. Serial.print("->");
  138. Serial.print(ds);
  139. Serial.print(",");
  140. Serial.println(ds);
  141.  
  142.  
  143. motorA.moveTo(ds);
  144. motorB.moveTo(ds);
  145.  
  146. while (motorA.distanceToGo() != 0 || motorB.distanceToGo() != 0)
  147. {
  148. motorA.run();
  149. motorB.run();
  150. }
  151.  
  152. Serial.println("motors at final position, calibrated");
  153. reportPosition();
  154. powerIsOn = true;
  155. isCalibrated = true;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement