Beginning with Lua

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,217
Reaction score
477
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
(updated)
For those beginning with Lua, here's a very basic example.
It will get some parameters and output them on the screen.


Go to scenarios\Cruising above Florida.scn and add "Script lua_test" to Environment.

The Lua example needs your vessels to be named like on that scenario.


Scenario (relevant part):
Code:
BEGIN_ENVIRONMENT
  System Sol
  Date MJD 54723.6753959915
  Script lua_test
END_ENVIRONMENT


Then save the Lua code below to script\lua_test.lua. You can create this on a text editor like notepad.

Lua code:
Code:
-- get our vessels, names must match scenario
v1 = vessel.get_interface('GL-01S')
v2 = vessel.get_interface('ISS')
hv = oapi.get_objhandle('GL-01S')
hp = oapi.get_objhandle('ISS')

-- define where we are going to write
noteTop = oapi.create_annotation()
noteTop:set_pos(0.3, 0.05, 1,1)
noteTop:set_colour({r=1, g=0.7, b=0.3})

noteSide = oapi.create_annotation()
noteSide:set_pos(0.1, 0.5, 1,1)
noteSide:set_colour({r=1, g=0.7, b=0.3})

-- dumb infinite loop
i = 1
while i  <= 5 do
    -- get parameters
    alt = v1:get_altitude()
    pitch = v1:get_pitch()
    bank = v1:get_bank()
    yaw = v1:get_yaw()
    angvel = 0 -- we will get this latter when get into vectors
    dir = 0    -- we will get this latter when get into vectors
    range = oapi.get_relativepos(hv,hp)
    hvpos =  oapi.get_globalpos(hv)
    hppos =  oapi.get_globalpos(hp)

    -- calculate distance to ISS
    range = vec.dist(hvpos,hppos)
    range2 = math.floor(range/1000)

    -- setup output text
    intro = "Altitute:" .. alt
	data= "Yaw " .. yaw .. "\nPitch " .. pitch .. "\nRotation ".. bank .."\nVel ".. angvel .."\nDir " .. dir  .. "\nISS distance ".. range2 
   
    -- write to screen
    noteTop:set_text (intro)
    noteSide:set_text (data)
    
    -- refresh update
    proc.wait_sysdt(0.1)
end


That's it. It should help absolute beginners to get started.


The example above was edited to fix some errors based on a better example by kuddel.
His solution is quite clever, and I'll use it as a base for some more advanced stuff.
The discussion below may not make much sense right now.
 
Last edited:

Donamy

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Oct 16, 2007
Messages
6,834
Reaction score
90
Points
123
Location
Cape
It will not load in Dx9 for me. Regular Orbiter works fine.

R-2 Beta 4
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,217
Reaction score
477
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
You are right.

There's some problem with the Dx client. Change the output to:

Code:
-- setup output text
    intro = "Altitute:" .. alt ..  "Yaw " .. yaw .. "/n Pitch " .. pitch .. "/n Rotation ".. bank .."/n Vel ".. angvel .."/n Dir " .. dir  .. "/n ISS distance ".. range2 
    --data= "Yaw " .. yaw .. "/n Pitch " .. pitch .. "/n Rotation ".. bank .."/n Vel ".. angvel .."/n Dir " .. dir  .. "/n ISS distance ".. range2 


    -- write to screen
    note:set_text (intro)
    --note2:set_text (data)

And it will work (although it's not recognizing line breaks)
 
Last edited:

Donamy

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Oct 16, 2007
Messages
6,834
Reaction score
90
Points
123
Location
Cape
it loads now with the problem you mentioned.
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,217
Reaction score
477
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
After some testing, there seems to be some trouble with the annotations on the graphic client, so I've changed to code to use the debug output - oapi.dbg_out(string).

Here's the updated Lua code:
Code:
-- get our vessels, names must match scenario
v1 = vessel.get_interface('GL-01S')
v2 = vessel.get_interface('ISS')
hv = oapi.get_objhandle('GL-01S')
hp = oapi.get_objhandle('ISS')

-- define where we are going to write 
note = oapi.create_annotation()
note:set_pos (0.3,0.05,0.9,0.95)
note:set_colour ({r=1,g=1,b=1})


-- dumb infinite loop
i = 1
while i  <= 5 do

    -- get parameters
    alt = v1:get_altitude()
    pitch = v1:get_pitch()
    bank = v1:get_bank()
    yaw = v1:get_yaw()
    angvel = 0
    dir = 0
    range = oapi.get_relativepos(hv,hp)
    hvpos =  oapi.get_globalpos(hv)
    hppos =  oapi.get_globalpos(hp)

    -- calculate distance to ISS
    range = vec.dist(hvpos,hppos)
    range2 = math.floor(range/1000)

    -- setup output text
    intro = "ISS distance ".. range2 
    data= "Altitute:" .. alt .. "|Yaw " .. yaw .. "|Pitch " .. pitch .. "|Rotation ".. bank .. "|ISS distance ".. range2 


    -- write to screen
    note:set_text (intro)
    oapi.dbg_out(data)
    
    -- refresh update
    proc.wait_sysdt(0.1)
end
 
Last edited:

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
1,665
Reaction score
192
Points
63
After some testing, there seems to be some trouble with the annotations on the graphic client
Nay, there were several issues with your (original) code ;)
e.g. line breaks are "\n" not "/n" (backslash!). It was the wrap-feature that made your line appear as multiple lines.

Some other issues were there, that I can not exactly pinpoint anymore (I think nil access/concatenation was one of them...)
But this:
PHP:
-- get our vessels, names must match scenario
v1 = vessel.get_interface('GL-01S')
v2 = vessel.get_interface('ISS')
h1 = oapi.get_objhandle('GL-01S')
h2 = oapi.get_objhandle('ISS')

-- define where we are going to write
noteTop = oapi.create_annotation()
noteTop:set_pos(0.3, 0.05, 1,1)
noteTop:set_colour({r=1, g=0.7, b=0.3})

noteSide = oapi.create_annotation()
noteSide:set_pos(0.1, 0.5, 1,1)
noteSide:set_colour({r=1, g=0.7, b=0.3})

dt = 0.25       -- [seconds]
lastRange = nil -- [m]
rr = nil        -- [m/s]

-- dumb infinite loop
while 1 do

    -- get parameters
    alt    = v1:get_altitude()
    pitch  = v1:get_pitch()
    bank   = v1:get_bank()
    yaw    = v1:get_yaw()

    -- calculate distance to ISS
    pos1 = oapi.get_globalpos(h1)
    pos2 = oapi.get_globalpos(h2)
    range  = vec.dist(pos1, pos2)

    -- calculate range rate (dx/dt)
    if lastRange ~= nil then
      rr = (lastRange-range) / 0.25
    end
    lastRange = range

    -- setup output data
    data = {
    --First 'altitude'-line is already printed on top ;)
    --"Alt: "   .. string.format("%.03f m", alt),
      "Pitch: " .. string.format("%.03f °", pitch*DEG),
      "Bank: "  .. string.format("%.03f °", bank*DEG),
      "Yaw: "   .. string.format("%.03f °", yaw*DEG),
      "Range: " .. string.format("%.03f km", range/1000)
    }
    if rr == nil then table.insert(data, "RangeRate: -")
    else              table.insert(data, "RangeRate: " .. string.format("%.03f m/s", rr)) end

    -- write to screen
    if alt == nil then  noteTop:set_text("Altitude: -")
    else                noteTop:set_text(string.format("Altitude: %.03f m", alt)) end

    noteSide:set_text( table.concat(data,"\n") );

    proc.wait_sysdt(dt)
end
works for both Inline & D3D9Client
 
Last edited:

Donamy

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Oct 16, 2007
Messages
6,834
Reaction score
90
Points
123
Location
Cape
Is it able to use SC3 vessels ?
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,217
Reaction score
477
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
Is it able to use SC3 vessels ?

Yes, it can run on top of any vessel.

---------- Post added at 10:52 ---------- Previous post was at 10:16 ----------


"Nay, there were several issues with your (original) code"

kuddel, Thanks for the corrections. I'll use them for a more advanced example :thumbup:

Don't take me wrong, but streamlined code will not be understood by someone that comes from Spacecraft3 for example.

Variables are easy to grasp, but arrays or dictionaries will not be understood unless people have some programming background.
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,217
Reaction score
477
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
Updated the example on the first post to fix the line break issue. Should work as advertised now :)

Kuddel's example will be the base of a more advanced example, but more on that latter.
 

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
PHP:
...
dt = 0.25       -- [seconds]
lastRange = nil -- [m]
rr = nil        -- [m/s]
...
    -- calculate range rate (dx/dt)
    if lastRange ~= nil then
      rr = (lastRange-range) / 0.25
    end
    lastRange = range
...
For my understanding: Regarding the range rate calculation, shouldn't the constant 0.25 be replaced with the variable dt?
 

kuddel

Donator
Donator
Joined
Apr 1, 2008
Messages
1,665
Reaction score
192
Points
63
Sure!
Functionally it makes no difference, but maintenance-wise you are 100% correct!
Almost any code has some flaws :thumbup:
 

xjflcf520

Member
Joined
Feb 26, 2021
Messages
61
Reaction score
5
Points
23
Location
Chana
Hello, I get the location information of the spaceship in Lua, using equ= oapi.get_ Equpos (), but it is found that the longitude and latitude are not the real longitude and latitude. How can I get the real longitude and latitude information of the spacecraft
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,217
Reaction score
477
Points
123
Location
Lisbon
Website
orbiterspaceport.blogspot.com
oapi.get_equpos returns a vessel's spherical equatorial coordinates (longitude, latitude and radius) with respect to the closest planet or moon.

Return values:
equ.lng - longitude [rad]
equ.lat - latitude [rad]
equ.rad - distance from object frame origin [m]

Don't know what you mean by "real" latitude and longitude.
Perhaps you are thinking about degrees instead of radians ?
For degrees Google tells me that you need to multiply the angle by 180/PI ;)
 
Last edited:

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
546
Reaction score
17
Points
33
Location
Back in Blighty - marooned at home
Hi Lua People.
Can you help me please.
I'm a complete noob with lua (n not much better with C++)
I want to use a script (aap.lua) to control a craft directly from a .dll that I compile.
I've got as far as loading the aap into the dll, and it compiles n works fine.
But I can't figure how to update the global values of the script, except manually through the lua terminal or mfd.
Is it possible to do it that way round ?
I see lots of examples of getting values from the script (embedding?) to the dll, but not the other way round.
A practical example would be a great help.
 
Top