Use the Force! Push & Pull Robots With an Invisible Force
AbstractIf you have seen any of the "Star Wars" movies, then you probably remember when Jedi knights like Luke Skywalker and Obi-Wan Kenobi used "The Force" to push, pull, and even throw objects — including some helpless battle droids. Have you ever wished you could do that in real life? In this robotics project, you will build and program a LEGO® robot that you can push and pull using an "invisible force" — almost like a real Jedi! We make no promise that you will be taking down the Death Star anytime soon, but this could be a good start.
Ben Finio, Ph.D., Science Buddies
Science Buddies would like to thank the Tufts University Center for Engineering Education and Outreach for providing the LEGO Mindstorms kit and NXT-G software used to develop this project.
- LEGO® and Mindstorms® are trademarks of the LEGO Group.
Build a LEGO® Mindstorms® robot that you can push and pull by programming it to maintain a preset distance from your hand.
If you saw any of the "Star Wars" movies, you probably remember Jedi knights like Luke Skywalker or villains like Darth Vader using "The Force" to lift, crush, and throw objects. Have you ever wished you could use an invisible force to move objects around in the real world? In this project, you will! Well, not just any object. First, you will have to build and program a robot that will respond to an "invisible force" from your hand. The invisible force will let you push and pull the robot back and forth without actually touching it! Watch this video of the demonstration robot we built at Science Buddies:
This project relies on a few key parts in the LEGO Mindstorms kit. First, you will need to use motors with wheels to build a robot that can move forward and backward. You will also need to use the NXT's ultrasonic sensor. An ultrasonic sensor measures the distance to an object by bouncing sound waves off it. First, the sensor emits a sound wave that travels forward at the speed of sound, away from the sensor. When that sound wave hits an object, part of it bounces back and eventually returns to the sensor. The farther away an object is, the longer it takes sound to bounce back and return. So, by measuring the amount of elapsed time from when the sound is emitted to when it returns, the sensor can determine how far away an object is, as Figure 1 illustrates.
Figure 1. The farther away an object is from the ultrasonic sensor, the longer sound has to travel to reach the object and then bounce back — which takes more time. This is how the ultrasonic sensor uses sound to measure distance.
Just building a robot with motors and an ultrasonic sensor won't be enough, however. You need to write a computer program to tell the robot what to do. The program you will write for your NXT robot in this project is based on this equation (this equation is called a proportional controller - see the Technical Note at the end of this section for more information):Equation 1.
- Motor power in the NXT software is a number between 0 and 100 that tells the motor how fast to spin. At 0 the motor will not move at all, and 100 means it is moving as fast as possible.
- Constant is a number you pick — it can be anything! The value of the constant will determine how fast your robot reacts when you move your hand.
- Measured distance is the distance to your hand, measured by the ultrasonic sensor.
- Desired distance is how far you want the robot to be from your hand.
To help you understand this equation, let's use some examples. Say you pick a value for constant = 2 and desired distance = 30 centimeters (cm). So, your equation is:Equation 2.
Now, think about what happens in three cases:
- Your hand is less than 30 cm from the robot.
- Your hand is exactly 30 cm from the robot.
- Your hand is more than 30 cm from the robot.
We'll use the first case as an example. Let's pretend your hand is 10 cm from the robot. So, you plug 10 cm into Equation 2 for the measured distance:Equation 3.
The answer is negative 40 — and a negative number means the robot will move backward, away from your hand. The robot "knows" that it is too close — it should be exactly 30 cm away. Can you use Equation 2 to figure out what will happen if your hand is exactly 30 cm from the robot, or more than 30 cm from the robot? Figure 2 shows what will happen — but make sure you can explain it using Equation 2!
Figure 2. This shows how the robot will behave if the desired distance in Equation 2 is set to 30 cm. If your hand is too close to the robot, it will back up, and if it is too far, the robot will move forward. If your hand is exactly 30 cm from the robot, the motor power will be 0, so it will stop.
Do you understand how the program will work? If not, go back and watch the video again — does it make more sense this time? Once you think you understand how you will be able to push and pull the robot, you are ready to move on to the Procedure section and start building.
Note: Understanding the information in this box is not required to complete the rest of the project — we include it just in case you are curious!
Equation 1 is called a proportional controller, because the control input is proportional to the error. In Equation 1, the control input is the motor power, and the error is the difference between the measured difference and the desired distance. The desired distance can also be referred to as the set point. The constant that multiplies by the error is called the proportional gain. So, you can write a more general equation:Equation 4.
This type of equation is used all the time in robotics and engineering. For example, you could also use it to control the angle of one of the motors in your NXT kit — this time the error would be based on your desired angle, and the measured angle from the NXT motor's built-in angle sensor. A more advanced version of the equation can even be used for things like cruise control in a car, or autopilot in an airplane! We don't have room to explain it all here — but if you are curious, you can do an Internet search for "PID control" or "proportional integral derivative control."
Terms and Concepts
- Ultrasonic sensor
- Speed of sound
- Elapsed time
- Computer program
- Motor power
- Measured distance
- Desired distance
- Proportional controller (advanced terms — not necessary to do the project)
- Control input
- Set point
- Proportional gain
- How do ultrasonic sensors work? Can you find examples in nature of animals that use sound to locate things? (Hint: search online for the word "echolocation.")
- How does Equation 1 work to control the robot's movement? Can you explain each case in Figure 2?
- How do you think changing the values of the desired distance or the constant in Equation 1 would affect how the robot moves?
- Can you research other examples in real life where an equation or computer program is used to control something? It doesn't always have to be about robotics - for example, the thermostat in your house controls temperature, so it doesn't get too hot or too cold.
Here are some useful introductions to programming LEGO's official NXT software. Remember that you can also consult the NXT User Guide CD that comes with your NXT kit.
- Valk, Laurens. The LEGO MINDSTORMS NXT 2.0 Discovery Book: A Beginner's Guide to Building and Programming Robots. San Francisco: No Starch Press, 2010.
- Griffin, Terry. The Art of LEGO MINDSTORMS NXT-G Programming. San Francisco: No Starch Press, 2010.
You will need to build a basic robot car for this project. You can certainly design your own car, but if you want help, look at the directions that come with your NXT kit, or search online for designs like LEGO's "TriBot" design.
Materials and Equipment
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.
- LEGO Mindstorms EV3 kit or LEGO Mindstorms NXT kit.
- Tape measure
Build Your Robot
First, you will need to build a basic robot. It should be able to move forward and backward, and have a forward-facing ultrasonic sensor. Feel free to come up with your own design, or use directions from your Mindstorms kit. Directions for the TriBot design are also available online (see Bibliography in the previous section). Figure 3 shows the robot we built at Science Buddies.
Figure 3. Science Buddies' simple robot design uses two motors and a forward-facing ultrasonic sensor.
Write Your Program
You have three options to create the program for this project:
- If you are an experienced NXT programmer, write your own program based on the information in the introduction (see technical note).
- Figure 4 shows a screenshot of the program we used. If you are comfortable with basic programming in NXT-G, you can create your own program and use Figure 4 for guidance (see technical note).
- If you are new to NXT programming and need some help getting started, download the program we used: "Use the Force Program (UseTheForce.rbt)".
The LEGO Mindstorms NXT proportional controller program uses blocks to program a robot to maintain a specific distance from certain objects. The program begins with a block representing a sonar sensors input that is run through 6 blocks of math equations and constants before connecting to two motor blocks that adjust the robots position.
Figure 4. This screenshot shows the program we used for the video in the Introduction. Our default values for the "Desired" and "Constant" blocks were 30 cm and -5, respectively.
Note that, even if you download our program, you might need to change a few things, depending on how you built your robot. Make sure that:
- You have the correct ports assigned to your ultrasonic sensor and motors. The default values in our program are Port 1 for the ultrasonic sensor and Ports A and C for the motors. You can change the ports by clicking on each block and then selecting the appropriate radio button (1, 2, 3, or 4 for sensors, and A, B, and C for motors) at the bottom of the screen.
- You have the correct sign (positive or negative) for the block labeled "Constant". This will depend on the orientation of the motors on your robot. The default value in our program is -5 (negative 5). If your motors are positioned like the ones in Figure 3, then this value should remain negative. If your motors are flipped upside down relative to the ones in Figure 3, then you should make this number positive. You can change this value by clicking on the "Constant" block and then entering a new value at the bottom of the screen.
If you are writing your own program from scratch or using ours as an example, you might be wondering what the "Compare" block is in Figure 4 (the third block from the right). This block is necessary because the "Motor" blocks only accept a number between 0 and 100 as an input for motor power, not -100 and +100. If you send a negative number to the motor power input, the program will automatically take its absolute value (for example, -50 becomes +50). This is a problem because the motor power calculated by Equation 1 from the Introduction can be either a positive or negative number. Remember:
So, to make the robot go in reverse, you have to change the "direction" input as well. The program works by first calculating the motor power using Equation 1 from the Introduction. Then, it determines if the motor power is positive or negative by comparing it to zero, using the "less than" operation in the "Compare" block. The "Compare" block outputs a binary value (true or false) - true if the motor power is less than zero, and false if the motor power is greater than zero. This binary value is then used as an input to the "Motor" block's direction, which accepts true for forward and false for reverse.
This way of doing things might seem a bit strange — but you need to make sure your robot can go forward and backward!
Download and Test Your Program
First, let's do a simple test to make sure your program is working.
- Aim the robot directly at a wall from about a meter away, and start the program. The robot should advance toward the wall and come to a stop.
- How quickly did the robot stop? Did it come to a stop smoothly, or jerk back and forth?
- Use your tape measure to measure the distance from the front of the ultrasonic sensor where the robot stopped to the wall. How does this distance compare to the desired distance you have set in your program? Note: if your tape measure only has marks for inches and you used centimeters in your program, you will need to convert inches to centimeters.
If your robot goes the wrong way (backs away from the wall instead of going toward it), then reverse the sign (positive or negative) of the "Constant" block in your program, and try again.
Now, try using your hand to "push" the robot back and forth.
- Put your hand in front of the ultrasonic sensor.
- If you pull your hand away from the robot (still being sure to keep it in front of the ultrasonic sensor), does the robot move forward? Do you have to move your hand very slowly, or can the robot keep up if you move your hand quickly?
- If you stop your hand, does the robot also come to a stop? Is the motion jerky or smooth?
- If you push your hand toward the robot, does the robot back away? Does it stop if you stop moving your hand?
Change the Values in Your Program
Now, find out what happens if you change the values for the "Desired Distance" and "Constant" blocks in your program. It will probably be easier if you change only one number at a time, and use a table to keep track of your results.
Important: as you pick new values for the "Desired Distance" and "Constant" blocks, keep in mind what is physically realistic for the ultrasonic sensor and motors. According to LEGO, the ultrasonic sensor has a range of 0-255 cm — so it doesn't make sense to enter a negative number, or a number greater than 255 cm (note: in practice, we have found the sensor's effective range to be less than that). Remember that the motor power accepts a number from 0 to 100 - so, it probably doesn't make sense to enter a very small number (like .00000001) or a very large number (like a million) for the "Constant." That would result in your robot either not moving at all, or always moving at full speed.
Table 1 shows just one example of how you could keep track of your data. For each trial, record the value for the desired distance and constant in your program. Then, you can also record things like:
- The actual distance the robot stops from the wall.
- The robot's speed as it approaches the wall or follows your hand. You could rate this qualitatively (on a scale like "slow/medium/fast") or use a stopwatch to time how long it takes the robot to come to a complete stop after starting a certain distance from the wall.
- The quality of the robot's stopping — for example, "smooth/medium/jerky."
If you can think of other ways to rate your robot's performance, feel free to add more columns to your table. What combination of settings gives you the "best" results (movement that is fast, but smooth and not jerky)?
|Trial||Desired Distance||Constant||Actual Distance||Speed||Stopping|
Ask an Expert
- This project will also work with the NXT's light sensor, since it can measure reflected light from your hand. You will have to change your code, though — not only to use the right sensor block, but because the light intensity value increases as you move your hand closer to the robot — this is the opposite of what happens with the ultrasonic sensor. Can you figure out why?
- If you increase the value of the "Constant" block in the program too much, your robot might become unstable — it will bounce back and forth when it approaches a wall, without ever actually coming to a complete stop. Can you find out how big the constant has to be before this happens? What happens as you continue to increase the value?
If you like this project, you might enjoy exploring these related careers: