04 - Intro Lego RCX Code

53
LEGO Mindstorms RIS 2.0 Programming: Lego RCX Code B.A. Juliano and R.S. Renner September 2004 California State University, Chico Intelligent Systems Laboratory Chico, CA 95929-0410 http://www.gotbots.org

Transcript of 04 - Intro Lego RCX Code

Page 1: 04 - Intro Lego RCX Code

LEGO Mindstorms RIS 2.0Programming: Lego RCX Code

B.A. Juliano and R.S. RennerSeptember 2004

California State University, ChicoIntelligent Systems Laboratory

Chico, CA 95929-0410http://www.gotbots.org

Page 2: 04 - Intro Lego RCX Code

LEGO Mindstorms• http://mindstorms.lego.com

Page 3: 04 - Intro Lego RCX Code

Robotics Invention System 2.0

• RIS Kit

Page 4: 04 - Intro Lego RCX Code

Contents of Kit

• RCX™ Microcomputer• CD-ROM Software• Contructopedia™• 3 Guided Challenges• 6 Pro Challenges• USB Infrared Transmitter• 718 pieces, including (the ISL kit has more!):

– 2 Motors– 2 Touch Sensors– 1 Light Sensor

Page 5: 04 - Intro Lego RCX Code

Compatibility

Netscape® Navigator or Microsoft™ Internet Explorer

Internet Browser (optional)

28.8 KBPSModem (optional)

Colors 16 bitColors

800 X 600 SVGA with 4 MB RAM Video Display

CD 8XCD-ROM Speed

Sound Blaster 16™ Windows® Compatible Sound Device

Sound

Windows® CompatibleMouse

115 MBAvailable Hard Disk Space

32 MBRAM

Pentium II 233 MHz CPU

Windows® 98*Operating System

Minimum Requirements for your PC:

•Not compatible with Windows NT or Windows 2000 operating systems.

Page 6: 04 - Intro Lego RCX Code

• The RCX Brick– A microcomputer – 3 Output Ports (A, B, C)

• For motors and other output devices

– 3 Input Ports (1, 2, 3)• For sensors

– Infrared port• Downloads programs from PC,

using infrared tower (also called IR Transmitter)

• Communicates with other robots

Lego Electronic Components

Page 7: 04 - Intro Lego RCX Code

RIS 2.0 RCX BrickAn autonomous LEGO microcomputer that can be programmed using a PC. The RCX serves as the brain of LEGO MINDSTORMS inventions.

Page 8: 04 - Intro Lego RCX Code

About the Brick

• LCD (Liquid Crystal Display)• Four buttons• One internal speaker• Three sensor inputs• Three actuator outputs• IR (Infrared) interface• Battery level monitor

Page 9: 04 - Intro Lego RCX Code

About the Brick

• Hitachi H8 series microcontroller• 8 bit CPU at 16 MHz• 32 KByte RAM• 16 KByte ROM for the BIOS• serial I/O (input/output)• ADC (analog digital converter)• built-in timers

Page 10: 04 - Intro Lego RCX Code

Features

• Instruction set with 57 basic instructions, including:– Multiply and divide instructions– Powerful bit-manipulation instructions

• High-speed operation– All frequently-used instructions are

executed in two to four states

Page 11: 04 - Intro Lego RCX Code

Software Architecture

• System ROM Layer• Low level control software • Interface to hardware• Second piece of software

– Firmware– Located in RCX ROM

Page 12: 04 - Intro Lego RCX Code
Page 13: 04 - Intro Lego RCX Code

Firmware

• Can bypass system ROM software– Allows complete control of RCX

• Erased when power removed• In RCX RAM• Can use standard Firmware OR

– Can be replaced with others– brickOS, etc.

Page 14: 04 - Intro Lego RCX Code

Caution

• When changing batteries you may loose Firmware and all programs– Replace one at a time– Do within one minute

Page 15: 04 - Intro Lego RCX Code

Multi-Tasking

• Threading– Multiple things at one time

• Can create new threads to accomplish Multi-Tasking

Page 16: 04 - Intro Lego RCX Code

Hitachi H8/3292Microcontroller

Page 17: 04 - Intro Lego RCX Code

Hitachi H8/3292 diagram

Page 18: 04 - Intro Lego RCX Code

Hitachi H8/3292 package

Page 19: 04 - Intro Lego RCX Code

H8/300 CPU• 8-bit data• 16-bit address space• 8 X 16 bit registers (r0-r7)

– R0 – function return– R7 – stack pointer

• 16 MHz clock

Page 20: 04 - Intro Lego RCX Code

H8/3292 Block Diagram

Page 21: 04 - Intro Lego RCX Code

RCX Memory Layout

Page 22: 04 - Intro Lego RCX Code

• Output: Motor• Input: Touch sensor, light sensor (built-

in wire)– Also available: rotation sensors

• Connectors: Wires with 2x2 connector plates on ends

I/O Devices

Page 23: 04 - Intro Lego RCX Code

Light Sensor

•Two components•Light detector•Small light source

•Light turned on to find something close. Amplifies difference between light and dark.

•Great for line following

Page 24: 04 - Intro Lego RCX Code

Light Sensor cont.

• Highly sensitive to ambient light– Sunlight is the worst

• How to solve problem– Take two light sensor readings and

subtract the background light• Sensitive to objects reflective

surface.– Thus, difficult to measure distance.

Page 25: 04 - Intro Lego RCX Code

Light Sensor cont.

• Light measured as– 0% (total darkness) to– 100% (very bright).

Page 26: 04 - Intro Lego RCX Code

Light Sensor cont.

voltagemeasurement

light energy

electrical flow

voltagemeasurement

light energy

photo-transistor

Page 27: 04 - Intro Lego RCX Code

Uses for Light Sensor

• Navigation • Black on white• White on black

• Find light• Avoid light• Sort colors

• black Lego bricks• yellow Lego bricks.

• Input Device • colors or gray levels on paper.

Page 28: 04 - Intro Lego RCX Code

Touch Sensor

force voltagemeasurement

electrical flow

Page 29: 04 - Intro Lego RCX Code

Uses for Touch Sensor

• Detect contact • Wall detection• Internal contact

• Arm contact with internal parts

• Constant contact• Make sure bot does not fall off of a table

• Input Device• Use as a push button

Page 30: 04 - Intro Lego RCX Code

Others

• You can buy or make you own sensors– Example: Temperature sensor– Search online– Lots of resources

http://www.plazaearth.com/usr/gasperi/lego.htm

Page 31: 04 - Intro Lego RCX Code

Mindstorms Programming

Page 32: 04 - Intro Lego RCX Code
Page 33: 04 - Intro Lego RCX Code

Sample Program Blocks

Page 34: 04 - Intro Lego RCX Code

• You can download and install the following MS Windows-based software:– The Lego Mindstorms RIS 2.0 software

• Software CD is in your LEGO Toolkit– Bricx Command Center Software

• http://bricxcc.sourceforge.net/• http://bricxcc.sourceforge.net/nqc/

• Also:– The Lego Mindstorms SDK

• http://mindstorms.lego.com/sdk2point5/

LEGO Software

Page 35: 04 - Intro Lego RCX Code

1. Start with some task/project that requires a robot with certain capabilities

2. Plan out a combination of (a) physical design and (b) program design

3. Build the physical robot.

4. Create a program in a robot programming language

5. Download the program to robot using IR Tower.

6. Design and carry out a series of tests of your robot

7. Modify plan of mechanics and/or program as needed and repeat from step 3.

Robot Design Process

Page 36: 04 - Intro Lego RCX Code

• The RCX brick has some built-in programming to provide basic functionality (like ROM or BIOS on a computer)

• Requires additional programming before you can write and download programs – this is called firmware (like operating system on PC)– Firmware lives in RAM – random access

memory that retains content only as long as power is supplied (batteries)

– Your programs live in RAM too, translated from your PC program to bytecode (like translating Java or C++ to machine code)

Robot Initialization: Firmware

Page 37: 04 - Intro Lego RCX Code

• The LEGO Mindstorms RIS 2.0 Launcher must be installed (from the CD; be sure to reboot your computer after initial installation).

• Leave the LEGO Mindstorms RIS 2.0 CD in the drive when you plug in your USB IR Tower.

• Exit all Quick Launch applications on the Task Bar that rely on a USB connection – these may interfere with the IR Tower. For example, PDA HotSync Manager applications are notorious for such interference.

Before Installing Firmware

Page 38: 04 - Intro Lego RCX Code

Installing Firmware

Click on the “Hardware Setup” button to confirm correct connection of the IR Tower.

Page 39: 04 - Intro Lego RCX Code

Installing Firmware

Be sure to position the RCX Brick in front of the IR Tower. During test, the IR Tower

should emit a green light.

Page 40: 04 - Intro Lego RCX Code

Installing Firmware

Your RCX Brick's display should change to the figure shown above if your computer was able to detect the RCX through the IR Tower.

Page 41: 04 - Intro Lego RCX Code

Installing Firmware

The LEGO Mindstorms RIS 2.0 Launcher displays a final warning message before uploading the firmware to the RCX Brick.

Page 42: 04 - Intro Lego RCX Code

Installing Firmware

As the firmware is being uploaded to the RCX Brick, the counter at the center of the display

counts to around 2500 ...

Page 43: 04 - Intro Lego RCX Code

Installing Firmware

Five default programs are also uploaded after successful installation of the firmware; then, the RCX Brick display should be as above.

Page 44: 04 - Intro Lego RCX Code

Installing Firmware

Finally, the LEGO Mindstorms RIS 2.0 Launcher displays

the following Settings panel.

Page 45: 04 - Intro Lego RCX Code

• Any programming language has:– ‘Task’ commands:

• Input/Output, calculation– ‘Decision Structures’ (If-then, yes-no):

• Statements that let you choose between two or more groups of statements

– ‘Repetition Structures’ (Repeat, For, While):• Statements that let you repeat a group of statements

• Most modern programming languages have:– ‘Subprograms’ (Subroutines, functions):

• Let you give a name to a whole set of statements to create a new single, complex statement

• Some modern programming languages have:– ‘Parallel processing’ (Concurrency)

• Perform two or more sets of tasks at the same time

Programming Languages

Page 46: 04 - Intro Lego RCX Code

• Program block:Program block:– Top block to begin every RCX Program

• Small blocks (green):Small blocks (green):– Task commands, such as input, output, calculations

• Wait Blocks (Red)Wait Blocks (Red) and Repeat Blocks (Orange):Repeat Blocks (Orange):– Repetition commands

• Yes or No blocks (yellow)Yes or No blocks (yellow)– Decision commands

• Big blocks and My blocksBig blocks and My blocks– Subprograms

• Sensor Blocks (blue)Sensor Blocks (blue)– Task blocks (Input)– Can run sequentially or concurrently

Lego RCX Code Blocks

Page 47: 04 - Intro Lego RCX Code

Small Blocks• Power

– Turn motors on or off– Set power level (1-8)– Set or reverse direction

• Sound– Beep (6 choices)– Tone (choose frequency)

• Comm(unication)– Send or Clear IR message– Display variable value– Display clock

• Variable– Do arithmetic

operations

• Reset– Reset light or timer

• Advanced

Page 48: 04 - Intro Lego RCX Code

Repetition Blocks• Repeat blocks

– Repeat a set number of times, forever, while some condition is true, until some condition is true

– Condition is value of internal or external sensor.

• Wait blocks– These make the RCX do nothing until

some condition is true

Page 49: 04 - Intro Lego RCX Code

Decision block• Yes or No Block

– Chooses between two options depending on whether a condition is true or false

– How could we choose between 3 options? Between 4 options?

Page 50: 04 - Intro Lego RCX Code

Sensor Blocks• Sensors that get outside information:

– Touch sensor– Light sensor– IR sensor– Others not part of standard kit: Rotation,

temperature, humidity, etc.• Sensors that get internal information

– Timer sensor– Variable sensor

• Sensor blocks attach to the main program block and operate concurrently to commands under the main program block

Page 51: 04 - Intro Lego RCX Code

Limits of RCX Code• Example 1: Program robot to

distinguish dark from light that works in a variety of room conditions.

• Example 2: Program robot to wander randomly, recording 50 light readings for later analysis

• These tasks cannot be done with RCX Code.

Page 52: 04 - Intro Lego RCX Code

• Firmware replacements:– BrickOS: open-source, C/C++ based, RTOS

• http://brickos.sourceforge.net/– leJOS: open-source, Java-based

• http://lejos.sourceforge.net/– xs Lisp: Lisp-based

• http://www.xslisp.com/– pbForth: Forth-based

• http://www.hempeldesigngroup.com/lego/

Alternate Firmware

Page 53: 04 - Intro Lego RCX Code

References

• Dean, Alice M. CS 102B: Robot Design, http://www.skidmore.edu/~adean/CS102B0409/

• InSciTE: Innovations in Science and Technology Education, www.HighTechKids.org

• LEGO.com Mindstorms Home, mindstorms.lego.com