Commit Graph

1055 Commits

Author SHA1 Message Date
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
Thomas Watson
e18449b1b8 AP_Scripting: use generic access userdata for serial ports
Adding another layer instead of just exposing UARTDriver bindings allows
substitution of the different functions for device simulation later.

Also take the opportunity to rework the docs a little.
2024-06-27 12:00:18 +10:00
Thomas Watson
c37dba60de AP_Scripting/generator: allow disabling userdata creation from Lua
Passing -1 to the argument count for the `creation` tag (name does not
matter) will stop the generator from giving Lua a function to construct
that userdata. The C `new_<name>` function still works.
2024-06-27 12:00:18 +10:00
Pierre Kancir
396bca359d AP_Scripting: add an example to use Copter Standby mode in SITL with LUA 2024-06-27 10:11:53 +10:00
Andrew Tridgell
10c6020a66 AP_Scripting: update docs 2024-06-27 10:11:14 +10:00
Andrew Tridgell
d87404adfa AP_Scripting: added mcu_temperature binding 2024-06-27 10:11:14 +10:00
Peter Barker
9bb343938f AP_Scripting: correct use-after-free in script statistics
run_next_script can free the script if the script runs over-time.

... so stop using data from that freed script structure!
2024-06-25 11:07:07 +10:00
Iampete1
cc99ebf872 AP_Scripting: allow logging lua boolean with uint8 format B 2024-06-25 11:01:10 +10:00
Andrew Tridgell
32750476be AP_Scripting: added more telemetry for INF Inject 2024-06-25 09:34:18 +10:00
Andrew Tridgell
b108d9cf09 AP_Scripting: added throttle and ignition control for INF_INject
allows full control over serial
2024-06-25 09:34:18 +10:00
TsuyoshiKawamura
b9bf7acbf1 AP_Scripting: add test samples of get_target_location/update_target_location for copter 2024-06-25 08:29:57 +09:00
Iampete1
a322ba4f77 AP_Scripting: add binding for GPS time_epoch_usec 2024-06-18 10:37:13 +10:00
Iampete1
1c26c241f1 AP_Scripting: add uint64 userdata 2024-06-18 10:37:13 +10:00
Iampete1
574b9939a5 AP_Scripting: allow auto generation of uint32 operators 2024-06-18 10:37:13 +10:00
Andrew Tridgell
c783f632c1 AP_Scription: added CANF logging to Halo6000 EFI driver
allows for easier debugging
2024-06-09 08:37:44 +10:00
Iampete1
1c5c80d244 AP_Scripting: Docs: remove stray grave 2024-06-08 07:29:53 +10:00
Iampete1
cb22a6389d AP_Scripting: add virtual class in docs to avoid need nil check warning 2024-06-07 06:30:22 +10:00
Iampete1
364419be81 AP_Scripting: docs: allow overload of manual bindings to allow documentation of optional arguments 2024-06-04 09:29:49 +10:00
Iampete1
f54ca766df AP_Scripting: tests: luacheck.lua: ignore lua-language-server 2024-06-04 09:29:49 +10:00