Kinvert will help you use the Anki Vector distance sensor using the Proximity class. In this Python example we will make a verbal ruler in the Vector SDK.
Can’t Wait to use Vector’s Distance Sensor?
Neither can we. This is Kinvert’s first time using the distance sensor in the front of Vector. This replaced Cozmo’s IR LED to add highly needed functionality to the SDK.
If you are interested in the differences between Cozmo and Vector be sure to check out our Anki Cozmo vs Vector Comparison.
Unfortunately you can’t do this with Cozmo. However, Cozmo is programmable also with the Cozmo SDK.
We are using some pretty simple commands to show you how to use this very handy distance sensor. You’ll also see how to make Vector say text, as well as get some practice with a for loop, conditional statements, and print commands.
Let’s take a look at the commands a bit before diving in to the code.
Anki Vector Distance Sensor Proximity Commands
What commands will we be using? Here they are:
- getting distance_mm and distance_inch
- for loops
We will link to the documentation once it is available online.
Keep in mind the argument we are passing is a value in radians per second.
We will use a combination of these commands to make Vector drive a figure 8 pattern.
Eventually there will likely be online documentation for this. For now, you can check out an article Anki wrote about us here: https://developer.anki.com/blog/features/interview/from-stem-to-learn/
Turning Vector into a Verbal Ruler – Code
Let’s take a look at the code. We will be using anki_vector.Robot.proximity.last_sensor_reading.distance to turn Vector into a super handy verbal ruler. At first I just had this using one unit, but then I realized the other half of the planet would get on my case. So I included both units here. So let’s dive in and see how we used the Anki Vector distance sensor to make this verbal ruler.
""" 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/ """ import anki_vector import time def main(): args = anki_vector.util.parse_command_args() with anki_vector.Robot() as robot: for _ in range(5): if robot.proximity.last_sensor_reading: distance = robot.proximity.last_sensor_reading.distance print("=====================================================================") print(distance.distance_inches) print("=====================================================================") robot.say_text(str(distance.distance_mm) + " millimeters or " + str("%.2f" % distance.distance_inches ) + "freedom units") time.sleep(4.0) else: print("Can't be bothered to work right now") robot.say_text("Hold your horses") time.sleep(3.0) if __name__ == "__main__": main()
How would you get Vector to do more measurements?
What if you only wanted one of these units, and not both?
Why do you think I added the if robot.proximity.last_sensor_reading to the code? What happens if you get rid of that? What might be a better way of handling the problem than I did? Please let me know in the comments below.
How would you increase the precision of the inch value Vector says by a one more significant figure?
Vectoring the Kinvert Way
Kinvert is well known for the fun STEM classes we provide. This is especially true of our Cozmo courses (and maybe we’ll have Vector courses soon too). To make these courses possible we use the Anki Cozmo SDK and the Anki Vector SDK.
We added a couple funny things Vector says. How could you change this verbal ruler to make it more funny?
We hope you enjoyed this Anki Vector Example Using the SDK using the Anki Vector distance sensor. We have a lot more of these on the way. So hold your horses 🙂
Please let us know in the comments below if this helped you, or if you made any cool modifications. We’d love to hear from you.
Cozmo Examples, Tutorials, and Projects for STEM Education Curriculum is the place for Cozmo examples, in case you don’t have Vector yet.