Kinvert shows you how to play a game of fetch with Vector. Vector will fetch the cube you tap.
Vector and Cozmo are a great way to learn Python coding. We have a detailed article on the differences between Cozmo vs Vector.
This is one of many Vector Coding Examples. This is also one of the Games Vector Can Play.
Vector Playing a Game of Fetch
Cozmo has a ton of games ready to be played. So we want to make more games for Vector.
We made Vector play fetch before by touching his back. This time we improved it so he will fetch the tapped cube.
We use the Anki Vector SDK to accomplish this.
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:
- https://amzn.to/2KN1ISk – 4 for $5 on Amazon
Fetch Those Commands
What commands will we be using? Here they are (this is Vector’s commands):
- threading.Event()
- robot.conn.request_control()
- robot.behavior.set_head_angle()
- robot.world.connect_cube()
- functools.partial()
- robot.events.subscribe()
- robot.say_text()
- time.sleep()
- robot.events.unsubscribe()
- robot.lift_height_mm
- robot.motors.set_lift_motor()
- robot.behavior.turn_in_place()
- robot.behavior.dock_with_cube()
- print()
- robot.behavior.drive_straight()
- evt.set()
It’s a fairly long program here.
Fetch Tapped Code
If you want to also play fetch with Cozmo you’ll need the code blocks under the Vector code. Here is a delightful example of the two playing fetch together.
https://www.youtube.com/watch?v=x6_MBEeSMPk
Let’s take a moment to talk about the code here.
We do our imports and call the main function.
Next we set up threading.
Vector connects to his cube with robot.world.connect_cube() and starts setting up events with functools.partial() and robot.events.subscribe() before starting the actual game.
Once this is done, Vector will play for 30 seconds. He waits for a cube tap, and when that happens he goes to the on_tapped_cube function we defined.
Within that function, he checks his lift position to see if he has already fetched, or if he needs to fetch.
The rest is hopefully fairly straight forward.
The big thing here is when he goes to fetch, which is under else. Sometimes his docking fails and it is due to his tracks being locked. So there is some code in there to deal with that error.
As you can tell by what Vector says, I still don’t know what is causing this.
Another important thing to note is that I subscribe and unsubscribe from events. I found that as Vector moves the cube it registers more tap events. He can get stuck in an endless loop and not really fetch since his movements ‘tap’ the cube. So I force Vector to stop watching for cube taps while he moves the cube.
""" 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.events import Events from anki_vector.util import degrees, distance_mm, speed_mmps import functools import threading import time def main(): ''' Vector will go fetch the cube when you tap it, then drop it when you tap it - about half of the time. Often times his tracks are locked and I don't know why yet. ''' evt = threading.Event() def on_tapped_cube(robot, event_type, event): robot.conn.request_control() robot.events.unsubscribe(on_tapped_cube, Events.object_tapped) print(robot.lift_height_mm) if robot.lift_height_mm > 50: robot.motors.set_lift_motor(-10) time.sleep(1) robot.say_text("Woof, woof, bow, wow") robot.behavior.turn_in_place(degrees(180)) time.sleep(1.0) robot.events.subscribe(on_tapped_cube, Events.object_tapped) else: robot.say_text("Yip yip") response = robot.behavior.dock_with_cube( robot.world.connected_light_cube, num_retries=3) print(response) print(response.result) print(response.result.code) #50331679 i = 0 while response.result.code and i < 1: robot.say_text("Growl") robot.say_text("My tracks are locked and Keith doesn't know why") robot.motors.set_lift_motor(-0.1) time.sleep(0.5) response = robot.behavior.dock_with_cube( robot.world.connected_light_cube, num_retries=3) i += 1 if i == 1: robot.say_text("Let's try this again") robot.behavior.set_lift_height(1.0, max_speed=1.0) time.sleep(0.5) robot.behavior.turn_in_place(degrees(180)) robot.behavior.drive_straight(distance_mm(100), speed_mmps(100)) time.sleep(1.0) robot.events.subscribe(on_tapped_cube, Events.object_tapped) evt.set() args = anki_vector.util.parse_command_args() with anki_vector.Robot(args.serial) as robot: robot.conn.request_control() print("Connecting...") robot.behavior.set_head_angle(degrees(0.0)) robot.world.connect_cube() print("Connected") on_tapped_cube = functools.partial(on_tapped_cube, robot) robot.events.subscribe(on_tapped_cube, Events.object_tapped) robot.say_text("I'm ready to play fetch") print('Vector is ready for a cube tap') j = 0 while j < 30: time.sleep(1.0) j += 1 robot.say_text("Yawn, I'm done") if __name__ == '__main__': main()
To have Cozmo play with Vector you’ll need this code:
Improving the Vector’s Fetch Game
This one is a more honest instead of quizzing question here. How do we avoid that TRACKS_LOCKED issue? How do I get around that?
Now on to the questions for newer coders to find the answers to.
How would we get Vector to lift the cube faster?
If we wanted Vector to drop the cube more gently what would we change?
Vector currently gets bored quickly. How would we get him to play the game longer than 30 seconds?
Other Things to Check Out
If this was somewhat interesting to you we do have a lot of other stuff you might like here at Kinvert.
Cozmo has plenty of stuff in Cozmo SDK, and Cozmo Examples.
If Python looks a bit difficult read What is Robotics and Kinvert’s Ultimate Guide to Block Coding as well as Age to Teach Kids Python.
We have a mailing list where members are notified of Cozmo / Vector updates as well as new articles from Kinvert. Popular items we send out include How to Replace Vector and Cozmo’s Cube Batteries.
Please let us know any changes you make in the comments below. We love to see how people customize games with Vector.