04 - Intro Lego RCX Code
Transcript of 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
LEGO Mindstorms• http://mindstorms.lego.com
Robotics Invention System 2.0
• RIS Kit
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
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.
• 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
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.
About the Brick
• LCD (Liquid Crystal Display)• Four buttons• One internal speaker• Three sensor inputs• Three actuator outputs• IR (Infrared) interface• Battery level monitor
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
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
Software Architecture
• System ROM Layer• Low level control software • Interface to hardware• Second piece of software
– Firmware– Located in RCX ROM
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.
Caution
• When changing batteries you may loose Firmware and all programs– Replace one at a time– Do within one minute
Multi-Tasking
• Threading– Multiple things at one time
• Can create new threads to accomplish Multi-Tasking
Hitachi H8/3292Microcontroller
Hitachi H8/3292 diagram
Hitachi H8/3292 package
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
H8/3292 Block Diagram
RCX Memory Layout
• 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
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
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.
Light Sensor cont.
• Light measured as– 0% (total darkness) to– 100% (very bright).
Light Sensor cont.
voltagemeasurement
light energy
electrical flow
voltagemeasurement
light energy
photo-transistor
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.
Touch Sensor
force voltagemeasurement
electrical flow
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
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
Mindstorms Programming
Sample Program Blocks
• 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
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
• 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
• 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
Installing Firmware
Click on the “Hardware Setup” button to confirm correct connection of the IR Tower.
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.
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.
Installing Firmware
The LEGO Mindstorms RIS 2.0 Launcher displays a final warning message before uploading the firmware to the RCX Brick.
Installing Firmware
As the firmware is being uploaded to the RCX Brick, the counter at the center of the display
counts to around 2500 ...
Installing Firmware
Five default programs are also uploaded after successful installation of the firmware; then, the RCX Brick display should be as above.
Installing Firmware
Finally, the LEGO Mindstorms RIS 2.0 Launcher displays
the following Settings panel.
• 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
• 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
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
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
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?
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
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.
• 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
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