3DCG屋さんの活動記録

PROFILE ★★こんな人です

3DCGを活用した映像や没入体験コンテンツの制作をしています。テクノロジーの社会実装に興味があり。テクニカルディレクター。面白いこと新しいことにワクワクする気持ちに『素直』でいつづける。

3DCG屋さんの活動記録

2017年12月31日日曜日

BlenderGame に ArduinoからSerialでボタン入力! その2


pyserial  inWaiting() でデータ受信の確認してる
https://www.youtube.com/watch?v=k2prOyRfimg&t=168
受信バッファとは http://www.7key.jp/nw/tcpip/tcp/window.html

pyseialのオプオプション(Serialオブジェクトのメソッド)
https://github.com/whosaysni/pyserial-doc-ja
http://pythonjp.osdn.jp/contrib/pySerial/README_JP.txt

とりま、Arduinoのボタン押してる間だけ動く、システム作成
Arduino
Blender Python
でそれぞれ設定


ちなみに、Arduinoでpyserialの送信で改行したいとき
必要な "\n" のバックスラッシュ。
Macでは 『option + ¥』 で 入力できる!


で、なぜかボタンを離しても一定時間信号受信してる。
シリアルモニターを同時起動すると、離した時に信号切れる・・・
おそらく、改行込みで送信した文字列が、受信バッファに溜まって
フレームレートの異なる環境(ArnordoとBlender)でずれる。



=========
Arduino
=========

//Arduinoでボタン押すとシリアルそうしん &LEDテントウ
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup(){
pinMode(buttonPin,INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
Serial.write("1");
Serial.write("\n");
digitalWrite(ledPin,HIGH);
}else{
digitalWrite(ledPin,LOW);
}
//delay(30);
}
===================
Blender python (for BGE)
===================

import bge
import serial
cont = bge.logic.getCurrentController()
rot = cont.actuators["saruRot"]
ser = serial.Serial("/dev/cu.usbmodem1411",9600,timeout=0) # timeout is need for readline()
def keyAction():
print (ser.inWaiting())
if ser.inWaiting()!=0:
cont.activate(rot)
if ser.inWaiting()>0:
cont.deactivate(rot)
#ser.flushInput()
#ser.flushOutput()
#ser.close()


================================
★スイッチ離したら重力で落下
 motionにz 0.2 を追加。
 おさるの右端タブでStatic を Rigidに変更
Bulled(物理演算)参考サイト

★GameSatrtのショートカット  キーボードのP
ショートカットまとめサイト


★Python使ってScaleの変動Logic


オブジェクトのScaleをPythonで変化させるのは
cont.owner.localScale.x
もしくは
cont.owner.localScale=[Ax,Ay,Az]

================================



えーと色々と書きましたが、まとめます。

試行錯誤の結果・・・

Arduinoのボタンを押している間だけBlenderで処理を実行
※ボタンOffが遅延することなく

ができました!!!!!!!

ArduinoとBlenderPythonの内容は以下の通り。

=========
Arduino
=========
上記のスケッチでOk

===================
Blender python (for BGE)
===================

import bge
import serial
cont = bge.logic.getCurrentController()
rot = cont.actuators["saruRot"]
ser = serial.Serial("/dev/cu.usbmodem1411",9600,timeout=0) # timeout is need for readline()
def keyAction():
serialData = ser.read(ser.inWaiting())
print (serialData)
lenData = len(serialData)
#print (ser.readline())
if lenData>0:
cont.activate(rot)
cont.owner.localScale = [3,3,3]
print("serial get")
else:
cont.deactivate(rot)
cont.owner.localScale = [1,1,1]
#ser.close()




 
Arduinoのボタンを押している間だけ、処理実行。
処理:回転&スケール3倍
重力適用しているので、処理終わると下に落ちる





1 件のコメント: