Skip to content

Commit dfa3279

Browse files
author
SICKAppSpace-dev
committed
Initial commit
0 parents  commit dfa3279

4 files changed

Lines changed: 213 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<manifest>
3+
<application name="Introduction_Training_LuaScriptBasics2">
4+
<crown name="Introduction_Training_LuaScriptBasics2">
5+
<desc>user script</desc>
6+
</crown>
7+
<meta key="author">AppSpace Sample</meta>
8+
<meta key="version">1.0.0</meta>
9+
<meta key="priority">low</meta>
10+
<meta key="copy-protected">false</meta>
11+
<meta key="read-protected">false</meta>
12+
<entry default="main.lua" path="scripts"/>
13+
</application>
14+
</manifest>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
-- -----------------------------------------------------------------------------
2+
print( "----- Strings" )
3+
-- -----------------------------------------------------------------------------
4+
5+
-- The standard Lua library 'string' supports nice convenient functions for working with strings:
6+
print( string.byte( "ABCDE", 1, -2 ) ) -- console output: "65 66 67 68"
7+
print( string.rep( "w", 3 ) .. ".sick.com" ) -- console output: "www.sick.com"
8+
print( string.reverse( "Hello" ) ) -- console output: "olleH"
9+
print( string.sub( "AppSpace", 6, -1 ) ) -- console output: "ace"
10+
print( string.format( "%s %.10f", "Pi is exactly", math.pi ) )
11+
-- console output: "Pi is exactly 3.1415926536"
12+
13+
-- -----------------------------------------------------------------------------
14+
print( "----- Concatenate tables and access elements" )
15+
-- -----------------------------------------------------------------------------
16+
17+
-- The standard Lua library 'table' offers a mighty set of functionality. Concatenating tables is very efficient in Lua:
18+
local myTable = { "one", "two", "three", "four" }
19+
print( table.concat( myTable ) ) -- console output: "onetwothreefour"
20+
print( table.concat( myTable, ":" ) ) -- console output: "one:two:three:four"
21+
print( table.concat( myTable, "-", 2, 4 ) ) -- console output: "two-three-four"
22+
print( table.maxn( myTable ) .. " == " .. #myTable ) -- console output: "4.0 == 4"
23+
-- Append an item, indices should be successive without gaps and start with 1, not 0:
24+
myTable[ #myTable + 1 ] = "five"
25+
table.insert( myTable, #myTable + 1, "six" )
26+
--table.insert( myTable, 9, "nine" )
27+
-- console output: "Runtime error: bad argument #2 to 'insert' (position out of bounds)"
28+
-- Appending a 'nil' item will not be considered:
29+
table.insert( myTable, #myTable + 1, nil )
30+
myTable[ #myTable + 1 ] = nil
31+
table.insert( myTable, #myTable + 1, "nine" )
32+
print( #myTable .. "; " .. myTable[ #myTable ] ) -- console output: "7; nine"
33+
-- Sort with inline function as sorting method:
34+
table.sort( myTable, function ( a, b ) return a < b end )
35+
print( table.concat( myTable, "," ) )
36+
-- console output "five,four,nine,one,six,three,two"
37+
38+
-- -----------------------------------------------------------------------------
39+
print( "----- Use tables as dictionaries or maps and access elements" )
40+
-- -----------------------------------------------------------------------------
41+
42+
local myMap = { key1 = "value1", key2 = false }
43+
print( myMap.key1 ) -- console output: "value1"
44+
-- Add a new key/value pair, value can also be a table:
45+
myMap.newKey = { "one", "two", "three" }
46+
-- Remove 'key2' from table:
47+
myMap.key2 = nil
48+
-- Use '.' to reach certain values:
49+
print( myMap.key1 ) -- console output: "value1"
50+
print( myMap.key2 ) -- console output: "nil"
51+
-- Alternatively, use square brackets to reach certain values:
52+
print( myMap[ "newKey" ][ 2 ] ) -- console output: "two"
53+
-- Some table operations do not work with dictionaries or maps:
54+
print( #myMap ) -- console output: "0"
55+
print( #myMap.newKey ) -- console output: "3"
56+
-- Lua will not check for the range of available indices:
57+
print( myMap.newKey[ 10 ] ) -- console output: "nil"
58+
59+
-- -----------------------------------------------------------------------------
60+
print( "----- Library math" )
61+
-- -----------------------------------------------------------------------------
62+
63+
-- The standard Lua library 'math' provides a large function set. Here is just a small excerpt:
64+
print( math.sin( math.pi / 2 ) ) -- console output: "1.0"
65+
print( string.format( "%f", math.sin( math.pi * 0.5 ) ) )
66+
-- console output: "1.000000"
67+
print( math.min( 56, 4, 76, 23, 2, 76 ) ) -- console output: "2"
68+
local myNumTable = { 15, -4, 76, 32, 10, 21, -76, 63 }
69+
table.sort( myNumTable, function ( a, b ) return math.abs( a ) < math.abs( b ) end )
70+
print( math.abs( myNumTable[ 1 ] ) ) -- console output: "4"
71+
local myNumber = 2 ^ 8 - 1
72+
local fraction, exponent = math.frexp( myNumber )
73+
print( fraction .. " * 2 ^ " .. exponent .. " = " .. fraction .. string.format( " * %i = %i", 2 ^ exponent, myNumber ) )
74+
-- console output: "0.99609375 * 2 ^ 8 = 0.99609375 * 256 = 255"
75+
76+
-- Try more using the standard Lua library 'math':
77+
78+
-- -----------------------------------------------------------------------------
79+
print( "----- if and inline condition" )
80+
-- -----------------------------------------------------------------------------
81+
82+
local a, b = 5, 5
83+
if ( a == b ) then
84+
print( a .. " is equal to " .. b ) -- console output: "5 is equal to 5"
85+
elseif ( a > b ) then
86+
print( a .. " is greater than " .. b ) -- console output: "5 is greater than 5"
87+
else
88+
print( a .. " is less than " .. b ) -- console output: "5 is less than 5"
89+
end
90+
local c, d = "blue", "red"
91+
if ( c ~= d ) then
92+
print( c .. " is not " .. d ) -- console output: "blue is not red"
93+
else
94+
print( c .. " is " .. d ) -- console output: "blue is red"
95+
end
96+
local x, y = 12, 34
97+
print( x .. ( x == y and " is equal to " or " is not equal to " ) .. y )
98+
-- console output: "12 is not equal to 34"
99+
100+
-- -----------------------------------------------------------------------------
101+
print( "----- while loop" )
102+
-- -----------------------------------------------------------------------------
103+
104+
a, b = 5, 1
105+
-- If condition is false at all, block will not be executed:
106+
while ( a > b ) do
107+
print( a .. " is still greater than " .. b )
108+
b = b + 1
109+
-- Do not forget about the condition to avoid infinite loops!
110+
end
111+
112+
-- -----------------------------------------------------------------------------
113+
print( "----- for loop" )
114+
-- -----------------------------------------------------------------------------
115+
116+
-- A numerical 'for' loop:
117+
local start, stop, step = 10, 0, -1
118+
-- If start index exceeds stop index at all, i.e. even in case of decreasing step, block will not be executed:
119+
for i = start, stop, step do
120+
print( i .. ( ( i % 2 == 0 ) and " is even" or " is odd" ) )
121+
end
122+
123+
-- A 'for' loop based on an array, i.e. table with numeric indices:
124+
for i, v in ipairs( { 1, 4, 9, 16, 25 } ) do
125+
print( "Index is " .. i .. ", value is " .. v )
126+
end
127+
128+
-- A 'for' loop based on a generic map, i.e. table with string indices:
129+
local capitals = { Germany = "Berlin", France = "Paris", Norway = "Oslo",
130+
England = "London", Spain = "Madrid", Ukraine = "Kiew", Austria = "Vienna",
131+
Portugal = "Lisbon", Italy = "Rome", Croatia = "Zagreb" }
132+
print( "Some european countries and their capitals (unsorted):" )
133+
for k, v in pairs( capitals ) do
134+
print( " - The capital of " .. string.format( "% 8s", k ) .. " is " .. v .. "." )
135+
end
136+
table.sort( capitals )
137+
print( "Some european countries and their capitals (sorted?):" )
138+
for k, v in pairs( capitals ) do
139+
print( " - The capital of " .. string.format( "% 8s", k ) .. " is " .. v .. "." )
140+
end
141+
-- Generic maps or dictionaries cannot be sorted, i.e. the 'pairs' iterator will jump unpredictable over the elements!
142+
143+
-- -----------------------------------------------------------------------------
144+
print( "----- repeat loop" )
145+
-- -----------------------------------------------------------------------------
146+
147+
-- A 'repeat' loop:
148+
local i = 0
149+
repeat
150+
print( "Counter is " .. i )
151+
i = i + 1
152+
-- Do not forget about the condition to avoid infinite loops!
153+
until i > 10
154+
-- If the condition is false at all, the block will be executed at least once!

‎LICENSE‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
4+
software, either in source code form or as a compiled binary, for any purpose,
5+
commercial or non-commercial, and by any means.
6+
7+
In jurisdictions that recognize copyright laws, the author or authors of this
8+
software dedicate any and all copyright interest in the software to the public
9+
domain. We make this dedication for the benefit of the public at large and to
10+
the detriment of our heirs and successors. We intend this dedication to be an
11+
overt act of relinquishment in perpetuity of all present and future rights to
12+
this software under copyright law.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
22+
For more information, please refer to <https://unlicense.org>

‎readme.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Introduction_Training_LuaScriptBasics2
2+
Familiarize with the basics of Lua part two
3+
4+
### How to Run
5+
Drag and drop this ZIP archive to the working directory of SICK AppStudio.
6+
Starting this SampleApp is possible either by running the SensorApp (F5) or
7+
debugging (F7+F10) it. Setting a breakpoint on the first row inside the 'main'
8+
function allows debugging step-by-step after the event 'Engine.OnStarted' has
9+
been emitted. Results can be seen in the image viewer on the DevicePage if
10+
available.
11+
To run this SampleApp a device with SICK Algorithm API and AppEngine >= v2.5.0
12+
is required, e.g. a SIM4000 with latest firmware installed. Alternatively, the
13+
embedded emulator within SICK AppStudio v3.6.0 or higher can be used.
14+
15+
### Information
16+
This SampleApp is part of the e-learning training courses for the SICK AppSpace
17+
introduction. Thus, the contained source code will be explained step-by-step and
18+
in more details within this course.
19+
The purpose of this one is to gain further knowledge about the structure and
20+
indexing within the scripting language Lua.
21+
22+
### Topics
23+
training, introduction, commands, syntax, lua, sampleapp, sick-appspace

0 commit comments

Comments
 (0)