こんにちは、ナレコム前川です。
今回は、Amazon Sumerian の公式チュートリアルの
IoT Thing, Shadow, and Script Actions を進めていきたいと思います。
それでは、早速進めていきましょう。
1. はじめに
シーンを作成する前に Pool ID が必要なので、Amazon Cognito の設定を行っていきます。
Cognito の設定は、 コチラ を見て
IoT Thing Shadow and Script Actions スタックの作成を行ってください。
スタックの作成が完了したら、 IoT Console を開きます。
開いたら、発行のトピックに $aws/things/box/shadow/update を入力し、
以下のコードを打ち込んでトピックを発行してください。
1{2 "state" : {3 "reported" : {4 "y_rotation" : 05 }6 }7}入力したら以下のようになるはずです。
これで準備は終わりです。
2. 環境設定
シーン作成
① ダッシュボードから Default Lighting を選択。
① ダッシュボードから Default Lighting を選択。
② シーン名を「IoT + Script Action Tutorial」にし、シーンを作成。
AWS configuration の設定
① Entities パネルから シーン名を選択し、AWS configuration を展開。
① Entities パネルから シーン名を選択し、AWS configuration を展開。
② Pool ID を入力し、 IoT Data Client を有効にする。
エンティティの作成
① Create Entity をクリックし、Box を追加。
State Machine の設定
① Box の Inspector パネルから State Machine コンポーネントを追加。
① Box の Inspector パネルから State Machine コンポーネントを追加。
② + ボタンで新しい Behavior を追加。
③ ステート名を「Poll & Rotate Box」に設定。
④ State を 4つ準備する。
⑤ それぞれの名前を以下のようにする。
・init
・Wait
・Get Thing State
・Rotate Box
State の設定
① Init State
1. AWS SDK Ready
アクションを追加。
2. Set As Initial State
になっていることを確認。
3. Wait
アクションを追加。
4. Time (ms)
を 2000 に設定。
5. Execute Script
アクションを追加。
6. + ボタン
をクリックし、Custom を選択。
- 編集モードに切り替える。
- 以下のプログラムと置き換える。
1'use strict';23 function enter(args, ctx) {4 window.AWSIotData.getThingShadow({ thingName: 'box' }, (error, data) => {5 if (error) {6 console.error('Error getting state', error);78 return ctx.transitions.failure();9 }1011 const payload = JSON.parse(data.payload);1213 ctx.behaviorData.yRotation = payload.state.reported.y_rotation;1415 ctx.transitions.success();16 });17 }1819 var parameters = [];7. Script名を「Get Thing State Script」
に変更。
- Scriptを保存。
④ Rotate Box State
8. Execute Script
アクションを追加。
9. + ボタン
をクリックし、Custom を選択。
- 編集モードに切り替える。
- 以下のプログラムと置き換える。
1'use strict';23 function enter(args, ctx) {4 try {5 ctx.entity.setRotation([0, Math.radians(ctx.behaviorData.yRotation), 0]);67 ctx.entity.transformComponent.setUpdated();8 } catch (error) {9 console.error('Error rotating box', error);1011 return ctx.transitions.failure();12 }1314 ctx.transitions.success();15 }1617 //Converts from degrees to radians18 Math.radians = function(degrees) {19 return degrees * Math.PI / 180;20 };2122 var parameters = [];10. Script名を「Rotate Box Script」
に変更。
- Scriptを保存。
⑤ それぞれのState を以下のように繋ぐ。
3. シーンの実行
シーンを実行する前に、 IoT Console からY軸の旋回値を設定します。
発行のトピックに $aws/things/box/shadow/update を入力し、以下のコードを打ち込み、トピックを発行します。
1{2 "state" : {3 "reported" : {4 "y_rotation" : 405 }6 }7}シーンを実行すると、Box が 40度回転します。
実行動画は以下です。
4. VR機器で実行
お持ちのWebVR 対応機器でシーンを実行してみてください。
VR機器でのシーン実行については、 コチラ で紹介しています。
今回のチュートリアルは以上です。
お疲れ様でした。