Jump to main content

Take a Hike: Train Your Robot Dog to Walk with a Virtual Leash

1
2
3
4
5
77 reviews

Abstract

Have you ever walked a dog on a leash? Depending on how well-behaved the dog is, it can be very easy or very difficult. It would be great if you could tell your dog exactly how to behave, right? With that in mind, in this project you will build a robotic dog using a LEGO® Mindstorms® kit that you can teach to "walk" using the beam from a flashlight as a virtual leash. But your robot dog will not know how to walk on a leash on its own — you will have to "train" it, just as you would with a real dog. Can you test your programming skills to find out if you can get your robot dog to behave nicely on a leash?

Summary

Areas of Science
Difficulty
 
Time Required
Average (6-10 days)
Prerequisites
Experience building with LEGO pieces and programming with LEGO Mindstorms is required for this project.
Material Availability
This project requires a LEGO Mindstorms kit and compatible programming software. See the Materials and Equipment list for details.
Cost
Very High (over $150)
Safety
No issues.
Credits

Ben Finio, Ph.D., Science Buddies

Science Buddies would like to thank the Tufts University Center for Engineering Education and Outreach at ceeo.tufts.edu for providing the LEGO Mindstorms kit and NXT-G software used to develop this project.

  • LEGO is a registered trademark of The LEGO Group.
  • Mindstorms is a registered trademark of The LEGO Group.

Objective

Use a LEGO Mindstorms kit to build a robotic dog that you train to "walk" using a flashlight as a virtual leash.

Introduction

Have you ever walked a dog on a leash? A well-trained dog will not tug on the leash or try to run off in a different direction — it will follow you around and behave well. If your dog is not well behaved on a leash, you can spend a good deal of time trying to train it to behave the way you want. In this project, you will build your own robot dog and "train" it to walk on a leash by writing a program to control its behavior. But you will not use a real leash — instead, you will use a flashlight beam as a "virtual" leash! Figure 1 shows our robot dog and our real dog.

A dog sits next to a LEGO NXT robot dog
Figure 1. A real dog and a robotic dog (can you guess which is which?). What aspects of training each dog are the same, and which are different? For example, do you think you can use dog treats to train the robotic dog?

But how, you may ask, could you possibly get a robot to follow a flashlight beam? The key is using the LEGO Mindstorm NXT's light sensor. Light sensors can act like a robot's "eyes," though they are much less complicated than your own eyes. Unlike your eyes or the optics of a camera, they do not take an entire image — they simply detect an amount of light, and output a number ranging from 0 (complete darkness) to 100 (very bright). If you build a robot that can steer itself using two motors, then you can write a program that uses the input from two different light sensors to control the robot's steering. This will enable you to "train" your robot dog to follow a flashlight properly. Figure 2 shows an example diagram of a robot with two motors and two light sensors.

Diagram of a light-following robot increasing the power to certain wheels based on where light is detected
Figure 2. This graphic illustrates the concept for a robot that can follow a flashlight beam. The robot has two motors and two forward-facing light sensors. When the flashlight moves from side to side, the left and right light sensors detect different amounts of light. When a robot needs to turn toward the light, the wheel that is farther from the light spins faster, and the wheel that is closer to the light spins slower (shown by the pink arrows), and the robot turns toward the light. When both sensors receive the same amount of light, the wheels spin at the same speed, so the robot advances in a straight line.

Training a robot dog to follow a flashlight beam might seem like a cool idea, but how exactly will you get it to work? You will have to write a program and download it to your NXT. LEGO's NXT software, NXT-G, is a graphical programming language based on icons that we refer to as "blocks." We will give you a quick overview of some of the important blocks in the NXT software that you will need for this project. If you are already familiar with NXT programming, you can skip the rest of the Introduction and move on.

Important: This project is not intended as a complete introduction to NXT programming. If you need help getting started with the basics — like creating a new file, writing a simple program, and downloading it to your NXT — we recommend that you consult your NXT user manual or one of the references in the Bibliography.

Light Sensor

The Light Sensor block can be found in the "Complete palette" tab under "Sensor" (Figure 3A). Once the block is inserted into your diagram (Figure 3B), it has an output for the intensity of the light sensor reading, which will be a number from 0 to 100 (there are other inputs and outputs that you will not use for this project). The options for the light sensor (Figure 3C) lets you select the correct sensor port: 1, 2, 3, or 4.

Note: The directions for this project assume that you will be using Light Sensors. The NXT Color Sensor is also compatible, because it can function in "Light Sensor" mode and measure light intensity. The Color Sensor block can also be found in the "Complete palette" under "Sensor," and it has a drop-down option to select "Light Sensor" mode in its options panel.

A light sensor block is selected in the LEGO NXT-G program and options for the block are displayed

A light sensor block is selected in the LEGO NXT-G program and is placed in the diagram area. It can be found on the left side of the screen under the sensor tab. The light sensor has a small sun shaped icon and can be clicked on to open an options panel. In the options panel there are options to change port numbers, light intensity and light generation.


Figure 3. (A) Where to find the Light Sensor block in the menu. (B) What the Light Sensor block will look like in your diagram. (C) The options panel for the Light Sensor.

Motor

The Motor block can be found in the "Complete palette" tab under "Action" (Figure 4A). It lets you control a single motor at a time. It has many options for inputs and outputs — in this project you will only use the "power" input (Figure 4B). The options panel enables you to set the correct port for the motor (A, B, or C) and the amount of time the motor runs. You will not need to use the other options for this project.

A motor block is selected in the LEGO NXT-G program and options for the block are displayed

A motor block is selected in the LEGO NXT-G program and is placed in the diagram area. It can be found on the left side of the screen in the action tab. The motor has a gear shaped icon and can be clicked on to open an options panel. In the options panel there are options to change port numbers, directions, power levels, and wait times.


Figure 4. (A) Where to find the Motor block in the menu. (B) What the Motor block will look like in your diagram. (C) The options panel for the Motor block.

Move

The Move block can be found under "Common" on the "Complete palette" tab (Figure 5A). Unlike the Motor block, which only controls one motor at a time, the Move block lets you control two motors at once, with separate power and steering inputs (Figure 5B). As with the Motor and Light Sensor blocks, the Move block has other options that you will not need for this project.

A move block is selected in the LEGO NXT-G program and options for the block are displayed

A move block is selected in the LEGO NXT-G program and is placed in the diagram area. It can be found on the left side of the screen in the common tab. The move block has two gear shaped icons and can be clicked on to open an options panel. In the options panel there are options to change port numbers, directions, steering, power levels, duration and braking and coasting actions.


Figure 5. (A) Where to find the Move block in the menu. (B) What the Move block will look like in your diagram. (C) The options panel for the Move block.

Math

The Math block can be found under "Data" on the "Complete palette" tab (Figure 6A). It takes two numbers as inputs, does a basic calculation, and outputs the answer (Figure 6B). The options panel for the Math block lets you select addition, subtraction, multiplication, division, absolute value, and square root (Figure 6C).

A math block is selected in the LEGO NXT-G program and options for the block are displayed

A math block is selected in the LEGO NXT-G program and is placed in the diagram area. It can be found on the left side of the screen in the data tab. The math block has a icon with arithmatic symbols and can be clicked on to open an options panel. In the options panel there are options to change arithmatic operations and set variables.


Figure 6. (A) Where to find the Math block in the menu. (B) What the Math block will look like in your diagram. (C) The options panel for the Math block.

Loop

You use a loop to make a program run more than once. A loop tells the program to "start over" once it reaches the end. This concept is used all the time in many different programming languages, and not just for the NXT. In NXT-G, the Loop can be found under "Flow" on the "Complete palette" tab (Figure 7A). The Loop can wrap around other blocks in your program to make them repeat. Figure 7B shows an empty Loop. The options panel for the Loop lets you set whether the Loop will run forever or for a certain amount of time, or tell it to stop based on a sensor reading or something else in your program (Figure 7C).

A loop is selected in the LEGO NXT-G program and options for the loop are displayed

A loop block is selected in the LEGO NXT-G program and is placed in the diagram area. It can be found on the left side of the screen in the flow tab. The math block has a icon with two parallel bars and can be clicked on to open an options panel. In the options panel there are options to change control and counter for the loop.


Figure 7. (A) Where to find the Loop in the menu. (B) What an empty Loop looks like in your diagram. (C) The options panel for the Loop.

Terms and Concepts

Questions

Bibliography

Here are some useful introductions to programming LEGO's official NXT software. Remember that you can also consult the NXT User Guide CD that came with your NXT kit.

Of course, you are free to design and build your own LEGO vehicle for your robot dog. If you need help building a robot, the LEGO Tribot design is a good place to start. Your NXT kit may have included some building instructions, but they are also available online if you search for them.

Materials and Equipment

LEGO Mindstorms Availability

Note: The LEGO Mindstorms product line was discontinued at the end of 2022. However, many Mindstorms kits are still in circulation and you can probably borrow one or purchase a used one. The instructions for this project were written for the LEGO Mindstorms NXT 2.0 kit. You can do this project with an EV3 kit, but the programming software is different. Follow LEGO's online resources to learn how to program your EV3 robot.

Experimental Procedure

Building Your Robot

Before you start programming, you will need to build your robot. If you are an experienced LEGO builder, feel free to create your own design. If you need help, the LEGO Tribot design is a good place to start. Regardless of your exact design, remember that you will need two motors (one for the left and another for the right) for steering, and two forward-facing light sensors (left and right — not above and below each other). Our robot design (along with our robot dog costume) is shown in Figure 8.

A LEGO tribot design with two motors and two light sensors next to a dog costume made of paper
Figure 8. Our NXT robot design, with two motors and two forward-facing light sensors (left), and our dog costume (right). Make sure your costume has "eye holes" for the light sensors!

Note: To avoid confusion, we recommend plugging your "left" and "right" sensors and motors into the left and right ports on the robot, respectively. So, (from the robot's point of view), the right-hand motor and sensor should be plugged into ports A and 1, respectively, and the left-hand motors and sensor should be plugged into ports C and 4, respectively.

Once you have finished building your robot, you are ready to start training your robot dog! You will do this in three steps — with beginner, intermediate, and advanced programs.

Beginner Training

The first program you write will be very simple. It takes the output of each light sensor (a number between 0 and 100) and connects it directly to the power input on a motor (which accepts numbers between -100 and +100). The trick is that these connections are criss-crossed — the left light sensor controls the right motor, and vice versa. This is what enables the robot to turn toward light instead of away from it. (If this does not make sense, refer back to Figure 2 in the Introduction section). Figure 9 shows a screenshot of the program you need to write. It uses two Light Sensor blocks and two Motor blocks inside a Loop. The intensity of Light Sensor 1 controls the power of Motor C, and the intensity of Light Sensor 4 controls the power of Motor A. You can also write these rules in equation form:



Equation 1:


Equation 2:

Blocks arranged in the LEGO NXT-G program connect one light sensor to each motor

Two light sensor blocks and two motor block in the diagram area in the program LEGO NXT-G. A line connects the first light sensor block to the first motor block and another line connects the second light sensor block to the second motor block.


Figure 9. The Beginner Training program. Using two Light Sensor blocks and two Motor blocks, connect the inputs and outputs as shown (it is OK to leave the inputs and outputs that you are not using disconnected), all inside a Loop. Pay close attention to the ports you set for your sensors and motors. Notice that the Loop is set to run "forever" (the infinity symbol, ∞). Be sure to uncheck the "Generate light" box for each Light Sensor.

Note: Remember to replace the Light Sensor block with a Color Sensor block if you are using a color sensor, and set it to "Light Sensor" mode in the drop-down menu at the bottom of the screen. Everything else in the program should remain the same.

After you have written this program and downloaded it to your NXT robot, get your flashlight — it is time to test it out! Can you get your robot to follow you by pointing the flashlight's beam at it? If you aim the beam more toward one side of the robot, does it turn toward that side? How well does it turn? Remember to aim your beam at the light sensors and not at the floor in front of the robot.

Troubleshooting: Depending on how you built your robot and how you wired your motors and sensors, as well as where you are testing your robot, you could run into three potential problems at this point:
  1. My robot is going backward! Change the "direction" from "forward" to "backward" in the Options panel for each one of your Motor blocks. This will reverse the direction in which the motors spin. Re-download your program and make sure your robot moves forward.
  2. My robot is turning away from the light instead of toward it! Switch the connections between your sensors and motors in your program (connect Light Sensor 1 to Motor A, and Light Sensor 4 to Motor C). Re-download your program and make sure your robot turns in the right direction.
  3. My robot is moving and turning, but not responding to my flashlight! Make sure you are not near another very bright light source, like an open window on a sunny day. Other bright lights could overwhelm your flashlight beam. If need be, try moving to a darker room if possible.

Did it work? If so, congratulations! Your robot dog has graduated from beginner obedience class. You are ready to move on to the next level.

Intermediate Training

In the Beginner Training program, you connected the Light Sensor's intensity values directly to the motor's power inputs. This works, but it has some limitations. The light intensity is always greater than or equal to zero (it has a range of 0-100). This means that your motors will both always spin forward. But remember that the motor power has an input range of -100 to +100, meaning it can accept negative numbers to make the motor spin backward. If one motor spins backward and the other spins forward, your robot can turn much faster. So, you will modify your program to use a single Move block instead of two Motor blocks. The Move block has one input for steering and one input for power.

To make the robot respond to two Light Sensors, you can use the difference between the light sensor intensity values as an input to the Move block's steering value. You can use the average of the two readings as the input to the power value. This can be expressed in equations:



Equation 3:


Equation 4:

Take a minute to think about what these equations mean. What happens if both Light Sensors have the exact same value? The steering value will be zero — so the robot will go straight. If the values are different, the steering value will not be zero — and the robot will turn. Can you figure out what the "power" equation does? What happens if both Light Sensors read zero, or both read 100?

The program is shown in Figure 10:

Blocks arranged in the LEGO NXT-G program use data from light sensors and math blocks to control motors

Two light sensor blocks, three math blocks, a constant block and motor block in the diagram area in the program LEGO NXT-G. A line connects the first light sensor block to the first math block and the third math block. The second light sensor block is connected to the first math block and the third math block. The first math block is connected to the second math block. The constant block is connected to the second math block. The second and third math blocks are connected to the motor block.


Figure 10. The Intermediate Training program, which follows Equations 3 and 4. Notice how you must use Math blocks (including a "Constant" block, found under the "Data" menu) to calculate the difference and average of the two Light Sensor values. Pay close attention to assigning the proper ports to your motors and sensors in the options panel for each block, and selecting the correct mathematical operations (addition, subtraction, and division in this case). The value of the "Constant" block (the suitcase icon with a lock in the middle) should be set to 2. Be sure to uncheck the "Generate light" box for each Light Sensor.

Note: Remember to replace the Light Sensor block with a Color Sensor block if you are using a Color Sensor, and set it to "Light Sensor" mode in the drop-down menu at the bottom of the screen. Everything else in the program should remain the same.

After you have written your program, download it to your robot, get your flashlight, and test it out!

Troubleshooting: The troubleshooting steps you will follow in this case are slightly different from those in the "Beginner Training" program.

Note: If your robot is turning the wrong way: Rather than switching around the wires connecting inputs and outputs (there are a lot of them!), it is easier to just switch the ports on your two Light Sensor blocks. You can do this in the options panel for each block.

If your robot is going backward: Switch the "direction" to "backward" in the Options panel for the Move block.

Does it work? If so, how does it compare to the Beginner Training program? Are you (and your robot) ready to move on to Advanced Training?

Advanced Training

The Intermediate Training program enabled your robot's wheels to spin forward and backward, which should also have enabled it to make tighter turns. However, there is no way to adjust how fast the robot turns or moves forward in response to light. This Advanced Training program will do a little more math to let you adjust the speed at which your robot turns and moves forward — giving you exactly the behavior you want! This program will follow two new equations, which are slightly modified versions of Equations 3 and 4:



Equation 5:


Equation 6:

The introduction of Constant A and Constant B gives you control over the forward speed and turning. For example, if you make Constant A larger, the robot will turn faster. If you make it smaller, the robot will turn slower. The same effect applies to Constant B and forward speed. Note that if you make Constant A and Constant B both equal to one, then Equations 5 and 6 are the same as Equations 3 and 4. Figure 11 shows the example program that uses these equations.

Blocks arranged in the LEGO NXT-G program use data from light sensors and math blocks to control motors

Two light sensor blocks, five math blocks, three constant blocks and motor block in the diagram area in the program LEGO NXT-G. A line connects the first light sensor block to the first math block and the third math block. The second light sensor block is connected to the first math block and the third math block. The first math block is connected to the second math block. The constant block is connected to the second math block. The second math block and third constant block are connected to the fifth math block. The third math block and second constant block are connected to the fourth math block. The fourth and fifth math block are connected to the motor block.


Figure 11. The Advanced Training program, which follows Equations 5 and 6. Two new constants have been introduced that are multiplied by the difference and average of the Light Sensor readings, respectively. As always, pay attention to the port assignments for your motors and sensors. The values of the Constant A and Constant B are up to you! Be sure to uncheck the "Generate light" box for each Light Sensor.

Write the program in Figure 11, remembering to pick your own values for Constant A and Constant B. Download the program to your robot, and try it out.

Note: Remember to replace the Light Sensor block with a Color Sensor block if you are using a Color Sensor, and set it to "Light Sensor" mode in the drop-down menu at the bottom of the screen. Everything else in the program should remain the same.

How does your robot behave? Is the behavior different from the previous training programs? Now, try different values for Constant A and Constant B. What happens to the turning and forward speed? Can you pick values that give you the behavior you like best? If so, congratulations — you have successfully trained your robot dog!

Troubleshooting: To troubleshoot the Advanced Training program, follow the same troubleshooting steps that were presented for the Intermediate program.

Compare Your Programs

How different was your robot's behavior with each program? Which one did you like best? Can you think of a good way to describe the results of each different program?

Congratulations, you are now a real robot dog trainer!

icon scientific method

Ask an Expert

Do you have specific questions about your science project? Our team of volunteer scientists can help. Our Experts won't do the work for you, but they will make suggestions, offer guidance, and help you troubleshoot.

Global Connections

The United Nations Sustainable Development Goals (UNSDGs) are a blueprint to achieve a better and more sustainable future for all.

This project explores topics key to Industry, Innovation and Infrastructure: Build resilient infrastructure, promote sustainable industrialization and foster innovation.

Variations

  • What happens if you change the physical location of the light sensors on your robot (how far apart they are from each other, and/or the direction they are facing)? Does this affect the robot's steering and behavior?
  • The directions in this project train the robot to follow the beam from a flashlight. Can you modify the program so you can push the robot with a flashlight beam? Can you still steer it in the right direction?
  • Can you devise an experiment to measure how fast the robot turns and moves forward using the Advanced Training program, for different values of the constants in Equations 5 and 6?
  • What about programming other behaviors into your robot dog? Can you use the touch or sound sensors to make it respond to touch or voice commands? For example, could you tap the robot to tell it to hold still (regardless of whether it sees the flashlight beam), and then use a voice command to tell it to start again?

Careers

If you like this project, you might enjoy exploring these related careers:

Career Profile
Have you watched "The Transformers" cartoon series or seen the "Transformers" movies? Both shows are about how good and evil robots fight each other and the humans who get in the middle. Many TV shows and movies show robots and humans interacting with each other. While this is, at present, fantasy, in real life robots play a helpful role. Robots do jobs that can be dangerous for humans. For example, some robots defuse landmines in war-stricken countries; others work in harsh environments like… Read more
Career Profile
Mechanical engineers are part of your everyday life, designing the spoon you used to eat your breakfast, your breakfast's packaging, the flip-top cap on your toothpaste tube, the zipper on your jacket, the car, bike, or bus you took to school, the chair you sat in, the door handle you grasped and the hinges it opened on, and the ballpoint pen you used to take your test. Virtually every object that you see around you has passed through the hands of a mechanical engineer. Consequently, their… Read more
Career Profile
Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. Read more
Career Profile
Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer. Read more

News Feed on This Topic

 
, ,

Cite This Page

General citation information is provided here. Be sure to check the formatting, including capitalization, for the method you are using and update your citation, as needed.

MLA Style

Science Buddies Staff. "Take a Hike: Train Your Robot Dog to Walk with a Virtual Leash." Science Buddies, 8 Feb. 2023, https://www.sciencebuddies.org/science-fair-projects/project-ideas/Robotics_p011/robotics/lego-robot-dog?from=Blog. Accessed 19 Mar. 2024.

APA Style

Science Buddies Staff. (2023, February 8). Take a Hike: Train Your Robot Dog to Walk with a Virtual Leash. Retrieved from https://www.sciencebuddies.org/science-fair-projects/project-ideas/Robotics_p011/robotics/lego-robot-dog?from=Blog


Last edit date: 2023-02-08
Top
We use cookies and those of third party providers to deliver the best possible web experience and to compile statistics.
By continuing and using the site, including the landing page, you agree to our Privacy Policy and Terms of Use.
OK, got it
Free science fair projects.