steps_calibration.ino 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <AccelStepper.h>
  2. #include <math.h> // For mathematical operations
  3. // Stepper driver interface type
  4. #define rotInterfaceType AccelStepper::FULL4WIRE
  5. #define inOutInterfaceType AccelStepper::FULL4WIRE
  6. #define ROT_PIN1 14
  7. #define ROT_PIN2 12
  8. #define ROT_PIN3 26
  9. #define ROT_PIN4 27
  10. #define INOUT_PIN1 16
  11. #define INOUT_PIN2 17
  12. #define INOUT_PIN3 18
  13. #define INOUT_PIN4 19
  14. // Define stepper motor objects
  15. AccelStepper rotStepper(rotInterfaceType, ROT_PIN1, ROT_PIN3, ROT_PIN2, ROT_PIN4);
  16. AccelStepper inOutStepper(inOutInterfaceType, INOUT_PIN1, INOUT_PIN3, INOUT_PIN2, INOUT_PIN4);
  17. // Calibration variables
  18. long rotSteps = 0; // Steps for the ROT axis
  19. long inOutSteps = 0; // Steps for the INOUT axis
  20. void setup() {
  21. Serial.begin(115200); // Start serial communication
  22. Serial.println("Stepper Calibration Tool");
  23. // Set max speeds and accelerations
  24. rotStepper.setMaxSpeed(1000);
  25. rotStepper.setAcceleration(500);
  26. inOutStepper.setMaxSpeed(1000);
  27. inOutStepper.setAcceleration(500);
  28. // Initial message
  29. Serial.println("Commands:");
  30. Serial.println(" r <steps> - Move rotation axis by <steps>");
  31. Serial.println(" i <steps> - Move in-out axis by <steps>");
  32. Serial.println(" reset r - Reset ROT axis steps to 0");
  33. Serial.println(" reset i - Reset IN-OUT axis steps to 0");
  34. Serial.println(" Example: r 200 (moves ROT axis 200 steps)");
  35. }
  36. void loop() {
  37. if (Serial.available() > 0) {
  38. String input = Serial.readStringUntil('\n'); // Read user input
  39. input.trim();
  40. if (input.length() > 0) {
  41. if (input.startsWith("reset")) {
  42. // Reset commands
  43. char axis = input.charAt(6); // Get the axis to reset ('r' or 'i')
  44. if (axis == 'r') {
  45. // Reset ROT axis steps
  46. rotSteps = 0;
  47. Serial.println("ROT axis steps reset to 0.");
  48. } else if (axis == 'i') {
  49. // Reset IN-OUT axis steps
  50. inOutSteps = 0;
  51. Serial.println("IN-OUT axis steps reset to 0.");
  52. } else {
  53. Serial.println("Invalid reset command. Use 'reset r' or 'reset i'.");
  54. }
  55. } else {
  56. // Movement commands
  57. char axis = input.charAt(0); // First character is the axis
  58. long steps = input.substring(2).toInt(); // Convert remaining input to steps
  59. if (axis == 'r') {
  60. // Move rotation axis
  61. Serial.print("Moving ROT axis by ");
  62. Serial.print(steps);
  63. Serial.println(" steps.");
  64. rotSteps += steps;
  65. rotStepper.move(steps);
  66. while (rotStepper.run()); // Wait for movement to finish
  67. Serial.print("Current ROT steps: ");
  68. Serial.println(rotSteps);
  69. } else if (axis == 'i') {
  70. // Move in-out axis
  71. Serial.print("Moving IN-OUT axis by ");
  72. Serial.print(steps);
  73. Serial.println(" steps.");
  74. inOutSteps += steps;
  75. inOutStepper.move(steps);
  76. while (inOutStepper.run()); // Wait for movement to finish
  77. Serial.print("Current IN-OUT steps: ");
  78. Serial.println(inOutSteps);
  79. } else {
  80. Serial.println("Invalid command. Use 'r <steps>' or 'i <steps>'.");
  81. }
  82. }
  83. }
  84. }
  85. }