Ease tablet interaction — EnforceTabletReachability

Goal

In this tutorial, we will use EnforceTabletReachability in order to make Pepper reduce his movements, and therefore ease user interaction on the tablet.

Prerequisites

Before stepping in this tutorial, you should:

Let’s start a new project

  • Start a new project, let’s call it TabletReachabilityPepper.
  • Robotify it and make sure it implements the QiSDK & the Robot Life Cycle.

For further details, see: Creating a robot application.

Creating an EnforceTabletReachability action

In this section, we will see how to create an EnforceTabletReachability action.

Start by storing the following field in your MainActivity:

/// Store qiContext
private QiContext qiContext;

// Store the action.
private EnforceTabletReachability enforceTabletReachability;

Then, put the following code in the onRobotFocusGained method:

@Override
public void onRobotFocusGained(final QiContext qiContext) {
    // Store qiContext
    this.qiContext = qiContext;

    // Build EnforceTabletReachability action.
    enforceTabletReachability = EnforceTabletReachabilityBuilder.with(qiContext).build();

    ...
}

In order to observe the effect of EnforceTabletReachability, we will make Pepper talk when his tablet reaches the wanted position. To do so, add the following code in the same onRobotFocusGained method:

// On position reached listener
enforceTabletReachability.addOnPositionReachedListener(() -> {
    Log.i(TAG, "The tablet now is in position.");

    // Say action
    String textToSay = "My movements are now limited. Cancel the action to see the difference.";
    Say say = SayBuilder.with(qiContext)
            .withText(textToSay)
            .build();

    say.run();
});

Also, place this code in the onRobotFocusLost method:

@Override
public void onRobotFocusLost() {
    // Remove the QiContext.
    this.qiContext = null;

    // Remove all the listeners
    enforceTabletReachability.removeAllOnPositionReachedListeners();
}

Running the action

In this section, we will see how to run the EnforceTabletReachability action.

Start by storing this following field in your MainActivity:

// Store the action's future.
private Future<Void> enforceTabletReachabilityFuture;

Then, create a startEnforceTabletReachability() method in your MainActivity.

private void startEnforceTabletReachability() {
    // Run the action asynchronously
    enforceTabletReachabilityFuture = enforceTabletReachability.async().run();

    // Handle the action's end
    enforceTabletReachabilityFuture.thenConsume(future -> {
        // Log the eventual errors
        if (future.hasError()) {
            Log.e(TAG, "The EnforceTabletReachability action finished with error.", future.getError());
        } else {
            Log.i(TAG, "The EnforceTabletReachability action has finished.");
        }

        // Say text when the action is cancelled
        String text = "My movements are back to normal. Run the action again to see the difference.";

        Say s = SayBuilder.with(qiContext)
                .withText(text)
                .build();

        s.run();
    });
}

Calling this method will run the EnforceTabletReachability action asynchronously. We also added some code to make Pepper talk when the action is done. This will allow us to observe the difference in Pepper’s movements when the action is running or not.

Stopping the action

Add this simple stopEnforceTabletReachability to your MainActivity:

private void stopEnforceTabletReachability() {
    enforceTabletReachabilityFuture.requestCancellation();
}

Calling this method will stop the EnforceTabletReachability action that is running.

Testing EnforceTabletReachability

Let’s test our code using a button:

Note

The following code is just a quick way to test the functionality, this is not what you would do in a real application.

In the activity_main.xml layout file, add the following Button:

<Button
    android:id="@+id/tablet_reachability_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enforce tablet reachability"/>

In the onCreate method, add the following code:

// Run the EnforceTabletReachability when button clicked.
Button enforceTabletReachabilityButton = (Button) findViewById(R.id.tablet_reachability_button);
enforceTabletReachabilityButton.setOnClickListener(v -> {
    if (enforceTabletReachability == null) {
        Log.e(TAG, "EnforceTabletReachability has not been built yet");
    } else if (enforceTabletReachabilityFuture == null || enforceTabletReachabilityFuture.isDone()) {
        // The EnforceTabletReachability action is not running
        startEnforceTabletReachability();
    } else {
        // The EnforceTabletReachability is running
        enforceTabletReachabilityFuture.requestCancellation();
    }
});

Clicking on the button will either start or stop the EnforceTabletReachability action, accordingly to its running state.

Let’s try it

github_icon The sources for this tutorial are available on GitHub.

Step Action

Install and run the application.

For further details, see: Running an application.

Choose “Enforce tablet reachability”.

Notice that Pepper moves his arms and torso while talking.

Click on the button and the robot will reduce his movements, easing tablet interaction.

Click again on the button and the robot will go back to his previous state.

https://developer.softbankrobotics.com/sites/default/files/repository/60_rst_pepper/public/source/_build/html/_images/enforcetabletreachability.png

You are now able to ease tablet interaction on Pepper!