Orbiter BETA Lua development

xjflcf520

Member
Joined
Feb 26, 2021
Messages
70
Reaction score
6
Points
23
Location
Chana
Hello again,

I've managed to port the old project/solution to Visual Studio 2017 and was able to dig into the touchdownpoints issue.
There was actually a bug in the interface implementation ?
So, in the 1st post you'll find a new ZIP (LuaScript 2021-03-30(dlls).zip) for Orbiter BETA which you can try!
I will provide the Builds (ZIPs) for Orbiter 2016 when I find the time to do it.

Attached, my test Lua (overwrite Orbiters Script\Tests\unit_test.lua file and run it via Scenarios\Tests\unit_test.scn).
I had to rename it to unit_test.lua.txt as .lua is a not accepted attachment extenssion, but I guess you know how to rename it ;)

The "relevant" parts here:
Code:
add_line("Test: vessel:get_touchdownpoints()")
v = vessel.get_interface("GL-01")
td1,td2,td3 = v:get_touchdownpoints()
assert(td1.x == 0 and td1.y == -1.5 and td1.z == 9 and td2.x == -6 and td2.y == -0.8 and td2.z == -5 and td3.x == 3 and td3.y == -1.2 and td3.z == -5)
v:Gear(1)
proc.wait_simdt(7)
td1,td2,td3 = v:get_touchdownpoints()
assert(td1.x == 0 and td1.y == -2.57 and td1.z == 10 and td2.x == -3.5 and td2.y == -2.57 and td2.z == -1 and td3.x == 3.5 and td3.y == -2.57 and td3.z == -1)
v:Gear(0)
proc.wait_simdt(7)
td1,td2,td3 = v:get_touchdownpoints()
assert(td1.x == 0 and td1.y == -1.5 and td1.z == 9 and td2.x == -6 and td2.y == -0.8 and td2.z == -5 and td3.x == 3 and td3.y == -1.2 and td3.z == -5)
pass()

add_line("Test: vessel:get_touchdownpointcount()")
v = vessel.get_interface("GL-01")
tdc = v:get_touchdownpointcount()
assert(tdc == 13)
pass()

exp = {} -- expected "gear up" results
exp[ 0] = {pos={x=0, y=-1.5, z=9}, stiffness=1e7, damping=1e5, mu=3.5445e-08, mu_lng=3}
exp[ 1] = {pos={x=-6, y=-0.8, z=-5}, stiffness=1e7, damping=1e5, mu=2.21197e-08, mu_lng=3}
exp[ 2] = {pos={x=3, y=-1.2, z=-5}, stiffness=1e7, damping=1e5, mu=4.24352e-08, mu_lng=3}
exp[ 3] = {pos={x=-8.5, y=-0.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=-9.12894e+306, mu_lng=3}
exp[ 4] = {pos={x=8.5, y=-0.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=-1.63524e-206, mu_lng=3}
exp[ 5] = {pos={x=-8.5, y=-0.4, z=-3}, stiffness=1e7, damping=1e5, mu=2.00016, mu_lng=3}
exp[ 6] = {pos={x=8.5, y=-0.4, z=-3}, stiffness=1e7, damping=1e5, mu=1.03171e+26, mu_lng=3}
exp[ 7] = {pos={x=-8.85, y=2.3, z=-5.05}, stiffness=1e7, damping=1e5, mu=1.42505e+103, mu_lng=3}
exp[ 8] = {pos={x=8.85, y=2.3, z=-5.05}, stiffness=1e7, damping=1e5, mu=9.53414e-130, mu_lng=3}
exp[ 9] = {pos={x=-8.85, y=2.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=5.50352e-53, mu_lng=3}
exp[10] = {pos={x=8.85, y=2.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=5.88118e-308, mu_lng=3}
exp[11] = {pos={x=0,y=2, z=6.2}, stiffness=1e7, damping=1e5, mu=2.93186e-308, mu_lng=3}
exp[12] = {pos={x=0,y=-0.6, z=10.65}, stiffness=1e7, damping=1e5, mu=1.42005e-306, mu_lng=3}

exp2 = {} -- expected "gear down" results
exp2[0] = {pos={x=0, y=-2.57, z=10}, stiffness=1e6, damping=1e5, mu=9.09091e-08, mu_lng=1.6}
exp2[1] = {pos={x=-3.5, y=-2.57, z=-1}, stiffness=1e6, damping=1e5, mu=4.54545e-07, mu_lng=3}
exp2[2] = {pos={x=3.5, y=-2.57, z=-1}, stiffness=1e6, damping=1e5, mu=4.54545e-07, mu_lng=3}
exp2[3] = {pos={x=-8.5, y=-0.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=-5.10637e-310, mu_lng=3}
exp2[4] = {pos={x=8.5, y=-0.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=3.11088e-302, mu_lng=3}
exp2[5] = {pos={x=-8.5, y=-0.4, z=-3}, stiffness=1e7, damping=1e5, mu=0, mu_lng=3}
exp2[6] = {pos={x=8.5, y=-0.4, z=-3}, stiffness=1e7, damping=1e5, mu=0, mu_lng=3}
exp2[7] = {pos={x=-8.85, y=2.3, z=-5.05}, stiffness=1e7, damping=1e5, mu=0, mu_lng=3}
exp2[8] = {pos={x=8.85, y=2.3, z=-5.05}, stiffness=1e7, damping=1e5, mu=0, mu_lng=3}
exp2[9] = {pos={x=-8.85, y=2.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=nil, mu_lng=3}
exp2[10] = {pos={x=8.85, y=2.3, z=-7.05}, stiffness=1e7, damping=1e5, mu=-3.53744e-310, mu_lng=3}
exp2[11] = {pos={x=0, y=2, z=6.2}, stiffness=1e7, damping=1e5, mu=2.76101e-309, mu_lng=3}
exp2[12] = {pos={x=0, y=-0.6, z=10.65}, stiffness=1e7, damping=1e5, mu=0, mu_lng=3}

add_line("Test: vessel:get_touchdownpoints(GEARUP \"NewAPI\")")
v = vessel.get_interface("GL-01")
tdc = v:get_touchdownpointcount()
for i=0,tdc-1 do
  tdp = v:get_touchdownpoints(i)
  assert(tdp.pos.x == exp[i].pos.x and tdp.pos.y == exp[i].pos.y and tdp.pos.z == exp[i].pos.z
     and tdp.stiffness == exp[i].stiffness
     and tdp.damping == exp[i].damping
   --and  tdp.mu == exp[i].mu
     and tdp.mu_lng == exp[i].mu_lng
  )
end
pass()
v:Gear(1)
proc.wait_simdt(7)
add_line("Test: vessel:get_touchdownpoints(GEARDOWN \"NewAPI\")")
for i=0,tdc-1 do
  tdp = v:get_touchdownpoints(i)
  assert(tdp.pos.x == exp2[i].pos.x and tdp.pos.y == exp2[i].pos.y and tdp.pos.z == exp2[i].pos.z
     and tdp.stiffness == exp2[i].stiffness
     and tdp.damping == exp2[i].damping
   --and  tdp.mu == exp2[i].mu
     and tdp.mu_lng == exp2[i].mu_lng
  )
end
pass()
v:Gear(0)
proc.wait_simdt(7)
add_line("Test: vessel:get_touchdownpoints(GEARUP2 \"NewAPI\")")
for i=0,tdc-1 do
  tdp = v:get_touchdownpoints(i)
  assert(tdp.pos.x == exp[i].pos.x and tdp.pos.y == exp[i].pos.y and tdp.pos.z == exp[i].pos.z
     and tdp.stiffness == exp[i].stiffness
     and tdp.damping == exp[i].damping
   --and  tdp.mu == exp[i].mu
     and tdp.mu_lng == exp[i].mu_lng
  )
end
pass()

add_line("Test: vessel:set_touchdownpoints(\"NewAPI\")")
v = vessel.get_interface("GL-01")
arr = {}
arr[1] = v:get_touchdownpoints(0) -- NEW, 'cause index provided
arr[2] = v:get_touchdownpoints(1) -- NEW,   "     "     "
arr[3] = v:get_touchdownpoints(2) -- NEW,   "     "     "
arr[4] = v:get_touchdownpoints(3) -- NEW,   "     "     "
arr[5] = v:get_touchdownpoints(4) -- NEW,   "     "     "
arr[6] = v:get_touchdownpoints(5) -- NEW,   "     "     "
v:set_touchdownpoints(arr)
pass()

--note:set_colour ({r=0,g=0.9,b=0})
add_line("=== All tests passed ===", "green")
Hello, I tried to use part of the code about setting touchdownpoints in your Lua file, and verified whether it was set successfully, but the test failed. Is it because my spaceship was created through spacecraft 3 instead of being created as a DLL model that my spaceship could not set touchdownpoints
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,272
Reaction score
3,244
Points
203
Location
Toulouse
AFAIK you don't even need a module if you go "full LUA" ; I think to remember that Martins provided a full LUA implementation of the ShuttlePB at some point (correct me if I'm wrong).

Edit : yes just checked out search for "ScriptPB.cfg" in Orbiter 2016 beta configuration files.
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
Wow, didn't know that[1] existed - thanks for the hint @N_Molson ?

[1] Config\Vessels\ScriptPB.cfg
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,272
Reaction score
3,244
Points
203
Location
Toulouse
Yes, I'd say the whole LUA API is pretty much functional, but was very under-documented ?
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
2,064
Reaction score
507
Points
113
Lua for Orbiter also never got too much love from developers, so I can understand that there were other things more important.
But at lease someone has tried to use it :D ...and immediately a bug was found ?
 

Mars Bleu

New member
Joined
Jan 1, 2018
Messages
8
Reaction score
3
Points
3
After reading each post of this thread, I downloaded the Lua DLLs for Orbiter 2016. Once installed these new dll instead of the
originals of Orbiter 2016, I ran my homemade Lua script, and all went OK.
I am interrested by the new possibilities of access to new API methods, like get_forcevector.

I inserted: current_force=v:get_forcevector(), and my running Lua script froze...
current_force=oapi.get_forcevector(), and also freezing.

A try with get_COG_elev: current_COG_elev=v:get_COG_elev() made my script freeze.
current_COG_elev=oapi.get_COG_elev() froze too.

Another try with the indexed v:get_machnumber() let my script run.

Did I miss something with the "new" OAPI keywords?


I'd like to "extract" a G value to display it on my simpit.
 

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
After reading each post of this thread, I downloaded the Lua DLLs for Orbiter 2016. Once installed these new dll instead of the
originals of Orbiter 2016, I ran my homemade Lua script, and all went OK.
I am interrested by the new possibilities of access to new API methods, like get_forcevector.

I inserted: current_force=v:get_forcevector(), and my running Lua script froze...
current_force=oapi.get_forcevector(), and also freezing.

A try with get_COG_elev: current_COG_elev=v:get_COG_elev() made my script freeze.
current_COG_elev=oapi.get_COG_elev() froze too.

Another try with the indexed v:get_machnumber() let my script run.

Did I miss something with the "new" OAPI keywords?


I'd like to "extract" a G value to display it on my simpit.
Can you post the code that "freezes" ?
Here is what I've used in the past:

Code:
--rounding numbers at the required decimal place, minus idp for rounding at tens, hundreds etc
function round(num, idp)
    local mult = 10^(idp or 0)
    return math.floor(num * mult + 0.5) / mult
end

function get_G()
    local v = vessel.get_focusinterface()
    local hobj = v:get_gravityref()
    local body_m = oapi.get_mass(hobj)
    local r = oapi.get_size(hobj)
    local g_0 = (GGRAV*body_m/r^2)/G0
    local force = v:get_forcevector()
    local weight = v:get_weightvector()
    local mass = v:get_mass()
    local g_force_v = vec.div(vec.sub(force,weight),mass)
    g_force = vec.length(g_force_v)/G0
    if g_force > 99.99 then
        g_force = 99.99
    end    
    if v:is_landed() ~= nil and round(g_force,2) == 0.00 then  --take care of the bug that shows 0G when landed
        g_force = g_0
    end
    return round(g_force,2)
end

Here is an example in the sim:
Code:
--setup a note on screen
note = oapi.create_annotation()
note:set_pos (0.375,0.1,1,1)
note:set_colour ({r=1,g=0.6,b=0.2})
note:set_size (0.85)

--loop to get the Gs in every frame
goals = 0
while goals < 1 do
   g_force = get_G()
   msg = string.format("G: %.2f", g_force)
   note:set_text(msg)
   proc.skip()
   if goals > 0 then end
end
 
Last edited:

Mars Bleu

New member
Joined
Jan 1, 2018
Messages
8
Reaction score
3
Points
3
Thank you for your two examples. That makes me realize I wanted to go too fast. I should have tried
v:get_forcevector() on a small* code like you show, instead of include it in my big "main" script.

*Small, but makes me learn a lot about Lua.

OK to post my script, but it makes almost 800 lines, and works with an Arduino sketch running
on a Due. Does it fit here?

The organization:

Read the length of the .scn file to read to define the dimensions of the following array.
Load in an array all the lines of the scn file.
Read the lines relatives to the Xr2, my favorite spacebird.
Shape the Xr2 extracted data (46 bytes) and send them by USB port to initialize my simpit (building still in process).
A refresh loop collects OAPI data and sends them to the Due, until a button is pressed on the simpit. This is where I tried vainly "v:get_forcevector()"

BTW, Widdernix has been very helpul for data transmitting Lua -> Arduino/Arduino->Lua

Regards,
Mars Bleu
 
Last edited:

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
Thank you for your two examples. That makes me realize I wanted to go too fast. I should have tried
v:get_forcevector() on a small* code like you show, instead of include it in my big "main" script.

*Small, but makes me learn a lot about Lua.

OK to post my script, but it makes almost 800 lines, and works with an Arduino sketch running
on a Due. Does it fit here?

The organization:

Read the length of the .scn file to read to define the dimensions of the following array.
Load in an array all the lines of the scn file.
Read the lines relatives to the Xr2, my favorite spacebird.
Shape the Xr2 extracted data (46 bytes) and send them by USB port to initialize my simpit (building still in process).
A refresh loop collects OAPI data and sends them to the Due, until a button is pressed on the simpit. This is where I tried vainly "v:get_forcevector()"

BTW, Widdernix has been very helpul for data transmitting Lua -> Arduino/Arduino->Lua

Regards,
Mars Bleu
If my reply solved your problem, there is no need to post your script, unless you want to share it with the rest of the forum.
If that's the case, then you can either post the code between [CODE /CODE] tags , or rename it to .txt and attach it to your post.
 

Mars Bleu

New member
Joined
Jan 1, 2018
Messages
8
Reaction score
3
Points
3
Indeed, problem more than solved!
Sharing my codes with the rest of the forum makes part of my projects.
I had begun a thread about it, but the lack of time made me stop for a while.

Anyway, I'm gonna make the most of the indications you posted, and improve
my coding, that I learnt by doing.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,327
Reaction score
3,248
Points
138
Location
Massachusetts
Do equivalents to oapiReadScenario_nextline(scn, line) and oapiWriteScenario_int(scn, "status", status) exist for Lua in Orbiter 2016 to read and write animation and vessel status and states to the scenario file?
 
Top