User Tools

Site Tools


linux:legomindstorms

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:legomindstorms [2013/12/30 16:56] rlunarolinux:legomindstorms [2022/12/02 22:02] (current) – external edit 127.0.0.1
Line 6: Line 6:
  
 {{ :linux:20131230_113225.jpg?direct&400 |}} {{ :linux:20131230_113225.jpg?direct&400 |}}
 +
 +I suppose that if you come to this page you are somewhat familiar with Lego Mindstorms, however, I will describe some of the main elements of the kit: 
 +
 +    * The RCX is the "brain" of the robot. Is a yellow box that can be programmed. The good part is that you can replace the firmware easily, in fact [[http://tldp.org/HOWTO/Lego/|there are several solutions out there that replace the firmware]].
 +    * A device that you can connect to the computer via an RS232. I call in this document "the black box" because of this color.
 +    * Motors, sensors, etc. The usual robot stuff. 
  
 ===== Options to have it work under Linux ===== ===== Options to have it work under Linux =====
Line 13: Line 19:
   - Use of [[http://bricxcc.sourceforge.net/nqc/|Not Quite C (nqc)]]: it's a programming language similar to C, that can be used to generate code compatible with the RCX (the brain that goes into the robot)   - Use of [[http://bricxcc.sourceforge.net/nqc/|Not Quite C (nqc)]]: it's a programming language similar to C, that can be used to generate code compatible with the RCX (the brain that goes into the robot)
   - Use of [[http://www.lejos.org/|Lejos]]: it's a Java Virtual Machine for the RCX. This replaces completely the firmware in the RCX as long as the batteries are connected to the unit (RCX). For me it's a good solution because I am quite familiar with Java, so it's a natural way to go. Unfortunately, this is a major obstacle for my son and presumably I will have to do all the programming stuff...   - Use of [[http://www.lejos.org/|Lejos]]: it's a Java Virtual Machine for the RCX. This replaces completely the firmware in the RCX as long as the batteries are connected to the unit (RCX). For me it's a good solution because I am quite familiar with Java, so it's a natural way to go. Unfortunately, this is a major obstacle for my son and presumably I will have to do all the programming stuff...
-  - Use a VirtualBox Windows Machine and run the original software. This option has the advantage that the less problematic. Specially with the RS232, as you will see soon. 
-  - Use of Not Quite C, but in a Windows virtual machine.  
-  - Use of Lejos, but in a Windows virtual machine. 
  
-===== What??? No RS232??? =====+Of course, there are other options, but I won't discuss here.
  
-Yes, my computer doesn't have a RS232 interface. Hence, I have to simulate it with an special adapter:+===== RS232 Communication in the XXI Century =====
  
-[[http://www.amazon.com/StarTech-RS232-Serial-Adapter-Cable/dp/B000067SNB/ref=sr_1_1?ie=UTF8&qid=1388418081&sr=8-1&keywords=StarTech+USB+to+RS232|{{ :linux:img-20131230-wa0000.jpg?direct&300 |}}]]+Modern computers doesn't have an RS232 interface anymoreThe replacement for this are the USB ports, that any computer have lots of themYou can simulate an RS232 with an adapter. For my purposes I've bought an adapter from Star Tech called ICUSB232. Check it out:
  
  
 +[[http://www.amazon.com/StarTech-RS232-Serial-Adapter-Cable/dp/B000067SNB/ref=sr_1_1?ie=UTF8&qid=1388418081&sr=8-1&keywords=StarTech+USB+to+RS232|{{ :linux:img-20131230-wa0000.jpg?direct&300 |}}]]
  
-**The problem is** that this cable comes with a fantastic driver.... for Windows and Mac.  +==== Checking that it works under Linux ====
- +
-{{ :linux:20131230_114807.jpg?direct&300 |}} +
- +
-So, the working of this solution depends greatly with the hability or good work of the Ubuntu and Linux engineers. +
  
-===== Before Everything =====+One step is to check that it works properly under linux. Here are the basic steps:
  
-Don't plug the USB-RS232 adaptor yet!!! Before this, run a ''tail -f /var/log/messages'' or ''tail -f /var/log/syslog'' (depending of your version of Linux). To be sure, just run both at a time:+Don't plug the USB-RS232 adapter yet!!! Before this, run a ''tail -f /var/log/messages'' or ''tail -f /var/log/syslog'' (depending of your version of Linux). To be sure, just run both at a time:
  
 <code shell> <code shell>
Line 49: Line 49:
 </code> </code>
  
-This **''pl2303 converter now attached to ttyUSB0''** means that your USB device is ''/dev/ttyUSB0''.+This **''pl2303 converter now attached to ttyUSB0''** means that your USB device is **''/dev/ttyUSB0''**. This is very important in the further steps
  
  
-===== First Attempt: Not Quite C =====+===== Not Quite C =====
  
 This is my first attempt and my first try to make the USB-RS232 adaptor to work. I know that Java doesn't have a good USB or serial support, so the second try will be a major problem. My hopes are that NQC, as probably is made natively in C or similar, will have a native support for USB/RS-232 more robust. This is my first attempt and my first try to make the USB-RS232 adaptor to work. I know that Java doesn't have a good USB or serial support, so the second try will be a major problem. My hopes are that NQC, as probably is made natively in C or similar, will have a native support for USB/RS-232 more robust.
  
-__You can find documentation about NQC here: [[http://bricxcc.sourceforge.net/nqc/]]__.+==== Documentation ==== 
 + 
 +NQC is a programming language by itself, don't take it slightly. __You can find documentation about NQC here: [[http://bricxcc.sourceforge.net/nqc/]]__.
  
 ==== Hands on ==== ==== Hands on ====
  
-You can install nqc in Ubuntu by typing:+You can install NQC in Ubuntu by typing:
  
 <code shell> <code shell>
-sudo apt-get install nqc+sudo apt-get install nqc
 </code> </code>
  
-You can make a hello world to make sure that the USB-RS232 is working properly +My first step will be to reach the RS232 adapter correctly and reach the black boxSo, connect your RS232 adapter to the black box and make sure this is on, and with the batteries connected. The "on" in this unit means to move the switch to the biggest triangle: 
- +
-**First**, connect the unit to the PC and make sure it'on. The "on" in this unit means to move the switch to the biggest triangle: +
  
 {{ :linux:20131230_124252.jpg?direct&300 |}} {{ :linux:20131230_124252.jpg?direct&300 |}}
  
-**Next**, turn on the RCX and point it to the black unit, so both can see each other. Remember that the connection is via infrared. +**Optionally**, you can turn on the RCX and point it to the black unit, so both can see each other. Remember that the connection is via infrared. 
  
-**Next**, run the following code: +Now run the following code:
  
 <code shell> <code shell>
Line 80: Line 80:
 </code> </code>
  
-When running it, I've observed no error messages and no ''Could not open serial port or USB device'' message. This is important.+When running it, I've observed no error messages and no ''Could not open serial port or USB device'' message. This is important. When running this command, you should see a green light in the black box during a moment: 
 + 
 + 
 +{{ :linux:20131230_135707.jpg?direct&300 |}} 
  
 **Other commands to test proper running** **Other commands to test proper running**
  
-I've run this command:+You can try other commands that doesn't require to have a program like this:
  
 <code shell> <code shell>
Line 91: Line 95:
 </code> </code>
  
-So I'm afraid further investigation is needed.+==== Install the firmware in the RCX (the yellow box) ====
  
-**Last try**+The next step is installing the firmware in the RCX. There are various firmwares out there, but for NQC we need the official firmware from LEGO.
  
-I've attached a motor to the "C" square of the RCX: 
  
-{{ :linux:20131230_120903.jpg?direct&300 |}}  
  
-And this is my first program:  +===Where is the firmware???===
- +
-<code nqc> +
-task main() +
-+
-    OnFwd( OUT_C ); +
-    Wait( 400 ); +
-    OnRev( OUT_C ); +
-    Wait( 400 );  +
-    Off( OUT_C ); +
- +
-+
- +
-</code> +
- +
-When I try to run it, I get the following error:  +
- +
-<code shell> +
-$ nqc -S/dev/ttyUSB0 -run Hello.nqc  +
-No firmware installed on RCX +
- +
-</code> +
- +
-So, I definitely think that the RCX is completely blank, and I have to install some sort of firmware to have it properly working.  +
- +
-====Where is the firmware???====+
  
 Check out the box of mindstorm: it should be a CD which contains the firmware. To get the firmware from the CD, you should face the following scenarios: Check out the box of mindstorm: it should be a CD which contains the firmware. To get the firmware from the CD, you should face the following scenarios:
Line 132: Line 109:
   - The firmware file is not in the cd, but it is in one of the *cab files. In that case, you can find instruction in this page:[[http://www.17od.com/2013/01/13/using-lego-mindstorms-on-ubuntu/]]. In a nutshell, ''sudo apt-get install unshield'' and then ''unshield x PATH-TO-YOUR-CAB-FILE''.   - The firmware file is not in the cd, but it is in one of the *cab files. In that case, you can find instruction in this page:[[http://www.17od.com/2013/01/13/using-lego-mindstorms-on-ubuntu/]]. In a nutshell, ''sudo apt-get install unshield'' and then ''unshield x PATH-TO-YOUR-CAB-FILE''.
  
-==== Downloading the firmware ====+=== Downloading the firmware ===
  
-In the argot, placing the firmware file in the yellow box (the RCX) is called "download the firmware". I don't know why, because for me is more proper the opposite term (upload), but the terms were set when I came to this world+Placing the firmware file in the yellow box (the RCX) is called "download the firmware". I particularly think that "download" is not the best word for this, but it'the word used everyplace 
  
 Run the following command:  Run the following command: 
Line 147: Line 124:
 {{ :linux:20131230_135659.jpg?direct&300 |}} {{ :linux:20131230_135659.jpg?direct&300 |}}
  
-and a green diode lighting in the black box:+and a green diode lighting in the black box (it's not easy to see it):
  
-INSERT PICTURE HERE+{{ :linux:20131230_135707.jpg?direct&300 |}}
  
 After the process is finished, the LCD screen of the RCX should show a 0000 message, and a sound is reproduced: After the process is finished, the LCD screen of the RCX should show a 0000 message, and a sound is reproduced:
  
-INSERT PICTURE HERE+{{ :linux:20131230_140058.jpg?direct&300 |}}
  
 The installation of the firmware will be there until the batteries are removed. The installation of the firmware will be there until the batteries are removed.
Line 165: Line 142:
 Will have the effect that in the RCX the program number 2 is selected: Will have the effect that in the RCX the program number 2 is selected:
  
-INSERT PICTURE HERE+{{ :linux:20131230_142258.jpg?direct&300 |}}
  
  
-==== Executing the program ====+==== My first program ==== 
 + 
 + 
 +I've attached a motor to the "C" square of the RCX: 
 + 
 +{{ :linux:20131230_120903.jpg?direct&300 |}}  
 + 
 +And this is my first program:  
 + 
 +<code c> 
 +task main() 
 +
 +    OnFwd( OUT_C ); 
 +    Wait( 400 ); 
 +    OnRev( OUT_C ); 
 +    Wait( 400 );  
 +    Off( OUT_C ); 
 + 
 +
 + 
 +</code> 
  
 To run and download the program into the RCX, you should do the following: To run and download the program into the RCX, you should do the following:
Line 183: Line 181:
  
 ===== Second Attempt: LEJOS ===== ===== Second Attempt: LEJOS =====
 +
 +==== Install ant ====
 +
 +You should have ''ant'' installed. To do so, 
 +
 +<code shell>
 +$ sudo apt-get install ant
 +</code>
 +
 +And should have ''libusb-dev'' installed also. 
 +
 +<code shell>
 +sudo apt-get install libusb-dev
 +</code>
 +
 +After doing that, make sure that the usb.h file is available in your destination system. 
 +
 +
 +==== Hands on ====
  
 [[http://www.lejos.org/|Lejos]] is a Java Virtual Machine for the RCX (the yellow box). It's a replacement for the Lego firmware, so, if your CD of mindstorms is broken, it's the perfect solution. [[http://www.lejos.org/|Lejos]] is a Java Virtual Machine for the RCX (the yellow box). It's a replacement for the Lego firmware, so, if your CD of mindstorms is broken, it's the perfect solution.
  
 The web page has a [[http://www.lejos.org/rcx/tutorial/index.html|fantastic tutorial]] that shows the steps to be taken to have this stuff running.  The web page has a [[http://www.lejos.org/rcx/tutorial/index.html|fantastic tutorial]] that shows the steps to be taken to have this stuff running. 
 +
 +When I've checked out the web page, the download link didn't work. But you can go directly to the [[http://sourceforge.net/projects/lejos|sourceforge webpage]] in order to download the files. 
 +
 +When you download and uncompress the file ''leJOS_NXJ_0.9.1beta-3.tar.gz'', you should check out the README.html file. 
 +
 +Then start by setting the environment variable NXJ_HOME: 
 +
 +<code shell>
 +$ cd LEJOS_NXJ_DIRECTORY   # the files ACKNOWLEDGMENTS.txt, LICENSE.txt, RELEASENOTES.txt should be there
 +$ NXJ_HOME=$(pwd)
 +$ export NXJ_HOME
 +</code>
 +
 +Then go to the ''build'' directory and run ''ant''. This is for construct the USB support for java. 
 +
 +In my case, this command failed. The problem was in the ''build.xml'' file located in NXJ_HOME/build/libnxt. I've reconstructed the compiler command: 
 +
 +<code shell>
 +gcc -Wall -std=gnu99 -fPIC main_jlibnxt.c
 +</code>
 +
 +
 +And it fails with this error: 
 +
 +<code>
 +main_jlibnxt.c:28:17: fatal error: usb.h: No such file or directory
 +</code> 
 +
 +**Solution:**
 +
 +    - Locate the usb.h file in your filesystem: ''find / -iname usb.h 2>/dev/null'': in my system this appears in ''/usr/include/usb.h''
 +
 +I've fixed the solution executing the ''make'' command and then:
 +
 +<code shell>
 +$ cd NXJ_HOME/build
 +$ ant
 +</code>
 +
 +
 +
  
  
linux/legomindstorms.1388418991.txt.gz · Last modified: 2022/12/02 22:02 (external edit)