GoTo – Mastering AttachedFrame

no_virtual_ robotCannot be tested on an emulated robot, requires a real robot.


In this tutorial, we will see how Pepper can find and follow a human, using AttachedFrames.


Before stepping in this tutorial, you should:

Let’s start a new project

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

For further details, see: Creating a robot application.

Creating the target frame

We want Pepper to follow the human and keep 1 meter between the human and him. To achieve this, we need to build a target frame that moves with the human frame, using an AttachedFrame.

This target frame must be attached to the human frame and the transform between the human frame and the target frame must be a 1 meter translation on the X axis:


To create an AttachedFrame, use the makeAttachedFrame method on the base frame and provide it the transform between the base frame and the frame you want to create. Add the createTargetFrame method to your MainActivity class:

private Frame createTargetFrame(Human humanToFollow) {
    // Get the human head frame.
    Frame humanFrame = humanToFollow.getHeadFrame();
    // Create a transform for Pepper to stay at 1 meter in front of the human.
    Transform transform = TransformBuilder.create().fromXTranslation(1);
    // Create an AttachedFrame that automatically updates with the human frame.
    AttachedFrame attachedFrame = humanFrame.makeAttachedFrame(transform);
    // Returns the corresponding Frame.
    return attachedFrame.frame();

Executing the GoTo action

To perform the GoTo action, we will need the following fields in the MainActivity class:

// The QiContext provided by the QiSDK.
private QiContext qiContext;
// Store the action execution future.
private Future<Void> goToFuture;

In the onRobotFocusGained and onRobotFocusLost methods, add the following code:

public void onRobotFocusGained(QiContext qiContext) {
    Log.i(TAG, "Focus gained.");
    // Store the provided QiContext.
    this.qiContext = qiContext;

public void onRobotFocusLost() {
    Log.i(TAG, "Focus lost.");
    // Remove the QiContext.
    this.qiContext = null;

We can now create a GoTo action from the target frame and run it.

Add a GoTo field in your MainActivity:

// Store the GoTo action.
private GoTo goTo;

Add the following followHuman method:

private void followHuman(Human human) {
    // Create the target frame from the human.
    Frame targetFrame = createTargetFrame(human);

    // Create a GoTo action.
    goTo = GoToBuilder.with(qiContext)

    // Execute the GoTo action asynchronously.
    goToFuture = goTo.async().run();

Stopping the movement

To stop the GoTo action, we use the requestCancellation method on goToFuture:

private void stopMoving() {
    // Cancel the GoTo action asynchronously.
    if (goToFuture != null) {

Testing the functionality

We will implement this functionality using:

  • a Button to make Pepper follow the closest human,
  • a Button to stop Pepper’s movement.

Modify your activity_main.xml file with the following code:

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        app:layout_constraintTop_toTopOf="parent" />

        app:layout_constraintTop_toBottomOf="@+id/follow_button" />


Defining the UI state:

Add the the following fields in the MainActivity class:

private Button followButton;
private Button stopButton;

Add the following code in the onCreate method:

followButton = (Button) findViewById(;
stopButton = (Button) findViewById(;

And the following methods:

private void enterWaitingForOrderState() {
    Log.i(TAG, "Waiting for order...");
    runOnUiThread(() -> {

private void enterMovingState() {
    Log.i(TAG, "Moving...");
    runOnUiThread(() -> {

Call enterMovingState in the followHuman method, when the GoTo action starts:

// Update UI when the GoTo action starts.

Do not forget to remove this listener on GoTo in the onRobotFocusLost method:

// Remove on started listeners from the GoTo action.
if (goTo != null) {

Call enterWaitingForOrderState at the end of the onRobotFocusGained method:

public void onRobotFocusGained(QiContext qiContext) {
    Log.i(TAG, "Focus gained.");
    // Store the provided QiContext.
    this.qiContext = qiContext;


And in the followHuman method, when the GoTo action finishes:

// Update UI when the GoTo action finishes.
goToFuture.thenConsume(future -> {
    if (future.isSuccess()) {
        Log.i(TAG, "Target reached.");
    } else if (future.isCancelled()) {
        Log.i(TAG, "Movement stopped.");
    } else {
        Log.e(TAG, "Movement error.", future.getError());

Finding the closest human:

To find the closest human, we need to compute the distance between a human and the robot. Add the following getDistance method:

private double getDistance(Frame robotFrame, Human human) {
    // Get the human head frame.
    Frame humanFrame = human.getHeadFrame();
    // Retrieve the translation between the robot and the human.
    Vector3 translation = humanFrame.computeTransform(robotFrame).getTransform().getTranslation();
    // Get the translation coordinates.
    double x = translation.getX();
    double y = translation.getY();
    // Compute and return the distance.
    return Math.sqrt(x*x + y*y);

And add this method to get the closest human:

private Human getClosestHuman(List<Human> humans) {
    // Get the robot frame.
    final Frame robotFrame = qiContext.getActuation().robotFrame();

    // Compare humans using the distance.
    Comparator<Human> comparator = new Comparator<Human>() {
        public int compare(Human human1, Human human2) {
            return, human1), getDistance(robotFrame, human2));

    // Return the closest human.
    return Collections.min(humans, comparator);

We need to find the humans around the robot, using the HumanAwareness service. Add the searchHumans method in your MainActivity class:

private void searchHumans() {
    HumanAwareness humanAwareness = qiContext.getHumanAwareness();
    Future<List<Human>> humansAroundFuture = humanAwareness.async().getHumansAround();
    humansAroundFuture.andThenConsume(humans -> {
        // If humans found, follow the closest one.
        if (!humans.isEmpty()) {
            Log.i(TAG, "Human found.");
            Human humanToFollow = getClosestHuman(humans);
        } else {
            Log.i(TAG, "No human.");

Defining UI actions:

Add the following code in the onCreate method:

// Search humans on follow button clicked.
followButton.setOnClickListener(v -> {
    if (qiContext != null) {
        // Wait 3 seconds before following.
        FutureUtils.wait(3, TimeUnit.SECONDS).andThenConsume(ignored -> searchHumans());

// Stop moving on stop button clicked.
stopButton.setOnClickListener(v -> {
    Log.i(TAG, "Stopping...");

Let’s try it

github_icon The sources for this tutorial are available on GitHub.

  1. Install and run the application.

    For further details, see: Running an application.

  2. Choose “Mastering attached frame”.

  3. Make sure the robot’s hatch is closed and that Pepper sees you.

  4. Click on the “Follow” button and move around.

    Pepper will start to follow you.

  5. Wait for Pepper to reach you or click on the “Stop” button.

    Pepper will stop following you.


You are now able to make Pepper follow a human!