正しい距離移動するようにする

正しいパルス数にする

車輪は直径約4.75cmでステッピングモーターは400パルスで一回転する。 これから、xcm移動するには何パルス必要かを計算する。

改良SteppingMotorクラス
    class SteppingMotor
    {
      public GPIOMem clock;
      public GPIOMem cw;
      static GPIOMem motor;
      private bool direction=false;
      private int numberOfTimes;
      static public bool StopFlag{ get; set; }
      static public int TotalTime{ get; set; }
      protected int sleepTime;
      public const double oneStep=4.75*Math.PI/400;
      public SteppingMotor(GPIOPins clockPin, GPIOPins cwPin, bool dir)
      {
        if(motor==null) motor = new GPIOMem (GPIOPins.V2Plus_Pin_P1_29);
        motor.PinDirection = GPIODirection.Out;
        clock = new GPIOMem (clockPin);
        cw = new GPIOMem(cwPin);
        clock.PinDirection = GPIODirection.Out;
        cw.PinDirection = GPIODirection.Out;
        clock.Write(false);
        direction=dir;
        cw.Write(direction);
      }

      public void Start(object sleepTime)
      {
        this.sleepTime = (int)sleepTime;
        if(this.sleepTime>=0) cw.Write(direction);
        else cw.Write(!direction);
        numberOfTimes = (int)(TotalTime / (int)sleepTime + 0.5);
        double length = oneStep * numberOfTimes;
        Console.WriteLine ("Pulses ={0} Length = {1,6:f2}cm", numberOfTimes, length);
        Thread thread = new Thread (new ThreadStart (Move));
        StopFlag = false;
        thread.Start ();
      }

      public void Move()
      {
        StopFlag=false;
        for (int i = 0; i < numberOfTimes; i++) {
          if (StopFlag) {
            return;
          }
          clock.Write (true);
          for (int j = 0; j < 2000; j++) ;
          clock.Write (false);
          Thread.Sleep (sleepTime);
        }
        StopFlag = true;
      }

      public void MotorOn()
      {
        motor.Write (true);
      }
      public void MotorOff()
      {
        motor.Write (false);
      }
    }
 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 


直進させる

どのくらいの時間は知っているかをモニターしてみるので、ストップウォッチクラスを用います。そのためのusingが必要です、

using System.Diagnostics; // stopwatch

直進させるさせるメインプログラム
      static void Main(string[] args)
      {
        bool forward = true;
        SteppingMotor R = new SteppingMotor (GPIOPins.V2Plus_Pin_P1_33,
                          GPIOPins.V2Plus_Pin_P1_31, forward);
        SteppingMotor L = new SteppingMotor (GPIOPins.V2Plus_Pin_P1_32,
                          GPIOPins.V2Plus_Pin_P1_36, !forward);
        double pathway = 40.0;
        int Rt = 5;
        int Lt = 5;
        SteppingMotor.TotalTime = Rt * (int)(pathway / SteppingMotor.oneStep + 0.5);
        Console.WriteLine("Straight ahead start {0}msec", SteppingMotor.TotalTime);

        R.MotorOn ();
        Stopwatch stopwatch = Stopwatch.StartNew ();
        R.Start (Rt);
        L.Start (Lt);
        while (!SteppingMotor.StopFlag);
        stopwatch.Stop ();
        long span = stopwatch.ElapsedTicks;
        Console.WriteLine("Straight end. Time={0,7:f3}sec", span/10e6);
      R.MotorOff();
    }
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 


課題

上記プログラムは後進できません。次のメインプログラムを用いて後進できるように修正しなさい。
前進して更新するプログラム
    static void Main(string[] args)
    {
        bool forward = true;
        SteppingMotor R = new SteppingMotor (GPIOPins.V2Plus_Pin_P1_33,
                          GPIOPins.V2Plus_Pin_P1_31, forward);
        SteppingMotor L = new SteppingMotor (GPIOPins.V2Plus_Pin_P1_32,
                          GPIOPins.V2Plus_Pin_P1_36, !forward);
        double pathway = 40.0;
        int Rt = 5;
        int Lt = 5;
        SteppingMotor.TotalTime = Rt * (int)(pathway / SteppingMotor.oneStep + 0.5);
        Console.WriteLine("Straight ahead start {0}msec", SteppingMotor.TotalTime);

        R.MotorOn ();
        Stopwatch stopwatch = Stopwatch.StartNew ();
        R.Start (Rt);
        L.Start (Lt);
        while (!SteppingMotor.StopFlag);
        stopwatch.Stop ();
        long span = stopwatch.ElapsedTicks;
        Console.WriteLine("Straight end. Time={0,7:f3}sec", span/10e6);
        R.MotorOff();
        Thread.Sleep(500);

        R.MotorOn ();
        R.Start (-Rt);
        L.Start (-Lt);
        stopwatch.Restart ();
        while (!SteppingMotor.StopFlag);
        stopwatch.Stop ();
        span = stopwatch.ElapsedTicks;
        Console.WriteLine("backword end. Time={0,7:f3}sec", span/10e6);
        R.MotorOff ();
    }
 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