Lua Scripting Live Stream [recording available on YouTube]

I will certainly give a brief intro and overview, but I do not want to spend too much time on Lua syntax and intricacies. This existing video is a primer of that sort and may be good to watch before attending the session.

2 Likes

I want to use one channel for it.

Up - tx off, mid,down - tx on

Up,mid - cam1, down - cam2

If you can afford a second channel mapped to the switch, again, transmitter mixing is what you should pursue.

If not, a Lua script can achieve it. There are lots of examples of switch position monitoring and GPIO/relay/servo output. I will not likely tackle this specific case during the live stream, since it is already very well documented.

@Yuri_Rage I have corrected the script according to the recommendations. But unfortunately not on all points, there is not enough experience. Look at my file.
I created a variable something Changed, but I’m not sure that I did everything correctly to track the switch

As I understand it, I need to create another variable with the name for example script_rc_1. the variable is needed to activate the mission.

I understood about the strict binding to the names of missions and channel numbers.

Attached below is a script and a map of my PWMs.

Also, to avoid misunderstanding, I attached a photo of the radio, with notes on the control channels.

MODE.txt (233 Bytes)

1 Like

Great initiative. Interested :+1:

1 Like

I have not forgotten about this! Sometimes I get ahead of myself, and an idea takes shape before I have the time to devote to it. I apologize to you all for letting this languish a bit.

Let’s reinvigorate the subject and make it happen. I’m going to set aside a couple of hours on Saturday, 16 April, at 1600 CST (2100 UTC).

Link to follow!

3 Likes

I only got a few ideas for a single script to write for this session, and most of them would be a bit difficult to implement within SITL (but they were all good ideas!), so here’s a brief outline of what I think the session should cover:

  • Brief overview of Lua syntax and structures
  • Brief overview of ArduPilot’s Lua idiosyncrasies
    • Documentation sources/links
    • VS Code and SITL setup
    • Hello World
    • Hello World in a simple recursive loop
    • Hello World in a more complex “state machine” type algorithm
  • Reading RC switch values and controlling servo/relay output
  • Reading sensor (GPS/accelerometer/etc) values and acting on them conditionally
  • Further short demos or discussion generated by questions along the way

I will probably demo this all within the Rover branch, since it requires very little setup in SITL before one can “drive” around. I do have the ability to connect a transmitter as a joystick, so we can explore Copter or Plane as time allows. But the vehicle/frame type is pretty inconsequential, since scripting basics transfer very directly from one to another.

I intend this to be fairly interactive, so we can deviate as needed so long as there’s a way to demonstrate the given topic within the constraints of SITL (or time).

I think 2 hours should be sufficient, but I’ll stick around a bit longer as needed.

2 Likes

Even if I can’t make the live session I will be watching the video after the fact! Thanks for taking this on.

1 Like

I have wanted to do a param banks example for ages.

It would read a param file from the SD card and load all the params when you set a switch. Maybe saving the original param set to a new file and reseting to those when the switch is returned.

2 Likes

I almost included parameter access as a topic above. Time permitting, parameter manipulation and SD file manipulation would be great additions.

I have been considering my own dynamic parameter set via Lua to account for the fact that on a hydrostatic vehicle, autopilot throttle command vs expected speed can vary with hydraulic pressure/engine RPM.

I’m really looking forward to this!

1 Like

So this is coming up. Yuri, where is the link?

I will post it a few hours prior to the stream. Still working out a few logistics. Most of my tomorrow will be spent prepping!

1 Like

If this helps, for similar streamings we have been using an HDMI encoder like this:
image
Use a laptop with HDMI output (or USB to HDMI adapter) for an external monitor, connect this output the encoder input, and the encoder output to your monitor. Stream to a Twitch channel.

In this way, no software to worry about. What you put on the external monitor can be watched (with your PC sound (directed to HDMI) or microphone on encoder) only connecting to that Twitch channel.

Although with 20 to 30" delay, this is plug and play.

@Webillo, that’s a hardware solution to a software problem. No need.

Stream will be hosted on Zoom.

I will not simulcast to other media, but I will capture the entire broadcast for upload to YouTube later this weekend.

Going live in just under 2 hours from this post’s timestamp.

Topic: Lua Scripting Live Stream
Time: Apr 16, 2022 04:00 PM Central Time (US and Canada)

(stale link removed)

I have opened the Zoom session about 15 mins early and should be good to begin the demo(s) on time!

Thanks to all who attended! It was a small audience, but an excellent group. I was grappling with the “waiting room” and speaking/demoing live, so my sincerest apologies if you got to the waiting room and left in frustration.

Thanks so much to @ktrussell, @geofrancis, @jax200, and the others whose forum tags I missed.

Particular thanks to @iampete for taking time in the middle of the night to teach us all a bit more along the way!

The stream recorded very well, including all discussion. I did some minor editing for a total runtime of 2 hours, 20 minutes, available on YouTube at this link.

Slides, examples, etc will be made available on GitHub as well, and I will make at least one PR to include the discussed GPS fix script in AP_Scripting/examples.

6 Likes

Sad I had to miss it. I have to watch later.

1 Like

Slideshow and examples are hosted on GitHub:

@iampete - ahrs:get_vibration() is an existing binding, so we were wrong about that!



Since I mentioned a table (array) demo but never actually did one, here’s a short primer:

Declare a table like so:
local tbl = {'apples', 'oranges', 'bananas'}

Though I used all strings, the element types do not have to match.

Unless indices are expliclty defined, tables are integer indexed starting at 1. Use square brackets to reference an element index like so:
local element = tbl[1] -- element is now assigned 'apples'

Unlike more strict language array types, Lua tables are not fixed size and can be expanded on the fly:
tbl[4] = 'peaches'

For a 0-based table, declare like so:

local nerdy_tbl = {
    [0] = 'count',
    [1] = 'from',
    [2] = 'zero!'
}

Indices must be unique, but they do not have to be integers. This is also a valid table (that you should never use :slight_smile: ):

local confusing_tbl = {
    ['zero'] = true,
    [1] = 3.1415927,
    [true] = false
}

Multi-dimensional tables are also possible:

local multi_tbl = {
    {'apples', 'oranges', 'bananas'},
    {'peaches', 'apricots', 'durian'}
}
local element = multi_tbl[1][2] -- element is now assigned 'oranges'

For more info (like iterating over table elements), visit https://lua.org

2 Likes

I never promised no homework, so if you can explain (not just guess the output of) the logic presented in this obfuscated mess, you have an excellent handle on Lua scope, typing, and, to an extent, tables:

True = false

local function False()
    local True = true
    return false
end

if not True then
    True = False
else
    True = true
end

False = {
    [True()] = true,
    [not True()] = false
}

print(False[true])