Commit Graph

1144 Commits

Author SHA1 Message Date
Andrew Tridgell 88957235d2 Scripting: added Lat,Lon to aerobatics POSM/POST
this makes "map POSM POST" work on aerobatic scripts
2024-09-26 16:44:13 +10:00
Iampete1 4894675977 AP_Scripting: add binding for vehicle `set_target_rate_and_throttle` 2024-09-25 08:47:30 +10:00
timtuxworth bbd9148b4b AP_Scripting: example for use Location::AltFrame for guided_state.target_alt_frame 2024-09-24 22:51:02 +10:00
Peter Barker bb9a0a5b2f AP_Scripting: re-order initialiser lines so -Werror=reorder will work 2024-09-24 22:50:28 +10:00
Thomas Watson 8b63488431 AP_Scripting: remove dependency on tmpnam
tmpnam is never linked and not necessary, and naming it/redefining it
introduces problems on platforms that do have a definition.
2024-09-24 22:49:18 +10:00
Clyde McQueen 8b37100772 autotest: large outliers in sub terrain test have low sq 2024-09-23 19:14:34 +10:00
Peter Barker 83c9b32093 AP_Scripting: add bindings for the AP_TemperatureSensor 2024-09-22 20:34:33 +10:00
bugobliterator 7513281f65 AP_Scripting: add bindings get/set full mode for GPIO
allows setting alternate pin modes pin drive strengths etc.
2024-09-21 10:07:30 +10:00
Henry Wurzburg df90b640ca AP_Scripting:Add parameter controller applet 2024-09-18 13:10:26 +10:00
Peter Barker 12d8a4d543 AP_Scripting: adjust description of aux function 90 (EK3 Source Set 2024-09-16 22:34:34 +10:00
Andrew Tridgell cbba88fccd AP_Scripting: added 2nd supply voltage to SkyPower EFI driver
and accept both extended and 11-bit CAN
2024-09-14 09:24:03 +10:00
yuri-rage cd4501e62d AP_Scripting: add advance-wp.lua 2024-09-12 14:23:43 +10:00
Andrew Tridgell 53a4b5a843 AP_Scripting: add handle_external_position_estimate binding
can be used for offboard navigation systems
2024-09-12 06:58:57 +10:00
Peter Barker 222a26ee20 AP_Scripting: tweak conditions for scripting-requires-filesystem
AP_FILESYSTEM_READING_ENABLED isn't quite right as we can't read scripts from @SYS.
2024-09-10 11:47:37 +10:00
Peter Barker 777aab6e0c AP_Scripting: no warning if no ./scripts and no real filesystem
it is possible to build for boards without storage (so no Posix, no Fatafs), but still have scripts in ROMFS.

In this case we will use the backend AP_Filesystem_backend base class when doing file operations.  This will alway fail to open directories, so when we try to load scripts from SCRIPTS_DIRECTORY it will always fail.

This leads to a warning being emitted:

Lua: State memory usage: 2796 + 5227
AP: Lua: open directory (./scripts) failed
AP: hello, world
Time has wrapped

Which isn't great.

Detect we are working on this filesystem and don't warn.
2024-09-10 09:10:49 +10:00
Tatsuya Yamaguchi 29b9157a20 AP_Scripting: use SourceSetSelection enum class 2024-09-08 10:02:29 +10:00
Randy Mackay b46d22f08b AP_Scripting: minor docs fix 2024-09-04 08:37:08 +09:00
Randy Mackay 1df029a679 AP_Scripting: minor spelling fix 2024-09-04 08:37:08 +09:00
Randy Mackay ba9bb22c3c AP_Scripting: add camera-change-settings applet 2024-09-03 09:04:39 +09:00
Randy Mackay 79983944c6 AP_Scripting: change_camera_setting binding 2024-09-03 09:04:39 +09:00
Willian Galvani 616d74f1b0 AP_Scripting: add note about generating code for extension fields 2024-08-24 14:18:53 +01:00
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
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
Iampete1 93642026aa AP_Scripting: tests: check.json: check files it gitignore but not self 2024-06-04 09:29:49 +10:00
Andrew Tridgell a2459c6e61 AP_Scripting: use NEW_NOTHROW for new(std::nothrow) 2024-06-04 09:20:21 +10:00
Andrew Tridgell d305cb47f8 AP_Scripting: added GPS yaw binding
useful for seeing if switching to a GPS yaw source set in EK3 will work
2024-05-29 12:56:57 +10:00
Andrew Tridgell bf9f3b8660 AP_Scripting: added notch_switch example
switch between two notch setups using attenuation change
2024-05-29 12:56:43 +10:00
petrosilius 1d810bfa6f AP_Scripting: added tracker Pelco-D control script
This script uses the scaled output from the antennatracker servos and map them to corresponding Pelco-D messages to be sent via a RS-485 interface to a motorized base (can be anything from motorized tracker to a PTZ camera).
If your FCU doesnt offer a RS-485 interface by default, you can use or TTL-RS485- or USB-RS485-adapters.

Pelco-D allows to control using either speed-/differential- or absolute-control control of the pan-/tilt-axis.
Currently the script uses speed based control using by mapping the "ContinuousRotation" type servos outputs to the corresponding Pelco-D messages.
The absolute control messages are implemented nevertheless for future use.

The script assumes therefor at least the following parameters to be set:

SCR_ENABLE = 1
SERVO_PITCH_TYPE = 2  # ContinuousRotation type servo
SERVO_YAW_TYPE = 2    # ContinuousRotation type servo
SERIALx_PROTOCOL = 28 # serial port used by luascript

Additionally the PITCH2SRV and YAW2SRV tuning needs to be done as described by the antennatracker description.
Also keep attention to the PITCH_MIN, PITCH_MAX and YAW_RANGE parameters to fit your Pelco-D hardware!
2024-05-28 17:42:52 +10:00
Iampete1 f5e7bfcc48 AP_Scripting: remove support for REPL 2024-05-28 10:10:14 +10:00
Iampete1 713eda617f AP_Scripting: allow new flow control type in `set_flow_control` binding 2024-05-28 09:48:19 +10:00
Iampete1 0c6f396438 AP_Scripting: docs: document all functions currently documented on the wiki 2024-05-22 18:13:53 +10:00
Iampete1 91cdea1a8d AP_Scripting: examples: fault_handling.lua: ignore errors. 2024-05-21 09:42:22 +10:00
Iampete1 3a7bf8dd7d AP_Scripting: ignore lua language check issues resulting from https://github.com/ArduPilot/ardupilot/pull/26939 2024-05-21 09:42:22 +10:00
Iampete1 d2e95583d6 AP_Scripting: docs: update 2024-05-21 09:42:22 +10:00