Main Content

Develop Path-Following Robot Using Raspberry Pi and Simulink

This example shows how to use the Simulink® Support Package for Raspberry Pi® Hardware to develop a path-following robot.

Introduction

The support package enables you to build a simple path-following robot by interfacing sensors and motors and by configuring the GPIO pins of Raspberry Pi Hardware board. This example uses two IR sensors to detect whether the robot moves along the specified path and two motors to correct the robot if it deviates. The Raspberry Pi Hardware board receives information from the two IR sensors mounted on either side of the robot, and it instructs the motors connected on either side of the robot to either move forward or stop or move right or left by analyzing the sensor information. You can consider black patch on a white surface as the path for the robot.

This example shows how to develop and deploy a path-follower robot using the Raspberry Pi hardware.

Prerequisites

Complete the Get Started with Simulink Support Package for Raspberry Pi Hardware example.

Required Hardware

To run this example, you need the following hardware.

  • Raspberry Pi hardware board

  • IR sensors

  • Motors

  • Motor driver hardware such as L298 driver

  • Batteries

  • Chassis with wheels to set up the above hardware and make a robot

  • Path for the robot to follow

Hardware Setup

Sensor Setup

  • Connect the two IR sensors to GPIO pins 14 and 24 of the Raspberry Pi hardware board.

  • Mount the two IR sensors near the front-facing section of chassis to detect whether the robot deviates from left or right.

DC Motor Setup

Use two motors to control the direction of the robot and connect them to driver hardware such as the L298 driver, as the Raspberry Pi board does not have enough current. You can power driver hardware with external power supply such as batteries. Link the two motors to the two wheels of the chassis. You can refer to the motors as left and right depending on their connections to the robot.

  • Connect enable pins of the driver hardware to GPIO pins 9 and 10 of Raspberry Pi board and configure them accordingly. The enable pins of the driver hardware control the start and stop actions in the left and right motors.

  • Connect IN1 and IN2 pins of the driver hardware corresponding to the left motor to GPIO pins 17 and 27 of the Raspberry Pi board and configure them accordingly.

  • Connect IN3 and IN4 pins of the driver hardware corresponding to right motor to GPIO pins 11 and 15 of the Raspberry Pi board and configure them accordingly. The input pins control the motor direction.

Simulink Model

Open the raspberrypi_robotics_pathfollowing Simulink model.

The model is divided into three areas based on the functionality of the blocks in each area.

Data Source

The IR Sensor Data subsystem acquires data from the left and right IR sensors and outputs it to the Motor Control subsystem. This data is useful to determine if the robot is following the path or deviating from it.

Algorithm

The Motor Control subsystem reads the IR sensor data and forwards it to the Actuator subsystem that controls the power and direction of the motors connected. The motors run at a constant speed, which you can specify in the interactive dashboard panel below the algorithm block.

Actuators

This block instructs the motors to turn either left or right to correct the path of the robot and drives the motors such that the robot follows the path again.

This subsystem completes one of the following actions.

  • If the IR sensor data indicates that the robot is following the path, the subsystem instructs the motors to move forward at the specified speed.

  • If the left IR sensor detects a black line, indicating that the robot is deviating on the left, the Actuator subsystem instructs the left motor to stop and the right motor to move forward, resulting in a left turn by rotating the right wheel alone.

  • If the right IR sensor detects a black line, indicating that the robot is deviating on the right, the Actuator subsystem instructs the right motor to stop and the left motor to move forward, resulting in a right turn by rotating the left wheel alone.

Deploy Simulink Model on Raspberry Pi Hardware

Follow these steps to deploy the Simulink model.

1. On the Hardware tab of the Simulink model, in the Mode section, select Run on board.

2. On the Hardware settings tab of the Simulink model, in the Hardware Implementation section. Select Enable deployment for Dashboard blocks.

3. In the Deploy section of the Simulink model, click Build, Deploy & Start. The generated code is built on the Raspberry Pi hardware and runs automatically.

4. Below the Simulink model, click View diagnostics to open the Diagnostic Viewer dialog window.

5. Click the index.html link to open the dashboard panel in a web browser after the model is successfully deployed on your Raspberry Pi hardware.

6. The dashboard panel opens in a web browser, where you can control the reference speed of the motor.

7. Observe the robot following the specified path by driving its motors using the live IR sensor data.

Other Things to Try

Follow the steps in this example and develop path-following robot using color or ultrasonic sensors.

See Also