Advertisement
Guest User

Untitled

a guest
Dec 16th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | Source Code | 0 0
  1. //unipolar driver
  2. #define PHASE1 10
  3. #define PHASE2 9
  4. #define PHASE3 6
  5. #define PHASE4 5
  6.  
  7. //switches assign
  8. #define FWDx4  2
  9. #define FWDx8  3
  10. #define FWDx16 4
  11. #define REVx4  11
  12. #define REVx8  12
  13. #define REVx16 13
  14.  
  15. //command move speed scale
  16. #define COMMAND_SPEED_SCALE 1.0
  17.  
  18.  
  19.  
  20. int pins[4] = {PHASE1, PHASE2, PHASE3, PHASE4};
  21. int foward[4][4] =
  22. {
  23.   {HIGH, HIGH, LOW, LOW},
  24.   {LOW, HIGH, HIGH, LOW},
  25.   {LOW, LOW, HIGH, HIGH},
  26.   {HIGH, LOW, LOW, HIGH},
  27. };
  28. /*
  29.   int reverse[4][4] =
  30.   {
  31.   {LOW, LOW, HIGH, HIGH},
  32.   {LOW, HIGH, HIGH, LOW},
  33.   {HIGH, HIGH, LOW, LOW},
  34.   {HIGH, LOW, LOW, HIGH},
  35.   };
  36. */
  37.  
  38. //14cycle/1secでちょうどよい速度になるはず
  39.  
  40. void setup() {
  41.   // put your setup code here, to run once:
  42.   Serial.begin(9600);
  43.   Serial.print("+00:00:00#");
  44.   pinMode(PHASE1, OUTPUT);
  45.   pinMode(PHASE2, OUTPUT);
  46.   pinMode(PHASE3, OUTPUT);
  47.   pinMode(PHASE4, OUTPUT);
  48.  
  49.   pinMode(FWDx4, INPUT_PULLUP);
  50.   pinMode(FWDx8, INPUT_PULLUP);
  51.   pinMode(FWDx16, INPUT_PULLUP);
  52.  
  53.   pinMode(REVx4, INPUT_PULLUP);
  54.   pinMode(REVx8, INPUT_PULLUP);
  55.   pinMode(REVx16, INPUT_PULLUP);
  56. }
  57.  
  58. bool commandMove = false;
  59. float commandSpeedScale = 1.0;
  60. void procLX200command();
  61. String command = "";
  62.  
  63.  
  64. void loop() {
  65.   // put your main code here, to run repeatedly:
  66.   unsigned long t = millis();
  67.   unsigned long count = 0;
  68.  
  69.   bool reverse_flag = false;
  70.   float speed_scale = 1.0;
  71.   while (Serial.available() > 0)
  72.   {
  73.     char c = Serial.read();
  74.     if (c == -1)break;
  75.     if (c == 10)break;
  76.     command += char(c);
  77.     //    Serial.print(char(c));
  78.     if (/*c == 0x06 || */
  79.       c == '#' || c == '\n')
  80.     {
  81.       //      if(c == 0x06)Serial.print("ACK ");
  82.       //      else Serial.print(command + String(" "));
  83.       procLX200command();
  84.       command = "";
  85.       break;
  86.     }
  87.   }
  88.  
  89.  
  90.   //ボタン入力で正転・逆転、速度変更の係数を変える
  91.   if (digitalRead(FWDx4) == LOW)
  92.   {
  93.     commandMove = false;
  94.     speed_scale = 4.0;
  95.   }
  96.   if (digitalRead(FWDx8) == LOW)
  97.   {
  98.     commandMove = false;
  99.     speed_scale = 8.0;
  100.   }
  101.   if (digitalRead(FWDx16) == LOW)
  102.   {
  103.     commandMove = false;
  104.     speed_scale = 16.0;
  105.   }
  106.   if (digitalRead(REVx4) == LOW)
  107.   {
  108.     commandMove = false;
  109.     speed_scale = 4.0;
  110.     reverse_flag = true;
  111.   }
  112.   if (digitalRead(REVx8) == LOW)
  113.   {
  114.     commandMove = false;
  115.     speed_scale = 8.0;
  116.     reverse_flag = true;
  117.   }
  118.   if (digitalRead(REVx16) == LOW)
  119.   {
  120.     commandMove = false;
  121.     speed_scale = 16.0;
  122.     reverse_flag = true;
  123.   }
  124.  
  125.   if (commandMove)
  126.   {
  127.     speed_scale = commandSpeedScale;
  128.     if (speed_scale < 0)
  129.     {
  130.       reverse_flag = true;
  131.       speed_scale = -speed_scale;
  132.     }
  133.   }
  134.  
  135.   count = t / (1000 / 14.0 / 1.0027855 / speed_scale);
  136.   count %= 4;
  137.   if (reverse_flag)count = 3 - count;
  138.  
  139.   digitalWrite(pins[0], foward[count][0]);
  140.   digitalWrite(pins[1], foward[count][1]);
  141.   digitalWrite(pins[2], foward[count][2]);
  142.   digitalWrite(pins[3], foward[count][3]);
  143.  
  144.   delay(1);
  145.  
  146. }
  147.  
  148. void procLX200command()
  149. {
  150.   //LX200互換コマンド処理
  151.   static bool longFormat = true;
  152.   command.replace("\n", "");
  153.   String ret = "";
  154.  
  155.   if(command.equals("#"))
  156.   {  ret = "00:00:00#";
  157.  
  158.   }else if (command.charAt(0) == 0x06)
  159.   {
  160.     ret = "P";
  161.   }
  162.   else if (command.equals(":U#"))
  163.   {
  164.     longFormat = !longFormat;
  165.   }
  166.   else if (command.equals(":GVP#"))
  167.   {
  168.     //機器の名前
  169.     ret = "PENTAX#";
  170.   }
  171.   else if (command.equals(":GVN#"))
  172.   {
  173.     ret = "01.0#";
  174.   }
  175.   else if (command.equals(":GR#") || command.equals(":GS#"))
  176.   {
  177.     if (longFormat)
  178.       ret = "00:00:00#";
  179.     else
  180.       ret = "00:00.0#";
  181.   }
  182.   else if (command.equals(":GD#") || command.equals(":GA#"))
  183.   {
  184.     if (longFormat)
  185.     {
  186.       ret = "+00:00:00#";
  187.     }
  188.     else
  189.     {
  190.       ret = "+00:00#";
  191.     }
  192.   }
  193.   else if (command.equals( ":Me#"))
  194.   {
  195.     commandMove = true;
  196.     commandSpeedScale = 1.0 - COMMAND_SPEED_SCALE;
  197.   }
  198.   else if (command.equals(":Mw#"))
  199.   {
  200.     commandMove = true;
  201.     commandSpeedScale = 1.0 + COMMAND_SPEED_SCALE;
  202.   }
  203.   else if (command.equals(":Qe#") || command.equals(":Qw#") || command.equals(":Q#"))
  204.   {
  205.     commandMove = false;
  206.   }
  207.   else if (command.length() > 2 && command[0] == ':' && command[1] == 'S')
  208.   {
  209.     ret = "1";
  210.   }
  211.  
  212.   command = "";
  213.  
  214.   if (ret.length() != 0)
  215.   {
  216.     Serial.println(ret);
  217.     Serial.flush();
  218.   }
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement