arduino_code.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 2
  7. #define dirPin_rot 5
  8. #define stepPin_InOut 3
  9. #define dirPin_InOut 6
  10. #define rot_total_steps 16000.0
  11. #define inOut_total_steps 5760.0
  12. #define BUFFER_SIZE 10 // Maximum number of theta-rho pairs in a batch
  13. // Create stepper motor objects
  14. AccelStepper rotStepper(rotInterfaceType, stepPin_rot, dirPin_rot);
  15. AccelStepper inOutStepper(inOutInterfaceType, stepPin_InOut, dirPin_InOut);
  16. // Create a MultiStepper object
  17. MultiStepper multiStepper;
  18. // Buffer for storing theta-rho pairs
  19. float buffer[BUFFER_SIZE][2]; // Store theta, rho pairs
  20. int bufferCount = 0; // Number of pairs in the buffer
  21. bool batchComplete = false;
  22. void setup() {
  23. // Set maximum speed and acceleration
  24. rotStepper.setMaxSpeed(2000); // Adjust as needed
  25. rotStepper.setAcceleration(1); // Adjust as needed
  26. inOutStepper.setMaxSpeed(2000); // Adjust as needed
  27. inOutStepper.setAcceleration(1); // Adjust as needed
  28. // inOutStepper.setPinsInverted(true, false, false);
  29. // Add steppers to MultiStepper
  30. multiStepper.addStepper(rotStepper);
  31. multiStepper.addStepper(inOutStepper);
  32. // Initialize serial communication
  33. Serial.begin(115200);
  34. Serial.println("READY");
  35. homing();
  36. }
  37. void loop() {
  38. // Check for incoming serial commands or theta-rho pairs
  39. if (Serial.available() > 0) {
  40. String input = Serial.readStringUntil('\n');
  41. // Ignore invalid messages
  42. if (input != "HOME" && !input.endsWith(";")) {
  43. Serial.println("IGNORED");
  44. return;
  45. }
  46. if (input == "HOME") {
  47. homing();
  48. return;
  49. }
  50. // If not a command, assume it's a batch of theta-rho pairs
  51. if (!batchComplete) {
  52. int pairIndex = 0;
  53. int startIdx = 0;
  54. // Split the batch line into individual theta-rho pairs
  55. while (pairIndex < BUFFER_SIZE) {
  56. int endIdx = input.indexOf(";", startIdx);
  57. if (endIdx == -1) break; // No more pairs in the line
  58. String pair = input.substring(startIdx, endIdx);
  59. int commaIndex = pair.indexOf(',');
  60. // Parse theta and rho values
  61. float theta = pair.substring(0, commaIndex).toFloat(); // Theta in radians
  62. float rho = pair.substring(commaIndex + 1).toFloat(); // Rho (0 to 1)
  63. buffer[pairIndex][0] = theta;
  64. buffer[pairIndex][1] = rho;
  65. pairIndex++;
  66. startIdx = endIdx + 1; // Move to next pair
  67. }
  68. bufferCount = pairIndex;
  69. batchComplete = true;
  70. }
  71. }
  72. // Process the buffer if a batch is ready
  73. if (batchComplete && bufferCount > 0) {
  74. for (int i = 0; i < bufferCount; i++) {
  75. movePolar(buffer[i][0], buffer[i][1]);
  76. }
  77. bufferCount = 0; // Clear buffer
  78. batchComplete = false; // Reset batch flag
  79. Serial.println("READY");
  80. }
  81. }
  82. void homing() {
  83. Serial.println("HOMING");
  84. // Move inOutStepper inward for homing
  85. inOutStepper.setSpeed(-5000); // Adjust speed for homing
  86. while (true) {
  87. inOutStepper.runSpeed();
  88. if (inOutStepper.currentPosition() <= -5760 * 1.1) { // Adjust distance for homing
  89. break;
  90. }
  91. }
  92. inOutStepper.setCurrentPosition(0); // Set home position
  93. Serial.println("HOMED");
  94. }
  95. void movePolar(float theta, float rho) {
  96. // Convert polar coordinates to motor steps
  97. long rotSteps = theta * (rot_total_steps / (2.0 * M_PI)); // Steps for rot axis
  98. long inOutSteps = rho * inOut_total_steps; // Steps for in-out axis
  99. // Calculate offset for inOut axis
  100. float revolutions = theta / (2.0 * M_PI); // Fractional revolutions (can be positive or negative)
  101. long offsetSteps = revolutions * 1600; // 1600 steps inward or outward per revolution
  102. // Apply the offset to the inout axis
  103. inOutSteps += offsetSteps;
  104. // Define target positions for both motors
  105. long targetPositions[2];
  106. targetPositions[0] = rotSteps;
  107. targetPositions[1] = inOutSteps;
  108. // Move both motors synchronously
  109. multiStepper.moveTo(targetPositions);
  110. unsigned long lastStepTime = 0;
  111. const unsigned long stepInterval = 10; // 10 ms between checks
  112. multiStepper.runSpeedToPosition(); // Blocking call
  113. }