Vector Game Touch Custom Eye Color

A new Anki Vector game. Make a custom Vector eye color by touching his back. Win this Vector Game by keeping his eyes yellow for one second. Can you do it?

You can copy and paste this code to play the game with Vector.

If you want to learn Python, Cozmo and Vector are great choices. One of our popular articles is Cozmo vs Vector.

This is one of our many unique Vector Coding Examples.

When touching Vector’s full back, literally use multiple fingers and cover the entire golden touchpad on his back.

Eyes With Touch Control?

Please be sure to let us know what you think of the game and video 🙂 – https://youtu.be/3T25J9UhewQ

Using the Anki Vector SDK enables us to control many things, including Vector’s eyes.

One of the biggest requests from our mailing list is to give more examples of customizing Vectors eyes.

Another common request is for more Anki Vector Games.

So we created this game where you can customize Vector’s eye color by touching his back.

Let’s Touch on the Commands

Play Vector Games make a custom eye color for Vector by touching his back

What commands will we be using? Let’s take a look:

  • robot.behavior.set_head_angle()
  • robot.say_text()
  • for loops
  • robot.touch.last_sensor_reading.raw_touch_value
  • robot.touch.last_sensor_reading.is_being_touched
  • time.sleep()
  • robot.behavior.set_eye_color()
  • if
  • and
  • robot.anim.play_animation()
  • elif
  • else
  • break

Not the simplest program we’ve made. I’ll probably make a simpler lesson available to those taking our online course where you can just control his eyes and do what you want with that. For now, I wanted to make a game and it can be fairly complicated code for beginners.

Anyway, let’s dive in to the code.

Look and Feel of the Code

This code is pretty long, and I don’t have a ton of time right now. So I’m going to keep the explanation brief for now.

Long story short, we watch Vector’s touch sensor, do some math, and calculate a value for the hue.

We watch to see if the value is within a certain threshold and make sure that threshold is maintained for 1 second.

Once they win, we move on to the next color.

Play Vector Game match custom eye color by touching Vector's back using Kinvert's Python SDK program


"""
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
from math import cos
import time


def main():
    args = anki_vector.util.parse_command_args()

    with anki_vector.Robot(args.serial) as robot:
        robot.behavior.set_head_angle(degrees(45))
        robot.say_text("Don't touch me yet")
        
        untouchTotal = 0
        for i in range(20):
            untouchTotal += robot.touch.last_sensor_reading.raw_touch_value
        untouched = untouchTotal/20.0
        time.sleep(1.0)
        robot.say_text("touch my whole back")
        while not robot.touch.last_sensor_reading.is_being_touched:
            time.sleep(0.1)
        time.sleep(1.0)
        touchTotal = 0
        for i in range(20):
            touchTotal += robot.touch.last_sensor_reading.raw_touch_value
        touched = touchTotal/20.0

        diff = touched - untouched
        
        robot.say_text("Experiment with how to change my eye color")
        robot.say_text("Keep my eyes red for one full second")
        score = 0
        wins = 0
        for i in range(100):
            for j in range(50):
                value = (robot.touch.last_sensor_reading.raw_touch_value - untouched) / diff
                robot.behavior.set_eye_color(hue=value, saturation=1.0)
                time.sleep(0.01)
                print(score)
                if wins == 0 and value < 0.02: #red lowHue score += 0.01 if score > 1.0:
                        wins += 1
                        robot.say_text("Good job")
                        animation = 'anim_pounce_success_02'
                        robot.anim.play_animation(animation)
                        robot.say_text("Keep my eyes purple for one full second")
                        robot.behavior.set_head_angle(degrees(45))
                        score = 0
                if wins == 0 and value > 0.98: #red hiHue
                    score += 0.01
                    if score > 1.0:
                        wins += 1
                        robot.say_text("Good job")
                        animation = 'anim_pounce_success_02'
                        robot.anim.play_animation(animation)
                        robot.say_text("Keep my eyes purple for one full second")
                        robot.behavior.set_head_angle(degrees(45))
                        score = 0
                elif wins == 1 and value > .77 and value < .94: #purple score += 0.01 if score > 1.0:
                        wins += 1
                        robot.say_text("Good job")
                        animation = 'anim_pounce_success_02'
                        robot.anim.play_animation(animation)
                        robot.say_text("Keep my eyes blue for one full second")
                        robot.behavior.set_head_angle(degrees(45))
                        score = 0
                elif wins == 2 and value > .5 and value < .75: #blue score += 0.01 if score > 1.0:
                        wins += 1
                        robot.say_text("Good job")
                        animation = 'anim_pounce_success_02'
                        robot.anim.play_animation(animation)
                        robot.say_text("Keep my eyes green for one full second")
                        robot.behavior.set_head_angle(degrees(45))
                        score = 0
                elif wins == 3 and value > .25 and value < .42: #green score += 0.01 if score > 1.0:
                        wins += 1
                        robot.say_text("Good job")
                        animation = 'anim_pounce_success_02'
                        robot.anim.play_animation(animation)
                        robot.say_text("Keep my eyes yellow for one full second")
                        robot.behavior.set_head_angle(degrees(45))
                        score = 0
                elif wins == 4 and value > .1 and value < .175: #yellow score += 0.01 if score > 1.0:
                        wins += 1
                        robot.say_text("Good job")
                        animation = 'anim_pounce_success_02'
                        robot.anim.play_animation(animation)
                        robot.say_text("You win")
                        
                else:
                    score = 0
            if wins == 5:
                ang = robot.pose_angle_rad + 0.01
                robot.behavior.set_head_angle(degrees(45))
                robot.behavior.set_lift_height(1.0)
                while robot.pose_angle_rad < ang:
                    print(ang)
                    print(robot.pose_angle_rad)
                    time.sleep(0.01)
                robot.say_text("Thanks Keith's wife for suggesting the fist bump")
                time.sleep(0.5)
                robot.say_text("Please like the video. Keith is very self conscious about it, l o l")
                break
                
if __name__ == '__main__':
    main()


Python Code Improvements

How would we make it harder to win with yellow?

What about if we wanted to make it easier to win with yellow?

Why did we have two different thresholds for red?

How could this Python code be re-written to be cleaner using functions?

We Also Touch On Other Topics…

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

If Python seems a bit difficult keep your eye on What is Robotics and Kinvert’s Ultimate Guide to Block Coding.

Do you want to learn more Python? Check out Anki Vector Examples as well as Anki Cozmo Examples.

Want to stay up to date with all things Vector and Cozmo? We have a bunch of great stuff we send to the mailing list. You can unsubscribe at any time.

If you have any questions or comments please let us know in the comment sections below 🙂

4 responses to “Vector Game Touch Custom Eye Color

  1. The touch code is not copying over correctly. Vector got stuck after :untouchTotal = 0
    for i in range(20):
    untouchTotal += robot.touch.last_sensor_reading.raw_touch_value
    untouched = untouchTotal/20.0
    time.sleep(1.0)
    robot.say_text(“touch my whole back”)
    So i removed the :NOT: after while NOT and made it to the next step but it kicked vector out of the program.

    Steve

  2. Can you describe what you’re physically doing?

    He needs his whole back to be touched or he won’t calibrate.

    The code needs to wait for you to touch his whole back. That’s why he is waiting while not being touched.

    Sometimes when I copy code over I get indentation errors but I don’t see any here.

  3. I copied it several times, and got several different responses. I just did it again and it worked perfectly. I am wondering if my wifi is having issues. I am now enrolled in Python classes. I want to get to where he will respond with questions and save them to the robot. This is a neat game GOOD JOB!!
    Steve

    1. Thanks Steve!

      Glad you like the game and have it working.

      Have you seen the Free Trial for our Cozmo Course?

      I don’t remember if you also own a Cozmo?

      We’re looking at making a similar course for Vector in the near future 🙂

      Thanks again Steve,

      Keith

Leave a Reply

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