Show last authors
1 = Program LEGO Mindstorms with leJOS and SCCharts =
2
3
4
5 {{toc minLevel="2"/}}
6
7 ----
8
9 == Overview ==
10
11 Mindstorms is a product family from Lego, with sensors, motors and a programmable brick. The newest iteration of the product family is the EV3 programmable brick. Its predecessors are NXT and RCX. In the following we will see how to develop applications for the NXT brick.
12
13 Several open-source, third-party replacements for the offical Lego firmware have been developed. These support many well known programming languages, such as Java, C/C++, Python, Lua, etc. In the following we will use KIELER SCCharts to program Mindstorms running the Lego Java Operating System ([[leJOS>>url:http://www.lejos.org/||shape="rect"]]). Therefore we will first install leJOS NXJ and flash its firmware. Afterwards we will create a simple SCCharts project in KIELER that we will compile and deploy to the NXT brick.
14
15 ----
16
17 == Download and install leJOS ==
18
19 Download and extract the newest archives for your Operating System from [[Sourceforce>>url:http://sourceforge.net/projects/nxt.lejos.p/files/||shape="rect"]] (Linux/Mac) or use the Setup.exe (Windows).
20
21 The further installation is explained in detail at [[http:~~/~~/www.lejos.org/nxt/nxj/tutorial/Preliminaries/GettingStarted.htm>>url:http://www.lejos.org/nxt/nxj/tutorial/Preliminaries/GettingStarted.htm||shape="rect"]].
22
23 === Known issues ===
24
25 == Linux ==
26
27 On Linux there is an issue when uploading the firmware because of a kernel module ([[http:~~/~~/ubuntuforums.org/showthread.php?t=1123633>>url:http://ubuntuforums.org/showthread.php?t=1123633||shape="rect"]]). If you can't upload the firmware with your Linux OS, add **blacklist cdc_acm**{{code language="none"}}{{/code}} at the very end of the file **{{code language="none"}}/etc/modprobe.d/blacklist.conf{{/code}}**. Afterwards execute **{{code language="none"}}sudo rmmod cdc_acm{{/code}}**. This will remove the cdc_acm module from the kernel and prevent its restart. Now try to flash the firmware again.
28
29 Another issue is that the development package of **libusb** has to be installed. On Ubuntu you can do this by using **{{code language="none"}}sudo apt-get install libusb-dev{{/code}}**.
30
31 Furthermore, to use USB connection, a java library has to be compiled via ant. To do this perform **cd /path/to/leJOS/build** and start ant. If the ant build tool is not installed on your system, you can do so via **sudo apt-get install ant**.
32
33 ----
34
35 == Test the Mindstorm ==
36
37 A simple Hello World application for the Mindstorms is developed as part of the leJOS tutorial [[http:~~/~~/www.lejos.org/nxt/nxj/tutorial/Preliminaries/FirstProgram.htm>>url:http://www.lejos.org/nxt/nxj/tutorial/Preliminaries/FirstProgram.htm||shape="rect"]]
38
39 If this works with your device, you are able to start using KIELER to develop applications for the NXT brick.\\
40
41 ----
42
43 == The Eclipse plugin for leJOS ==
44
45 There is an Eclipse plugin for leJOS which adds a project creation wizard and launch configuration to the platform.
46
47 1. You have to install it via the Eclipse Marketplace (//Help > Eclipse Marketplace...//).
48 OR
49 1. Install the plugin manually (Help > Install new Software...). Use the following update site\\
50 11. for NXT: [[http:~~/~~/www.lejos.org/tools/eclipse/plugin/nxj/>>url:http://www.lejos.org/tools/eclipse/plugin/nxj/||shape="rect"]]
51 11. for EV3: [[http:~~/~~/www.lejos.org/tools/eclipse/plugin/ev3/>>url:http://www.lejos.org/tools/eclipse/plugin/ev3/||shape="rect"]]
52 \\\\
53
54 If you have an **NXT** **brick**, install the **leJOS NXJ** Plug-in. If you have an **EV3 brick**, install the **leJOS EV3** plugin.
55
56 [[image:attach:lejos_eclipse_plugin.png]]
57
58 After the installation, the plugin requires a **little configuration**. Go to //Window > Preferences > leJOS NXJ// and enter the base directory of your **leJOS** **installation** in the **NXJ_HOME field**.
59
60 For the **EV3 plugin**, the required dialog is under //Window > Preferences > leJOS EV3//. You have to set the **leJOS installation directory**. Furthermore the leJOS EV3 plugin requires the IP address to connect to the brick (it may work without, but its safer to directly set the name. Reduces headache ). Check **Connect to named brick** and enter the **IP adress** of the brick (displayed on the brick at startup).
61
62 ----
63
64 == Creating an Example Project ==
65
66 The following shows how to create a project, that will turn on a light if a button is pressed.
67
68 === Create a new project: ===
69
70 1. Choose //File > New > Project > KIELER SCCharts > SCCharts Project//
71 1. In the project creation wizard that opens, select //Mindstorms NXJ// or// Mindstorms EV3// (depending on your brick) as environment and hit //finish//
72 1. The project wizard from the leJOS plugin opens. Set the project name to //Flashlight// and click //finish//.
73 1. The project is created and the model file is opened in an editor (This might take a few seconds).
74
75 === Edit the model: ===
76
77 Change the contents of the model file to the following code and save it.
78
79 {{code title="Floodlight.sct" theme="Eclipse" language="sct"}}
80 scchart Flashlight {
81
82 @Wrapper TouchSensor, S4
83 input bool button;
84
85 @Wrapper Floodlight, S1
86 output bool light;
87
88 initial state lightOff
89 --> lightOn with button / light = true;
90
91 state lightOn
92 --> lightOff with !button / light = false;
93 }
94 {{/code}}
95
96 This model will start in the state lightOff. If the button is pressed, it will turn on the light and change to the corresponding state, where the light is turned off, as soon as the button is not pressed anymore.
97
98 The annotations on the input and output variable are used to define which wrapper code is used to set / read them. **@Wrapper TouchSensor, S4** will set the input variable to true iff the touch sensor on the port S4 is pressed. **@Wrapper Floodlight, S1** on the output variable will turn on the red led of the light sensor that is attatched to port S1 iff the variable is true.
99
100 The available wrapper code snippets are defined in the //snippets// directory of the project in ftl files (FreeMarker template files).
101
102 **Note:** The Floodlight of the EV3 has a pretty high latency when switching between on and off.
103
104 **Note: **To view ftl files with highlighting, you may want to install the //FreeMarker IDE// feature from the JBoss Tools. However, this is not necessary to work with KIELER. JBoss Tools is available in the Eclipse Market Place and via update site. The update site for Eclipse Mars is [[http:~~/~~/download.jboss.org/jbosstools/mars/stable/updates/>>url:http://download.jboss.org/jbosstools/mars/stable/updates/||shape="rect"]] whereas the update site for Eclipse Luna is [[http:~~/~~/download.jboss.org/jbosstools/updates/stable/luna/>>url:http://download.jboss.org/jbosstools/updates/stable/luna/||shape="rect"]] . Note that only the //FreeMarker IDE// feature is required (Abridged JBoss Tools > FreeMarker IDE).**
105 **
106
107 === Launch the project: ===
108
109 With the mouse over the SCT file in the project explorer, perform //Right Click > Run As > KiCo Compilation.//
110
111 A launch config is created, which compiles the model to Java code and creates wrapper code from the annotations in the model file. Afterwards this output is compiled and deployed to the Mindstorms brick, by using the launch shortcut from the leJOS plugin. If any errors occur, you can see them in the Console View.
112
113 For a deeper understanding of the project launch and initialization, take a look at the [[wiki page for Prom>>url:http://rtsys.informatik.uni-kiel.de/confluence/pages/viewpage.action?pageId=13762626||shape="rect"]].
114
115 ----
116
117 == Using the Remote Console (RConsole) ==
118
119 The display of the **NXT brick** is rather small compared to a Monitor. To ease debugging, one can print to a Remote Console (RConsole), if the USB cable is connected. This enables easier collection for example of sensor data.
120
121 To use the RConsole, **uncomment** the **RConsole** lines in the wrapper code template **Main.ftl**. Start the **nxjconsoleviewer** tool in the bin directory of your **leJOS installation**. Now, when **starting the application**, the brick tries to connect with the nxjconsoleviewer. **Press the //Connect//** button. If connected succesfully, RConsole.println(...) commands will be written to this window.
122
123 The **EV3 brick** has a similar feature. However it does not require any code changes. Just run the ev3console program in the bin directory of your leJOS installation from command line. The output of the brick will be printed to this command line.
124
125 ----
126
127 == Problem Solving ==
128
129 The following presents typical issues and how to solve them.
130
131 |=(((
132 Issue
133 )))|=(((
134 Typical Error Messages
135 )))|=(((
136 Description
137 )))|=(((
138 Solution
139 )))
140 |(((
141 leJOS EV3 does not support Java 8
142 )))|(((
143 "java.lang.UnsupportedClassVersionError"
144
145 "unsupported major.minor version"
146 )))|(((
147 You compile the sources in your project with Java 8 and upload them to the brick. However the lejos EV3 does not support Java 8
148 )))|(((
149 Go to the project properties and switch to Java 7 (Right Click on project > Properties > Java Compiler > Compiler compliance level)
150 )))
151
152
153
154 ----
155
156