Lua Scripting¶
Starting with version 7.0, XCSoar can be extended using Lua scripts.
Lua is a language that is easy to learn, powerful enough for XCSoar and light: the interpreter library weighs just 200 kB. Lua is a common language choice for integrated scripting languages.
Learning Lua¶
print("Hello World")
This manual will not attempt to teach you basic Lua. There are enough resources on the internet, for example:
Programming in Lua, a book on Lua
Tutorial Directory on the lua-users wiki
Just to get you started from here, here’s some more example code:
-- comment starts with a double hyphen
--[[
multi
line
comment
]]--
i = 42
if i > 1 then
print("i=" .. i)
elseif i == 0 then
print("zero")
else
error("negative")
end
a = {1, 'a', 3.14}
print(a[2])
function f(a, b)
return a * b
end
print(f(2, 3))
Running Lua¶
The directory XCSoarData/lua/
may contain Lua scripts (*.lua
).
The directory XCSoarData/lua/lib/
may contain Lua libraries to be
loaded with require
.
After startup, XCSoar starts the script init.lua
(if it
exists).
The InputEvent RunLuaFile
can be used to start additional
scripts. If no parameter is given, the user is asked to choose a file.
Note that the InputEvent subsystem is deprecated and will be removed
once Lua support is complete.
As long as a Lua script runs, the XCSoar user interface is blocked. Be careful not to write scripts that loop forever.
Once the Lua script finishes, the Lua interpreter is shut down –
unless the script has registered a callback (e.g. a timer
). In
that case, the Lua script stays resident until it unregisters all
callbacks (or until XCSoar quits or the user stops the script
explicitly).
Running Lua on the Command Line¶
You can experiment with XCSoar Lua scripts with the RunLua command-line tool, e.g.:
make output/UNIX/bin/RunLua
./output/UNIX/bin/RunLua test/lua/geo.lua
Most libraries are missing here (everything specific to a full
XCSoar), but some very basic things can be tested here, for example
the HTTP client and the geo library (class GeoPoint
). Being a
small non-interactive command-line program, it is also easy to debug
with gdb and valgrind.
Lua Standard Libraries¶
XCSoar enables the following Lua standard libraries:
package
table
string
math
Lua’s print()
function writes to the XCSoar log file
(XCSoarData/xcsoar.log
).
The error()
function aborts the Lua script and reports the specified
error message to the user.
XCSoar adds another function to the root namespace: alert()
. It
shows a dialog with the specified message, and returns as soon as the
user has closed the dialog. This function is experimental, and may
disappear or be renamed at any time. Most importantly: do not abuse
it, as it may annoy the user.
XCSoar’s Lua API¶
The package/namespace xcsoar
provides access to XCSoar. It
contains the following names:
Name |
Description |
---|---|
|
The XCSoar version number, for example |
|
A class which describes a geodetic location on earth’s surface. |
|
Access to sensor data. See The Blackboard. |
|
The map view. See The Map. |
|
Access to airspace data. See Airspace. |
|
Access to wind data and settings. See Wind. |
|
Access to logger settings. See Logger. |
|
Access to replay system. See Replay. |
|
Access to tracking settings. See Tracking. |
|
Class for scheduling periodic callbacks. See Timers. |
|
HTTP client. See HTTP Client. |
|
Deliver plain text data to somebody; the user will be asked to pick a recipient (Android only). |
The Blackboard¶
The blackboard provides access to sensor data, such as GPS location.
The following attributes are provided by xcsoar.blackboard
:
Name |
Description |
---|---|
|
A monotonic wall clock time [s], with an undefined reference. |
|
A wall clock time [s], since midnight (UTC) of the day the flight started. Not strictly monotonic (can warp under certain circumstances). |
|
A date table describing the current date and time (UTC), preferably from the GPS. Not strictly monotonic (can warp under certain circumstances). |
|
The current location (table with keys |
|
The current altitude above MSL [m]. |
|
The current altitude above ground [m]. |
|
The current flying direction above ground [°]. |
|
The aircraft speed relative to the ground [\(m/s\)]. |
|
The true airspeed [\(m/s\)]. |
|
The bank angle [°]. |
|
The pitch angle [°]. |
|
The current magnetic heading in [°]. |
|
The current g-load. |
|
The static pressure [Pascal]. |
|
The pitot pressure [Pascal]. |
|
The dynamic pressure [Pascal]. |
|
The current temperature. |
|
The current humidity. |
|
The external battery voltage [V]. |
|
The internal battery-level [percent]. |
|
The non-compensated vertical speed [\(m/s\)]. |
|
The total-energy-compensated vertical speed [\(m/s\)]. |
|
The netto variometer value [\(m/s\)]. |
Any of these (except for clock
) may be nil
if its value is not
known, e.g. if there is no GPS fix.
The Map¶
The map provides access to XCSoar’s map view.
The following attributes are provided by xcsoar.map
:
Name |
Description |
---|---|
|
The current reference location (may be aircraft location or pan location). |
|
Gives back if the panning mode is active at the moment. |
|
Activates the panning mode. |
|
Disables the panning mode. |
|
Leaves the panning mode. |
|
Pans to the given location. |
|
Pans the cursor by dx and dy. |
|
Zooms the map, factor -2 to 2. |
|
Opens the next page. |
|
Opens the previous page. |
|
Show the map; disable thermal assistant or other widgets replacing the map view. |
Airspace¶
The Airspace provides access to airspace data, such as name / distance to the next airspace.
The following attributes are provided by xcsoar.airspace
:
Name |
Description |
---|---|
|
The vertical distance to the next airspace [m]. |
|
The name of the next vertical airspace. |
|
The horizontal distance to the next airspace [m]. |
|
The name of the next horizontal airspace. |
Task¶
The Task provides access to task data such as distances / bearing to the next waypoint.
The following attributes are provided by xcsoar.task
:
Name |
Description |
---|---|
|
The true bearing to the next waypoint [°]. For AAT tasks, this is the true bearing to the target within the AAT sector. |
|
The difference between the glider’s track bearing, to the bearing of the next waypoint, or for AAT tasks, to the bearing to the target within the AAT sector [°]. |
|
The true bearing from the next waypoint to your position. [°]. |
|
The distance to the currently selected waypoint [m]. For AAT tasks, this is the distance to the target within the AAT sector. |
|
The distance to the currently selected waypoint [m]. For AAT tasks, this is the distance to the origin of the AAT sector. |
|
Estimated time [s] required to reach next waypoint, assuming performance of ideal MacCready cruise/climb cycle. |
|
Estimated arrival local time at next waypoint, assuming performance of ideal MacCready cruise/climb cycle. |
|
Arrival altitude at the next waypoint relative to the safety arrival height. |
|
Arrival altitude at the next waypoint with MC 0 setting, relative to the safety arrival height. |
|
Additional altitude required to reach the next turnpoint. |
|
Absolute arrival height at the next waypoint in final glide. |
|
The required glide ratio over ground to reach the next waypoint, given by the distance to the next waypoint divided by the height required to arrive at the safety arrival height. |
|
Distance to finish around remaining turn points. |
|
Estimated time required to complete task, assuming performance of ideal MacCready cruise/climb cycle. |
|
Estimated arrival local time at task completion, assuming performance of ideal MacCready cruise/climb cycle. |
|
Arrival altitude at the final task turn point relative to the safety arrival height. |
|
Arrival altitude at the final task turn point, with MC 0 setting, relative to the safety arrival height. |
|
Additional altitude required to finish the task. |
|
Average cross country speed while on the current task, not compensated for altitude. |
|
Achieved cross country speed while on the current task, compensated for altitude. Equivalent to Pirker cross country speed remaining. |
|
Instantaneous cross country speed while on the current task, compensated for altitude. Equivalent to instantaneous Pirker cross country speed. |
|
Average cross country speed while on the current task over the last hour, not compensated for altitude. |
|
The required glide ratio over the ground to finish the task, given by the distance to go divided by the height required to arrive at the safety arrival height. |
|
Assigned Area Task time remaining. |
|
Difference between estimated task time and AAT miminum time. |
|
Assigned Area Task distance around target points for remainder of task. |
|
Assigned Area Task maximum distance possible for remainder of task. |
|
Assigned Area Task minimum distance possible for remainder of task |
|
Assigned Area Task average speed achievable around target points remaining in minimum AAT time. |
|
Assigned Area Task average speed achievable if flying maximum possible distance remaining in minimum AAT time. |
|
Assigned Area Task average spped achievable if flying minimum possible distance remaining in minimum AAT time. |
|
The contiguous period the plane has been below the task start max. height. |
|
Estimated time required to reach next waypoint, assuming current ground speed is maintained. |
|
Estimated time required to complete task, assuming current ground speed is maintained. |
|
Efficiency of cruse, 1 indicates perfect MacCready performance. |
Settings¶
The Settings provides access to xcsoar settings, such as MC value.
The following attributes are provided by xcsoar.settings
:
Name |
Description |
---|---|
|
The current set MacCready Value [\(m/s\)]. |
|
The current used bug settings in terms of polar degradation. |
|
The current wingload. |
|
Ballast of the glider. 0 means no ballst, 0.3 means 30% of the maximum ballast the glider can carry. |
|
Area pressure for barometric altimeter calibration [Pascal]. |
|
The forecast ground temperature [Kelvin]. |
|
The MacCready setting used, when safety MC is enabled for reach calculations, in task abort mode and for determining arrival altitude at airfields. |
|
The STF risk factor reduces the MacCready setting used to calculate speed to fly as the glider gets low, in order to compensate for risk. |
|
A permanent polar degradation, 1 means no degradation, 0.5 indicates the glider’s sink rate is doubled. |
|
The height above terrain that the glider should arrive at for a safe landing. |
|
The height above trerrain that the glider must clear during final glide. |
|
Sets the MacCready value. |
|
Sets the bugs, 1.0 means no bugs, 0.5 means 50% polar degradation. |
|
Sets the QNH [Pascal]. |
|
Sets the ballst, 0 means no ballst, 0.5 means 50% of the maximum ballst the glider can carry. |
|
Sets the maximum temperature [Kelvin]. |
Wind¶
The Settings provides access to xcsoar wind data and settings.
The following attributes are provided by xcsoar.wind
:
Name |
Description |
---|---|
|
Wind mode, 0: Manual, 1: Circling, 2: ZigZag, 3: Both. |
|
Sets wind mode (0..3). |
|
Determines whether the snail trail is drifted with the wind when displayed in circling mode, 0: Off, 1: On. |
|
Turns Taildrift Off / On (0..1). |
|
The Source of the current wind, 0: None, 1: Manual, 2: Circling, 3: Both, 4: External. |
|
The current wind speed [\(m/s\)]. |
|
Sets manual the wind speed [\(m/s\)]. |
|
The current wind bearing [°]. |
|
Sets manual the wind bearing [°]. |
|
Clears the wind settings and calculations. |
Logger¶
The Settings provides access to xcsoar Logger data and settings.
The following attributes are provided by xcsoar.logger
:
Name |
Description |
---|---|
|
Gives back the set pilot name. |
|
Sets the pilot name. |
|
The time interval between logged points when not circling [seconds]. |
|
Sets time interval between logged points when not circling [seconds]. |
|
The time interval between logged points when circling [seconds]. |
|
Sets time interval between logged points when circling [seconds]. |
|
Status of the auto-logger; 0 = On, 1 = Take off only, 2 = Off. |
|
Sets the Autologger mode; 0 = On, 1 = Take off only, 2 = Off. |
|
Status of the NMEA-Logger; 0 = Off, 1 = On. |
|
Enables the NMEA-Logger. |
|
Disables the NMEA-Logger. |
|
Status of the log-book; 0 = Off, 1 = On. |
|
Enables the logbook. |
|
Disables the logbook. |
|
The current set logger-id. |
|
Sets the logger-id. |
Tracking¶
The Settings provides access to xcsoar Tracking settings.
The following attributes are provided by xcsoar.tracking
:
Name |
Description |
---|---|
|
States if skylines tracking is enabled. |
|
Enables skylines tracking. |
|
Disables skylines tracking. |
|
States if skylines roaming is enabled. |
|
The skylines tracking interval [seconds]. |
|
Sets the tracking interval [seconds]. |
|
If enabled, shows friends on the map, download the position of your friends live from the SkyLines server. |
|
Enables the display of friends on the map. |
|
Disables the display of friends on the map. |
|
If enabled shows nearby traffic |
|
Enables the display of nearby traffic on the map. |
|
Disables the display of nearby traffic on the map. |
|
States if livetrack24 is enabled. |
|
Enables livetrack24. |
|
Disables livetrack24. |
|
Livetrack24 tracking interval [seconds]. |
|
Sets the tracking interval [seconds]. |
|
Get current vehicle name. |
|
Sets the livetrack24 vehiclename. |
Replay¶
The Settings provides access to xcsoar Replay system.
The following attributes are provided by xcsoar.replay
:
Name |
Description |
---|---|
|
Starts replay from file |
|
Stops replay. |
|
Fast forwards |
|
Sets replay clock rate to |
|
Gets replay clock rate. |
|
Gets replay virtual time [in seconds]. |
Timers¶
The class xcsoar.timer
implements a timer that calls a given Lua
function periodically.
xcsoar.timer.new(60, function(t)
print("A minute has passed")
t:cancel()
end)
The following methods are available in xcsoar.timer
:
Name |
Description |
---|---|
|
Create a new instance and schedule it. The period is a numeric value in seconds. |
|
Cancel the timer. |
|
Reschedule the timer. |
HTTP Client¶
The class xcsoar.http.Request
can be used to send HTTP requests.
request = xcsoar.http.Request:new(
"https://download.xcsoar.org/repository")
response = request:perform()
print("status", response.status)
for name, value in pairs(response.headers) do
print("header", name, ":", value)
end
print("body", response.body)
The xcsoar.http.Request
interface:
Name |
Description |
---|---|
|
Creates a new instance. One instance can only be used once. |
|
Sends the request and waits for the response. Returns a response object. |
The response interface:
Name |
Description |
---|---|
|
The numeric HTTP status code. |
|
A table of response headers. |
|
The response body (as string). |
Legacy¶
Before version 7.0, XCSoar was adapted using the InputEvent subsystem (see Appendix [sec:input-events]). During the Lua transition, Lua scripts can invoke InputEvents, for example:
xcsoar.fire_legacy_event("Setup", "basic")
xcsoar.fire_legacy_event("Zoom", "basic")
This function will be removed before the final 7.0 release.