「指定した方向を見るプログラム」
レイアウト
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<Button android:id="@+id/btn_move_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(1, -1)" app:layout_constraintHorizontal_bias="0" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btn_move_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(1000, 0)" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btn_move_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(1, 1)" app:layout_constraintHorizontal_bias="1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> |
onCreateの中に書くもの
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
QiSDK.register(this, this); //それぞれボタンを押されたら、後述のmappingしたfreeframeとLookAtメソッドを呼ぶ findViewById(R.id.btn_move_1).setOnClickListener( v -> { if(qiContext == null || freeFrameRight == null) return; lookAt(freeFrameRight); }); findViewById(R.id.btn_move_2).setOnClickListener( v -> { if(qiContext == null || freeFrameForward == null) return; lookAt(freeFrameForward); }); findViewById(R.id.btn_move_3).setOnClickListener( v -> { if(qiContext == null || freeFrameLeft == null) return; lookAt(freeFrameLeft); }); |
onRobotFocusGainedの中に書くもの
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Robot Frameを取得(図のオレンジ矢印) //(ここに処理を記述1) //起点となるフレームと目的のフレームの位置関係を作成 //前1m右1mの位置を指定(右斜め45度、黄色セル) //(ここに処理を記述2) //Mappingオブジェクトから空のFree Frameを作成し、起点となるフレームと位置関係を使って、FreeFrameを目的の位置に更新 //(ここに処理を記述3) //同様に正面と、左45度のFree Frameを作成 transform = TransformBuilder.create().from2DTranslation(1000, 0); freeFrameForward = mapping.makeFreeFrame(); freeFrameForward.update(robotFrame, transform, System.currentTimeMillis()); transform = TransformBuilder.create().from2DTranslation(1, 1); freeFrameLeft = mapping.makeFreeFrame(); freeFrameLeft.update(robotFrame, transform, System.currentTimeMillis()); |
LookAtメソッド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//指定されたFreeFrameの方向を見るlookAtメソッド private void lookAt(final FreeFrame transform){ if ((futureLookAt != null && !futureLookAt.isDone())) futureLookAt.requestCancellation(); //既にLookAt actionを実行中の場合は既存のLookAtをキャンセルする。 //LookAtはキャンセルされるまで指定の方向を見続ける。 futureLookAt = transform.async().frame() .andThenCompose(//LookAtBuilderでLookAtオブジェクトをbuildする。 //(ここに処理を記述1) ) .andThenCompose( //LookAtクラスのsetPolicyメソッドで、体ごと向くように設定 //(ここに処理を記述2) ).thenConsume(voidFuture -> { //終了したらログ出力とトースト表示 if(voidFuture.hasError()) Log.d("SampleApp", voidFuture.getErrorMessage()); runOnUiThread(() -> Toast.makeText( getApplicationContext(), "完了", Toast.LENGTH_SHORT).show()); }); } |
「指定した方向に進むプログラム」
onRobotFocusGainedの中に書くもの
1 2 3 |
transform = TransformBuilder.create().from2DTranslation(-1, 0); freeFrameForward = mapping.makeFreeFrame(); freeFrameForward.update(robotFrame, transform, System.currentTimeMillis()); |
Gotoメソッド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//指定されたFreeFrameに移動するgoToメソッド private void goTo(final FreeFrame transform){ //既にGoTo actionを実行中の場合は既存のGoToをキャンセルする。 if ((futureMove != null && !futureMove.isDone())) futureMove.requestCancellation(); //LookAtと異なり、指定のFree Frameに移動し続けるわけではない。 //他のアクション同様にGoToBuilderでGoToオブジェクトをbuildし、run()することで移動させることが出来る futureMove = transform.async().frame() .andThenCompose( //(ここに処理を記述) ) .andThenCompose( //(ここに処理を記述) ).thenCompose(voidFuture -> { //GoTo actionが完了したら、完了と発話するように実装 if(voidFuture.hasError()) Log.d("SampleApp", voidFuture.getErrorMessage()); return SayBuilder.with(qiContext).withText("完了").buildAsync(); }).andThenCompose(say -> say.async().run()); } |