magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm)...

13

Click here to load reader

Transcript of magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm)...

Page 1: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Chapter 9. using the brick buttons and rotation sensorsIn addition to the Touch, Color, and Infrared Sensors, the EV3 contains two types of built-in sensors: Brick Buttons and Rotation Sensors. You can use the Brick Buttons on the EV3 brick to control or influence a program while it’s running. For example, the program can ask you to press one of the buttons to choose what the robot should do next.

Each of the EV3 motors has a built-in Rotation Sensor that determines the position of the motor, allowing you to precisely control wheels or other mechanisms. The sensor also measures the motor speed, making it possible to detect when a motor is moving slower or faster than intended.

using the brick buttonsYou can use the EV3 brick’s Up, Down, Left, Right, and Center buttons in your programs just as you use the Touch Sensor. You can make your robot respond by playing a sound when you press a particular button, for example. You can also make the robot wait for the button to be released or bumped (a press followed by a release).

One interesting way to use multiple buttons in a program is to create a menu on the EV3 screen, letting you choose the next action in the program. The ButtonMenu program in Figure   9-1  plays one of three sounds based on which button the user presses.

Two Display blocks show a simple menu on the screen, asking the user to choose whether the robot should say “Hello,” “Okay,” or “Yes.” Then, a Wait block (in Brick Buttons – Compare mode) pauses the program until the user presses either the Left, Center, or Right button.

DISCOVERY #48: LONG MESSAGE!

Difficulty:   Time: 

When you display a long message on the EV3 screen, you might find that the screen is too small to display it entirely. Create a program that lets you use the Down button to scroll through your message.

HINT

Make the robot display some new text on the screen each time you press the button.

DISCOVERY #49: CUSTOM MENU!

Difficulty:   Time: 

Page 2: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Can you expand the ButtonMenu program to make your robot do useful things besides playing sounds? Take three programs you made previously, turn them into My Blocks, and place them in the switch of the ButtonMenu program. Reconfigure the Display blocks to describe what happens as you press each button.

TIP

This technique is often used in robotics competitions because it provides a way to start different programs very quickly. To change the actions of each sub program, simply modify the blocks in each My Block.

Figure 9-1. The ButtonMenu program. The Wait block is configured in Brick Buttons – Compare – Brick Buttons mode, and the Switch block is in Brick Buttons – Measure – Brick Buttons mode.

Next, a Switch block (in Brick Buttons – Measure mode) determines which button is being pressed, and the robot plays the requested sound. After the Wait block completes, the Switch block runs so quickly that the button is still pressed by the time the Switch checks the button state, even if you release it right away.

using the rotation sensorWhen you tell the robot to move forward for three rotations with the Move Steering block, the vehicle knows when to stop moving because the Rotation Sensor in each EV3 motor tells the EV3 how much it has turned. The program can also tell you how fast a motor is currently turning by measuring how fast the motor position changes.

Page 3: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

You can use Wait, Loop, and Switch blocks in Motor Rotation mode to measure motor position (Degrees mode or Rotations mode) and motor speed (Current Power mode).

motor positionThe motor position tells you how much a motor has turned since you started the program. Use the Port View app on the EV3 brick, navigate to motor B or C, and rotate the motors with your hands to see the sensor values change.

When you first start Port View (or your own program), the sensor value is 0. The value becomes positive when you rotate a motor forward; it becomes negative if you turn it backward past 0, as shown in Figure   9-2 . For example, if you rotate the motor forward by 90 degrees and then backward for one rotation (360 degrees), the motor should report a position of −270 degrees.

You can use the position measurement to create a program that plays a sound if you turn one wheel 180 degrees forward by hand, as shown in Figure   9-3 . A Wait block in Motor Rotation – Compare – Degrees mode waits until the Rotation Sensor value is greater than or equal to (≥) 180 degrees. Because these sensors are built into the EV3 motors, they are always connected to output ports (you use the motor on output port B in this program).

Figure 9-2.  If you program a Large or Medium Motor to go forward, it turns in the direction of the blue arrow and the Rotation Sensor value becomes positive.

Page 4: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Figure 9-3. The HandRotate program makes the robot say “Okay” once you rotate motor B forward by 180 degrees. Note that the Wait block would do the same thing if you used Motor Rotation – Compare – Rotations mode

with a threshold value of 0.5.

resetting the motor positionNow suppose you want to repeat the actions in the HandRotate program with a Loop block so that the sound plays again when you rotate the wheel another 180 degrees. The first sound plays when you rotate the motor forward by 180 degrees. But during the second run of the loop, the sound would play immediately because the sensor value is already greater than 180 degrees, which is not what you want.

The solution is to reset the Rotation Sensor value to 0 at the beginning of the Loop, using a Motor Rotation block in Reset mode, as shown in Figure   9-4 . (You’ll explore the other features of this block and the other Sensor blocks later in Chapter   14 .)

Run the program and verify that you hear the sound once each time you rotate the wheel 180 degrees forward.

rotational speedThe Rotation Sensor calculates how fast a motor turns as a value between −100% and 100% based on the rate at which the motor position changes. The value is positive when the motor turns forward (blue arrow in Figure   9-2 ), negative when the motor turns backward (green arrow), and 0 when the motor is not turning.

For the Large Motor, a Current Power sensor value of 50% corresponds to a rotational speed of 85 rotations per minute (rpm). You can reach this speed by rotating a motor with your hands or by using any of the Move blocks with its Power setting at 50%.

NOTE

Page 5: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Current Power mode measures rotational speed; it does not measure current or power consumption!

Figure 9-4. The HandRotateReset program sets the sensor value to 0 at the beginning of each loop with the Rotation Sensor block in Reset mode. Note that the Play Type setting in the Sound block is Play Once (1) so that the

program doesn’t wait for the sound to finish.

DISCOVERY #50: BACK TO THE START!

Difficulty:   Time: 

Can you make a program that returns a motor to the position it was in when the program started? The robot should give you five seconds to turn the motor to a random position manually, and then the motor should return to its starting point. Use the decision tree shown in Figure   9-5  as a guide for your program.

FIGURE  9-5.  THE FLOW DIAGRAM FOR DISCOVERY #50. HOW DOES THE ROBOT DETERMINE THAT A MOTOR HAS BEEN TURNED BACKWARD?

CALCULATING THE ROTATIONAL SPEED

Page 6: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

You can calculate the speed measured in rotations per minute (rpm) using the Current Power value as follows:

large motorrotational speed (rpm) = sensor value × 1.70

medium motorrotational speed (rpm) = sensor value × 2.67

For example, if the Current Power value of a Large Motor is 30%, the motor rotates at 30 × 1.70 = 51 rotations per minute. Because one rotation per minute is equivalent to six degrees per second, you can calculate the rotational speed in degrees per second (deg/s) as follows:

rotational speed (deg/s) = rotational speed (rpm) × 6

In this example, you get 51 × 6 = 306 degrees per second.

Figure 9-6. The PushToStart program

MEASURING ROTATIONAL SPEED IN A PROGRAM

To measure rotational speed in a program, you use the Current Power mode of the Rotation Sensor, as shown in Figure   9-6 . The PushToStart program uses a Wait block configured to pause the program until motor B reaches a sensor value of 30% (51 rpm). Then, a Move Steering block takes over at the same speed. Run the program and push EXPLOR3R forward with your hands until it begins to move by itself.

DISCOVERY #51: COLORED SPEED!

Difficulty:   Time: 

Create a program that continuously changes the brick status light color to green if motor B is rotating forward, orange if it’s rotating backward, and red if the motor stands still. Rotate motor B with your hands to test your program.

HINT

Page 7: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

You’ll need a Loop block, two Switch blocks, and three Brick Status Light blocks.

understanding speed regulationSo far, you’ve been using several kinds of green Move blocks to make your robot move. These blocks make the motors turn at a constant, regulated speed. When the motors slow down because of an obstacle or an incline, the EV3 supplies some extra power to the motor to keep it going at the desired speed. The Power setting on these blocks actually specifies the speed that the motors try to maintain. That is, a Large Motor turning at 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light task at 40% speed (68 rpm).

When you don’t want the EV3 to supply that extra power to maintain constant speed, you can use unregulated speed.

seeing speed regulation in actionTo see the difference between regulated speed and unregulated speed, you’ll create a program that makes the robot drive up a slope, such as a table with one end lifted up in the air. First, the robot drives at unregulated speed for three seconds, and then it drives at regulated speed for three seconds.

You need two Unregulated Motor blocks (one for each motor) from the advanced tab of the Programming Palette, with their Power setting at 20%, as shown in Figure   9-7 . After waiting for three seconds, you stop the motors by setting their power to 0%. To drive at a regulated speed of 20% (34 rpm), the program uses the Move Steering block that you’ve seen before.

Page 8: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Figure 9-7. The SteepSlope program. Note that I used a Sequence Wire to split the program in two for better visibility, but you won’t need to do this in your program.

Place the EXPLOR3R on a tilted surface and run the SteepSlope program. You should find that the robot drives quite slowly up the slope during the first three seconds and that it goes faster during the next three seconds.

During the first part of the drive, the EV3 switches on both motors and leaves them alone for the next three seconds. The robot runs slowly because it takes more power to drive up a hill than to drive on even terrain. During the second part, the Rotation Sensors tell the robot that it’s going slowly, prompting the EV3 brick to supply some extra power to get the robot back up to speed.

stopping a stalled motorIf you try to slow down one of the wheels when a Move Steering block runs, you’ll feel that the robot tries harder to get back up to speed. This is fine for wheeled vehicles, but it’s often undesired for mechanisms that don’t make complete turns, such as a claw mechanism.

To avoid this problem, you can use the Unregulated Motor block and the Rotation Sensor to detect when the motor is stalled (blocked), as demonstrated by the WaitForStall program (see Figure   9-8 ). The program switches on motor B at 30% power and waits for the speed to drop below 5%, indicating that the motor is stalled. If you run this program on the EXPLOR3R, your robot should drive in circles until you slow down the robot by blocking its path.

Page 9: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Figure 9-8. The WaitForStall program turns motor B until it is stalled. Note that the first Wait block is required to give the motor some time to get up to speed—otherwise, the rotational speed would be 0 when the speed is first

measured, causing the program to end immediately.

further explorationNow that you’ve learned how to work with all of the sensors in the EV3 set, you can create robots that interact with their environment. The EXPLOR3R is, of course, only one example. As you continue reading this book, you’ll build several robots with sensors, each of which will use sensors differently.

So far, you’ve learned to use the components that are essential to create a working robot: the EV3, the motors, the sensors, and the programming software. The following chapters will explore each of these subjects in more detail so that you’ll be able to create increasingly sophisticated (and fun!) robots. In the next chapter, you’ll begin looking at how you can use the Technic building elements in the EV3 set to construct your own robots.

The following Discoveries will help you explore more possibilities with the sensors you’ve seen in this chapter.

DISCOVERY #52: BRICK BUTTONS REMOTE!

Difficulty:   Time: 

Remove the EV3 brick from your robot (leaving the cables connected), and create a program that lets you move the robot around by pressing the EV3 buttons. Make the robot drive forward if you press the Up button, go left if you press the Left button, and so on.

HINT

Use a Switch block in Brick Buttons – Measure – Brick Buttons mode.

DISCOVERY #53: LOW SPEED OBSTACLE DETECTION!

Difficulty:   Time: 

Page 10: magbuhatrobotics.files.wordpress.com  · Web viewChapter 9. using the brick ... 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light

Can you make your robot drive around the room and move away from obstacles without the use of the Touch, Color, or Infrared Sensors? Make the robot drive forward using Unregulated Motor blocks until it detects an obstacle. The robot should then reverse, turn around, and continue driving in a new direction.

HINT

The rotational speed of a motor drops when the robot runs into an obstacle.

DESIGN DISCOVERY #11: AUTOMATIC HOUSE!

Building:   Programming: 

Have you ever built houses from regular LEGO bricks? Now that you know how to work with motors, how to use sensors, and how to make working programs, how about trying to build a robotized house with the EV3?

TIP

Use a motor to automatically open the door when someone presses the doorbell (the Touch Sensor), and set an intruder alarm that sounds when the Infrared Sensor sees someone. Use another motor to close and open the shutters based on the light level measured with the Color Sensor.