Home Store Project Ideas Project Guide Ask An Expert Blog Careers Teachers Parents Students

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

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.

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?

Objective

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

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.

Cite This Page

MLA Style

Science Buddies Staff. "Take a Hike: Train Your Robot Dog to Walk with a Virtual Leash" Science Buddies. Science Buddies, 27 Oct. 2014. Web. 19 Dec. 2014 <http://www.sciencebuddies.org/science-fair-projects/project_ideas/Robotics_p011.shtml?from=Blog>

APA Style

Science Buddies Staff. (2014, October 27). Take a Hike: Train Your Robot Dog to Walk with a Virtual Leash. Retrieved December 19, 2014 from http://www.sciencebuddies.org/science-fair-projects/project_ideas/Robotics_p011.shtml?from=Blog

Share your story with Science Buddies!

I did this project I Did This Project! Please log in and let us know how things went.


Last edit date: 2014-10-27

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.

Robotics science project real dog and 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.

Robotics science project lego mindstorms NXT light sensor steering diagram
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.

Robotics science project  Lego NXT-G light sensor block, menu, options
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.

Robotics science project  Lego NXT-G motor block, menu, options
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.

Robotics science project  Lego NXT-G move block, menu, options
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).

Robotics science project Lego NXT-G math block, menu, options
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).

Robotics science project Lego NXT-G loop block, menu, options
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

  • Robot
  • Motor
  • Power
  • Light sensor
  • Light intensity
  • Program
  • Graphical programming language
  • Icons
  • NXT blocks
    • Light sensor
    • Motor
    • Move
    • Math
  • Loop

Questions

  • Can you find examples of real-life robot dogs? (Hint: For starters, check out this video on YouTube)
  • What other types of sensors are included with the NXT kit? What other kinds of sensors do robots use in real life?
  • Can you find examples of other programming languages? What are programming languages used for besides robotics? (Hint: Have you ever played video games or used the Internet?)
  • Can you think of how you would use the NXT blocks listed in the Introduction to program a robot to follow the beam from a flashlight?

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:

News Feed on This Topic

 
, ,
Reading level:
Note: A computerized matching algorithm suggests the above articles. It's not as smart as you are, and it may occasionally give humorous, ridiculous, or even annoying results! Learn more about the News Feed

Materials and Equipment

Substituting LEGO Mindstorms EV3 for NXT:

This project idea was written using the LEGO Mindstorms NXT 2.0 kit. This project can be done with the EV3 kit, because the building system and sensors are very similar between the two kits. However, the programming environment differs between the two kits. To do this project with EV3 you should still follow the general procedure in this project idea, but you may need to consult additional resources for help programming your robot.

For more information about the NXT and EV3 systems, and compatibility between the two, see Comparing EV3 with NXT and LEGO Mindstorms Frequently Asked Questions.

  • LEGO Mindstorms EV3 kit, available at toy stores or online from Amazon.com, or a LEGO Mindstorms NXT kit
    • Note: the Procedure section gives example code in NXT-G, but not the new EV3 software.
  • Depending on which sensors you already have (typically, NXT and EV3 kits only come with one color sensor), you have several options for this project. The project requires two sensors total (so you may need to order an extra one), and will work with any of these combinations:
    • Two NXT light sensors
    • Two NXT color sensors, operating in "Light Sensor" mode
    • One NXT light sensor and one NXT color sensor operating in "Light Sensor" mode
    • Note: there is also a new color sensor (which can act as a regular light sensor) for the EV3 kit, available from Amazon.com. The EV3 brick will work with the old NXT sensors, but the new EV3 sensors will not work on the old NXT brick.
  • Flashlight
  • Crafts materials to make a robot dog costume (optional)

Share your story with Science Buddies!

I did this project I Did This Project! Please log in and let us know how things went.

Experimental Procedure

Substituting LEGO Mindstorms EV3 for NXT:

This project idea was written using the LEGO Mindstorms NXT 2.0 kit. This project can be done with the EV3 kit, because the building system and sensors are very similar between the two kits. However, the programming environment differs between the two kits. To do this project with EV3 you should still follow the general procedure in this project idea, but you may need to consult additional resources for help programming your robot.

For more information about the NXT and EV3 systems, and compatibility between the two, see Comparing EV3 with NXT and LEGO Mindstorms Frequently Asked Questions.

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.

Robotics science project LEGO Mindstorms NXT light-sensor steering robot and robot costume
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:
Robotics science project beginner Lego NXT-G program for light sensor steering
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:

Robotics science project intermediate Lego NXT-G program for light sensor steering
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.

Robotics science project advanced Lego NXT-G program for light sensor steering
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!

Share your story with Science Buddies!

I did this project I Did This Project! Please log in and let us know how things went.


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?

Share your story with Science Buddies!

I did this project I Did This Project! Please log in and let us know how things went.

Ask an Expert

The Ask an Expert Forum is intended to be a place where students can go to find answers to science questions that they have been unable to find using other resources. If you have specific questions about your science fair project or science fair, 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.

Ask an Expert

Related Links

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

female robotics engineer adjusting robot

Robotics Engineer

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 the bottom of the ocean and on the planet Mars. At the heart of every robot is a robotics engineer who thinks about what a robot needs to do and works with several engineering disciplines to design and put together the perfect piece of equipment. Read more
Mechanical engineer building prototype

Mechanical Engineer

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 skills are in demand to design millions of different products in almost every type of industry. Read more
Computer programmer typing on a keyboard

Computer Programmer

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
NASA flight software engineer

Computer Software Engineer

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

 
, ,
Reading level:
Note: A computerized matching algorithm suggests the above articles. It's not as smart as you are, and it may occasionally give humorous, ridiculous, or even annoying results! Learn more about the News Feed

Looking for more science fun?

Try one of our science activities for quick, anytime science explorations. The perfect thing to liven up a rainy day, school vacation, or moment of boredom.

Find an Activity