18
1 St. Mary´s School, Panama Robotics Ch. 5: Robolab, by: Ernesto E. Angulo J. T a b l e of C o n t e n t s Objectives ................................................................................................................. 2 Starting the program ................................................................................................. 2 Programming ............................................................................................................ 3 Downloading ............................................................................................................. 8 Tools ......................................................................................................................... 9 Icons......................................................................................................................... 9 Loops and jumps ..................................................................................................... 11 Multiple tasks ......................................................................................................... 12 Timers .................................................................................................................... 12 Variables ................................................................................................................. 14 Sensors ................................................................................................................... 15 Touch sensors ................................................................................................... 15 Touch sensor fork ...................................................................................... 16 Rotational sensor ............................................................................................. 17 Controlling distance .................................................................................. 17 Electric generator .................................................................................................... 18 5 Robolab

St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

1

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Table of Contents Objectives .................................................................................................................2 Starting the program.................................................................................................2 Programming ............................................................................................................3 Downloading.............................................................................................................8 Tools.........................................................................................................................9 Icons.........................................................................................................................9 Loops and jumps.....................................................................................................11 Multiple tasks .........................................................................................................12 Timers ....................................................................................................................12 Variables .................................................................................................................14 Sensors ...................................................................................................................15 Touch sensors...................................................................................................15 Touch sensor fork......................................................................................16 Rotational sensor .............................................................................................17 Controlling distance ..................................................................................17 Electric generator....................................................................................................18

5

Robolab

Page 2: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

2

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Objectives of the Chapter:

To learn how to work with Robolab; To learn the name and use of the commands; To learn how to write programs.

Starting the Program RoboLab is the programming language that you will use to tell your robots what to do. You write the programs on your desktop computer and then download them to the RCX using the

Infrared Tower. RoboLab uses icons (pictures) to represent different robot or program functions. You string these

icons together in a certain order to create the logic of your program.

Getting Started • Launch the RoboLab application by double-clicking on the RoboLab shortcut.

• Click on the Programmer button.

• Double click on Inventor 4 to bring up a new program window.

5

Robolab

Page 3: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

3

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Programming Let’s begin by writing a simple program. This program will turn on one of the motors for 2 seconds and then turn it off. A blank program window looks like this. The GO traffic light icon is the beginning of the program, and the STOP traffic light icon is the end of the program.

The Functions palette contains all of the icons that you will need to write your program. The first icon that we will need is one that turns on the motor. Our motor is connected to Port A on the RCX. The icon that we will need looks like a motor with the letter A over it. The white arrow defines which direction to turn on the motor. In this case, it is the forward direction. To place the icon in the program, click on the proper motor icon in the Functions palette, hold down the mouse button, drag the icon to the program window, and place it next to the Go stoplight icon.

Page 4: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

4

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

It is also useful to have the Help window showing. Click on the Help menu and select Show Help to this window. The Help window gives you information about each icon display. Motors can be programmed to run at different power levels from Power Level 1 (lowest power) to Power Level 5 (highest power). The Power Level icons can be found in the modifiers sub-palette of the Functions palette. Select the Power Level 5 icon and drag it under the motor icon.

Page 5: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

5

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

The next icon we need is a time delay that makes the program “wait” for 2 seconds. You will find this icon in the sub-palette with the picture of a watch. Click on the icon that says 2s and drag it next to the motor icon on the program window.

The last icon we will need is one that turns the motor off. This icon has a picture of a stop sign with a letter A, for motor port A. Drag this icon next to the time delay icon on the program window.

Page 6: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

6

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

The final step in writing the program is linking all of the icons together. To do this, you need the wire tool from the Tools pallette. To activate the Tools pallette, select Show Tools Palette from the Windows menu.

The wire tool is selected here. To link the icons together, click on the upper right corner of the first icon and then on the upper left corner of the icon that you want to connect. Every time you connect two icons or commands, you will see a pink dashed line

Page 7: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

7

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

You also need to connect the Power Level modifier to the motor icon. The modifier’s connections are represented by a blue line.

Page 8: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

8

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Downloading 1. Be sure that your program is correct and have no mistakes. 2. Be sure that your IR Tower has batteries (if needed, some tower does not need batteries). 3. Connect the tower to the proper port according to your model 4. Place the RCX at about two inches from the RCX 5. Turn the RCX on 6. Cover the RCX and the IR Tower with a box

The lights may interfere with the IR signal. Additionally, your signal might be received by another RCX.

If the download was successful, then you should hear a rising sweep sound from the RCX. The program is now ready to run.

Here are some problems that you may have while downloading.

If two icons are not connected together properly, then you will see an error list. The error list should describe what the problem is so you can fix it.

If you see this error message, then something has gone wrong with the download procedure. Double-check the following:

The Infrared tower’s serial cable is connected to the computer.

The Infrared tower is close enough to the RCX.

The RCX is turned on.

The light in the classroom is not interfering with the Infrared signal

Page 9: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

9

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Tools

Other useful tools that you will need when writing a program are shown here.

The Select tool lets you select icons in the program

window. Use the Select tool to reposition icons, or select them and then use the Delete key to remove them.

The Text tool lets you put in text in the program window. You will need this to fill in the Text Box modifier that will be used later. It is also useful to write text next to your program to describe what it does.

Icons Here is a brief description of the other icons that you will see when writing programs. These icons will be introduced and explained in subsequent lessons.

Start Program: This icon must be at the beginning of every program.

End Program: This icon must be at the end of every program or every separate task in a program.

• Motor On: These icons turn on the motors in the forward or reverse directions.

Power Level: These icons define the power level for a motor.

Motor Off: These icons turn off the motors.

A B C

Page 10: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

10

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Time Delay: These icons make the program wait for the specified amount of time.

Beep: This icon plays 1 of 6 different sounds. Touch Sensor: These icons let you use the touch sensor in your program. You can select between touch-in, that makes your robot react when the touch sensor is in, and Touch-off your robot reacts when the touch sensor is released.

Light Sensor: These icons let you use the light sensor in your program.

Rotation Sensor: These icons let you use the rotation sensor in your program.

Timers: These icons let you control your program based on time.

Containers: These icons let you use variables in your program. Variables are numbers whose value you do not know until the program is run.

For Loop: These icons let you repeat a certain part of your program a fixed number of times.

Page 11: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

11

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Jumps: These icons let you jump from one point in your program to another point.

Multi-task: This icon lets you do more than one thing simultaneously in your program.

Loops and Jumps For Loops allow you to repeat part of your program for a certain number of times. This way, you don’t have to rewrite the same piece of the program over and over again.

Put this icon at the beginning of your For Loop

and this icon at the end.

Use a Text Box modifier, which is found in the Modifiers sub-palette, to denote how many times you want the pro-gram to execute the loop. You can also use the random dice modifier for a random number of times.

Jumps are another set of icons that lets you control the flow of your program. When a program reaches the jump icon,

it will immediately jump to the landing icon, which is the one you see at your right.

There are 5 different pairs of colored jumps. You can also specify a jump/landing pair with a unique identification number using a Text Box modifier. Each color jump will seek for its own landing color.

Page 12: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

12

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Multiple Tasks So far, the programs that you have written have only one task. A task is a set of program icons that are linked together sequentially.

RoboLab allows you to have multiple tasks in the same program. This is sometimes called multi-tasking. Multi-tasking means your robots can appear to be doing 2 different things at the same time. You can have up to 10 separate tasks in a program.

To create multiple tasks, you need a task fork icon. Each separate task must end with a red stoplight icon.

At your left you have a simple example of using a task fork icon. This program contains 2 tasks, one that turns on motor A, and the other that turns on motor B. Both motors are on for 2 seconds and then turned off.

Timers The RCX's internal clock can be used as another input to control your program. These are called timers. You can have 3 different timers in a program, red, yellow, and blue. One way to use a timer is to have the program wait until a certain amount of time has passed before proceeding. It is important to reset your timer before you begin. Time is specified in tenths of a second, so 2 seconds would be written as the number 20. This program resets the red timer, then waits until 2 seconds has passed and turns on motor A.

A

B

RED

Page 13: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

13

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

The timer fork can be used to do different things depending on the amount of time that has passed. This program waits until the touch sensor has been pushed in.

If the touch sensor is pressed 3 seconds or less after starting the program (remember 3 seconds is written as 30), then a descending sweep sound is played. If more than 3 seconds have passed, then a rising sweep sound is played. Remember that a fork statement needs a fork merge at the end of the decision.

You can access the value of a timer by using the timer value icons. This is useful for storing the value of the timer in a container.

Programming Challenge: Write a program that times how long it takes for you to push in a touch sensor after starting the program. If the touch sensor is pressed within 3 seconds of starting the program, play a descending sweep sound. If more than 3 seconds have passed, play a rising sweep sound. (Answer given at the end of this lesson).

Program description:

Reset the red timer. Wait for the touch sensor on Port 1 to be pressed. Make a decision based on the amount of time that has passed using the timer fork. If 3 seconds or less have passed (written as 30), then play a descending sweep. If more than 3 seconds have passed, play a rising sweep. Don’t forget the fork merge.

The rotation sensor can be programmed to wait for a certain number of sensor counts. Recall that there are 16 counts per revolution of the rotation sensor.

RED

Page 14: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

14

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

This program turns on motor A and waits until the rotation sensor reads 80 counts, then turns off motor A. This would be 5 complete revolutions of the motor axle. Don't forget to reset the rotation sensor!

There is also a rotation sensor fork. In this program, if the number of rotation sensor counts is 80 or less, it lets motor A run. Once the number of counts is above 80, it stops motor A. Remember that all forks need a fork merge icon at the end of the decision.

VA variable is a number in your program that is not known until the program is running. This could be the value of a sensor, like the number of times the touch sensor was pressed, the amount of time that has passed between certain events, or other things that are not known until the program starts.

In RoboLab, variables are called containers. Like timers, you can have up to 3 different containers, red, blue, and yellow.

You can set to zero a container, place a number in a container, or place the value of a sensor, like a rotational sensor on port 3, in a container. These icons show these 3 things.

A

Page 15: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

15

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

You can perform simple mathematical operations on the contents of a container. These icons let you add, subtract, multiply, or divide the values that are in the containers with other numbers.

SSensors are crucial components to any machine that calls itself a robot. Sensors provide information about itself and the environment in which it is. The robot’s computer program then decides what to do based on this information and its own high level task instructions. In the Lego Mindstorms kit, you will find three types of sensors: touch sensors, light sensors, and rotational sensors.

Touch Sensors Touch sensors are programmed to detect when their buttons have been pushed in or released. You need to specify which sensor port a touch sensor is connected to. This icon causes the program to wait indefinitely until the touch sensor on port 1 is pushed in.

This icon waits until the touch sensor is released.

Touch sensors can be used to control the motors. This program waits indefinitely until the touch sensor has been pushed in. When this happens, motor A is commanded to turn on. Motor A continues to run until the touch sensor has been released, and motor A is commanded to stop.

A

Page 16: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

16

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

The touch sensor acts like a switch. When it hits an obstacle, the button on the touch sensor presses in, otherwise it is pressed out. This sensor is useful for detecting if your robot has hit anything. It detects contact with objects in the robot's environment and reports to the RCX if its button is pressed or not. The touch sensor acts like a light switch in your house. When the button is pressed, this closes an electrical circuit and lets electricity flow through the sensor. The RCX detects this electrical flow and knows that the button has been pressed. When the button is out, the circuit is broken and no electricity flows. It is important to connect the cable to the touch sensor in the correct way. This image shows the right way to connect the cable to the touch sensor.

The image at your left shows the wrong way to connect the cable to the touch sensor. The correct way to connect it is shown in the picture at the prior page.

Touch Sensor Fork

The fork is a new programming concept known as conditionals. A fork is a decision in the program that specifies what the program should do depending on the value of a particular sensor.

For example, the touch sensor has two values; button-in or button-out. The touch sensor fork icon is used to have the program doing different things depending on if the touch sensor is pushed in or released.

In this program, if the touch sensor is pushed in, then motor A is turned on and motor C is ordered to stop. If the touch sensor is released, motor C is turned on and motor A is stopped. All fork statement needs a fork merge at the end of the decision.

A

C

Page 17: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

17

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

Rotational Sensors The rotation sensor measures the rotation of an axle. As the axle turns, the rotation sensor counts how many times the axle rotates. There are 16 counts in 1 complete revolution of the axle. The number of counts is what the RCX reads as input. When the axle stops rotating, the rotation sensor stops counting. If the axle starts to rotate again, the rotation sensor begins counting where it left off. If the axle rotates in the opposite direction, the rotation sensor counts backward. It is important to reset the rotation sensor at the start of your program.

Rotation sensors are very useful to measure and control how far your robot moves. By measuring how many times your robot's wheels turn, and knowing the circumference of the wheels, you can measure how far your robot has moved. You can also use a rotation sensor to measure rotational speed. If you count how many revolutions an axle makes in a certain amount of time, then you can use this to calculate the number of rotations per second of the axle.

C D

Up to now, you have been using time to control how long the motors of your robot are activated. As the batteries in the RCX run low or if the surface that your robot is moving on changes, that could affect the amount of time that you need to keep the motors running to get the robot to move the same amount of distance.

Rotation sensors are very useful to measure and control how far your robot moves no matter what the conditions are. By measuring how many times your robot’s wheels turn, and knowing the circumference of the wheels, you can control how far your robot moves.

Recall that the circumference of a circle is the distance around the border of the circle.

The circumference is equal to the diameter of the circle times pi.

You can have your robot travel a precise distance by using the rotation sensor. If you know the diameter of the wheel, then you can compute how many revolutions it must make from its circumference. If you know how many revolutions the wheel must make, then you know how many rotation sensor counts are needed to travel that distance.

Page 18: St. Mary´s School, Panama Robolab 5 Table of Contents€¦ · Robotics St. Mary´s School, Panama Ch. 5: Robolab, by: Ernesto E. Angulo J. Downloading 1. Be sure that your program

18

St. Mary´s School, Panama Robotics

Ch. 5: Robolab, by: Ernesto E. Angulo J.

The rotation sensor only has 16 counts per revolution. That means the sensor only has an angular resolution of 360 / 16 = 22.5 degrees per count.

You can increase the angular resolution by using gears. In this case, one gear is attached to the motor (input gear) and the other one to the Rotation Sensor.

The gear ratio is 1 to 5. That means every time the motor makes one revolution, the rotation sensor’s axle makes 5. This results in 16 x 5 = 80 counts per motor revolution, or an angular resolution of 360 / 80 = 4.5 degrees. The angular resolution is 5 times greater.

Using gears to slow down the wheels helps increase the angular resolution even more. In this gear train, the gear ratio between the rotation sensor’s axle (input) and the wheel’s axle (output) is 125 to 1. That means, for each revolution of the wheel, the rotation sensor’s axle rotates 125 times.

This provides 125 x 16 = 2000 counts per wheel revolution! This is an angular resolution of 360 / 2000 = 0.18 degrees.

Electric Generator You already know that motors turn electrical power into rotational power. The electric power comes from batteries, and the motor converts it into rotational power to turn the wheels of your robot. But is it possible to go the other way? Can you turn rotational power into electrical power? The answer is YES! If you turn the motor's axle, you actually generate electricity. This is called an electric generator. Try the following experiment. Connect two motors together with a motor cable. Connect wheels to each of the motor's axles. Now turn one of the wheels. Does the other wheel turn?

When you do this, you are turning rotational power, supplied by you, into electrical power, which is transferred across the motor cable to the other motor, which turns it back into rotational power.