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, thus //Mindstorms// will be used exchangeable with //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"]].
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. You can install it via the Eclipse Marketplace (//Help > Eclipse Marketplace...//)
46
47 [[image:attach:lejos_eclipse_plugin.png]]
48
49 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**.
50
51 ----
52
53 == Configure KIELER ==
54
55 With the Eclipse plugin for leJOS installed, you can now use KIELER SCCharts for an model-based approach of programming.
56
57 First check the environment settings for NXJ of the KIELER tool (//Window > Preferences > KIELER > Environments//). On the **Execute** tab, ensure that only the commands for your operating system are checked.
58
59 Hit the //Variables// button and search for **nxj.home**. If the variable exists, your good. If it does not exist, ensure that you installed and configured the Ecplise plugin for leJOS NXJ correctly.
60
61 [[image:attach:environments_preferences.png]]
62
63 **Note:** If you already created and launched a project, the settings of the environment are copied to the launch configuration. It thus may be necessary to alter settings on the launch configuration as well as the environment.
64
65 ----
66
67 == Creating an Example Project ==
68
69 The following shows how to create a project, that will turn on a light if a button is pressed.
70
71 === Create a new project: ===
72
73 1. Choose //File > New > Project > KIELER SCCharts > SCCharts Project//
74 1. In the project creation wizard that opens, select //Mindstorms NXJ// as environment and hit //finish//
75 1. The project wizard from the leJOS plugin opens. Set the project name to //Flashlight// and click //finish//.
76 1. The project is created and the model file is opened in an editor (This might take a few seconds).
77
78 === Edit the model: ===
79
80 Change the contents of the model file to the following code and save it.
81
82 {{code title="Floodlight.sct" theme="Eclipse" language="sct"}}
83 scchart Flashlight {
84
85 @Wrapper TouchSensor, S4
86 input bool button;
87
88 @Wrapper Floodlight, S1
89 output bool light;
90
91 initial state lightOff
92 --> lightOn with button / light = true;
93
94 state lightOn
95 --> lightOff with !button / light = false;
96 }
97 {{/code}}
98
99 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.
100
101 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.**
102 **
103
104 === Launch the project: ===
105
106 With the mouse over the SCT file in the project explorer, perform //Right Click > Run As > KiCo Compilation.//
107
108 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 shell commands that are defined in the Mindstorms NXJ environment.
109
110 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"]].
111
112 ----
113
114 == Using the Remote Console (RConsole) ==
115
116 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.
117
118 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**.
119
120 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.