0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Node-RED Raspberry Pi 三角波形

Posted at

Node-REDは無事動いたが
今後通信させたときにOPC UAの時のように三角波形を出したかったので
functionを使用してみた。
Node-REDはノンコーディングが利点と書いてあった気がしたが気にしない。

完成したフロー

functionはJavaScriptのようなのでSTで書いた内容はそのまま使用できない。
ちょっとネットで調べてみたけど型変換とかがうまくいかず思うように動かなかったので
ChatGPT先生に聞いて、ちょこっと触ったところ上手くいきました。

image.png

プログラム

三角波形作成部分(function 1)

初期化処理
flow.set("icount", 0);
flow.set("bonoff", false);
flow.set("bupdown", false);
flow.set("irate", 100);
flow.set("isignal", 1000);
コード
let icount = flow.get("icount") || 0;
let bonoff = flow.get("bonoff") ;
let bupdown = flow.get("bupdown") ;
let irate = flow.get("irate") ;
let isignal = flow.get("isignal") ;

icount = icount + 1;

//ONOFF
if(icount % 20 == 0){
    bonoff= !(bonoff);
}

//Sig
if(isignal > 11000 || isignal < -1000){  
    irate = irate* ((2 * (bupdown ? 1 : 0)) - 1);
    bupdown =!(bupdown);
}
isignal = isignal + irate;

flow.set("icount", icount);
flow.set("bonoff", bonoff);
flow.set("bupdown", bupdown);
flow.set("irate", irate);
flow.set("isignal", isignal);

msg.payload = {
    icount: icount,
    bonoff: bonoff,
    bupdown: bupdown,
    irate: irate,
    isignal: isignal
};
return msg;

三角波形取り出し部分(isignal toridasi)

コード
var isignal =msg.payload.isignal;
msg.payload=isignal ;
return msg;

完成したダッシュボード画面

image.png

次回予定

今度はTCPかUDPを使って、Node-RED同士で通信させようかと思っています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?