忘備録-備忘録

技術的な備忘録

シリアルプロッタを作る

2016-01-27 18:02:26 | processing
Arduinoに追加されたシリアルプロッタは手軽に使えて動作の確認も簡単です。
自動的に、縦軸のレンジが自動で変化するのはお手軽に使えて良いのですが、サンプリングレートが高くなると表示が変化しすぎて目が回るような感じになります。
縦軸、横軸のレンジを決めて動作するプログラムがほしくなりました。手軽に使えるProcessing言語を利用して作ってみました。ひな型になるプログラムが「Arduino シリアル通信 その1:processing」にあったのでそのプログラムを手直ししたものです。

シリアル受信データ
2つの数字をカンマ区切りで一定間隔に出力したものです。シリアルポートから受信します。
-0.369629,0.001532
-0.450195,0.002479
-0.546875,0.003281
-0.650879,0.004101
-0.796875,0.004864

動作画面


プログラムリスト
  1. // 参考サイト http://shirotsuku.sakura.ne.jp/blog/?p=192
  2. import processing.serial.*;
  3. int displaytime = 4; //表示時間
  4. float gscale = 1; // 縦軸の倍率
  5. int samplingrate = 1000; //サンプリングレート
  6. int displayxsize = 1200; //ディスプレイの横幅
  7. int displayysize = 800; //ディスプレイの高さ
  8. String comport = "COM5"; //シリアルポート
  9. int comspeed = 115200; //通信速度
  10. int DATASIZE = samplingrate * displaytime;
  11. //int lf = 0x0d; // ASCII linefeed
  12. //シリアルポートを使用する
  13. Serial myPort;
  14. float val;
  15. float [] x = new float[DATASIZE]; //グラフ用データ x:時間
  16. float [] y1 = new float[DATASIZE]; //グラフ用データ y:電圧
  17. float [] y2 = new float[DATASIZE]; //グラフ用データ y:電圧
  18. void setup()
  19. {
  20.   frameRate(50); //20msec毎にグラフを更新
  21.   size(displayxsize, displayysize);
  22.   myPort = new Serial(this, comport, comspeed);
  23.   //myPort.bufferUntil(lf);
  24.   textSize(20);
  25.   //グラフ用データの初期化
  26.   for (int i = 0; i < x.length; i++) {
  27.     x[i] = (float)i * (float)(displayxsize-100)/(float)DATASIZE;
  28.     y1[i] = 0;
  29.     y2[i] = 0;
  30.   }
  31. }
  32. void draw()
  33. {
  34.   background(0);
  35.   //グラフエリアの描画
  36.   fill(255);
  37.   // text("-1.6V",10,310);
  38.   // text("+1.6V",10,54);
  39.   text("0sec", displayxsize - 70, displayysize-10);
  40.   float t = DATASIZE/1000;
  41.   text("-" + t + "sec", 30, displayysize-10);
  42.   fill(0, 255, 0);
  43.   //text(val/255.0*5,560,100);
  44.   //グラフのプロット
  45.   pushMatrix();
  46.   translate(50, displayysize-50);
  47.   scale(1, -1);
  48.   fill(0);
  49.   stroke(255);
  50.   rect(0, 0, displayxsize-100, displayysize-94);
  51.   stroke(0, 255, 0);
  52.   strokeWeight(1);
  53.   for (int i = 0; i < x.length - 1; i++) {
  54.     line(x[i], y1[i], x[i+1], y1[i+1]);
  55.   }
  56.   stroke(255, 0, 0);
  57.   for (int i = 0; i < x.length - 1; i++) {
  58.     line(x[i], y2[i], x[i+1], y2[i+1]);
  59.   }
  60.   popMatrix();
  61. }
  62. int bufsize = 512;
  63. char[] buf = new char[bufsize];
  64. int i = 0;
  65. void serialEvent(Serial myPort) {
  66.   char c = char(myPort.read());
  67.   //if(c=='\r') return;
  68.   if ((i<bufsize)&&(c!='\n'))
  69.   {
  70.     buf[i] = c;
  71.     i++;
  72.   } else
  73.   {
  74.     buf[i]=0;
  75.     i=0;
  76.     String s = new String(buf);
  77.     float data[] = float(s.split(",", 0));
  78.     //グラフ用データの更新
  79.     for (int i = 0; i < y1.length - 1; i++) {
  80.       y1[i] = y1[i+1];
  81.       y2[i] = y2[i+1];
  82.     }
  83.     // 0〜(displayysize-94)の間で変化するように
  84.     y1[y1.length - 1] = (data[0]+0)*(displayysize-94)*gscale/2 + (displayysize-94)/2;
  85.     // 0〜(displayysize-94)の間で変化するように
  86.     y2[y2.length - 1] = (data[1]+0)*(displayysize-94)*gscale/2 + (displayysize-94)/2;
  87.     //print(data[0]);
  88.     //print(" ");
  89.     //println(s);
  90.   }
  91. }


最新の画像もっと見る

コメントを投稿