Cosmo-ZをPythonで遠隔操作してグラフ描画
Cosmo-ZをPythonで遠隔操作して、波形のグラフを描くことができるようになりました。
初めてのプログラムですが、すぐにこんなことができるって、Pythonすごい。
# encoding:utf-8
import sys
import argparse
import datetime
# 定数の設定
convert_xy=1
server="192.168.2.100"
points = int(sys.argv[1])
freq = 100
# 引数の解釈
if(len(sys.argv) <= 1):
print "Usage: python cszcli.py capture_length [freq=FREQUENCY] [server=SERVER_NAME]"
sys.exit()
# NumPyライブラリのインポート(遅い)
print "import NumPy and MatPlotLib"
import numpy as np
from matplotlib import pyplot
print "done"
# XMLRPCのインポート
print "import XMLRPC lib"
try:
import xmlrpclib as xmlrpc_client
except ImportError:
import xmlrpc.client as xmlrpc_client
print "done"
# サーバに接続
print "Connect serer.."
cosmoz = xmlrpc_client.ServerProxy('http://' + server + ':21068')
print "done"
# Cosmo-Zの操作
cosmoz.open()
adc_vrange = cosmoz.adc_vrange()
adc_info = cosmoz.adc_info()
cosmoz.adc_setfreq(freq)
while(1):
cosmoz.capture(0xff,points,"auto")
print "capture done."
print "Data transfer start"
# データの取得
time = np.array(map(int,cosmoz.getdata(0).split(",")))
ch1 = np.array(map(int,cosmoz.getdata(1).split(",")))
ch2 = np.array(map(int,cosmoz.getdata(2).split(",")))
ch3 = np.array(map(int,cosmoz.getdata(3).split(",")))
ch4 = np.array(map(int,cosmoz.getdata(4).split(",")))
ch5 = np.array(map(int,cosmoz.getdata(5).split(",")))
ch6 = np.array(map(int,cosmoz.getdata(6).split(",")))
ch7 = np.array(map(int,cosmoz.getdata(7).split(",")))
ch8 = np.array(map(int,cosmoz.getdata(8).split(",")))
print "Data transfer end"
if(convert_xy):
# 時間へ変換
adc_freq = adc_info["sampling_freq_mhz"];
if(adc_freq != 0):
samp_period = 1 / adc_freq
else:
samp_period = 1;
time = time * samp_period
# 電圧へ変換
vmax = adc_vrange["max_input_voltage"]
vmin = adc_vrange["min_input_voltage"]
resolution = float(adc_vrange["adc_resolution"])
ch1 = ch1 / resolution * (vmax - vmin) + vmin
ch2 = ch2 / resolution * (vmax - vmin) + vmin
ch3 = ch3 / resolution * (vmax - vmin) + vmin
ch4 = ch4 / resolution * (vmax - vmin) + vmin
ch5 = ch5 / resolution * (vmax - vmin) + vmin
ch6 = ch6 / resolution * (vmax - vmin) + vmin
ch7 = ch7 / resolution * (vmax - vmin) + vmin
ch8 = ch8 / resolution * (vmax - vmin) + vmin
else:
print time
# 波形の描画
print "Plot start"
pyplot.cla()
pyplot.clf()
# pyplot.figure()
pyplot.title('Cosmo-Z ' + " captuer " + str(points) + 'points. ' + str(datetime.datetime.now()))
pyplot.plot(time, ch1, color='brown', label='ch1')
pyplot.plot(time, ch2, color='red', label='ch2')
pyplot.plot(time, ch3, color='orange', label='ch3')
pyplot.plot(time, ch4, color='yellow', label='ch4')
pyplot.plot(time, ch5, color='green', label='ch5')
pyplot.plot(time, ch6, color='blue', label='ch6')
pyplot.plot(time, ch7, color='fuchsia', label='ch7')
pyplot.plot(time, ch8, color='gray', label='ch8')
pyplot.text(0,max(max(ch1),max(ch2),max(ch3),max(ch4),max(ch5),max(ch6),max(ch7),max(ch8)), "Sampling freq " + str(freq) + "MHz",fontsize=11 , color="skyblue")
if(convert_xy):
pyplot.ylabel("Voltage [V]")
pyplot.xlabel("Time [us]")
else:
pyplot.ylabel("ADC Value [LSB]")
pyplot.xlabel("Sampling point")
pyplot.legend()
pyplot.pause(.01)
raw_input('Hit any key to capture next wave')
Cosmo-Zのサーバからは2044,2045,2057・・・みたいなテキストで計測データが送られてくるので、それをカンマで区切って配列に入れて、配列をmapでintに変換して、それをNumPyの配列に変換しています。
そして、NumPyの配列の各要素にADC値→電圧に変換するための係数をかけて、グラフにして表示しています。
上の波形はNaIシンチレータから受信したパルスですが、pyplotはマウスで拡大できて便利です。
実行時のコンソール。
サーバ側のCosmo-ZのLinuxイメージは明日にはでもリリースしたいと思います。
| 固定リンク





コメント