動くかどうかやってみよう

取りあえずモーターを動かす

 パルスを送ると回ることを実験してみます。これをやらないと、どこが間違っているのかが分からなくなります。必ずできるところから作っていきます。

メインプログラムだけで動かしてみる
    static void Main(string[] args)
    {
      GPIOMem motorOn = new GPIOMem (GPIOPins.V2Plus_Pin_P1_29);
      motorOn.PinDirection = GPIODirection.Out;
      motorOn.Write (true);

      GPIOMem clockR = new GPIOMem (GPIOPins.V2Plus_Pin_P1_33);
      clockR.PinDirection = GPIODirection.Out;
      GPIOMem CWCCWR = new GPIOMem (GPIOPins.V2Plus_Pin_P1_31);
      CWCCWR.PinDirection = GPIODirection.Out;
      CWCCWR.Write (true);

      int speed = 200000;
      while(true)
      {
        clockR.Write (true);
        for (int i = 0; i < speed; i++)  ;
        clockR.Write (false);
        for (int i = 0; i < speed; i++) ;
      }
    }
 1 
 2 
 3 モーターの電源オンオブジェクト
 4 出力に指定
 5 モーターの電源オン
 6 
 7 右のモーターのクロック
 8 
 9 方向の指定
10 
11 
12 
13 回るスピード指定。手抜き
14 
15 
16 パルスを送ると回る
17 
18 
19 
20 
21 


オブジェクト指向で書き直す

 オブジェクト指向の良いところは、右の車輪を制御するクラスを作れば、同様なオブジェクトを作れば左の車輪を制御できることになり、プログラムが簡単になります。ここでは、右の車輪を動かすクラスを作ってみます。

右の車輪を動かす
   class SteppingMotor
   {
      public GPIOMem clockR;
      public GPIOMem cwR;
      private GPIOMem motor;
      private bool direction=false;
      public int speed { get; set;}
      public SteppingMotor(int speed)
     {
        motor = new GPIOMem (GPIOPins.V2Plus_Pin_P1_29);
        motor.PinDirection = GPIODirection.Out;
        clockR = new GPIOMem (GPIOPins.V2Plus_Pin_P1_33);
        cwR = new GPIOMem(GPIOPins.V2Plus_Pin_P1_31);
        clockR.PinDirection = GPIODirection.Out;
        cwR.PinDirection = GPIODirection.Out;
        clockR.Write(false);
        direction=true;
        cwR.Write(direction);
      }
      public void Move(int time)
      {
        for (int i = 0; i < time; i++) 
        {
          clockR.Write (true);
          for (int j = 0; j < speed; j++) ;
          clockR.Write (false);
          for (int j = 0; j < speed; j++) ;
        }
      }
      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 


メインプログラムを作る

 speedについてはまだ考える必要があるので、今作っているクラスは不完全ですが、取りあえず前進させます。

メインプログラム
    static void Main(string[] args)
    {
      SteppingMotor robo = new SteppingMotor (200000);
      robo.MotorOn ();
      robo.Move (10000);
    }
 1 
 2 
 3 取りあえずのスピードで
 4 電源を入れる
 5 10000パルス送る
 6 


直進させる

 左の車輪を回転方向を逆にするだけで後を全て同じ設定にすると直進します。

両輪を同時に動かすクラス
    class SteppingMotor
    {
      public GPIOMem clockR, clockL;
      public GPIOMem cwR, cwL;
      private GPIOMem motor;
      private bool direction=false;
      public int speed { get; set;}
      public SteppingMotor(int speed)
      {
        motor = new GPIOMem (GPIOPins.V2Plus_Pin_P1_29);
        motor.PinDirection = GPIODirection.Out;
        clockR = new GPIOMem (GPIOPins.V2Plus_Pin_P1_33);
        clockL = new GPIOMem (GPIOPins.V2Plus_Pin_P1_32);
        cwR = new GPIOMem(GPIOPins.V2Plus_Pin_P1_31);
        cwL = new GPIOMem(GPIOPins.V2Plus_Pin_P1_36);
        clockR.PinDirection = GPIODirection.Out;
        clockL.PinDirection = GPIODirection.Out;
        cwR.PinDirection = GPIODirection.Out;
        cwL.PinDirection = GPIODirection.Out;
        clockR.Write(false);
        clockL.Write(false);
        direction=true;
        cwR.Write(direction);
        cwL.Write(!direction);
      }
      public void Move(int time)
      {
        for (int i = 0; i < time; i++) 
        {
          clockR.Write (true);
          clockL.Write (true);
          for (int j = 0; j < speed; j++) ;
          clockR.Write (false);
          clockL.Write (false);
          for (int j = 0; j < speed; j++) ;
        }
      }
      public void MotorOn()
      {
        motor.Write (true);
      }
      public void MotorOff()
      {
        motor.Write (false);
      }
    }
  }


両輪を同時に動かす

今までは、取りあえず動くクラスを使ってモーターを動かす雰囲気を楽しんでもらいました。これからは、左右のモーターのスピードを設定し、同時に開始し、終了するようなクラスにします。同時に開始・終了するにはクラス変数 StopFlag を用います。

SteppingMotorクラス
  class SteppingMotor
  {
    public GPIOMem clock;
    public GPIOMem cw;
    static GPIOMem motor;
    private bool direction=false;
    public int speed{ get; set; }
    static public bool StopFlag{ get; set; }
    protected int sleepTime;
    public SteppingMotor(GPIOPins clockPin, GPIOPins cwPin, bool dir, int speed)
    {
      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);
      this.speed=speed;
    }

    public void Start(object time)
    {
      sleepTime = (int)time;
      Thread thread = new Thread (new ThreadStart (Move));
      StopFlag = false;
      thread.Start ();
    }

    public void SetSpeed(int speed)
    {
      this.speed=speed;
    }
    public void Move()
    {
      StopFlag=false;
      for (int i = 0; i < speed; 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 同期させるためのStopFlag
 9 パルス間の間隔時間
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 スレッドを作りMoveメソッドを動作させる
28 
29 
30 
31 
32 
33 
34 
35 
36 指定されたspeedカウントのパルスを作る
37 
38 
39 
40 停止命令が来たら即刻停止
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 


メインプログラム
        static void Main(string[] args)
        {
            bool forward = true;
            bool backward = false;
            int t = 2000;
            int Rt = 3;
            int Lt = 3;
            SteppingMotor R = new SteppingMotor (GPIOPins.V2Plus_Pin_P1_33,
                GPIOPins.V2Plus_Pin_P1_31, forward, t/Rt);
            SteppingMotor L = new SteppingMotor (GPIOPins.V2Plus_Pin_P1_32,
                GPIOPins.V2Plus_Pin_P1_36, backward, t/Lt);
            R.MotorOn ();
            R.Start (Rt);
            L.Start (Lt);
            while (!SteppingMotor.StopFlag)
            Console.WriteLine("end");
        }
      }
 1 
 2 
 3 
 4 
 5 走行する距離
 6 数字が大きいと遅くなる
 7 
 8 
 9 t/Rtは必要なパルス数
10 
11 t/Ltは必要なパルス数
12 モーターOn
13 右モーターを動かす
14 左モーターを動かす
15 モーターが停止するまで待つ
16 
17 
18