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]]
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/nightly>>url:http://eclipse.baeyens.it/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 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 === Simulating the model ===
87
88 A simulation of models is created as part of a build inside //kieler-gen/sim/bin//. To use these simulations, 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.
89
90 === Available Wrapper Code Snippets ===
91
92 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.
93
94 (% class="wrapped" %)
95 |=(((
96 Snippet Name and Parameters
97 )))|=(((
98 Description
99 )))|=(((
100 Use on
101 )))|=(% colspan="1" %)(% colspan="1" %)
102 (((
103 Variable type
104 )))|=(((
105 Remark
106 )))|=(% colspan="1" %)(% colspan="1" %)
107 (((
108 Defined in File
109 )))
110 |(% colspan="1" %)(% colspan="1" %)
111 (((
112 **Clock,** milliseconds
113 )))|(% colspan="1" %)(% colspan="1" %)
114 (((
115 Sets a variable to true if the time in milliseconds passed.
116 )))|(% colspan="1" %)(% colspan="1" %)
117 (((
118 input
119 )))|(% colspan="1" %)(% colspan="1" %)
120 (((
121 bool
122 )))|(% colspan="1" %)(% colspan="1" %)
123 (((
124 See also ResetClock.
125 )))|(% colspan="1" %)(% colspan="1" %)
126 (((
127 timing.ftl
128 )))
129 |(% colspan="1" %)(% colspan="1" %)
130 (((
131 **ResetClock,** clockVariableName, autoFalse
132 )))|(% colspan="1" %)(% colspan="1" %)
133 (((
134 Resets a clock, such that the full time intervall of the clock has to elapse, before the clock will be set to true again.
135
136 If autoFalse is true, the reset variable will be set to false automatically.
137 )))|(% colspan="1" %)(% colspan="1" %)
138 (((
139 output
140 )))|(% colspan="1" %)(% colspan="1" %)
141 (((
142 bool
143 )))|(% colspan="1" %)(% colspan="1" %)
144 (((
145 autoFalse is true per default.
146 )))|(% colspan="1" %)(% colspan="1" %)
147 (((
148 timing.ftl
149 )))
150 |(% colspan="1" %)(% colspan="1" %)
151 (((
152 **Time**
153 )))|(% colspan="1" %)(% colspan="1" %)
154 (((
155 Reads the elapsed time since program start in milliseconds.
156 )))|(% colspan="1" %)(% colspan="1" %)
157 (((
158 input
159 )))|(% colspan="1" %)(% colspan="1" %)
160 (((
161 unsigned
162 )))|(% colspan="1" %)(% colspan="1" %)
163 (((
164 \\
165 )))|(% colspan="1" %)(% colspan="1" %)
166 (((
167 timing.ftl
168 )))
169 |(% colspan="1" %)(% colspan="1" %)
170 (((
171 **TickDuration,** targetInMilliseconds
172 )))|(% colspan="1" %)(% colspan="1" %)
173 (((
174 Delays the execution until the tick loop takes at least as long as the given target duration.
175
176 The input variable is set to the actual tick loop duration.
177 )))|(% colspan="1" %)(% colspan="1" %)
178 (((
179 input
180 )))|(% colspan="1" %)(% colspan="1" %)
181 (((
182 unsigned
183 )))|(% colspan="1" %)(% colspan="1" %)
184 (((
185 Should be used on the very first input variable in the model, so that waiting is the last action in the tick loop.
186 )))|(% colspan="1" %)(% colspan="1" %)
187 (((
188 timing.ftl
189 )))
190 |(% colspan="1" %)(% colspan="1" %)
191 (((
192 **Delay
193 **
194 )))|(% colspan="1" %)(% colspan="1" %)
195 (((
196 Lets the program delay the time in milliseconds of the variable value.
197 )))|(% colspan="1" %)(% colspan="1" %)
198 (((
199 output
200 )))|(% colspan="1" %)(% colspan="1" %)
201 (((
202 unsigned
203 )))|(% colspan="1" %)(% colspan="1" %)
204 (((
205 \\
206 )))|(% colspan="1" %)(% colspan="1" %)
207 (((
208 timing.ftl
209 )))
210 |(% colspan="1" %)(% colspan="1" %)
211 (((
212 **SerialRate,** baud**
213 **
214 )))|(% colspan="1" %)(% colspan="1" %)
215 (((
216 Sets the baud rate for communication. This is done only in the initilization, not in the loop.
217 )))|(% colspan="1" %)(% colspan="1" %)
218 (((
219 output
220 )))|(% colspan="1" %)(% colspan="1" %)
221 (((
222 unsigned
223 )))|(% colspan="1" %)(% colspan="1" %)
224 (((
225 \\
226 )))|(% colspan="1" %)(% colspan="1" %)
227 (((
228 print.ftl
229 )))
230 |(% colspan="1" %)(% colspan="1" %)
231 (((
232 **Print**
233 )))|(% colspan="1" %)(% colspan="1" %)
234 (((
235 Prints a string variable if the string is not 0.
236 )))|(% colspan="1" %)(% colspan="1" %)
237 (((
238 output
239 )))|(% colspan="1" %)(% colspan="1" %)
240 (((
241 string
242 )))|(% colspan="1" %)(% colspan="1" %)
243 (((
244 \\
245 )))|(% colspan="1" %)(% colspan="1" %)
246 (((
247 print.ftl
248 )))
249 |(((
250 **DigitalWrite,** pin
251 )))|(((
252 Sets the pin value to HIGH if the variable is true and to LOW otherwise.
253 )))|(((
254 output
255 )))|(((
256 bool
257 )))|(((
258 \\
259 )))|(((
260 read_and_write.ftl
261 )))
262 |(((
263 **DigitalRead,** pin
264 )))|(((
265 Sets the variable value to the pin state (HIGH or LOW).
266 )))|(((
267 input
268 )))|(((
269 bool
270 )))|(((
271 \\
272 )))|(((
273 read_and_write.ftl
274 )))
275 |(((
276 **Analog**Write, pin
277 )))|(((
278 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.
279 )))|(((
280 output
281 )))|(((
282 int
283 )))|(((
284 \\
285 )))|(((
286 read_and_write.ftl
287 )))
288 |(% colspan="1" %)(% colspan="1" %)
289 (((
290 **AnalogRead,** pin
291 )))|(% colspan="1" %)(% colspan="1" %)
292 (((
293 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.
294 )))|(% colspan="1" %)(% colspan="1" %)
295 (((
296 input
297 )))|(% colspan="1" %)(% colspan="1" %)
298 (((
299 int
300 )))|(% colspan="1" %)(% colspan="1" %)
301 (((
302 \\
303 )))|(% colspan="1" %)(% colspan="1" %)
304 (((
305 read_and_write.ftl
306 )))
307
308 ----
309
310 == Using the Serial Monitor ==
311
312 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.
313
314 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.//
315
316 The output of all open connections will be combined in the text editor.
317
318 [[image:attach:Screenshot_20160509_125654.png]]
319
320 ----
321
322 == Problem Solving ==
323
324 The following presents typical issues and how to solve them.
325
326 (% class="wrapped" %)
327 |=(((
328 Issue
329 )))|=(((
330 Typical Error Messages
331 )))|=(((
332 Description
333 )))|=(((
334 Solution
335 )))
336 |(((
337 The upload protocol is not set
338 )))|(((
339 avrdude: Error: Could not find USBtiny device (0x1781/0xc9f)
340 )))|(((
341 You launch a project and there is no different behaviour on your Arduino device, although there are changes in code.
342
343 You try to launch the project and compilation finishes successful. However The upload fails because the upload protocol is not correctly configured.
344 )))|(((
345 Go to the project properties and select //Default// in the field //Upload Protocol// (Right click on project > Properties > Arduino > Arduino Board Selection)
346 )))