C++ QuestionEarth-Tracking HGA

Max-Q

99 40
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
Donator
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
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
Donator
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.

Max-Q

99 40
Yes. I just tried inverting them after, and now the HGA points at... nothing at all.

GLS

Well-known member
Orbiter Contributor
Could oapiGetGbodyByName("Earth") be returning NULL for some reason, and the rest "defaults" to the Sun?

Urwumpe

Not funny anymore
Donator
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
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
Donator
There is something missing in the story. what is the position vector you get for Earth?

Max-Q

99 40
X: 45652447.987518311
Y: 12817189.951978654
Z: 399439813.61301041

That is before inverting, converting, anything.

n72.75

Tutorial Publisher
Donator

GLS

Well-known member
Orbiter Contributor
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
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!

Replies
6
Views
633
Replies
10
Views
830
Replies
8
Views
1K
Replies
3
Views
405
Replies
13
Views
597