esp32.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 ROT_PIN1 14
  7. #define ROT_PIN2 12
  8. #define ROT_PIN3 26
  9. #define ROT_PIN4 27
  10. #define INOUT_PIN1 16
  11. #define INOUT_PIN2 17
  12. #define INOUT_PIN3 18
  13. #define INOUT_PIN4 19
  14. #define rot_total_steps 12800
  15. #define inOut_total_steps 4642
  16. // #define inOut_total_steps 4660
  17. const double gearRatio = 100.0f / 16.0f;
  18. #define BUFFER_SIZE 10 // Maximum number of theta-rho pairs in a batch
  19. // Create stepper motor objects
  20. AccelStepper rotStepper(AccelStepper::FULL4WIRE, ROT_PIN1, ROT_PIN3, ROT_PIN2, ROT_PIN4); // Rot axis
  21. AccelStepper inOutStepper(AccelStepper::FULL4WIRE, INOUT_PIN1, INOUT_PIN3, INOUT_PIN2, INOUT_PIN4); // In-out axis
  22. // Create a MultiStepper object
  23. MultiStepper multiStepper;
  24. // Buffer for storing theta-rho pairs
  25. double buffer[BUFFER_SIZE][2]; // Store theta, rho pairs
  26. int bufferCount = 0; // Number of pairs in the buffer
  27. bool batchComplete = false;
  28. // Track the current position in polar coordinates
  29. double currentTheta = 0.0; // Current theta in radians
  30. double currentRho = 0.0; // Current rho (0 to 1)
  31. bool isFirstCoordinates = true;
  32. double totalRevolutions = 0.0; // Tracks cumulative revolutions
  33. double maxSpeed = 500;
  34. double maxAcceleration = 5000;
  35. double subSteps = 1;
  36. int modulus(int x, int y) {
  37. return x < 0 ? ((x + 1) % y) + y - 1 : x % y;
  38. }
  39. void setup()
  40. {
  41. // Set maximum speed and acceleration
  42. rotStepper.setMaxSpeed(maxSpeed); // Adjust as needed
  43. rotStepper.setAcceleration(maxAcceleration); // Adjust as needed
  44. inOutStepper.setMaxSpeed(maxSpeed); // Adjust as needed
  45. inOutStepper.setAcceleration(maxAcceleration); // Adjust as needed
  46. // Add steppers to MultiStepper
  47. multiStepper.addStepper(rotStepper);
  48. multiStepper.addStepper(inOutStepper);
  49. // Initialize serial communication
  50. Serial.begin(115200);
  51. Serial.println("R");
  52. homing();
  53. }
  54. void loop()
  55. {
  56. // Check for incoming serial commands or theta-rho pairs
  57. if (Serial.available() > 0)
  58. {
  59. String input = Serial.readStringUntil('\n');
  60. // Ignore invalid messages
  61. if (input != "HOME" && input != "RESET_THETA" && !input.startsWith("SET_SPEED") && !input.endsWith(";"))
  62. {
  63. Serial.println("IGNORED");
  64. return;
  65. }
  66. if (input.startsWith("SET_SPEED"))
  67. {
  68. // Parse and set the speed
  69. int spaceIndex = input.indexOf(' ');
  70. if (spaceIndex != -1)
  71. {
  72. String speedStr = input.substring(spaceIndex + 1);
  73. double speed = speedStr.toDouble();
  74. if (speed > 0) // Ensure valid speed
  75. {
  76. rotStepper.setMaxSpeed(speed);
  77. inOutStepper.setMaxSpeed(speed);
  78. Serial.println("SPEED_SET");
  79. Serial.println("R");
  80. }
  81. else
  82. {
  83. Serial.println("INVALID_SPEED");
  84. }
  85. }
  86. else
  87. {
  88. Serial.println("INVALID_COMMAND");
  89. }
  90. return;
  91. }
  92. if (input == "HOME")
  93. {
  94. homing();
  95. return;
  96. }
  97. if (input == "RESET_THETA")
  98. {
  99. isFirstCoordinates = true;
  100. currentTheta = 0;
  101. currentRho = 0;
  102. Serial.println("THETA_RESET"); // Notify Python
  103. Serial.println("R");
  104. return;
  105. }
  106. // If not a command, assume it's a batch of theta-rho pairs
  107. if (!batchComplete)
  108. {
  109. int pairIndex = 0;
  110. int startIdx = 0;
  111. // Split the batch line into individual theta-rho pairs
  112. while (pairIndex < BUFFER_SIZE)
  113. {
  114. int endIdx = input.indexOf(";", startIdx);
  115. if (endIdx == -1)
  116. break; // No more pairs in the line
  117. String pair = input.substring(startIdx, endIdx);
  118. int commaIndex = pair.indexOf(',');
  119. // Parse theta and rho values
  120. double theta = pair.substring(0, commaIndex).toDouble(); // Theta in radians
  121. double rho = pair.substring(commaIndex + 1).toDouble(); // Rho (0 to 1)
  122. buffer[pairIndex][0] = theta;
  123. buffer[pairIndex][1] = rho;
  124. pairIndex++;
  125. startIdx = endIdx + 1; // Move to next pair
  126. }
  127. bufferCount = pairIndex;
  128. batchComplete = true;
  129. }
  130. }
  131. // Process the buffer if a batch is ready
  132. if (batchComplete && bufferCount > 0)
  133. {
  134. rotStepper.enableOutputs();
  135. inOutStepper.enableOutputs();
  136. // Start interpolation from the current position
  137. double startTheta = currentTheta;
  138. double startRho = currentRho;
  139. if (isFirstCoordinates) {
  140. homing();
  141. isFirstCoordinates = false;
  142. }
  143. for (int i = 0; i < bufferCount; i++)
  144. {
  145. interpolatePath(
  146. startTheta, startRho,
  147. buffer[i][0], buffer[i][1],
  148. subSteps
  149. );
  150. // Update the starting point for the next segment
  151. startTheta = buffer[i][0];
  152. startRho = buffer[i][1];
  153. }
  154. rotStepper.disableOutputs();
  155. inOutStepper.disableOutputs();
  156. batchComplete = false; // Reset batch flag
  157. bufferCount = 0; // Clear buffer
  158. Serial.println("R");
  159. }
  160. }
  161. void homing()
  162. {
  163. Serial.println("HOMING");
  164. inOutStepper.enableOutputs();
  165. // Move inOutStepper inward for homing
  166. inOutStepper.setSpeed(-maxSpeed); // Adjust speed for homing
  167. long currentInOut = inOutStepper.currentPosition();
  168. while (true)
  169. {
  170. inOutStepper.runSpeed();
  171. if (inOutStepper.currentPosition() <= currentInOut - inOut_total_steps * 1.1)
  172. { // Adjust distance for homing
  173. break;
  174. }
  175. }
  176. inOutStepper.setCurrentPosition(0); // Set home position
  177. rotStepper.setCurrentPosition(0);
  178. currentTheta = 0.0; // Reset polar coordinates
  179. currentRho = 0.0;
  180. inOutStepper.disableOutputs();
  181. Serial.println("HOMED");
  182. }
  183. void movePolar(double theta, double rho)
  184. {
  185. if (rho < 0.0)
  186. rho = 0.0;
  187. else if (rho > 1.0)
  188. rho = 1.0;
  189. long rotSteps = lround(theta * (rot_total_steps / (2.0f * M_PI)));
  190. double revolutions = theta / (2.0 * M_PI);
  191. long offsetSteps = lround(revolutions * (rot_total_steps / gearRatio));
  192. // Now inOutSteps is always derived from the absolute rho, not incrementally
  193. long inOutSteps = lround(rho * inOut_total_steps);
  194. inOutSteps -= offsetSteps;
  195. long targetPositions[2] = {rotSteps, inOutSteps};
  196. multiStepper.moveTo(targetPositions);
  197. multiStepper.runSpeedToPosition(); // Blocking call
  198. // Update current coordinates
  199. currentTheta = theta;
  200. currentRho = rho;
  201. }
  202. void interpolatePath(double startTheta, double startRho, double endTheta, double endRho, double subSteps)
  203. {
  204. // Calculate the total distance in the polar coordinate system
  205. double distance = sqrt(pow(endTheta - startTheta, 2) + pow(endRho - startRho, 2));
  206. long numSteps = max(1, (int)(distance / subSteps)); // Ensure at least one step
  207. for (long step = 0; step <= numSteps; step++)
  208. {
  209. double t = (double)step / numSteps; // Interpolation factor (0 to 1)
  210. double interpolatedTheta = startTheta + t * (endTheta - startTheta);
  211. double interpolatedRho = startRho + t * (endRho - startRho);
  212. // Move to the interpolated theta-rho
  213. movePolar(interpolatedTheta, interpolatedRho);
  214. }
  215. }