04 - intro lego rcx code
TRANSCRIPT
![Page 1: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/1.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/2.jpg)
LEGO Mindstorms• http://mindstorms.lego.com
![Page 3: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/3.jpg)
Robotics Invention System 2.0
• RIS Kit
![Page 4: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/4.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/5.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/6.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/7.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/8.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/9.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/10.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/11.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/12.jpg)
![Page 13: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/13.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/14.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/15.jpg)
Multi-Tasking
• Threading– Multiple things at one time
• Can create new threads to accomplish Multi-Tasking
![Page 16: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/16.jpg)
Hitachi H8/3292Microcontroller
![Page 17: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/17.jpg)
Hitachi H8/3292 diagram
![Page 18: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/18.jpg)
Hitachi H8/3292 package
![Page 19: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/19.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/20.jpg)
H8/3292 Block Diagram
![Page 21: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/21.jpg)
RCX Memory Layout
![Page 22: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/22.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/23.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/24.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/25.jpg)
Light Sensor cont.
• Light measured as– 0% (total darkness) to– 100% (very bright).
![Page 26: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/26.jpg)
Light Sensor cont.
voltagemeasurement
light energy
electrical flow
voltagemeasurement
light energy
photo-transistor
![Page 27: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/27.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/28.jpg)
Touch Sensor
force voltagemeasurement
electrical flow
![Page 29: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/29.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/30.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/31.jpg)
Mindstorms Programming
![Page 32: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/32.jpg)
![Page 33: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/33.jpg)
Sample Program Blocks
![Page 34: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/34.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/35.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/36.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/37.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/38.jpg)
Installing Firmware
Click on the “Hardware Setup” button to confirm correct connection of the IR Tower.
![Page 39: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/39.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/40.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/41.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/42.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/43.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/44.jpg)
Installing Firmware
Finally, the LEGO Mindstorms RIS 2.0 Launcher displays
the following Settings panel.
![Page 45: 04 - Intro Lego RCX Code](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/45.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/46.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/47.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/48.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/49.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/50.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/51.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/52.jpg)
• 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](https://reader031.vdocuments.net/reader031/viewer/2022013114/5536eb7655034650678b4c0b/html5/thumbnails/53.jpg)
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