Facebook
From bug , 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 52
  1. #include <SpeedyStepper.h>
  2. //const int MOTOR_STEP_PINX = 3;
  3. //const int MOTOR_DIRECTION_PINX = 4;
  4.  
  5. //ramps 1.4
  6. const int MOTOR_STEP_PINX = 54;
  7. const int MOTOR_DIRECTION_PINX = 55;
  8. const int enableX = 38;
  9.  
  10. const int MOTOR_STEP_PINY = 60;
  11. const int MOTOR_DIRECTION_PINY = 61;
  12. const int enableY = 56;
  13.  
  14. const int MOTOR_STEP_PINZ = 46;
  15. const int MOTOR_DIRECTION_PINZ = 48;
  16. const int enableZ = 62;
  17.  
  18. const int MOTOR_STEP_PINA = 26;
  19. const int MOTOR_DIRECTION_PINA = 28;
  20. const int enableA = 24;
  21.  
  22. String dataIn = " ";
  23.  
  24. double angulosX[10];
  25. double angulosY[10];
  26. double angulosZ[10];
  27. double angulosA[10];
  28.  
  29. int countX;
  30. int countY;
  31. int countZ;
  32. int countA;
  33.  
  34. int x;
  35. int y;
  36. int z;
  37. int a;
  38.  
  39.  
  40.  
  41.  
  42. SpeedyStepper stepperX;
  43. SpeedyStepper stepperY;
  44. SpeedyStepper stepperZ;
  45. SpeedyStepper stepperA;
  46.  
  47. void setup() {
  48.  
  49.   stepperX.connectToPins(MOTOR_STEP_PINX, MOTOR_DIRECTION_PINX);
  50.   stepperY.connectToPins(MOTOR_STEP_PINY, MOTOR_DIRECTION_PINY);
  51.   stepperZ.connectToPins(MOTOR_STEP_PINZ, MOTOR_DIRECTION_PINZ);
  52.   stepperA.connectToPins(MOTOR_STEP_PINA, MOTOR_DIRECTION_PINA);
  53.  
  54.   Serial.begin(9600);
  55.   pinMode(enableX, OUTPUT);
  56.   digitalWrite(enableX, LOW);
  57.   pinMode(enableY, OUTPUT);
  58.   digitalWrite(enableY, LOW);
  59.   pinMode(enableZ, OUTPUT);
  60.   digitalWrite(enableZ, LOW);
  61.   pinMode(enableA, OUTPUT);
  62.   digitalWrite(enableA, LOW);
  63. }
  64.  
  65.  
  66. void loop() {
  67.  
  68.     Serial.println(angulosX[0]);
  69.     Serial.println(angulosY[0]);
  70.     Serial.println(angulosZ[0]);
  71.     Serial.println(angulosA[0]);
  72.  
  73.  
  74.     if (Serial.available() > 0) {
  75.       dataIn = Serial.readStringUntil('\n');
  76.  
  77.          if (dataIn.startsWith("X"))
  78.       {
  79.        
  80.         angulosX[countX] = dataIn.substring(1, dataIn.length()).toDouble();
  81.         countX += 1;
  82.         //Serial.println(count);
  83.       }
  84.  
  85.  
  86.          if (dataIn.startsWith("Y"))
  87.       {
  88.        
  89.         angulosY[countY] = dataIn.substring(1, dataIn.length()).toDouble();
  90.         countY += 1;
  91.         //Serial.println(count);
  92.       }
  93.  
  94.  
  95.          if (dataIn.startsWith("Z"))
  96.       {
  97.         angulosZ[countZ] = dataIn.substring(1, dataIn.length()).toDouble();
  98.         countZ += 1;
  99.         //Serial.println(count);
  100.       }
  101.  
  102.         if (dataIn.startsWith("A"))
  103.       {
  104.        
  105.         angulosA[countA] = dataIn.substring(1, dataIn.length()).toDouble();
  106.         countA += 1;
  107.         //Serial.println(count);
  108.       }
  109.  
  110.    
  111.  
  112.      
  113.  
  114.           if (dataIn.startsWith("J"))
  115.       {
  116.          for (int i = 0; i <= 10; i++) {
  117.          stepperX.setSpeedInStepsPerSecond(100*10);
  118.          stepperX.setAccelerationInStepsPerSecondPerSecond(100*10);  
  119.          stepperX.setupMoveInSteps(angulosX[i]*85.3);
  120.          
  121.          stepperY.setSpeedInStepsPerSecond(100*10);
  122.          stepperY.setAccelerationInStepsPerSecondPerSecond(100*10);  
  123.          stepperY.setupMoveInSteps(angulosY[i]*85.3);
  124.  
  125.          stepperZ.setSpeedInStepsPerSecond(100*10);
  126.          stepperZ.setAccelerationInStepsPerSecondPerSecond(100*10);  
  127.          stepperZ.setupMoveInSteps(angulosZ[i]*85.3);
  128.  
  129.          stepperA.setSpeedInStepsPerSecond(100*10);
  130.          stepperA.setAccelerationInStepsPerSecondPerSecond(100*10);  
  131.          stepperA.setupMoveInSteps(angulosA[i]*85.3);
  132.          
  133.        while((!stepperX.motionComplete()) || (!stepperY.motionComplete()) || (!stepperZ.motionComplete()) || (!stepperA.motionComplete()))  
  134.      {
  135.         stepperX.processMovement();
  136.         stepperY.processMovement();
  137.         stepperZ.processMovement();
  138.         stepperA.processMovement();
  139.      }
  140.      
  141.         y += 1;
  142.         x += 1;
  143.         z += 1;
  144.         a += 1;
  145.     }
  146.       }
  147.    
  148.     }
  149. }
  150.  
  151. python code:
  152.  
  153. import linecache
  154. import time  
  155. import serial
  156.  
  157. serialcomm = serial.Serial('COM12', 9600)
  158.  
  159. serialcomm.timeout = 1
  160.  
  161. time.sleep(1)
  162.  
  163.  
  164. f = open("commands.txt", "r")
  165.  
  166. time.sleep(0.2)
  167.  
  168. count = 1
  169.  
  170. while True:
  171.     print(1)
  172.     commandsFile = linecache.getline('commands.txt',count)
  173.     file = commandsFile.split(" ")
  174.  
  175.     for x in file:
  176.         print(x)
  177.         serialcomm.write((x + '\n').encode())
  178.         time.sleep(0.5)
  179.         #serialcomm.flush()
  180.     else:
  181.         count += 1
  182.         print(count)
  183.  
  184.  
  185.