Show last authors
1 = Program Arduino with SCCharts =
2
3
4
5 {{toc minLevel="2"/}}
6
7 ----
8
9 == Overview ==
10
11 Arduino is a project and community wich creates open-source software and open-source hardware. The Arduino boards and software are well suited for novices and expierenced programmers alike to create digital devices. In the following we will see how to develop applications for the Arduino boards using SCCharts together with the [[Arduino Eclipse Plugin>>url:http://eclipse.baeyens.it/||shape="rect"]]. Therefore we will first download and setup the development environment and afterwards create and upload a small example project.
12
13 If you want to learn the SCCharts langugage first, you can follow these links:
14
15 * [[Introduction to SCCharts>>doc:SCCharts (pre 1\.0)]]
16 * [[The Textual SCCharts Language SCT>>doc:Textual SCCharts Language SCT]]
17 * [[SCCharts Examples>>doc:Examples]]
18
19 ----
20
21 == Download and Configure KIELER
22 ==
23
24 Download and unpack the nightly build of KIELER for your OS. It is available at the [[doc:Downloads]] page.\\
25
26 **Note:** Java 1.8 is needed on all operating systems. With Java 1.7 not all plugins of KIELER will be loaded.
27
28 === The Arduino Eclipse Plugin ===
29
30 There is an Eclipse plugin for Arduino, which makes it easy to create and deploy projects and to use the IDE features of the C/C++ Development Tools (CDT) when programming Arduino.
31
32 You have to install the plugin manually via **Help > Install new Software... **. Use the following update site: [[http:~~/~~/eclipse.baeyens.it/update/V4/nightly>>url:http://eclipse.baeyens.it/update/V4/nightly||shape="rect"]]
33
34 The installation might take a few minutes because it will install the Arduino Tools as well as required software such as the CDT. Furthermore it will download the newest version of the Arduino software and libraries.
35
36 ----
37
38 == Creating an Example Project ==
39
40 The following shows how to create a project, which will turn an LED on and off repeatedly.
41
42 === Create a new project: ===
43
44 1. Choose //File > New > Project > KIELER SCCharts > SCCharts Project//
45 1. In the project creation wizard that opens, select //Arduino// as environment and hit //finish//
46 1. The project wizard from the Arduino Eclipse Plugin opens. Set the project name to //Blinky// and click //next//.
47 1. Set the configuration for your Arduino board. In the field //Upload Protocol// select //Default//.
48 These settings can be changed later in the project preferences (Right click on project > Preferences > Arduino)
49 1. Click //finish//.
50 1. The project is created and the model file is opened in an editor (This might take a few seconds).
51
52 === Edit the model: ===
53
54 Change the contents of the model file to the following code and save it.
55
56 {{code language="sct" theme="Eclipse" title="Floodlight.sct"}}
57 scchart BlinkyModel {
58
59 @Wrapper Clock, 500
60 input bool clock;
61
62 @Wrapper DigitalWrite, 13
63 output bool light;
64
65 initial state lightOff
66 --> lightOn with clock / light = true;
67
68 state lightOn
69 --> lightOff with clock / light = false;
70 }
71 {{/code}}
72
73 This model will start in the state lightOff. If the variable //clock// is true, it will switch its state, going from off to on and from on to off. Thereby it sets the light variable so that the led will blink.
74
75 The annotations on the input and output variable are used to define which wrapper code is used to set / read them. **@Wrapper Clock, "500"** will set the input variable to true for one tick every 500ms. **@Wrapper DigitalWrite, "13"** will set pin 13 to HIGH if the variable is true and to LOW if it is false. We assume that an **LED is connected** to the Arduino board on **pin 13**.
76
77 The available wrapper code snippets are defined in the //assets// directory of the project in ftl files (FreeMarker template files). In the default configuration of a new SCCharts project for Arduino, the file //main.ftl// is processed to create the entry point of the program. Thus it includes other templates containing the available code snippets. The table below gives an overview of the available wrapper code snippets.
78
79 **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 stable releases is [[http:~~/~~/download.jboss.org/jbosstools/neon/stable/updates/ >>url:http://download.jboss.org/jbosstools/neon/stable/updates/||rel="nofollow" shape="rect" class="external-link"]]. Note that only the //FreeMarker IDE// feature is required (Abridged JBoss Tools > FreeMarker IDE).**
80 **
81
82 === Build the project: ===
83
84 After the modeling is done, the project can be built. Select //Project > Build Project//. This will create a new folder //kieler-gen// with the compiled model file. Further the wrapper code for the model is processed. After the build finished sucessfully, the ino file for the project has been updated with the new wrapper code for the model, and thus the project can be deployed to the Arduino board. The Eclipse Arduino Plugin provides an upload button for this task in the toolbar.
85
86 ==== Excluding the simulation folder from the CDT build ====
87
88 A simulation of models is created as part of a build inside //kieler-gen/sim/bin//. The files inside the //sim// folder are compiled separately and should not be compiled using the CDT. Otherwise errors will occur, for example because every simulation has its own main function and the CDT expects only one for the project.
89
90 Thus the folder //kieler-gen/sim// has to be excluded from the CDT build. Therefore select the folder and do Right Click > Resource Configurations > Exclude From Build.
91
92 === Simulating the model ===
93
94 To use the simulation inside the folder //kieler-gen/sim/bin//, first change to the Simulation Perspective, which provides new buttons in the toolbar. The executables in the //bin// folder can then be launched via //Right Click > KIELER Simulation//. The output of a running simulation can be seen in the Data Pool View.
95
96 === Available Wrapper Code Snippets ===
97
98 There are several wrapper code snippets that can be used as annotations on input and output variables in the model file. These snippets are inserted in the main file template as part of the project launch. The available snippets are listed below.
99
100 (% class="wrapped" %)
101 |=(((
102 Snippet Name and Parameters
103 )))|=(((
104 Description
105 )))|=(((
106 Use on
107 )))|=(% colspan="1" %)(% colspan="1" %)
108 (((
109 Variable type
110 )))|=(((
111 Remark
112 )))|=(% colspan="1" %)(% colspan="1" %)
113 (((
114 Defined in File
115 )))
116 |(% colspan="1" %)(% colspan="1" %)
117 (((
118 **Clock,** milliseconds
119 )))|(% colspan="1" %)(% colspan="1" %)
120 (((
121 Sets a variable to true if the time in milliseconds passed.
122 )))|(% colspan="1" %)(% colspan="1" %)
123 (((
124 input
125 )))|(% colspan="1" %)(% colspan="1" %)
126 (((
127 bool
128 )))|(% colspan="1" %)(% colspan="1" %)
129 (((
130 See also ResetClock.
131 )))|(% colspan="1" %)(% colspan="1" %)
132 (((
133 timing.ftl
134 )))
135 |(% colspan="1" %)(% colspan="1" %)
136 (((
137 **ResetClock,** clockVariableName, autoFalse
138 )))|(% colspan="1" %)(% colspan="1" %)
139 (((
140 Resets a clock, such that the full time intervall of the clock has to elapse, before the clock will be set to true again.
141
142 If autoFalse is true, the reset variable will be set to false automatically.
143 )))|(% colspan="1" %)(% colspan="1" %)
144 (((
145 output
146 )))|(% colspan="1" %)(% colspan="1" %)
147 (((
148 bool
149 )))|(% colspan="1" %)(% colspan="1" %)
150 (((
151 autoFalse is true per default.
152 )))|(% colspan="1" %)(% colspan="1" %)
153 (((
154 timing.ftl
155 )))
156 |(% colspan="1" %)(% colspan="1" %)
157 (((
158 **Time**
159 )))|(% colspan="1" %)(% colspan="1" %)
160 (((
161 Reads the elapsed time since program start in milliseconds.
162 )))|(% colspan="1" %)(% colspan="1" %)
163 (((
164 input
165 )))|(% colspan="1" %)(% colspan="1" %)
166 (((
167 unsigned
168 )))|(% colspan="1" %)(% colspan="1" %)
169 (((
170 \\
171 )))|(% colspan="1" %)(% colspan="1" %)
172 (((
173 timing.ftl
174 )))
175 |(% colspan="1" %)(% colspan="1" %)
176 (((
177 **TickDuration,** targetInMilliseconds
178 )))|(% colspan="1" %)(% colspan="1" %)
179 (((
180 Delays the execution until the tick loop takes at least as long as the given target duration.
181
182 The input variable is set to the actual tick loop duration.
183 )))|(% colspan="1" %)(% colspan="1" %)
184 (((
185 input
186 )))|(% colspan="1" %)(% colspan="1" %)
187 (((
188 unsigned
189 )))|(% colspan="1" %)(% colspan="1" %)
190 (((
191 Should be used on the very first input variable in the model, so that waiting is the last action in the tick loop.
192 )))|(% colspan="1" %)(% colspan="1" %)
193 (((
194 timing.ftl
195 )))
196 |(% colspan="1" %)(% colspan="1" %)
197 (((
198 **Delay
199 **
200 )))|(% colspan="1" %)(% colspan="1" %)
201 (((
202 Lets the program delay the time in milliseconds of the variable value.
203 )))|(% colspan="1" %)(% colspan="1" %)
204 (((
205 output
206 )))|(% colspan="1" %)(% colspan="1" %)
207 (((
208 unsigned
209 )))|(% colspan="1" %)(% colspan="1" %)
210 (((
211 \\
212 )))|(% colspan="1" %)(% colspan="1" %)
213 (((
214 timing.ftl
215 )))
216 |(% colspan="1" %)(% colspan="1" %)
217 (((
218 **SerialRate,** baud**
219 **
220 )))|(% colspan="1" %)(% colspan="1" %)
221 (((
222 Sets the baud rate for communication. This is done only in the initilization, not in the loop.
223 )))|(% colspan="1" %)(% colspan="1" %)
224 (((
225 output
226 )))|(% colspan="1" %)(% colspan="1" %)
227 (((
228 unsigned
229 )))|(% colspan="1" %)(% colspan="1" %)
230 (((
231 \\
232 )))|(% colspan="1" %)(% colspan="1" %)
233 (((
234 print.ftl
235 )))
236 |(% colspan="1" %)(% colspan="1" %)
237 (((
238 **Print**
239 )))|(% colspan="1" %)(% colspan="1" %)
240 (((
241 Prints a string variable if the string is not 0.
242 )))|(% colspan="1" %)(% colspan="1" %)
243 (((
244 output
245 )))|(% colspan="1" %)(% colspan="1" %)
246 (((
247 string
248 )))|(% colspan="1" %)(% colspan="1" %)
249 (((
250 \\
251 )))|(% colspan="1" %)(% colspan="1" %)
252 (((
253 print.ftl
254 )))
255 |(((
256 **DigitalWrite,** pin
257 )))|(((
258 Sets the pin value to HIGH if the variable is true and to LOW otherwise.
259 )))|(((
260 output
261 )))|(((
262 bool
263 )))|(((
264 \\
265 )))|(((
266 read_and_write.ftl
267 )))
268 |(((
269 **DigitalRead,** pin
270 )))|(((
271 Sets the variable value to the pin state (HIGH or LOW).
272 )))|(((
273 input
274 )))|(((
275 bool
276 )))|(((
277 \\
278 )))|(((
279 read_and_write.ftl
280 )))
281 |(((
282 **Analog**Write, pin
283 )))|(((
284 Sets the voltage of the given analog IO pin via pulse-width modulation (PWM). Integers from 0 to 1023 are linearly mapped to an pseudo voltage from 0V to 5V.
285 )))|(((
286 output
287 )))|(((
288 int
289 )))|(((
290 \\
291 )))|(((
292 read_and_write.ftl
293 )))
294 |(% colspan="1" %)(% colspan="1" %)
295 (((
296 **AnalogRead,** pin
297 )))|(% colspan="1" %)(% colspan="1" %)
298 (((
299 Reads the value of the given analog IO pin. Voltage from 0V to 5V is linearly mapped to an integer value from 0 to 1023.
300 )))|(% colspan="1" %)(% colspan="1" %)
301 (((
302 input
303 )))|(% colspan="1" %)(% colspan="1" %)
304 (((
305 int
306 )))|(% colspan="1" %)(% colspan="1" %)
307 (((
308 \\
309 )))|(% colspan="1" %)(% colspan="1" %)
310 (((
311 read_and_write.ftl
312 )))
313
314 ----
315
316 == Using the Serial Monitor ==
317
318 The Arduino Eclipse Plugin has a Serial Monitor (Window > Show View > Other > Arduino > Serial monitor view). Here you can open connections to serial ports and read and write to them.
319
320 To open a connection, click the plus button and select the serial port you want to conenct to as well as the serial rate that is used to transmit data. Click //OK.//
321
322 The output of all open connections will be combined in the text editor.
323
324 [[image:attach:Screenshot_20160509_125654.png]]
325
326 ----
327
328 == Problem Solving ==
329
330 The following presents typical issues and how to solve them.
331
332 (% class="wrapped" %)
333 |=(((
334 Issue
335 )))|=(((
336 Typical Error Messages
337 )))|=(((
338 Description
339 )))|=(((
340 Solution
341 )))
342 |(((
343 The upload protocol is not set
344 )))|(((
345 avrdude: Error: Could not find USBtiny device (0x1781/0xc9f)
346 )))|(((
347 You launch a project and there is no different behaviour on your Arduino device, although there are changes in code.
348
349 You try to launch the project and compilation finishes successful. However The upload fails because the upload protocol is not correctly configured.
350 )))|(((
351 Go to the project properties and select //Default// in the field //Upload Protocol// (Right click on project > Properties > Arduino > Arduino Board Selection)
352 )))
353 |(% colspan="1" %)(% colspan="1" %)
354 (((
355 The sim folder is not excluded from the CDT build
356 )))|(% colspan="1" %)(% colspan="1" %)
357 (((
358 Problem markers on files inside the folder //kieler-gen/sim//
359 )))|(% colspan="1" %)(% colspan="1" %)
360 (((
361 The CDT tries to compile the simulation of models as part of a build, which causes problems.
362 )))|(% colspan="1" %)(% colspan="1" %)
363 (((
364 Select the sim folder and do Right Click > Resource Configurations > Exclude From Build
365 )))