Anki Vector Robot Play Fetch

Kinvert shows you how to program the Anki Vector robot to play fetch. The Vector robot play fetch program is easy to write and we show you how.

Both Vector and Cozmo are an amazing way to learn programming. If you want to know the differences between the two check out Cozmo vs Vector.

This is one of many Vector Coding Examples.

Vector slideshow on face with convert_image_to_screen_data and set_screen_with_image_data

Playing Fetch With Vector

Vector is already pretty fun and lovable. We can add to the fun by getting him to play fetch.

He will wag his tail waiting to be touched on the back. Eventually when the SDK is updated we plan to say “Hey Vector, go fetch” and he’ll do it. But for now the SDK is still in Alpha testing.

You can read more about the Anki Vector SDK in Python.

How to replace the cube battery for Cozmo and Vector battery replacement

To do this, at least for now, you have to be connected to Vector’s cube. So you can’t have a dead battery.

It takes a very specific battery and you can’t find it just anywhere.

We have an article on How to Replace Cozmo and Vector’s Cube Battery.

TLDR – The Most Popular Battery Currently:

Fetch the Commands

What commands will we be using? Here they are:

  • robot.behavior.drive_off_charger()
  • robot.behavior.set_head_angle()
  • robot.behavior.set_lift_height()
  • robot.world.connect_cube()
  • robot.say_text()
  • robot.motors.set_wheel_motors()
  • robot.behavior.dock_with_cube()
  • robot.behavior.turn_in_place()
  • robot.behavior.drive_straight()
  • robot.world.disconnect_cube()
  • time.sleep()

This is a pretty long program but it is still fairly simple and straight forward.

You can see an article about Kinvert and also find documentation in top navigation at https://developer.anki.com/blog/features/interview/from-stem-to-learn/

Fetch the Code

Using set_eye_color in the anki vector sdk alpha release to make rainbow eyes with Kinvert

Let’s go over the code to make Vector play fetch.

We do our imports at the top, then define our main function. Vector will drive off his charger, and make sure his head and arms are down.

He connects to his cube, which can take a while sometimes. Then he’ll remind you that he messes up sometimes.

He will then start wagging his tail with the set_wheel_motors() commands. He is waiting for you to touch his back with robot.touch.last_sensor_reading.is_being_touched.

Once you touch his back, he will leave that while loop and proceed to dock with the cube using robot.behavior.dock_with_cube().

He then checks to see if he docked, and lifts his arms with robot.motors.set_lift_motor. Vector will then turn around and bring you back the cube using robot.behavior.turn_in_place and robot.behavior.drive_straight.

He wags his tail again as before waiting to be touched. When touched that is telling him “drop” and he will drop the cube for you.

Currently he will try to fetch 5 times.


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
Free for personal use
"""

import anki_vector
from anki_vector.util import degrees, distance_mm, speed_mmps
import time

def main():
    '''Play fetch with Vector
    
    Touch his back to signal to him that he can go get his cube
    
    Once he comes back, touch his back again to tell him to put down the cube
    '''
    args = anki_vector.util.parse_command_args()

    with anki_vector.Robot(args.serial) as robot:
        robot.behavior.drive_off_charger()
        
        robot.behavior.set_head_angle(degrees(-5.0))
        robot.behavior.set_lift_height(0.0)

        print("Connecting to a cube...")
        robot.world.connect_cube()

        robot.say_text("I am still learning this game so I might make mistakes")

        for i in range(5):
            dock_response = False
            docking_result = None
            while not robot.touch.last_sensor_reading.is_being_touched:
                robot.motors.set_wheel_motors(-50, 50)
                time.sleep(0.1)
                robot.motors.set_wheel_motors(50, -50)
                time.sleep(0.1)
            robot.motors.set_wheel_motors(0, 0)
            time.sleep(0.25)
            dock_response = robot.behavior.dock_with_cube(
                robot.world.connected_light_cube,
                num_retries=3)
            if dock_response:
                docking_result = dock_response.result
                robot.motors.set_lift_motor(50)
                time.sleep(1)
                robot.behavior.turn_in_place(degrees(180))
                robot.behavior.drive_straight(distance_mm(100), speed_mmps(100))    
                while not robot.touch.last_sensor_reading.is_being_touched:
                    robot.motors.set_wheel_motors(-50, 50)
                    time.sleep(0.1)
                    robot.motors.set_wheel_motors(50, -50)
                    time.sleep(0.1)
                robot.motors.set_wheel_motors(0, 0)
                time.sleep(0.25)
                robot.motors.set_lift_motor(-50)
                time.sleep(1)
                robot.say_text("Woof, woof, bow, wow")
                robot.behavior.turn_in_place(degrees(180))
                
        robot.world.disconnect_cube()

if __name__ == "__main__":
    main()

Python Code Improvements

Anki Vector Coding Example using the touch sensor in the Anki Vector SDK with Kinvert

Once we have better voice commands (remember this is the Alpha testing version currently) we’ll switch from touch to voice.

How can we improve the Anki Vector Robot Fetch code a bit?

If you wanted Vector to act a little more excited to go fetch, what might you change?

How would we get Vector to fetch more than 5 times?

To get Vector to drop the cube a little more gently, what might we change to do that?

What else could you get Vector to say to be a little more funny?

You Might Also Be Interested In…

Cozmo drawing for online coding class learn to program anki cozmo robot in python

If you’re reading about how to program Vector to play fetch, chances are you’ll be interested in other stuff here at Kinvert.

If you have a Cozmo instead of Vector check Anki Cozmo SDK and Cozmo Examples. Cozmo can be programmed in very similar ways to Vector.

If a fully written language seems difficult, read What is Robotics and Kinvert’s Ultimate Guide to Block Coding.

We also have a mailing list where we keep people up to date with all things Cozmo and Vector. The mailing list is always the first to know when there is a new article or video, or any news about Cozmo or Vector. We help give them tips to get the most out of their little robot friends, and you can unsubscribe at any time.

If you have any questions, please let us know in the comments below. Hope this helped!

2 responses to “Anki Vector Robot Play Fetch

Leave a Reply

Your email address will not be published. Required fields are marked *