Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. /*
  2. 1. mój program daje komendę unlock i jest ignorowane
  3.    if (digitalRead(home_switch_alt) == HIGH || (digitalRead(home_switch_alt) == LOW && movedAlt == 0))
  4. 2. są przesyłane na zmianę koordynaty alt i az, po wysłaniu program czeka na serial z arduino mówiący że
  5.    ruch się zakończył
  6. 3. pod koniec przelotu, kiedy alt będzie wynosiło mniej więcej 0, a przycisk na pinie 13 będzie wciśnięty,
  7.    program wie że przelot się zakończył i oś az jest resetowana do pozycji zerowej
  8. */
  9.  
  10. #include "AccelStepper.h"
  11.  
  12. AccelStepper stepperAz(1, 2, 3);
  13. AccelStepper stepperAlt(1, 8, 10);
  14.  
  15. const int enPinAlt = 11;
  16. const int enPinAz = 4;
  17.  
  18. #define home_switch_az 7
  19. #define home_switch_alt 13
  20.  
  21. long TravelAz;
  22. int move_az_finished=1;
  23.  
  24. long TravelAlt;
  25. int move_alt_finished = 1;
  26.  
  27. long initial_homing_az=-1;
  28. const int baud = 9600;
  29. //const int timeout = 10;
  30.  
  31. String command;
  32. // String commandStr;
  33. bool bAz, bAlt;
  34. float temp;
  35. char first, rec;
  36. bool locked = true;
  37.  
  38. int movedAlt = 0;
  39. float last = 0;
  40.  
  41. // todo not working moveto
  42. void park() {
  43. //              Serial.println(isAzParked());
  44.   while (isAzParked() == 0) {
  45.   //              Serial.println("MOVING DOWN");
  46.     stepperAz.moveTo(stepperAz.currentPosition() + 3);
  47.   }
  48.   while (isAltParked() == 0) {
  49.     stepperAlt.move(1);
  50.   }
  51. }
  52.  
  53. void setup() {
  54.   Serial.begin(baud);
  55.   //Serial.setTimeout(timeout);
  56.  
  57.   pinMode(home_switch_az, INPUT_PULLUP);
  58.   pinMode(home_switch_alt, INPUT_PULLUP);
  59.   pinMode(enPinAlt, OUTPUT);
  60.   digitalWrite(enPinAlt, LOW);
  61.   pinMode(enPinAz, OUTPUT);
  62.   digitalWrite(enPinAz, LOW);
  63.  
  64.   delay(5);  
  65.  
  66.   stepperAlt.setCurrentPosition(0);
  67.   stepperAlt.setMaxSpeed(7);      
  68.   stepperAlt.setAcceleration(10.0);
  69.    
  70.   stepperAz.setCurrentPosition(0);
  71.   stepperAz.setMaxSpeed(100.0);
  72.   stepperAz.setAcceleration(30.0);
  73. }
  74.  
  75. bool isParked() {
  76.   return (isAzParked() == true) && (isAltParked() == true);
  77. }
  78.  
  79. bool isAzParked() {
  80.   bool azParked;
  81.   for (int i = 0; i < 100; i++) {
  82.     if (digitalRead(home_switch_az) == HIGH) {
  83.       last += 0;
  84.     } else {
  85.       last += 1;
  86.     }
  87.   }
  88.  
  89.   if (last / 100 > 0.9) {
  90.     azParked = true;
  91.   } else {
  92.     azParked = false;
  93.   }
  94.   last = 0;
  95.  
  96.   return azParked;
  97. }
  98.  
  99. bool isAltParked() {
  100.   return digitalRead(home_switch_alt) == HIGH ? false : true;
  101. }
  102.  
  103. void loop() {
  104.   if (isParked()) {
  105.     ///              Serial.println("Both axes parked");
  106.   } else {
  107.     ///              Serial.println("Not parked");
  108.   }
  109.  
  110.   if(Serial.available() > 0) {
  111.     command = Serial.readStringUntil('\n');
  112.   //              Serial.println(command);
  113.     // command += rec;
  114.     // command = Serial.readString();
  115.  
  116.     if (command.equals("park")) {
  117.       park();
  118.     } else if (command.equals("unlock")) {
  119.       locked = false;
  120.     }
  121.  
  122.     if (locked == false && isAzParked() == false) {
  123.       locked = true;
  124.     }
  125.    
  126.     first = command.charAt(0);
  127.     command.remove(0, 1);
  128.     temp = command.toInt();
  129.    
  130.     if (first == 'z') {
  131.     //              Serial.println("az");
  132.     //              Serial.println(temp);
  133.       TravelAz = temp;
  134.       // TravelAz = map(temp, 0.0, 360.0, 0, 1200); // pozmieniaj te wartości
  135.       bAz = true;
  136.       bAlt = false;
  137.       move_az_finished = 0;
  138.       // if radiotelescope is up
  139.       if (digitalRead(home_switch_alt) == HIGH) {
  140.  
  141.       } else { // is parked
  142.  
  143.       }
  144.       stepperAz.moveTo(TravelAz);
  145.       delay(100);
  146.     //              Serial.println("move az");
  147.     } else if (first == 't') {
  148.     //              Serial.println("alt");
  149.     //              Serial.println(temp);
  150.       TravelAlt = temp;
  151.       // TravelAlt = map(temp, 0.0, 90.0, 0, 1200); // pozmieniaj te wartości
  152.       bAz = false;
  153.       bAlt = true;
  154.       move_alt_finished = 0;
  155.       // if radiotelescope is up
  156.     //   if (digitalRead(home_switch_alt) == HIGH || (digitalRead(home_switch_alt) == LOW && movedAlt == 0) || (digitalRead(home_switch_alt) == LOW && locked == false)) {
  157.         stepperAlt.moveTo(TravelAlt);
  158.         delay(100);
  159.       //              Serial.println("move alt");
  160.     //   } else {
  161.       //              Serial.println("didn't move");
  162.     //   }
  163.       movedAlt++;
  164.     }
  165.  
  166.     if (bAlt) {
  167.       // ruszanie osią alt
  168.       while (stepperAlt.distanceToGo() != 0) {
  169.         //              Serial.println(stepperAlt.distanceToGo());
  170.          stepperAlt.run();  // Move Stepper into position
  171.       }
  172.  
  173.       // If move is completed display message on Serial Monitor
  174.       if ((move_alt_finished == 0) && (stepperAlt.distanceToGo() == 0)) {
  175.         //              Serial.println("Alt motion complete!");
  176.           Serial.write("%");
  177.           move_alt_finished=1;
  178.           locked = true;
  179.       }
  180.     } else if (bAz) {
  181.       // ==============================
  182.       // ruszanie osią az
  183.       while ((stepperAz.distanceToGo() != 0)) {
  184.         //              Serial.println(stepperAz.distanceToGo());
  185.          stepperAz.run();
  186.       }
  187.  
  188.       if ((move_az_finished == 0) && (stepperAz.distanceToGo() == 0)) {
  189.         //              Serial.println("Az motion complete!");
  190.           Serial.write("!");
  191.           move_az_finished=1;
  192.       }
  193.     }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement