Creating a robot application

1. Creating a project

To create a robot application, we first create a standard Android project, then we will convert it to a robot application.

Step Action

From Android Studio, choose: File > New > New Project

Give a name to your application.

When prompted by the wizard, choose the Phone and Tablet option, and make sure Android 6.0 (Marshmallow) is selected.

../_images/project_wizard.png
Finish the wizard selecting the default choices for all remaining steps.

2. Creating a robot application

Now that you have created the Android project, let’s convert it into a robot application:

Step Action

Once the project is created, choose File > New > Robot Application.

../_images/new_robot_app.png
Select the minimum Robot SDK version and the module to robotify. Then click OK.
Synchronize your project with the Gradle files.

Result

Your project is now a robot application!

As a result, you should see the following changes:

  • the robotsdk.xml file has been added to the assets/robot directory,
  • some new tools are available in the toolbar,
  • the QiSDK dependency has been added to your module’s build.gradle file,
  • a uses-feature tag has been added to your AndroidManifest.xml.

The robotsdk.xml file contains the minimum Robot SDK version for your project (the one you selected before).

The new available tools allow you to start a new robot emulator emulator and connect to a real robot connect.

The QiSDK dependency provides you all the functionalities to interact with Pepper.

The uses-feature tag states that your application uses Pepper and will only be available for the robot tablet.

3. Implementing QiSDK & the Robot Life Cycle

We have configured a robot application, now it is time to let our code take the control of Pepper’s body.

Step Action

Register the MainActivity to the QiSDK in the onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Register the RobotLifecycleCallbacks to this Activity.
    QiSDK.register(this, this);
}

And unregister it in the onDestroy method:

@Override
protected void onDestroy() {
    // Unregister the RobotLifecycleCallbacks for this Activity.
    QiSDK.unregister(this, this);
    super.onDestroy();
}

Tip: You could also use QiSDK.unregister(this) to unregister all the RobotLifecycleCallbacks for this Activity, just be careful not to inadvertently remove a callback that should stay registered.

Make your MainActivity class extends RobotActivity :

public class MainActivity extends RobotActivity

Why? This activity brings visual feedbacks such as a SpeechBar when a communication is running.

For further details, see: Mastering Focus & Robot lifecycle.

Make your MainActivity class implement the RobotLifecycleCallbacks interface:

public class MainActivity extends RobotActivity implements RobotLifecycleCallbacks

Why? This interface allows us to know when our code can run on Pepper. More precisely, we need to have the Focus for that.

For further details, see: Mastering Focus & Robot lifecycle.

Override the onRobotFocusGained, onRobotFocusLost and onRobotFocusRefused methods:

@Override
public void onRobotFocusGained(QiContext qiContext) {
    // The robot focus is gained.
}

@Override
public void onRobotFocusLost() {
    // The robot focus is lost.
}

@Override
public void onRobotFocusRefused(String reason) {
    // The robot focus is refused.
}

Why? The onRobotFocusGained method is called when our code can run on Pepper. For instance, we can run various actions on the robot.

When the onRobotFocusLost method is called, your Activity lost the focus and cannot run with Pepper.

Important

The onRobotFocusGained and onRobotFocusLost methods are executed on a background thread, so the UI thread will not be blocked when we will use the QiSDK synchronously.