esp32.ino 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 27
  7. #define ROT_PIN2 26
  8. #define ROT_PIN3 12
  9. #define ROT_PIN4 14
  10. #define INOUT_PIN1 19
  11. #define INOUT_PIN2 18
  12. #define INOUT_PIN3 17
  13. #define INOUT_PIN4 16
  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. // FIRMWARE VERSION
  37. const char* firmwareVersion = "1.4.0";
  38. const char* motorType = "esp32";
  39. int modulus(int x, int y) {
  40. return x < 0 ? ((x + 1) % y) + y - 1 : x % y;
  41. }
  42. void setup()
  43. {
  44. // Set maximum speed and acceleration
  45. rotStepper.setMaxSpeed(maxSpeed); // Adjust as needed
  46. rotStepper.setAcceleration(maxAcceleration); // Adjust as needed
  47. inOutStepper.setMaxSpeed(maxSpeed); // Adjust as needed
  48. inOutStepper.setAcceleration(maxAcceleration); // Adjust as needed
  49. // Add steppers to MultiStepper
  50. multiStepper.addStepper(rotStepper);
  51. multiStepper.addStepper(inOutStepper);
  52. // Initialize serial communication
  53. Serial.begin(115200);
  54. Serial.println("R");
  55. homing();
  56. }
  57. void getVersion()
  58. {
  59. Serial.println("Table: Mini Dune Weaver");
  60. Serial.println("Drivers: ULN2003");
  61. Serial.println("Version: 1.4.0");
  62. }
  63. void loop()
  64. {
  65. // Check for incoming serial commands or theta-rho pairs
  66. if (Serial.available() > 0)
  67. {
  68. String input = Serial.readStringUntil('\n');
  69. // Ignore invalid messages
  70. if (input != "HOME" && input != "RESET_THETA" && input != "GET_VERSION" && !input.startsWith("SET_SPEED") && !input.endsWith(";"))
  71. {
  72. Serial.println("IGNORED");
  73. return;
  74. }
  75. if (input == "GET_VERSION") {
  76. getVersion();
  77. }
  78. // Example: The user calls "SET_SPEED 60" => 60% of maxSpeed
  79. if (input.startsWith("SET_SPEED"))
  80. {
  81. // Parse out the speed value from the command string
  82. int spaceIndex = input.indexOf(' ');
  83. if (spaceIndex != -1)
  84. {
  85. String speedStr = input.substring(spaceIndex + 1);
  86. float speedPercentage = speedStr.toFloat();
  87. // Make sure the percentage is valid
  88. if (speedPercentage >= 1.0 && speedPercentage <= 100.0)
  89. {
  90. // Convert percentage to actual speed
  91. long newSpeed = (speedPercentage / 100.0) * maxSpeed;
  92. // Set the stepper speeds
  93. rotStepper.setMaxSpeed(newSpeed);
  94. inOutStepper.setMaxSpeed(newSpeed);
  95. Serial.println("SPEED_SET");
  96. }
  97. else
  98. {
  99. Serial.println("INVALID_SPEED");
  100. }
  101. }
  102. else
  103. {
  104. Serial.println("INVALID_COMMAND");
  105. }
  106. return;
  107. }
  108. if (input == "HOME")
  109. {
  110. homing();
  111. return;
  112. }
  113. if (input == "RESET_THETA")
  114. {
  115. isFirstCoordinates = true;
  116. currentTheta = 0;
  117. currentRho = 0;
  118. Serial.println("THETA_RESET"); // Notify Python
  119. Serial.println("R");
  120. return;
  121. }
  122. // If not a command, assume it's a batch of theta-rho pairs
  123. if (!batchComplete)
  124. {
  125. int pairIndex = 0;
  126. int startIdx = 0;
  127. // Split the batch line into individual theta-rho pairs
  128. while (pairIndex < BUFFER_SIZE)
  129. {
  130. int endIdx = input.indexOf(";", startIdx);
  131. if (endIdx == -1)
  132. break; // No more pairs in the line
  133. String pair = input.substring(startIdx, endIdx);
  134. int commaIndex = pair.indexOf(',');
  135. // Parse theta and rho values
  136. double theta = pair.substring(0, commaIndex).toDouble(); // Theta in radians
  137. double rho = pair.substring(commaIndex + 1).toDouble(); // Rho (0 to 1)
  138. buffer[pairIndex][0] = theta;
  139. buffer[pairIndex][1] = rho;
  140. pairIndex++;
  141. startIdx = endIdx + 1; // Move to next pair
  142. }
  143. bufferCount = pairIndex;
  144. batchComplete = true;
  145. }
  146. }
  147. // Process the buffer if a batch is ready
  148. if (batchComplete && bufferCount > 0)
  149. {
  150. rotStepper.enableOutputs();
  151. inOutStepper.enableOutputs();
  152. // Start interpolation from the current position
  153. double startTheta = currentTheta;
  154. double startRho = currentRho;
  155. for (int i = 0; i < bufferCount; i++)
  156. {
  157. if (isFirstCoordinates)
  158. {
  159. // Directly move to the first coordinate of the new pattern
  160. long initialRotSteps = buffer[0][0] * (rot_total_steps / (2.0 * M_PI));
  161. rotStepper.setCurrentPosition(initialRotSteps);
  162. inOutStepper.setCurrentPosition(inOutStepper.currentPosition() + (totalRevolutions * rot_total_steps / gearRatio));
  163. currentTheta = buffer[0][0];
  164. totalRevolutions = 0;
  165. movePolar(buffer[0][0], buffer[0][1]);
  166. isFirstCoordinates = false; // Reset the flag after the first movement
  167. } else
  168. {
  169. interpolatePath(
  170. startTheta, startRho,
  171. buffer[i][0], buffer[i][1],
  172. subSteps
  173. );
  174. }
  175. // Update the starting point for the next segment
  176. startTheta = buffer[i][0];
  177. startRho = buffer[i][1];
  178. }
  179. rotStepper.disableOutputs();
  180. inOutStepper.disableOutputs();
  181. batchComplete = false; // Reset batch flag
  182. bufferCount = 0; // Clear buffer
  183. Serial.println("R");
  184. }
  185. }
  186. void homing()
  187. {
  188. Serial.println("HOMING");
  189. inOutStepper.enableOutputs();
  190. // Move inOutStepper inward for homing
  191. inOutStepper.setSpeed(-maxSpeed); // Adjust speed for homing
  192. long currentInOut = inOutStepper.currentPosition();
  193. while (true)
  194. {
  195. inOutStepper.runSpeed();
  196. if (inOutStepper.currentPosition() <= currentInOut - inOut_total_steps * 1.1)
  197. { // Adjust distance for homing
  198. break;
  199. }
  200. }
  201. inOutStepper.setCurrentPosition(0); // Set home position
  202. rotStepper.setCurrentPosition(0);
  203. currentTheta = 0.0; // Reset polar coordinates
  204. currentRho = 0.0;
  205. inOutStepper.disableOutputs();
  206. Serial.println("HOMED");
  207. }
  208. void movePolar(double theta, double rho)
  209. {
  210. long rotSteps = lround(theta * (rot_total_steps / (2.0f * M_PI)));
  211. double revolutions = theta / (2.0 * M_PI);
  212. long offsetSteps = lround(revolutions * (rot_total_steps / gearRatio));
  213. // Now inOutSteps is always derived from the absolute rho, not incrementally
  214. long inOutSteps = lround(rho * inOut_total_steps);
  215. totalRevolutions += (theta - currentTheta) / (2.0 * M_PI);
  216. if (!isFirstCoordinates)
  217. {
  218. inOutSteps -= offsetSteps;
  219. }
  220. long targetPositions[2] = {rotSteps, inOutSteps};
  221. multiStepper.moveTo(targetPositions);
  222. multiStepper.runSpeedToPosition(); // Blocking call
  223. // Update current coordinates
  224. currentTheta = theta;
  225. currentRho = rho;
  226. }
  227. void interpolatePath(double startTheta, double startRho, double endTheta, double endRho, double subSteps)
  228. {
  229. // Calculate the total distance in the polar coordinate system
  230. double distance = sqrt(pow(endTheta - startTheta, 2) + pow(endRho - startRho, 2));
  231. long numSteps = max(1, (int)(distance / subSteps)); // Ensure at least one step
  232. for (long step = 0; step <= numSteps; step++)
  233. {
  234. double t = (double)step / numSteps; // Interpolation factor (0 to 1)
  235. double interpolatedTheta = startTheta + t * (endTheta - startTheta);
  236. double interpolatedRho = startRho + t * (endRho - startRho);
  237. // Move to the interpolated theta-rho
  238. movePolar(interpolatedTheta, interpolatedRho);
  239. }
  240. }