前回では、mono(c#)でGPIOから矩形波を読み込んでみました。
今回は、mono(C#)とPythonで比較してみます。
Arduinoから矩形波を出力しラズパイのGPIOで読み込みます。
Pin.No | 名称1 | 用途 | 利用 | IN/OUT | Arduino Pin | 周波数 |
16 | GPIO23 | GPIO23 | パルス1 | IN | 13 | 5Hz |
18 | GPIO24 | GPIO24 | パルス2 | IN | 12 | 50Hz |
20 | GND | GND | GND | GND | ||
22 | GPIO25 | GPIO25 | パルス3 | IN | 11 | 500Hz |
Pythonのバージョンは、3.5.0 monoはWindows10のVisualStudio Communicationで、.Net4.5.2コンパイルしてみます。 ラズパイは、800MHzで動かしています。
GPIO23,24,25を連続で1秒間に何回くらいループするかを見てみます。
Python3.5.0 Wiribpi2 |
Python3.5.0 Rpi.GPIO |
Python2.7.10 Wiringpi2 |
Python2.7.10 Rpi.GPIO |
mono .Net4.5.2 |
62,650回 | 58,500回 | 104,000回 | 94,750回 | 88,150回 |
Python3よりはmonoの方が4割くらい早いですね。でもPython2にはかないません。
もう少し、monoが早いかと思っていましたがそれほどではありません。
以下 サンプルプログラムです。
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 |
using System; using System.Runtime.InteropServices; using System.ComponentModel; using System.Threading; using Raspberry.IO.GeneralPurpose; namespace RasPiMonoTest { class Program { static void Main(string[] args) { var g23 = ConnectorPin.P1Pin16.ToProcessor(); var g24 = ConnectorPin.P1Pin18.ToProcessor(); var g25 = ConnectorPin.P1Pin22.ToProcessor(); var drv = GpioConnectionSettings.DefaultDriver; drv.Allocate(g23, PinDirection.Input); drv.Allocate(g24, PinDirection.Input); drv.Allocate(g25, PinDirection.Input); bool bP1o = false, bP2o = false, bP3o = false; while (true) { DateTime dt0 = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.000000")); DateTime dt1 = dt0.AddSeconds(1); int cnt = 0; int cntP1 = 0, cntP2 = 0, cntP3 = 0; while (DateTime.Now <= dt1) { var bP1 = drv.Read(g23); //Thread.Sleep(0); var bP2 = drv.Read(g24); //Thread.Sleep(0); var bP3 = drv.Read(g25); //Thread.Sleep(0); if (bP1o != bP1) { cntP1 += 1; bP1o = bP1; } if (bP2o != bP2) { cntP2 += 1; bP2o = bP2; } if (bP3o != bP3) { cntP3 += 1; bP3o = bP3; } cnt += 1; } Console.WriteLine(cnt.ToString() + ", " + cntP1.ToString() + ", " + cntP2.ToString() + "," + cntP3.ToString()); } } } } |
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 |
#coding: UTF-8 import pyximport; pyximport.install() import time import math import wiringpi2 as wiringpi #設定取得 wiringpi.wiringPiSetupGpio() gpio_p1 = 23 gpio_p2 = 24 gpio_p3 = 25 wiringpi.pinMode(23, 0) wiringpi.pinMode(24, 0) wiringpi.pinMode(25, 0) # パルス取得ルーチン def StartPulseReader(): bP1o = 0 #GPIO状態1 bP2o = 0 #GPIO状態2 bP3o = 0 #GPIO状態3 #指定秒数でGPIOの変化を取得 while 1: ftime_e = math.floor(time.time()) + 1 cnt = 0 cntP1 = 0 cntP2 = 0 cntP3 = 0 while time.time() <= ftime_e: bP1 = wiringpi.digitalRead(gpio_p1) bP2 = wiringpi.digitalRead(gpio_p2) bP3 = wiringpi.digitalRead(gpio_p3) if bP1o != bP1: cntP1 += 1 bP1o = bP1 #time.sleep(0) if bP2o != bP2: cntP2 += 1 bP2o = bP2 #time.sleep(0) if bP3o != bP3: cntP3 += 1 bP3o = bP3 #time.sleep(0) cnt += 1 print(str(cnt), str(cntP1), str(cntP2), str(cntP3)) if __name__ == '__main__': StartPulseReader() |
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 |
#coding: UTF-8 import time import math import RPi.GPIO as GPIO #設定取得 GPIO.setmode(GPIO.BCM) GPIO.cleanup() GPIO.setup(23, GPIO.IN) #GPIO=25を利用 GPIO.setup(24, GPIO.IN) #GPIO=25を利用 GPIO.setup(25, GPIO.IN) #GPIO=25を利用 # パルス取得ルーチン def StartPulseReader(): bP1o = 0 #GPIO状態1 bP2o = 0 #GPIO状態2 bP3o = 0 #GPIO状態3 #指定秒数でGPIOの変化を取得 while 1: ftime_e = math.floor(time.time()) + 1 cnt = 0 cntP1 = 0 cntP2 = 0 cntP3 = 0 while time.time() <= ftime_e: bP1 = GPIO.input(23) #パルス1 bP2 = GPIO.input(24) #パルス2 bP3 = GPIO.input(25) #パルス3 if bP1o != bP1: cntP1 += 1 bP1o = bP1 #time.sleep(0) if bP2o != bP2: cntP2 += 1 bP2o = bP2 #time.sleep(0) if bP3o != bP3: cntP3 += 1 bP3o = bP3 #time.sleep(0) cnt += 1 # time.sleep(0.0001) print(str(cnt), str(cntP1), str(cntP2), str(cntP3)) if __name__ == '__main__': StartPulseReader() |
第 1回 Raspberry Pi 2 の初期設定をしてみる-1
第 2回 Raspberry Pi 2 の初期設定をしてみる-2
第 3回 Raspberry Pi 2 の初期設定をしてみる-3
第 4回 Raspberry Pi 2 にWifiの設定をする
第 5回 Raspberry Pi 2 不要サービス停止
第 6回 Raspberry Pi 2 RAMディスクの設定
第 7回 Raspberry Pi 2 コンソールの設定
第 8回 Raspberry Pi 2 にウォッチドッグタイマーを導入する
第 9回 Raspberry Pi 2 でPythonの設定をする。
第10回 Raspberry Pi 2 でSambaの設定をする。
第11回 Windows10でPythonの開発環境を構築する。
第12回 Raspberry Pi 2のPython3でGPIOのパルスを取得する。
第13回 Raspberry Pi 2のPythonでGPIOを調べてみる。
第14回 Raspberry Pi 2のPythonでGPIOの割り込み処理をする。
第15回 Raspberry Pi 2のPython3でI2Cを設定する。
第16回 Raspberry Pi 2のPython3で加速度センサーを動かす。
第17回 Raspberry Pi 2にmonoをインストールしてC#を利用する。
第18回 Raspberry Pi 2にgoをインストールしてみる。
第19回 Raspberry Pi 2のGPIOをC#(mono)でパルス取得する。
第20回 Raspberry Pi 2のGPIO読み込みを、mono(C#)とPythonで比較する。