arduino_code.ino 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // Track the current position in polar coordinates
  23. float currentTheta = 0.0; // Current theta in radians
  24. float currentRho = 0.0; // Current rho (0 to 1)
  25. void setup() {
  26. // Set maximum speed and acceleration
  27. rotStepper.setMaxSpeed(5000); // Adjust as needed
  28. rotStepper.setAcceleration(5000); // Adjust as needed
  29. inOutStepper.setMaxSpeed(5000); // Adjust as needed
  30. inOutStepper.setAcceleration(5000); // Adjust as needed
  31. // Add steppers to MultiStepper
  32. multiStepper.addStepper(rotStepper);
  33. multiStepper.addStepper(inOutStepper);
  34. // Initialize serial communication
  35. Serial.begin(115200);
  36. Serial.println("READY");
  37. homing();
  38. }
  39. void loop() {
  40. // Check for incoming serial commands or theta-rho pairs
  41. if (Serial.available() > 0) {
  42. String input = Serial.readStringUntil('\n');
  43. // Ignore invalid messages
  44. if (input != "HOME" && !input.endsWith(";")) {
  45. Serial.println("IGNORED");
  46. return;
  47. }
  48. if (input == "HOME") {
  49. homing();
  50. return;
  51. }
  52. // If not a command, assume it's a batch of theta-rho pairs
  53. if (!batchComplete) {
  54. int pairIndex = 0;
  55. int startIdx = 0;
  56. // Split the batch line into individual theta-rho pairs
  57. while (pairIndex < BUFFER_SIZE) {
  58. int endIdx = input.indexOf(";", startIdx);
  59. if (endIdx == -1) break; // No more pairs in the line
  60. String pair = input.substring(startIdx, endIdx);
  61. int commaIndex = pair.indexOf(',');
  62. // Parse theta and rho values
  63. float theta = pair.substring(0, commaIndex).toFloat(); // Theta in radians
  64. float rho = pair.substring(commaIndex + 1).toFloat(); // Rho (0 to 1)
  65. buffer[pairIndex][0] = theta;
  66. buffer[pairIndex][1] = rho;
  67. pairIndex++;
  68. startIdx = endIdx + 1; // Move to next pair
  69. }
  70. bufferCount = pairIndex;
  71. batchComplete = true;
  72. }
  73. }
  74. // Process the buffer if a batch is ready
  75. if (batchComplete && bufferCount > 0) {
  76. // Start interpolation from the current position
  77. float startTheta = currentTheta;
  78. float startRho = currentRho;
  79. for (int i = 0; i < bufferCount; i++) {
  80. // Interpolate from the starting point to the next buffer point
  81. interpolatePath(
  82. startTheta, startRho, // Start theta and rho
  83. buffer[i][0], buffer[i][1], // End theta and rho
  84. 0.001 // Step size
  85. );
  86. // Update the starting point for the next segment
  87. startTheta = buffer[i][0];
  88. startRho = buffer[i][1];
  89. }
  90. bufferCount = 0; // Clear buffer
  91. batchComplete = false; // Reset batch flag
  92. Serial.println("READY");
  93. }
  94. }
  95. void homing() {
  96. Serial.println("HOMING");
  97. // Move inOutStepper inward for homing
  98. inOutStepper.setSpeed(-5000); // Adjust speed for homing
  99. while (true) {
  100. inOutStepper.runSpeed();
  101. if (inOutStepper.currentPosition() <= -inOut_total_steps * 1.1) { // Adjust distance for homing
  102. break;
  103. }
  104. }
  105. inOutStepper.setCurrentPosition(0); // Set home position
  106. currentTheta = 0.0; // Reset polar coordinates
  107. currentRho = 0.0;
  108. Serial.println("HOMED");
  109. }
  110. void movePolar(float theta, float rho) {
  111. // Convert polar coordinates to motor steps
  112. long rotSteps = theta * (rot_total_steps / (2.0 * M_PI)); // Steps for rot axis
  113. long inOutSteps = rho * inOut_total_steps; // Steps for in-out axis
  114. // Calculate offset for inOut axis
  115. float revolutions = theta / (2.0 * M_PI); // Fractional revolutions (can be positive or negative)
  116. long offsetSteps = revolutions * 1600; // 1600 steps inward or outward per revolution
  117. // Apply the offset to the inout axis
  118. inOutSteps += offsetSteps;
  119. // Define target positions for both motors
  120. long targetPositions[2];
  121. targetPositions[0] = rotSteps;
  122. targetPositions[1] = inOutSteps;
  123. // Move both motors synchronously
  124. multiStepper.moveTo(targetPositions);
  125. multiStepper.runSpeedToPosition(); // Blocking call
  126. // Update the current coordinates
  127. currentTheta = theta;
  128. currentRho = rho;
  129. }
  130. void interpolatePath(float startTheta, float startRho, float endTheta, float endRho, float stepSize) {
  131. // Calculate the total distance in the polar coordinate system
  132. float distance = sqrt(pow(endTheta - startTheta, 2) + pow(endRho - startRho, 2));
  133. int numSteps = max(1, (int)(distance / stepSize)); // Ensure at least one step
  134. for (int step = 0; step <= numSteps; step++) {
  135. float t = (float)step / numSteps; // Interpolation factor (0 to 1)
  136. float interpolatedTheta = startTheta + t * (endTheta - startTheta);
  137. float interpolatedRho = startRho + t * (endRho - startRho);
  138. // Move to the interpolated theta-rho
  139. movePolar(interpolatedTheta, interpolatedRho);
  140. }
  141. }