Commit Graph

1073 Commits

Author SHA1 Message Date
Randy Mackay 0d568b500a AP_Scripting: message_interval example handles 0hz 2024-08-21 11:41:50 +09:00
Andrew Tridgell d3a1d515cb AP_Scripting: added TECS tuning to revert param script 2024-08-21 06:46:56 +10:00
timtuxworth 8a93c6a264 AP_Scripting: Bindings for crosstrack in Lua 2024-08-20 09:10:42 +10:00
Loki077 0c29d4280c AP_Scripting: AP_ESC_Telem method update
-Updated AP_ESC_Telem methods to use ESC_TELEM_MAX_ESCS instead of NUM_SERVO_CHANNELS.
- the place where the function returns bool adding skip check.
2024-08-19 21:16:41 +10:00
Loki077 46c98d6c95 AP_Scripting: added lua binding get_last_update for efi and esc 2024-08-19 21:16:41 +10:00
Peter Barker 3194fa1385 AP_Scripting: correct compilation when AP_SCRIPTING_SERIALDEVICE_ENABLED false 2024-08-13 10:43:29 +10:00
George Zogopoulos 9df4796b70 AP_Scripting: Typo fixes 2024-08-06 14:03:27 +10:00
Thomas Watson a164abaafb AP_Scripting: add REPL applet
Implements a reasonably comfortable REPL accessible over serial entirely
as a loadable script.

Also accessible over MAVLink using QGroundControl's MAVLink Console and
the new mavport module.
2024-08-06 09:08:04 +10:00
Thomas Watson 87d2b017aa AP_Scripting: add mavport module
Abstracts the MAVLink SERIAL_CONTROL message to be usable as a Lua port,
in particular the DEV_SHELL device supported by QGroundControl's MAVLink
Console.
2024-08-06 09:08:04 +10:00
Andrew Tridgell d48f9ef12c AP_Scripting: added example copter_deploy.lua 2024-08-03 14:29:51 +10:00
Andrew Tridgell ebad1631d7 AP_Scripting: added arm_force() and baro get_altitude_difference() 2024-08-03 14:29:51 +10:00
Hayden Donald 52ba8e8513 AP_Scripting: Change serial depends HAL_GCS to AP_SERIALMANAGER
Serial bindings were dependant on HAL_GCS_ENABLED but this is not ideal, it should be dependant on AP_SERIALMANAGER_ENABLED so that serial can function without GCS
2024-07-31 08:27:34 +10:00
Thomas Watson dc4d1bacdd AP_Scripting: adjust string metatable setup to fix sandbox integrity
In Lua, strings are the only type that come with a default metatable.
The metatable must be shared by all string objects, and it is set to be
the `string` library table each time that library is opened. In
Ardupilot's scripting engine, the last script to load then has access to
the string metatable as the library is opened fresh for each script, as
its `string` library will have been set to the metatable.

Therefore, if two scripts are loaded, A first and B second, and script B
executes e.g. `string.byte = "haha"`, then `string.byte()` and
`s:byte()` for script B are broken. Because the metatable is shared,
this also breaks `s:byte()` for script A, which violates the integrity
of the sandbox.

Fix the issue by disabling the metatable setup functionality when the
string libary is opened, then manually opening an additional copy of the
library (which won't be given to any script) and setting it as the
string metatable during intialization.

This will break any script that modifies the string metatable for
constructive purposes, but such a script could have been broken if it
weren't the only script running anyway.
2024-07-30 10:54:00 +10:00
Thomas Watson 3a834e83c7 AP_Scripting: reference script environment directly
Referencing the original function to run is of questionable value and
the only user uses it to grab the script environent from the upvalues.

Instead, use a reference to the script environment table directly.
2024-07-30 10:33:56 +10:00
Thomas Watson 15255a36e4 AP_Scripting: reference function to run separately
Some bits of the code in the require machinery use the `lua_ref` to
access the script environment. However, this can change after the script
is rescheduled and it returns an arbitrary function to run next.

Resolve this by introducing `run_ref` which is specifically a reference
to the function to run next. `lua_ref` is preserved for the script
lifetime.
2024-07-30 10:33:56 +10:00
Thomas Watson da006d427d AP_Scripting: add test for require() after rescheduling
It can end up broken due to incorrect assumptions about the executed
function's upvalues, which might change after rescheduling.
2024-07-30 10:33:56 +10:00
Thomas Watson 6acfc98cf4 AP_Scripting: factor out ap_object checker
Reduces flash usage.

Saves ~160B.
2024-07-30 09:55:25 +10:00
Thomas Watson 212256731f AP_Scripting: improve object allocation efficiency
Make new_X return the new object instead of having to grab it to
configure it.

Saves ~1.3K.
2024-07-30 09:55:25 +10:00
Thomas Watson 3e05cd9729 AP_Scripting: factor out field access argument error
Saves ~830B.
2024-07-30 09:55:25 +10:00
Thomas Watson 575ed6224f AP_Scripting: improve binding index function efficiency
Move the string checks into the load functions to avoid duplicating it
for each binding.

Also sync up the return types to avoid an unnecessary conversion.

Saves ~1.5K.
2024-07-30 09:55:25 +10:00
Thomas Watson e66d2a1495 AP_Scripting: remove redundant memsets
Matches current generator behavior.
2024-07-30 09:55:25 +10:00
Thomas Watson 837b17e82e AP_Scripting: more intelligently manage Lua stack
The Lua stack is guaranteed to have at least LUA_MINSTACK (default 20)
slots upon entry to C. Check to see if we might need more than that
minimum and only in that case call the function to check and resize the
stack. In virtually all cases the check can then be optimized away.

Additionally remove the redundant "Out of stack" message. Lua already
says "stack overflow" and a null message is valid.

Saves ~330B.
2024-07-30 09:55:25 +10:00
Thomas Watson 7212e35fb0 AP_Scripting: slightly improve codegen
Get singleton pointer closer to where it is used.

Saves ~170B.
2024-07-30 09:55:25 +10:00
Ian Burwell 6b4e11090c AP_Scripting: Add BQ40Z shutdown example
This example intercepts PREFLIGHT_REBOOT_SHUTDOWN COMMAND_LONG's and if
param1==2, it shuts down the BQ40Z smart battery BMS. Otherwise it
passes through the COMMAND_LONG as a COMMAND_INT (this required updating
the gcs:run_command_int to return a MAV_RESULT rather than a bool).
2024-07-30 09:40:10 +10:00
Ian Burwell 9d34fa2a5e AP_Scripting: Add I2C transfer bindings
This adds bindings for an I2CDevice's transfer() function, an example,
and removes the nil return hint from the get_device() docs as it never
actually returns nil.
2024-07-30 09:40:10 +10:00
Thomas Watson b64ed6ca56 AP_Scripting: dynamically load some binding objects
Only create the binding object (singleton metatable/userdata or C
function reference) once the user first references a particular
singleton or userdata creation function. Once created, the object is
stored into the script's environment so it doesn't get recreated on the
next reference and there isn't any further overhead. The userdatas are
no longer shared between scripts which imposes a slight memory penalty
for multiple scripts using the same singleton but this avoids an
additional lookup time cost.

Userdata and ap_objects aren't eligible for this optimization as the C++
code might want a particular metatable at any time.

Saves ~9.3K Lua heap.
2024-07-23 10:34:52 +10:00
Thomas Watson 059af2117d AP_Scripting: place bindings into global table
The global table is then used as the __index metamethod of each state's
environment table. Avoids the overhead of loading binding objects into
each state. The binding objects are immutable from Lua so sandboxing is
not violated.

Does have the slight downside that a script can no longer know all the
binding names by enumerating _ENV.

Saves ~700B of memory per loaded script.
2024-07-23 10:34:52 +10:00
Thomas Watson 9cbec043d0 AP_Scripting: remove __call metamethod from singleton metatables
The __call metamethod was set to the metatable itself. With __call not
present, Lua will try to call the metatable (and fail), which is the
same behavior as with the __call metamethod set to the metatable.

Saves ~2K Lua heap.
2024-07-23 10:34:52 +10:00
Thomas Watson 8e303675fe AP_Scripting: don't put userdata and ap_objects into globals
They are never accessed from globals. Only their metatables are
accessed, using luaL_getmetatable.

Saves ~2.9K of Lua heap.
2024-07-23 10:34:52 +10:00
Thomas Watson 50eac0ef31 AP_Scripting: keep enum definitions in flash
Saves ~100B of statically allocated RAM.
2024-07-23 10:34:52 +10:00
Peter Barker 56773f09b4 AP_Scripting: docs for get_resistance 2024-07-22 09:09:26 +10:00
Peter Barker 6514a3522b AP_Scripting: add battery-resistance-checking example 2024-07-22 09:09:26 +10:00
Peter Barker e94ebe4d1a AP_Scripting: add battery resistance to bindings 2024-07-22 09:09:26 +10:00
Willian Galvani aca9940534 AP_Scripting: add Readme.md for generating message definitions 2024-07-20 20:45:41 +01:00
Andrew Tridgell 42a1717676 AP_Scripting: added MAV_CMD_SET_HAGL example
for external height source for fixed wing landing
2024-07-17 08:03:18 +10:00
Andrew Tridgell 7477c4e205 AP_Scripting: fixed remove call
need to use AP_Filesystem
2024-07-11 07:43:35 +10:00
Peter Barker 933aed5f91 AP_Scripting: add missing scheduler include
corrects compilation of cubeorange-periph-heavy
2024-07-09 07:40:03 +10:00
Andrew Tridgell 842a4f507b AP_Scripting: added NMEA 2000 EFI driver
this has been tested on a marine engine, and correctly produces key
telemetry data

the NMEA_2000.lua module is broken out to make it easier to add other
NMEA 2000 based drivers
2024-07-06 07:19:25 +10:00
George Zogopoulos 61c910b08b AP_Scripting: Added script for Quad-X CoM compensation
The script uses the scripting matrix to produce non-equal
front and back thrust, compensating for the lever arm between the center
of thrust and the center of mass.
2024-07-03 18:44:36 +10:00
Thomas Watson 717c5ff863 AP_Scripting: ignore hidden Lua files
On macOS, sometimes ._script.lua is created to store metadata when the
user copies script.lua over to their SD card. Previously, the scripting
engine would barf since the file is not Lua. Now, these files are
ignored.

Also avoids a case where a hidden and valid script might be loaded
without the user's knowledge.
2024-07-02 11:07:40 +10:00
Peter Barker e88d76d72d AP_Scripting: rangefinder singleton requires AP_RANGEFINDER_ENABLED
.... or HAL_PERIPH_ENABLE_RANGEFINDER
2024-07-02 09:17:26 +10:00
Andrew Tridgell 38ca478178 AP_Scripting: added gcs:run_command_int() binding 2024-07-01 20:08:29 +10:00
George Zogopoulos 2717470df1 AP_Scripting: Misc. small improvements.
Typo fix
Removed unused variable
2024-06-28 10:05:29 +10:00
Thomas Watson 6e0d7bddf1 AP_Scripting: add serial device simulation example 2024-06-27 12:00:18 +10:00
Thomas Watson f9b138724c AP_Scripting: add serial loopback test script
Tests that data can flow both ways with one end using protocol 28
(Scripting) and the other using the serial device simulation feature.
2024-06-27 12:00:18 +10:00
Thomas Watson 46e204dd6c AP_Scripting: clear serial device sim buffers appropriately
Ensures the script won't process data created before it started, and
that the protocol won't process data created after the script stopped.
2024-06-27 12:00:18 +10:00
Thomas Watson a077e4a3ed AP_Scripting: introduce serial device simulation support
Allows a script to simulate a device attached via any serial protocol.
The script can read and write data and have it handled according to the
protocol as if exchanged over a serial port. The script can then do
protocol translation, data filtering and validation,
hardware-in-the-loop simulation, experimentation, etc., especially in
combination with the scripting protocol which lets the script itself
handle an attached device and so interpose any communication.
2024-06-27 12:00:18 +10:00
Thomas Watson 4df2a1bce7 AP_Scripting/generator: generate depends for manual functions on singletons 2024-06-27 12:00:18 +10:00
Thomas Watson c18f9f733e AP_Scripting: add writestring for serial
Enables more efficient scripting.
2024-06-27 12:00:18 +10:00
Thomas Watson d93aa15f2a AP_Scripting: refactor serial readstring
Using `luaL_Buffer` avoids the need for any heap allocation in the
common case (count <= 512 bytes) and avoids stressing out the system
heap for large reads, instead using the script heap.

Zero net flash usage change.
2024-06-27 12:00:18 +10:00