arduino_code.ino 6.9 KB

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