リストの通りに自動運転させる

RasPiRoboクラス



RasPiRoboクラス
  class BasicPiRobo
  {
    public const int straight = 1;
    public const int rotate = 2;
    public const int turnR = 3;
    public const int turnL = 4;
    public const int turnP = 5;

    String fileName="myway.txt";
    BasicWheel robo = new BasicWheel ();
    Buttons btn = new Buttons ();
    LEDs led = new LEDs ();
    int [] schedule;
    public BasicPiRobo(String fileName)
    {
      this.fileName = fileName;
      List list = new List ();
      getSchedule (list);
      schedule = new int[list.Count];
      for (int i = 0; i < list.Count; i++)
        schedule [i] = list [i];
    }
    public BasicPiRobo(int[]schedule)
    {
      for (int i = 0; i < schedule.Length; i++)
        this.schedule [i] = schedule [i];
    }

    public int waitButton()
    {
      int n;
      while (true) {
        if (btn.waitPress (out n) != -1) {
          Console.WriteLine (n);
          led.Write (n, true);
          Thread.Sleep (100);
          led.Write (n, false);
          break;
        }
        Thread.Sleep (50);
      }
      return n;
    }
    public void go()
    {
      int Rt=1, Lt=1;
      int speed = 1;
      for (int i = 0; i < schedule.Length; i += 2) {
        switch (schedule [i]) {
          case straight:
              robo.move (schedule [i + 1], speed);
              break;
          case rotate:
              robo.rotete (schedule [i + 1], speed);
              break;
          case turnR:
              Rt = schedule [i + 1];
              break;
          case turnL:
              Lt = schedule [i + 1];
              break;
          case turnP:
              robo.turn (Rt, Lt, schedule [i + 1]);
              break;
        }
        Thread.Sleep (100);
      }
    }
    public void getSchedule( List<int> list)
    {
      try
      {
        using (StreamReader sr = new StreamReader (fileName))
        {
          String line;
          while ((line = sr.ReadLine ()) != null) {
            String[] cmd = line.Split (',');
            foreach (String str in cmd) {
              switch (str) {
                case "straight":
                     list.Add (straight);
                     break;
                case "rotate":
                     list.Add (rotate);
                     break;
                case "turnR":
                     list.Add (turnR);
                     break;
                case "turnL":
                     list.Add (turnL);
                     break;
                case "turnP":
                     list.Add (turnP);
                     break;
                default:
                     int x;
                     if (str.Length == 0)
                       break;
                     if (!int.TryParse (str, out x)) {
                       Console.WriteLine ("スペルミス"+str.Length);
                     }
                     list.Add (x);
                     break;
              } // switch
            } // foreach
          } //while
        } // using
      } // try
      catch(Exception){
        Console.WriteLine("File not found");
      } // catch
    } // method
  } // class
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101
102
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 


リスト通りの動作メインプログラム



リスト通りの動作
        static void Main(string[] args)
        {
            String fileName = "myway.txt";
            BasicPiRobo robo = new BasicPiRobo (fileName);
            while (true) {
                robo.waitButton ();
                robo.go ();
                Thread.Sleep (1000);
            }
        }
    }


デフォルトのファイルの位置はログインディレクトリですから、そこに以下のファイルを作っておきます。

動作リスト(myway.txt)
straight, 20
rotate, 90
straight, 30
rotate, 90
straight, 20
rotate, 90
straight, 30
rotate, 90