Menu Close

Anki Vector Distance Sensor

Online information technology education for children.

We will be using the following Anki Vector Distance Sensor Proximity Commands

Here they are:

robot.proximity.last_sensor_reading.distance

robot.behavior.say_text() – Behaviors represent a complex task which requires Vector’s internal logic to determine how long it will take. This may include combinations of animation, path planning or other functionality. Examples include drive_on_charger, set_lift_height, etc.

getting distance_mm – Represents a distance. The class allows distances to be returned in either millimeters or inches.Use the distance_inches() or distance_mm() convenience methods to generate a Distance instance.

for loops – A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

Use this program to easily measure distance.

#Marfag.co.uk

import anki_vector
import time

def main():
    args = anki_vector.util.parse_command_args()
    with anki_vector.Robot() as robot:
        for _ in range(2):
            if robot.proximity.last_sensor_reading:
                distance = robot.proximity.last_sensor_reading.distance
                print("*********************************************************************")
                print(distance.distance_mm)
                print("*********************************************************************")
                robot.behavior.say_text(str(distance.distance_mm) + " millimeters")
                time.sleep(4.0)
                
            else:
                print("It is very difficult for me")
                robot.behavior.say_text("I am hungry. Do you have pasta?")
                time.sleep(3.0)

if __name__ == "__main__":
    main()
Python

Leave a Reply