🖲️Misty OA

With the Obstacle Avoidance skill Misty will be able to drive and navigate your space without difficulties!

The idea behind this project is as simple as possible: if Misty detects a shorter distance between her sensors and a flat surface, for example, shorter than 40 cm, she changes direction and tries to avoid the obstacle by turning, going straight forward for a short distance, turning and checking the new distance.

To do so it will be necessary to use the TOF (Time of Flight) event that contains all the measurements of Misty's distance sensors.

from mistyPy.Robot import Robot
from mistyPy.Events import Events
import time

misty = Robot("YOUR-ROBOT-IP-ADDRESS")
misty.change_led(0, 255, 0)
misty.move_head(0, 0, 0)
misty.display_image("e_DefaultContent.jpg")

def TOF(data):
    if data["message"]["sensorPosition"] == "Center": #gets the central sensor distance
        dist = data["message"]["distanceInMeters"]
        print(dist)
        time.sleep(0.5)
        if dist < 0.4: # checks if it is less than 40cm
            misty.display_image("e_Contempt.jpg")
            misty.play_audio("s_PhraseOopsy.wav")
            print("obstacle")
            misty.drive_time(0, 50, 4500)
            time.sleep(5)
            misty.drive_time(20, 0, 2000)
            time.sleep(3)
            misty.drive_time(0, -50, 4500)
            time.sleep(5)
            misty.drive_time(0, 0, 2000)
            time.sleep(2)
            # Unregister the event to clean the data
            misty.unregister_event("TOF_event")
            # Registers the new event that will do the same
            misty.register_event(event_name='TOF_event_obstacle', event_type=Events.TimeOfFlight, callback_function=TOF_obstacle, keep_alive=True, debounce = 100)
            time.sleep(2)
        else :
            misty.drive(10, 0)
            print("free")

def TOF_obstacle(data):
    if data["message"]["sensorPosition"] == "Center":
        dist = data["message"]["distanceInMeters"]
        print(dist)
        time.sleep(0.5)
        if dist < 0.4:
            misty.display_image("e_Contempt.jpg")
            misty.play_audio("s_PhraseOopsy.wav")
            print("obstacle_obst")
            misty.drive_time(0, 50, 4500)
            time.sleep(5)
            misty.drive_time(20, 0, 2000)
            time.sleep(3)
            misty.drive_time(0, -50, 4500)
            time.sleep(5)
            misty.drive_time(0, 0, 2000)
            time.sleep(2)
            misty.unregister_event("TOF_event_obstacle")
            misty.register_event(event_name='TOF_event', event_type=Events.TimeOfFlight, callback_function=TOF, keep_alive=True, debounce = 100)
            time.sleep(2)
        else :
            misty.drive(10, 0)
            print("free_obst")

misty.register_event(event_name='TOF_event', event_type=Events.TimeOfFlight, callback_function=TOF, keep_alive=True, debounce = 100)
misty.keep_alive()

The reason why there are two functions that look almost exactly the same is related to an hidden issue in this project: the old distances.

After Misty detects an obstacle a time.sleep() command is sent to allow her to comfortably stop and proceed to the next action. During this time the TOF event is still active and this means that Misty keeps recording the distances, after entering the if statement which corresponds to the obstacle detected gate. When Misty will start recording the new measures, these will be the old ones and the skill will not work according to the reality.

The easiest way to get rid of them is simply to unregister the whole event, so every information about it will be deleted. After we register a completely new event, with a similar callback function, where only the name is different, that will keep Misty's behaviour the same way. Then when Misty meets a new obstacle she'll move and then she will unregister this second event (the first event is called "TOF_event", the second "TOF_event_obstacle") and go back to the previous one.

With this flow, the two functions and events loop without bothering each other.

In the code, after detecting an object, Misty turns to the left, travels for two seconds forward, and then turns to the right to be in the correct position for the next check.

To make Misty turn to the right you can replace the misty.drive_time() commands with these:

misty.drive_time(0, -50, 4500)
time.sleep(5)
misty.drive_time(20, 0, 2000)
time.sleep(3)
misty.drive_time(0, 50, 4500)
time.sleep(5)
misty.drive_time(0, 0, 2000)
time.sleep(2)

Enjoy your Obstacle Avoidance skill with Misty, personalize it and share it to the world!

Last updated