steps_calibration_tmc2209.ino 3.0 KB

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