FullArmControl.ino 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <AccelStepper.h>
  2. // Define the pins:
  3. // Stepper 0
  4. #define STEP_PIN0 2
  5. #define DIR_PIN0 30
  6. #define ENABLE_PIN0 31
  7. // Stepper 1
  8. #define STEP_PIN1 3
  9. #define DIR_PIN1 32
  10. #define ENABLE_PIN1 33
  11. // Stepper 2
  12. #define STEP_PIN2 4
  13. #define DIR_PIN2 34
  14. #define ENABLE_PIN2 35
  15. // Stepper 3
  16. #define STEP_PIN3 5
  17. #define DIR_PIN3 36
  18. #define ENABLE_PIN3 37
  19. // Define the steps per revolution of thr stepper motor
  20. const float STEPS_PER_REVOLUTION = 200.0;
  21. float i = 20; //Gear Transmission
  22. // Create myStepper Class and inherrt AccelStepper
  23. class myStepper: public AccelStepper{
  24. public:
  25. myStepper(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5, bool enable = true)
  26. :AccelStepper(interface, pin1, pin2, pin3, pin4, enable){}
  27. void rotateStepper(float degrees) {
  28. // Calculate the number of steps based on the desired angle
  29. float steps = degrees * (STEPS_PER_REVOLUTION / 360.0);
  30. // Move the stepper to the specified number of steps
  31. move(steps);
  32. // Enable the motor
  33. digitalWrite(ENABLE_PIN3, LOW);
  34. // Run the stepper until it reaches the target position
  35. while (distanceToGo() != 0) {
  36. run();
  37. }
  38. // Disable the motor
  39. //digitalWrite(ENABLE_PIN3, HIGH);
  40. }
  41. };
  42. myStepper stepper3(AccelStepper::DRIVER, STEP_PIN3, DIR_PIN3);
  43. const byte numChars = 32;
  44. char receivedChars[numChars];
  45. char tempChars[numChars]; // temporary array for use when parsing
  46. // variables to hold the parsed data
  47. float moveToAngels[4] = { 0, 0, 0, 0 };
  48. int isCalibrated = 0;
  49. boolean newData = false;
  50. //============
  51. void setup() {
  52. Serial.begin(9600);
  53. // Set the maximum speed and acceleration (you can adjust these values)
  54. stepper3.setMaxSpeed(400);
  55. stepper3.setAcceleration(400.0);
  56. // Set the enable pin as an output and disable the motor by default
  57. pinMode(ENABLE_PIN3, OUTPUT);
  58. digitalWrite(ENABLE_PIN3, LOW);
  59. // Set the motor direction (0 for clockwise, 1 for counter-clockwise)
  60. stepper3.setPinsInverted(false, false, true); // Adjust the last parameter if needed
  61. }
  62. //============
  63. void loop() {
  64. recvWithStartEndMarkers();
  65. if (newData == true) {
  66. strcpy(tempChars, receivedChars);
  67. // this temporary copy is necessary to protect the original data
  68. // because strtok() used in parseData() replaces the commas with \0
  69. parseData();
  70. //showParsedData();
  71. newData = false;
  72. //check received values
  73. Serial.println(moveToAngels[3]);
  74. stepper3.rotateStepper(moveToAngels[3] * i);
  75. //reset recieved values
  76. moveToAngels[0] = 0;
  77. moveToAngels[1] = 0;
  78. moveToAngels[2] = 0;
  79. moveToAngels[3] = 0;
  80. isCalibrated = 0;
  81. }
  82. }
  83. //============
  84. void recvWithStartEndMarkers() {
  85. static boolean recvInProgress = false;
  86. static byte ndx = 0;
  87. char startMarker = '<';
  88. char endMarker = '>';
  89. char rc;
  90. while (Serial.available() > 0 && newData == false) {
  91. rc = Serial.read();
  92. Serial.print(rc);
  93. if (recvInProgress == true) {
  94. if (rc != endMarker) {
  95. receivedChars[ndx] = rc;
  96. ndx++;
  97. if (ndx >= numChars) {
  98. ndx = numChars - 1;
  99. }
  100. } else {
  101. receivedChars[ndx] = '\0'; // terminate the string
  102. recvInProgress = false;
  103. ndx = 0;
  104. newData = true;
  105. }
  106. }
  107. else if (rc == startMarker) {
  108. recvInProgress = true;
  109. }
  110. }
  111. }
  112. //============
  113. void parseData() { // split the data into its parts
  114. char* strtokIndx; // this is used by strtok() as an index
  115. strtokIndx = strtok(tempChars, ","); // get the first part
  116. isCalibrated = atoi(strtokIndx); // convert this part to an integer
  117. strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  118. moveToAngels[0] = atoi(strtokIndx);
  119. strtokIndx = strtok(NULL, ",");
  120. moveToAngels[1] = atoi(strtokIndx); // convert this part to a float
  121. strtokIndx = strtok(NULL, ",");
  122. moveToAngels[2] = atof(strtokIndx); // convert this part to a float
  123. strtokIndx = strtok(NULL, ",");
  124. moveToAngels[3] = atof(strtokIndx); // convert this part to a float
  125. }
  126. //============
  127. /* void rotateStepper(float degrees) {
  128. // Calculate the number of steps based on the desired angle
  129. float steps = degrees * (STEPS_PER_REVOLUTION / 360.0);
  130. // Move the stepper to the specified number of steps
  131. stepper.move(steps);
  132. // Enable the motor
  133. digitalWrite(ENABLE_PIN1, LOW);
  134. // Run the stepper until it reaches the target position
  135. while (stepper.distanceToGo() != 0) {
  136. stepper.run();
  137. }
  138. // Disable the motor
  139. //digitalWrite(ENABLE_PIN1, HIGH);
  140. }
  141. **/
  142. //============
  143. // For debugging purposes
  144. void showParsedData() {
  145. Serial.print("Calibration: ");
  146. Serial.println(isCalibrated);
  147. Serial.print("Stepper1 ");
  148. Serial.println(moveToAngels[0]);
  149. Serial.print("Stepper2 ");
  150. Serial.println(moveToAngels[1]);
  151. Serial.print("Stepper3 ");
  152. Serial.println(moveToAngels[2]);
  153. Serial.print("Stepper4 ");
  154. Serial.println(moveToAngels[3]);
  155. }