Cerca codice

Course Lesson 9 of 10: Obstacle Avoidance using Raspberry Pi Pico 4WD Smart Car

Questa lezione fa parte di: Raspberry Pi Pico 4WD Smart Car

Course Lesson 9 of 10: Obstacle Avoidance using Raspberry Pi Pico 4WD Smart Car

Introduction

This guide, based on Course Lesson 9 of 10 from Robojax, demonstrates how to build an obstacle-avoiding robot using the SunFounder Raspberry Pi Pico 4WD Smart Car kit. The project combines a servo-driven ultrasonic sensor with motor control to create a robot that can navigate its environment autonomously. By sweeping the sensor like a radar, the car detects obstacles and intelligently chooses a clear path to proceed, making it a perfect introduction to robotics, sensor integration, and autonomous navigation.

This project isn't just a toy; it's a foundation for many practical applications. Here are a few ideas to inspire you:

  • Autonomous Exploration Robot: Build a robot that can map and explore a room or outdoor area without human intervention.
  • Smart Vacuum Cleaner: Use the same principles to create a robot that navigates a room, avoiding furniture and walls while cleaning.
  • Security Patrol Bot: Program the car to patrol a perimeter and use the ultrasonic sensor to detect and report unexpected obstacles or intrusions.
  • Educational Platform for AI: Use this project as a stepping stone to learn about more complex algorithms like pathfinding and simultaneous localization and mapping (SLAM).

This lesson focuses on the core logic and code that enable the car to make decisions. The car uses a servo motor to rotate an ultrasonic sensor, acting as a radar to measure distances from 0 to 180 degrees. The code analyzes this data to find the widest empty path and directs the car to move forward, left, or right accordingly. The video demonstrates the entire process, from testing the individual components to running the final obstacle avoidance algorithm in a real-world maze.

Hardware/Components

This project is based on the SunFounder Raspberry Pi Pico 4WD Smart Car kit. The essential components used in this lesson are:

  • Raspberry Pi Pico microcontroller board
  • SunFounder 4WD Smart Car chassis with four motors
  • Motor driver board
  • Ultrasonic distance sensor (HC-SR04 type)
  • Servo motor (for rotating the ultrasonic sensor)
  • Power supply (batteries for the car)

Wiring Guide

The SunFounder kit is designed for easy assembly, and the wiring is typically pre-connected via a dedicated expansion board. The ultrasonic sensor is mounted on the servo motor, which is then connected to the main control board. This setup allows the sensor to be rotated to scan the environment. For a detailed wiring diagram specific to your kit, please refer to the official SunFounder documentation.

The core connections involve the ultrasonic sensor's trigger and echo pins, and the servo motor's signal pin, all being connected to specific GPIO pins on the Raspberry Pi Pico. The motor driver is also connected to the Pico to control the four drive motors. The video mentions that the full documentation, including wiring diagrams, is available for download with the kit.

Code Explanation

This lesson focuses on the logic within the `obstacle_avoid` example. The code is written in MicroPython. While the full program is available for download, we will focus on the key, user-configurable parameters that control the car's behavior. These are typically found at the top of the script.

Here are the main variables you can adjust to fine-tune your robot's performance:


# Radar and movement parameters
radar_reference = 20  # Minimum distance in cm to be considered safe
step_angle = 10       # The angle increment for each radar scan step
forward_speed = 30    # Motor speed for moving forward (0-100)
turn_speed = 50       # Motor speed for turning (0-100)
forward_scan_angle = 50  # The angle the servo scans when no obstacle is detected
  • radar_reference: This is the crucial safety distance in centimeters. If an obstacle is detected closer than this value, the car will consider it a "danger" and initiate an avoidance maneuver. In the video, this is set to 20 cm.
  • step_angle: This defines the resolution of the radar scan. A smaller value (e.g., 10 degrees) means a more detailed scan but takes longer. A larger value would be faster but could miss narrow obstacles.
  • forward_speed & turn_speed: These control the power sent to the motors. You can adjust them to change the car's overall speed and agility. The turning speed is often set higher to ensure the car can pivot effectively.
  • forward_scan_angle: When no immediate danger is ahead, the car narrows its scan to this angle (centered on forward) to check for obstacles more frequently in its direct path, allowing for smoother forward movement.

The video also explains the main decision-making logic. A function `get_direction()` analyzes the list of distance readings (converted to 1s and 0s for safe/unsafe) to find the widest continuous path of "1"s. Based on where this widest path is, the function returns a direction: "forward", "left", or "right". The main loop then executes the corresponding action, turning the car and re-scanning until the path ahead is clear.

Live Project/Demonstration

The video provides a thorough demonstration of the project. It begins by testing the ultrasonic sensor alone to show how it measures distance and prints the values to the shell. The instructor then explains the obstacle avoidance algorithm using a flowchart, detailing the decision-making process for turning left and right. Finally, the car is placed in a small maze created from boxes to demonstrate the complete obstacle avoidance behavior in action. The car successfully navigates the maze, turning left and right to find a clear path forward.

Video Chapters

  • [00:00] Introduction to Obstacle Avoidance
  • [01:31] Overview of the SunFounder Smart Car Kit and Features
  • [02:47] How the Obstacle Avoidance Feature Works
  • [03:34] Testing the Ultrasonic Sensor
  • [07:45] Explaining the Obstacle Avoidance Algorithm and Flowchart
  • [12:49] Analyzing the Main Obstacle Avoidance Code
  • [15:43] Live Demonstration of the Obstacle Avoidance Logic
  • [22:53] Final Demonstration with a Maze of Obstacles
  • [24:35] Conclusion and Next Steps
Nessun codice allegato.

Risorse e riferimenti

Nessuna risorsa ancora.

File📁

Nessun file disponibile.