C++ Question Earth-Tracking HGA

Max-Q

99 40
Addon Developer
Joined
Jul 5, 2021
Messages
765
Reaction score
1,183
Points
108
Location
Cislunar Space
Website
www.orbiter-forum.com
So I am working on a project (Lunar Rover) where the HGA needs to be kept pointed at the earth. I have the code to drive the HGA to working, and I can make it stay pointed at any celestial body... EXCEPT Earth!

Here is my code:
C++:
VECTOR3 rearth;
VECTOR3 rearthinv;
VECTOR3 rearthloc;

oapiGetRelativePos(GetHandle(), oapiGetGbodyByName("Earth"), &rearth);
rearthinv = _V(-rearth.x, -rearth.y, -rearth.z);
Global2Local(rearthinv, rearthloc);

//These are the functions that actually move the HGA
HGAAzimuth(simdt, rearthloc);
HGAElevation(simdt, rearthloc);

This makes the HGA point at the sun, not earth!

If I change the 'oapiGetGbodyByName("Earth")' to, say 'oapiGetGbodyByName("Jupiter")' than it points at Jupiter as I would expect.
Same for anything but Earth! Mars works correctly, etc. Just not Earth, which, unfortunately is the one place in the solar system I need to send a signal to!
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,609
Reaction score
2,330
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Of course it must be it. You are in RELATIVE coordinates, not GLOBAL coordinates.

(After lengthy search in the API documentation and lots of thinking about weird return values... it suddenly hit me like Mjolnir. Sorry)


So, for the Global2Local function to work properly, you have to stay in the global coordinates - and thus simply request the global position of Earth.
 

Max-Q

99 40
Addon Developer
Joined
Jul 5, 2021
Messages
765
Reaction score
1,183
Points
108
Location
Cislunar Space
Website
www.orbiter-forum.com
So, I changed it to 'oapiGetGlobalPos(oapiGetGbodyByName("Earth"), &rearth);'
and it STILL is pointing at the sun! What am I missing here?
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,609
Reaction score
2,330
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
So, I changed it to 'oapiGetGlobalPos(oapiGetGbodyByName("Earth"), &rearth);'
and it STILL is pointing at the sun! What am I missing here?

Do you still invert the coordinates BEFORE converting them into local coordinates?

If its really pointing at the origin (0,0,0) of the global coordinate system, I would say the handle for Earth is invalid.
 

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
5,914
Reaction score
2,908
Points
188
Website
github.com
Could oapiGetGbodyByName("Earth") be returning NULL for some reason, and the rest "defaults" to the Sun?
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,609
Reaction score
2,330
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
That is the next question. Maybe the position vector is initialized to (0,0,0) and gets not overwritten by valid data, because the handle for Earth is invalid.
 

Max-Q

99 40
Addon Developer
Joined
Jul 5, 2021
Messages
765
Reaction score
1,183
Points
108
Location
Cislunar Space
Website
www.orbiter-forum.com
Could oapiGetGbodyByName("Earth") be returning NULL for some reason, and the rest "defaults" to the Sun?
Good thought.

I tested that by defining three OBJHANDLE variables and getting the handles for Earth, Sun, and GetHandle() and setting a breakpoint right after. All three handles are different and valid.

FYI,
'oapiGetRelativePos(GetHandle(), oapiGetGbodyByName("Jupiter"), &rearth);' Points at Jupiter CORRECTLY with my code as written.
'oapiGetGlobalPos(oapiGetGbodyByName("Jupiter"), &rearth);' dosen't work at all!
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,609
Reaction score
2,330
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
There is something missing in the story. what is the position vector you get for Earth?
 

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
5,914
Reaction score
2,908
Points
188
Website
github.com
X: 45652447.987518311
Y: 12817189.951978654
Z: 399439813.61301041

That is before inverting, converting, anything.
The last number puts the vessel in the "lunar region", so that seems ok.
 

Max-Q

99 40
Addon Developer
Joined
Jul 5, 2021
Messages
765
Reaction score
1,183
Points
108
Location
Cislunar Space
Website
www.orbiter-forum.com
It works! (Simplified)

Here is the winning code!
C++:
void Rover::EarthTracking(double simdt)
{
    VECTOR3 rearth;
    VECTOR3 rearthloc;

    OBJHANDLE hEarth = oapiGetObjectByName("Earth");

    //Global position of Earth, Moon and spacecraft, spacecraft rotation matrix from local to global
    oapiGetGlobalPos(hEarth, &rearth);

    Global2Local(rearth, rearthloc);

    HGAAzimuth(simdt, rearthloc);
    HGAElevation(simdt, rearthloc);
}

I now have HGA that stays pointed at the earth, even while driving!
 
Top