2017年10月24日火曜日

gnuplotで.pltファイルを読み込み自動出力するサンプル

output.txtからデータ出力するサンプルです.

$> file = 'output'
$> output = 1
$> load "plotter.plt"

  • グラフの大きさをmarginで指定する
  • 軸ラベルをつける
  • グリッドを表示する
  • 凡例を移動させる
  • 線のスタイルを変更する
  • multiplotによって1つの図に2つのグラフを表示する
  • if文処理により,変数output=1にセットした時のみ.epsファイルを出力する
  • 参照ファイル名を用いて出力ファイルを作成する
等の内容を含みます.

plotter.pltの中身は以下の通りです.

# plotter.plt

set terminal wxt 0
reset
set lmargin 9
set rmargin 2
set xlabel "Time [sec]"
set ylabel "Position [m]"
set grid
set key left bottom
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X"

if(exist("output")&& output == 1)\
set terminal postscript eps color enhanced "Arial,20";\
set output file."_X.eps";\
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X";\

set terminal wxt 1
set key left bottom
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y"

if(exist("output")&& output == 1)\
set terminal postscript eps color enhanced "Arial,20";\
set output file."_Y.eps";\
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y";\

set terminal wxt 2
set multiplot layout 2,1
unset xlabel
set format x ""
set key left bottom
set tmargin screen 0.99
set bmargin screen 0.585
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X"
set format x
set xlabel "Time [sec]"
set key left bottom
set tmargin screen 0.535
set bmargin screen 0.13
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y"
unset multiplot

if(exist("output")&& output == 1)\
set terminal postscript eps color enhanced "Arial,20";\
set output file."_Layout.eps";\
set multiplot layout 2,1;\
unset xlabel;\
set format x "";\
set key left bottom;\
set tmargin screen 0.99;\
set bmargin screen 0.585;\
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X";\
set format x;\
set xlabel "Time [sec]";\
set key left bottom;\
set tmargin screen 0.535;\
set bmargin screen 0.13;\
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y";\
unset multiplot;\

undefine output
set terminal wxt 0

2017年6月23日金曜日

ROSのtopicを他のPCからRVizで確認したくて2台のPCでを接続する

表題の通りROSを2台のPCで走らせてトピックをやり取りします(正しくは1台のPC上で走っているroscoreに他が接続する).何故やろうと思ったかというとロボットに組み込んだPCでSLAMを走らせているのだけれどリアルタイムでMAPの様子を確認したいから(ssh -Xでやっても良いのだけれどいかんせん重たくなる).ですのでロボット側がマスターとします.

ロボットPC
$ export ROS_HOSTNAME=robot_ip_adress
$ export ROS_MASTER_URI=http://robot_ip_adress:11311
$ roscore
これどうやら全てのターミナルでやる必要があるので~/.bashrcに
$ export ROS_HOSTNAME=robot_ip_adress
$ export ROS_MASTER_URI=http://robot_ip_adress:11311
の二行を最下部に書いてしまいましょう.

次に観測用のPC
$ export ROS_HOSTNAME=observer_ip_adress
$ export ROS_MASTER_URI=http://robot_ip_adress:11311
$ rosrun rviz rviz

これでO.K.

2017年6月18日日曜日

ヘッドレスでUbuntuを起動

ヘッドレス(ディスプレイ接続なし)でUbuntuを起動する.使用PCはZOTACのZBoxです.GPUはNvidia.通常の設定ではディスプレイを探しに行って見つからないずにOSが起動しないという状態になります.ディスプレイがなくても起動することを許可すればOK.

$ sudo nvidia-xconfig --allow-empty-initial-configuration

2017年6月13日火曜日

学内(社内)ネットワークからgitできない

githubからsubmoduleでcloneするときにconnectionがないと怒られる.

fatal: unable to connect to github.com:

原因は学内ネットワークのファイアウォールが強すぎるから

git://~

を弾いてしまっている.ということで「git://」を「https://」に書き換える.

git config --global url."https://".insteadOf git://

2017年2月7日火曜日

gnuplotで2系列データをmultiplotで上下に並べてx軸を揃えて比較する

タイトルの通りの内容です.制御入力と制御量を同時にグラフ出力するときにaxes xy2を使う方法がありますが,表示数が増えると見づらくなるので分けてしましたい.そこでmultiplotを使います.multiplotを使うと簡単に上下にグラフを並べられるのですが問題が2点.1)ラベルや軸目盛のせいでx軸の幅がずれる.2)時系列で上下に並べているので上のグラフのx軸のラベルと目盛が邪魔.ということで以下の通り処理してしまします.

set terminal wxt 0
reset
set lmargin 9 #x軸幅を揃えるためにグラフまでのマージンを決めてしまう(左側)
set rmargin 2 #右側のマージン
set ylabel "P[m], V[m/s], A[m/s2]"
unset xlabel  #xラベルを削除
set format x ""  #x目盛を削除
set grid
set multiplot layout 2,1  #multiplotの設定 layout 縦,横
p [:][-1:1] file.".txt" u 1:5 w l title "Xa", "" u 1:4 w l title "Xv", "" u 1:3 w l title "Xp","" u 1:2 w l title "Xdes"
set xlabel "Time [sec]"  #x軸のラベルを再設定(下のグラフ用)
set format x  #x軸の目盛を再設定(下のグラフ用)
set ylabel "Input"
p file.".txt" u 1:6 w l title "Input"
unset multiplot

ちなみに「file.".txt"」となっていますが,「file = 'name'」を設定しておけば「name.txt」がプロットされます.「.」は文字列の接続です.

2017年2月3日金曜日

gnuplotで時系列の3次元軌跡をgifに出力する

2次元データ,3次元データを時系列で表示する方法としてgnuplotでgif形式で出力するという方法をとります.以下で終わらせます.

gnuplot> load plotter.plt


plotter.pltの中身は
##ここから

if(exist("n")==0 || n<0)\  ##初期化のための条件式
reset;\
set xlabel "x [m]";\
set ylabel "y [m]";\
set zlabel "z [m]";\
\
set xrange [-1:1];\
set yrange [-1:1];\
set zrange [0:2];\
\
unset key;\
set ticslevel 0;\
\
set grid xtics ytics ztics;\
\
set terminal gif animate;\
set output "output.gif";\
\
n = 0;\                    #データの最初の行
n1 = 10000;\          #データの最後の行
dn = 100;\              #時系列の処理間隔
wn = 1000           #1度に表示する行数


##プロットする内容
sp "output.txt" every ::n::(n+wn) u 1:2:3 w l

#ループ処理
n = n + dn                
if ( (n+wn) < n1 ) reread

#終了処理
undefine n               
set terminal wxt

##ここまで

n行目から(n+wn)行目までを表示することを繰り返す,という処理をしています.繰り返し処理に"reread"を使用していますが,これはファイルの頭から実行し直すので,初期化処理に関連する部分は条件式を作ってあげる必要があります.n行目から(n+wn)行目までを表示するためには"every ::n::(n+wn)"としていますが,

every ::開始行::終了行

です."every"はもう少し他の使い方もありますが,現状必要な情報は上記のみでした.

また,gifということでファイルサイズに注意が必要になります.画像の枚数,すなわちおおよそ(n1/dn)枚が大きくなりすぎると再生するためにマシンパワーによっては悲鳴を上げ始めるので注意してください.

※実時間になっていません.実時間ぽくするためには
set terminal gif animate delay 数字
でフレーム時間が(数字/100) secになるので(デフォルトは5),dnに合わせて適宜調節をしてください.