Adding a spacecraft dynamically in Lua script

All about writing scripts for Celestia in Lua and the .cel system
Topic author
NASA_SimGuy
Posts: 24
Joined: 18.10.2018
With us: 5 years 6 months

Adding a spacecraft dynamically in Lua script

Post #1by NASA_SimGuy » 18.10.2018, 18:20

I have a working Lua script but I want to add a new spacecraft to the scene. I want to add several spacecrafts and there states using the accessors similar to celestia:newframe("universal")

After adding the spacecraft to the scene, I want to update the states from inside the Lua script.

Is this possible? Are there any good examples of this spacecraft addition from inside the Lua script?

Avatar
selden
Developer
Posts: 10190
Joined: 04.09.2002
With us: 21 years 8 months
Location: NY, USA

Post #2by selden » 19.10.2018, 11:31

Spacecraft are defined in SSC catalog files. All SSC definitions are loaded when Celestia first starts up. In other words, you can't define a spacecraft from scratch while Celestia is running.

However, you can define them with "Visible false" so they are invisible when they're loaded. You can change an object's Visible status using a Lua script while Celestia is running. 3D Meshes and their associated surface textures are not loaded until the first time they become visible, which causes Celestia to pause briefly (or not so briefly if they're large and complex).

You can write Lua routines which are used in CelX scripts or in SSC "ScriptedOrbit" and "ScriptedRotation" declarations to manipulate SSC objects while Celestia is running. See https://en.wikibooks.org/wiki/Celestia/Trajectories#ScriptedOrbit and https://en.wikibooks.org/wiki/Celestia/Rotation_Models#ScriptedRotation

Some Addons which consist of SSC objects being controlled by CelX and ScriptedOrbit definitions are available at

https://www.classe.cornell.edu/~seb/celestia/resurgence/index.html (interactive maps)
https://www.classe.cornell.edu/~seb/celestia/notebooks/index.html (interactive notebook which includes spacecraft flying around it)
https://www.classe.cornell.edu/~seb/celestia/hale_telescope.html (interactive model of the Hale telescope)
Selden

Topic author
NASA_SimGuy
Posts: 24
Joined: 18.10.2018
With us: 5 years 6 months

Not allowed to Navigate to Scripted Spacecraft

Post #3by NASA_SimGuy » 19.10.2018, 19:10

Thanks selden,

I created an SLS.ssc file defined below but I cannot Navigation|"Select Object" like I do for the ISS. I know the 3D model works because I substituted it in the ISS.scc and it showed up. I would have added the files if I knew how to attach a file to the forum. Thoughts?

# Space Launch System
"SLS vFS" "Sol/Earth"
{
Class "spacecraft"
Mesh "MPCV_EUS.cmod"
Radius 0.040

ScriptedOrbit
{
Module "MAVERIC"
Function "fetchSLSvFSpos"
}
ScriptedRotation
{
Module "MAVERIC"
Function "fetchSLSvFSquat"
}
Orientation [ 90 0 0 1 ]
}

The following functions are in my MAV.celx file. The pos and quaternions are calculated in the MAV.celx and the setvisible is true.

function fetchSLSvFS(t)
-- Create a new table
local orbit = {};
-- Save the parameter list
orbit.params = t;

-- Set the required fields boundingRadius and position; note that position is actually a function
-- Compute the bounding radius from the amplitudes
orbit.boundingRadius = 1000.0

-- The position function will be called whenever Celestia needs the position of the object
function orbit:position(tjd)
local t = tjd - 2451545.0
local x = posx[vFS]
local y = posy[vFS]
local z = posz[vFS]
return x, y, z
end
return orbit
end

-- prototype for the wobble scripted rotation has default values for
-- any parameters that are omitted in the ssc file.
wobbleproto =
{
Amplitude = 0,
Period = 1,
}

-- constructor method
function wobbleproto:new(o)
o = o or {} -- create table if one not provided
setmetatable(o, self)
self.__index = self

-- set the period to whatever value was specified in the ssc file;
-- slightly confusing because Celestia is case sensitive--period must
-- be lowercase, but the field from the ssc file is capitalized.
o.period = o.Period

return o
end

-- The orientation function. This implementation produces a back and forth
-- wobble of Amplitude degrees about the z axis.
function wobbleproto:orientation_vFS(tjd)
local t = tjd - 2451545.0

-- fetch the global quaternion representing the orientation
return q4[vFS], q1[vFS], q2[vFS], q3[vFS]
end
function fetchSLSvFSquat(sscvals)
-- create a new wobble rotation object
return wobbleproto:new(sscvals)
end

Added after 1 hour 7 minutes:
I have an update after learning a few things. I learned that I can press ~ to get my errors on the screen. Too Cool.

I noticed that I have errors relating to my lua script not found. I placed the lua script in several files not knowing where to place it. I could not find the plcae in the documentation where it belongs. After disseminating it to several directories, I not get a segmentation fault right away as it was loading sls.scc (my new lua script).

My code in the previous post has not changed. I must have a problem with that code somewhere while loading.

Avatar
selden
Developer
Posts: 10190
Joined: 04.09.2002
With us: 21 years 8 months
Location: NY, USA

Post #4by selden » 20.10.2018, 11:41

CelX scripts can be anywhere. They must not depend on any external lua files.

The lua files accessed by ScriptedOrbit and ScriptedRotation must be in the subdirectory named celxx which must be in the same directory as the .SSC catalog.

Errors in ScriptedOrbit and ScriptedRotation files usually crash Celestia instead of displaying error messages, so I usually debug their functions in CelX scripts first.

All of the code in a file defining a ScriptedOrbit (or ScriptedRotation) is executed when the ScriptedOrbit (or ScriptedOribit) is first encountered while Celestia is loading the SSC catalog. After that, only the function specified in that ScriptedOrbit (or ScriptedRotation) is called each time Celestia refreshes the screen.

When showing code in a post, it's best to surround it with the BBCode labels [ code] and [ /code] (without the spaces). It'll look like this:

Code: Select all

this is a line of code



One way to attach a file to a post is
1. select the button "Full Editor & Preview" (below the quick-edit pane)
2. select the button "Filename:" "Browse" (below the new editing pane)
3. find and select the desired zip file (on your local computer)
4. Optionally add a "File comment"
5. select the button "Filename:" "Add the file"
6. select the button "Preview" to see how your message looks, including the attachment
7. select the button "Submit" to post the message with the attachment
Attachments
test.zip
(143 Bytes) Downloaded 245 times
Selden

Avatar
gironde M
Posts: 818
Joined: 16.12.2016
Age: 71
With us: 7 years 4 months
Location: Montigny-Les-Metz, France

Post #5by gironde » 20.10.2018, 12:18

Warning :
I noticed while working on the module KeplerParamBox of Lut5 that if one compares the positions posx, posy, posz of Celx with those of a file .xyz one obtains:

posx = x position of the .xyz file
posy = z position of the .xyz file
posz = -y position of the .xyz file

I do not understand this inversion in the results but it can help you!


:hi:

Topic author
NASA_SimGuy
Posts: 24
Joined: 18.10.2018
With us: 5 years 6 months

Adding a spacecraft dynamically in Lua script

Post #6by NASA_SimGuy » 22.10.2018, 15:49

I now have an SLS vehicle with a ScriptedPosition and Rotation :smile:

I need to use my ScriptedPosition Function "vFSPos" to set the SLS position. I can hard code the orbit:position(tjd)'s returned position to a float but I want to make calls like celestia:find("Earth") and use other lua calls to fill the position's values. When I execute Celestia, the '~' command shows that the ssc fails when I try to call celestia:find("Earth")

I think the lua script makes up a dll and I cannot make celestia:xxx calls. Any ideas? My goal is to eventually read a UDP socket to set the spacecraft position.

Avatar
Croc M
Forum Admin
Posts: 437
Joined: 10.08.2016
With us: 7 years 9 months
Location: Udomlya, Tver region, Russia

Post #7by Croc » 22.10.2018, 16:13

NASA_SimGuy, I don’t create scripts, but I’ve been developing the Lua Univercal Tools GUI. Both the scripts and Tools use the CELX Lua Methods.

Here is one example of a module for searching the Earth and moving to the Earth:

Code: Select all

panel143Button.Action = (function()
    return
        function()
            local obs = celestia:getobserver()
            local obj = celestia:find("Earth")
            celestia:select(obj)
            obs:follow(obj)         
            obs:center(obj)
        end
    end) ()


I recommend to look at the files of the graphical interface.
I think there you will find the answers to your questions.
Creator of the GUI "Lua Universal Tools"
25/V/1947 - 30/III/2019

Topic author
NASA_SimGuy
Posts: 24
Joined: 18.10.2018
With us: 5 years 6 months

Scripted Positions/Quaternions

Post #8by NASA_SimGuy » 26.10.2018, 14:04

I want to learn about Celestia frames. Please don't just point me to the Wiki. Gironde wrote that I will have to swap y and z coordinates. Why is this? What coordinate system has huge z values w.r.t. x and y? I thought universal, ecliptic and universal were all the same.

I see where I can get the observer frame but my Lua script crashes when I try to get my scriptedObject frame. Why is this? I could learn more if I can get my scriptedObject frame . Then I could get myframe:getcoordinatesystem().

I have ECEF coordinate and I am trying to get them to show up properly in Celestia.

Avatar
selden
Developer
Posts: 10190
Joined: 04.09.2002
With us: 21 years 8 months
Location: NY, USA

Post #9by selden » 26.10.2018, 17:21

I haven't used frames much; about all I know for certain is that the coordinate system used internally by Celestia (the one used within ScriptedOrbit Lua routines) is rotated by 90 degrees relative to what you'd expect.

As I mentioned previously, it's usually better to debug Lua functions in CelX scripts rather than trying to debug them in ScriptedOrbits. When malformed Lua functions are used in CelX scripts, they tend to provide error messages (either in popup windows or in the console log) rather than crashing Celestia.
Selden

onetwothree
Site Admin
Posts: 704
Joined: 22.09.2018
With us: 5 years 7 months

Post #10by onetwothree » 27.10.2018, 16:05

NASA_SimGuy wrote:Gironde wrote that I will have to swap y and z coordinates. Why is this?

I suppose this is because Celestia is OpenGL application so it uses right-handed OpenGL's coordinate system.

Compare it with standard Cartesian coordinate system.

static Vector3d toStandardCoords(const Vector3d& v)
{
    return Vector3d(v.x(), -v.z(), v.y());
}

Topic author
NASA_SimGuy
Posts: 24
Joined: 18.10.2018
With us: 5 years 6 months

Post #11by NASA_SimGuy » 29.10.2018, 11:53

The y and Z might be swapped because Celestia uses a "Wandering frame" to reduce the changes of singularities when propagating.


Return to “Scripting”