Table of Contents
Total Page:16
File Type:pdf, Size:1020Kb
1 Lego and BrickOS 1.1. BrickOS Software Development Environment The hardware of the BrickOS software development environment includes a PC running Windows 2000/XP, a USB Lego infrared communication tower, and the RCX box, commonly called RCX brick. The PC is used as the development environment for the Lego robots, where the BrickOS kernel may be build and C/C++ programs are coded and then cross compiled using the GCC cross compiler. The BrickOS kernel needs to be downloaded to the Lego brick before any other program is loaded to the brick. The executable code is downloaded to the Lego brick through the infrared tower. The downloaded program(s) may be started for execution by a button of the Lego brick. Cygwin is a program that runs on top of Microsoft Windows 2000/XP to provide a Unix- like environment, where many Unix-utility programs such as GCC cross-platform compilers, make, vi etc. The BrickOS source is stored in a directory of Cygwin. The GCC cross-platform compiler is used to first compile the BrickOS environment containing the BrickOS kernel, a utility called firmdl3 to download the BrickOS kernel to the RCX brick, another utility called dll to download executables to the RCX brick. Application Programs C/C++ Library BrickOS Firmware in ROM Hitachi H8/3292 Figure 1 BrickOS Architecture 2/9/2007 -- 11:36 AM 1-44 Figure 2 BrickOS software development environment. The software architecture includes BrickOS kernel, which is downloaded to the RCX brick first. The C/C++ library is built on top of the BrickOS. 1.2. Compile and Link C/C++ programs In the directory where the BrickOS is installed, created a subdirectory, for example, named cs358. Store all of you source code files in this directory. All C program files should end with a file extension .c, and all C++ program .C. Copy the Makefile file from $BRICKOSHOME/demo/c++ to this directory. Edit the Makefile file. You may use Window’s wordpad or notepad to edit the file. Change lines 23 and 24 from relative paths to their absolute paths as shown in Figure 3. Replace the program names from line 11 to 18 with the programs you wish to compile. The backslash at the end of each line except the last line means that the current line continues to next. For example, if you want compile one C++ program named YourProgram1.C, then replace lines 11 through 18 with YourProgram1.lx; if you have two programs named YourProgram1.C and YourProgram2.C, then replace lines 11 through 18 with YourProgram1.lx \ YourProgram2.lx or, its equivalent YourProgram1.lx YourProgram2.lx 1 ### ========================================================================== 2 ### $Id: Makefile,v 1.9 2002/10/23 17:42:44 stephmo Exp $ 3 ### FILE: Makefile - make the C++ Language Demo Programs 4 ### brickOS - the independent LEGO Mindstorms OS 5 ### -------------------------------------------------------------------------- 6 7 # #LIBDIR# ### DO NOT REMOVE THIS LINE ### 8 KERNEL = $(BRICKOS_ROOT)/boot/brickOS 9 10 PROGRAMS = \ 11 tracer4.lx \ 12 threadWakeup.lx \ 13 threadShare.lx \ 14 thread.lx \ 15 sound.lx \ 16 segment.lx \ 17 lineTracer.lx \ 18 critical-sec.lx 19 2/9/2007 -- 11:36 AM 2-44 20 # extra dynamic sources 21 DOBJECTS= 22 23 include /brickos-0.9.0/Makefile.common 24 include /brickos-0.9.0/Makefile.user 25 26 all:: $(PROGRAMS) 27 @# nothing to do here but do it silently 28 29 .depend: *.[cC] 30 $(MAKEDEPEND) *.[cC] > .depend 31 32 depend:: .depend 33 @# nothing to do here but do it silently 34 35 # NOTE: --format=1 is not supported on Linux ([ce]tags from emacs2[01] packages) 36 # please set in your own environment 37 tag: 38 -ctags *.[cC] $(BRICKOS_ROOT)/include/*.h $(BRICKOS_ROOT)/include/*/*.h 39 -etags *.[cC] $(BRICKOS_ROOT)/include/*.h $(BRICKOS_ROOT)/include/*/*.h 40 41 clean: 42 rm -f *.o *.dis *.dis2 *~ *.bak *.tgz *.s *.ds1 *.ds2 *.dmap *.dcoff 43 rm -f *.srec *.map *.coff 44 45 realclean: clean 46 rm -f *.lx .depend tags TAGS 47 48 .PHONY: all depend tag clean realclean 49 50 # depencencies 51 # 52 ifndef NODEPS 52 -include .depend 53 endif Figure 3 A Sample Makefile Now start a cygwin terminal by double-clicking the cygwin icon on the desktop. You can compile the C++ programs using the make utility. Change to the cs358 directory and then enter make. Compilation errors should be displayed if there is any. If all compiled successfully, all the executable programs ending with file extension .lx should be created in the directory. The .lx files are the programs to be executed by the RCX brick. 1.3. Download BrickOS and Executables The RCX brick or the computer inside the brick does not have a hard disk or any non- volatile R/W storage. That means that the BrickOS operating system and user programs downloaded to the brick get lost once power is removed. In other words, the BrickOS kernel and user programs need to be downloaded again each time the batteries are replaced. The BrickOS kernel is located in $BRICKOSHOME/boot/brickOS.srec, and it can be downloaded to the RCX using the firmdl3 program located at $BRICKOSHOME/util/. To download the kernel, change to the home directory of BrickOS, and then enter util/firmdl3 –tty=usb boot/brickOS.srec 2/9/2007 -- 11:36 AM 3-44 Downloading .lx programs is done by a utility called dll which is stored in $BRICKOSHOME/util/ directory. For example, to download YourProgram.lx, change to directory cs358 and enter ../util/dll –tty=usb YourProgram.lx Now the green light on the infrared tower should lit up, indicating data transmission. When the transmission is completed, the cygwin terminal displays a new prompt. To execute the program on the RCX brick, press the green button (run) on the RCX brick. 1.4. A Black Line Tracer Robot This section describes a robot that can find a black track and then follow it. Figure 4 show the hardware design of the robot. It consists of the RCX brick, two light sensors located at the front of the robot in blue color and connected to the input ports 1 and 2, two motors located at the rear of the robot in gray and connected to the output ports A and B. The two side-by-side light sensors detect the color on the floor. The program uses the readings from those sensors to control the direction of the robot by moving the two belts in certain directions. Figure 4 Black-line tracer hardware. Figure 5 shows a test environment for the robot. The robot is initially placed in the center of the one-inch-wide black track and it moves forward until the black track is found. It then follows the track until the view button on the RCX brick is pressed. Figure 6 shows the controlling program in C++ for the robot. BrickOS defines a LightSensor class to encapsulate the functionality of physical light sensors. A light sensor may be connected to any of the three input ports: 1, 2, and 3. The LightSensor class defines an enum type, Port, with values of S1, S2, and S3, representing light sensors connected to the three input ports 1, 2, and 3, respectively. The constructor of the LightSensor class requires the input port be specified as shown in lines 16 and 17 of Figure 6, where the leftSensor object represents the light sensor connected to the input port 1 and the rightSensor to the input port 2. The program uses three methods of the LightSensor class: active, off, and get. The active method sets the sensor to active or provides voltage to the connector. The off method turns the sensor off or turns off the voltage to the connector. The get method returns an integer representing the color read by the sensor. The value returned by get ranges from 0 to 100 with 0 representing black and 100 white and other numbers representing different degrees of gray. For example, the black-line 2/9/2007 -- 11:36 AM 4-44 tracer program uses 35 as the threshold for black color, i.e., if the return values of get is less or equal to 35, it is treated as black. Line 12 defines the threshold for black and line 29, a while loop, checks the color from both sensors. If the readings from both sensors are higher than the threshold, the robot continues moving forward since no black line is found yet. Figure 5 Black-line tracer layout. Similarly, BrickOS also provides a Motor class to encapsulate the functionality of physical motors which may be connected to the three output ports A, B and C. An enum type, Port, is also defined with values of A, B and C representing the respective output ports. The black-line tracer employs four methods of the Motor class: speed, forward, reverse, and off. The speed method, for example, lines 22 and 23, sets the motor speed ranging from 0 to 255 with 255 for highest speed; the forward, lines 31 and 32, turns the motor forward; the reverse, lines 46 and 52 reverses the motor; and the off, lines 59 and 60, turns off power to the motor causing the motor to coast to a stop. Other functions worth mentioning are cputs, msleep, and PRESSED. The cputs(string) displays string to the LCD of the RCX brick. The msleep(milliseconds) puts the program on hold for milliseconds. The PRESSED is actually a macro which returns a true when the specified button has been pressed. In the sample, the second parameter, BUTTON_VIEW, represents the View (in block) button. In other words, the PRESSED macro returns a true when the View button is pressed.