arduino_code.ino 6.3 KB

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