Misty Lessons
  • Misty Lessons
    • 📖Welcome to Misty Lessons
    • 📚Get to know your Misty
    • 📲Connect to Misty
    • 👩‍💻Misty Studio
    • 🖥️Desktop Environment
    • ⬆️Update your Misty
    • 👥Projects
  • Blockly
    • 🧩Blockly Lessons
      • 🤸Lesson 1: Movement
      • 🎶Lesson 2: Voice and Sound
      • 🎭Lesson 3: Expressions
      • 🎥Lesson 4: Robot Cinema
      • 🛠️Lesson 5: Events
      • ☺️Lesson 6: Face recognition
      • 🔢Lesson 7: Variables and Functions
      • 💬Lesson 8: NLP
    • 🏫Blockly projects
  • Blockly Elements
    • ⚛️Misty Blocks
      • Movement
      • Speech
      • Audio
      • Vision
      • Events
      • Miscellaneous
      • NLP
      • System
    • 🔁Basic Blocks
      • Logic
      • Loops
      • Math
      • Text
      • Lists
    • 🅰️Advanced Blocks
      • Variables
      • Functions
  • Python
    • 🐍Python Lessons
      • 🦿Lesson 1: Loco-motion
      • 🤖Lesson 2 : Build a character
      • 🧠Lesson 3: Create memories
      • ⚒️Lesson 4: Event skills
      • 👀Lesson 5: Expand awareness
      • 🔗Lesson 6: Compact code
      • 🗣️Lesson 7: Start a conversation
  • Python Elements
    • 🐸Misty Python API
      • Motion and Mobility
      • Display and LED
      • Record Assets
      • Change/Remove Assets
      • Stream Assets
      • Get Assets
      • Events
      • Sensor Events
      • Speech and NLP
      • Arduino Backpack
      • System
      • Slam
    • 📗Python Definitions
  • Python Projects
    • 🔮MistyGPT
    • 🚨Misty Intruder Alert
    • 📺Conference Assistant
    • 🏷️QR code detector
    • 🕵️‍♂️Misty follow human
    • 👋Misty wave back
    • 🖲️Misty OA
    • 🌐Get weather
    • 🚚Misty Delivery
    • 🫂Motivational Misty
    • 🖼️Misty Museum Guide
    • 🎃Who for Halloween
  • ARDUINO
    • ♾️Arduino Backpack
    • 🦎Arduino
    • 🔧Arduino Lessons
      • 🔌Arduino to Misty
      • ➕Misty to Arduino
  • ARDUINO PROJECTS
    • 🛠️Misty Tracker
    • 🦾Misty Arm
  • Community Projects
    • 🌤️Misty weather forecaster
  • HARDWARE EXTENSION
    • ⚙️Arduino breadboard support
    • 🦾Misty arm
    • 🥤Tin holder
  • Resource Database
    • 📁Image files
    • 📁Audio files
    • 📁Languages
    • 📁Known objects
    • 📁NLP Actions
    • 📁Action Commands
    • 📁ChatGPT PDF files
    • 📁AR Tag Dictionary
    • ⚙️Technical Specifications
Powered by GitBook
On this page
  1. Python Projects

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 7 months ago

🖲️