LVLH direction vector

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
I'm stuck, I'm working on an autopilot for my Prometheus Launcher once again and I've actually managed to get it to work reasonably well thanks to an updated understanding of PID controllers. I'm looking to improve on it and I need to get the LVLH direction vector of the vessel as well as being able to calculate an arbitrary LVLH vector (i.e. I say 70 degrees pitch, 90 degree heading, 180 degree roll and get two vectors; pitch and heading would get me one). The best I've been able to figure out so far is to use some kind of rotation matrix along with HorizonRot. Heading seems easy, just the rotation about the Y-axis in local horizon coordinates, but pitch is getting me since it's a composite of the X and Z axes. What's the easiest way of getting these vectors? I'm at a bit of a loss here, I feel like the solution is right in front of me, but I'm not sure how to connect the dots.

Thanks for any help!
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,627
Reaction score
2,345
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Actually, you would just need a matrix that is defined by two rotating vectors and a third non-rotating one:

The local vertical vector (z) pointing towards Earth
The local horizon vector(x) pointing forward 90° from the vertical
The y-vector, which is perpendicular to both vertical and horizon.

http://www.dtic.mil/get-tr-doc/pdf?AD=ADA247550
 

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
666
Reaction score
20
Points
33
BTW, one advanced question:
How do real spacecraft’s rotate using their RCS?

Is it possible to reach any orientation with two (or n) bursts of thrust of all required RCS engines?

Or is a copiously thrust management required?

Regarding Zatnikitelman question:
I'm working on a solution for a similar problem.

My possible solution is the following but I need to test if it works:

1. Create the rotation matrix of the vessel. Rotation (pitch, yaw and bank) is relative to the local frame (e.g. Moon).

2. Rotate the Matrix around the pitch axis by 90°, then you can get the Pitch angle of the hover engine

3. Rotate the Matrix around the pitch axis by current pitch angle.
Then you can use x or z axis to get the heading of the hover engine (if y is upside)

You can have a look into this post, there you find some code examples how to create and rotate the rotation matrix and how to read out the new pitch. I can give you more code if I have a better solution.

I hope this helps you a bit...
 
Last edited:

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,627
Reaction score
2,345
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Is it possible to reach any orientation with two (or n) bursts of thrust of all reqired RCS engines?

In reality? No. You don't know your mass and C.G. precisely for example. Propellant sloshes. You always have to correct your model parameters with the feedback from reality.

You have multiple different strategies to get into the right orientation. The Shuttle can do multi-axis maneuvers, many simpler spacecraft can only perform single axis slew maneuvers.
 

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Ok, so I'm working on this a bit more. I think I have a solution for getting the current ship's orientation, I can normalize the vector given by GetHorizonAirspeedVector, and to get current rotations, derive a matrix from it and GetShipAirspeedVector. But I'm still lost on how to say something like 70 degrees pitch 90 degrees heading and 180 degrees roll and derive a "target" rotation to feed into the autopilot.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,627
Reaction score
2,345
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Ok, so I'm working on this a bit more. I think I have a solution for getting the current ship's orientation, I can normalize the vector given by GetHorizonAirspeedVector, and to get current rotations, derive a matrix from it and GetShipAirspeedVector. But I'm still lost on how to say something like 70 degrees pitch 90 degrees heading and 180 degrees roll and derive a "target" rotation to feed into the autopilot.

Well by defining a rotation matrix for these axes, depending on your axis order. The Shuttle for example uses PYR, but RPY is also common. With such a rotation matrix, you can calculate in both directions, either from angles to an orientation frame, or from an orientation frame (at least two vectors needed) to the angles, by using the inverse calculation.
 
Last edited:

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
666
Reaction score
20
Points
33
I think I found a solution for my similar problem.
By this way you can get the rotation matrix of your local horizon (In my test relativ to the moon):

Code:
    OBJHANDLE hVessel = oapiGetFocusInterface()->GetHandle();
    OBJHANDLE hMoon = oapiGetObjectByName("Moon");
    MATRIX3 m3Vessel = getRotationMatrixFromHandle(hVessel);
    MATRIX3 m3Moon  = getRotationMatrixFromHandle(hMoon);
    
    MATRIX3 m3rel = mul(invertMatrix(m3Vessel), m3Moon);

    double ln,lt,r;
    oapiGetEquPos(hVessel,&ln,&lt,&r);    
    
    VECTOR3 ve;
    ve.x = 0;
    ve.y = 1;
    ve.z = 0;
    m3rel = rotateMatrix(ve, m3rel, -ln); //Rotate m around ve

    ve.x = 0;
    ve.y = 0;
    ve.z = 1;
    m3rel = rotateMatrix(ve, m3rel, lt); //Rotate m around ve

    matrixOut(m3rel,4);  // m3rel is now the rotation matrix of the vessel to the local horizon

Maybe it helps you also...
 

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Ok so I've almost got a solution. I found I could get pitch and heading by rotating a z-unit vector (0,0,1) around first the x, then the y axis of the local horizon. To get roll I rotate a y-unit vector (0,1,0) with the same matrix (though performance reasons I'll probably just rotate it about an x-axis matrix later). However, it's not working quite right. For now I'm keeping things very simple using hard coded values for pitch and heading. I'm also not steering the vehicle, just using a dummy thruster to show where the vector should be pointing. However it only seems to be pointing about 20 degrees up, no matter what values I use, I can't increase the pitch. Does anyone see the issue here?
Code:
double x = 70; //pitch
double y = 90; //heading
double z = attitude.z;

MATRIX3 rotmatrix = _M(cos(y), 0, sin(y),
		(sin(x)*sin(y)), cos(x), (-cos(y)*sin(x)),
		(-cos(x)*sin(y)), sin(x), (cos(x)*cos(y)));

VECTOR3 tgtDirVector = mul(rotmatrix, _V(0, 0, 1));
VECTOR3 tgtRotVector = mul(rotmatrix, _V(0, 1, 0));

VECTOR3 locTgtDirVector;
VECTOR3 locTgtRotVector;
HorizonInvRot(tgtDirVector, locTgtDirVector);
HorizonInvRot(tgtRotVector, locTgtRotVector);

SetThrusterDir(dummyth, locTgtDirVector);
SetThrusterLevel(dummyth, 1.0);
sprintf(oapiDebugString(), "%.2lf, %.2lf, %.2lf %.2lf, %.2lf, %.2lf", tgtDirVector.x, tgtDirVector.y, tgtDirVector.z, locTgtDirVector.x, locTgtDirVector.y, locTgtDirVector.z);

[EDIT] Part of why it wasn't working is I'm stupid. I was using HorizonInvRot instead of just HorizonRot. But it's still not giving me entirely correct data. When my ship is flying at 90 degrees heading at 70 degrees of pitch, the dummyth exhaust should be aligned with my vessel and it isn't, it's still at a bit of an angle.
 
Last edited:

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Ok, I am so, so close. I was doing the rotation matrices wrong, basically I needed two matrices, one about the horizon x-axis, then one about the y-axis rather than multiplying the two matrices together which had the effect of rotating the second axis about the first. Anyways, now I have two vectors which describe my vessel's direction vector and roll vector (Orbiter calls them rot vectors for docking ports and attachment ports, basically "up"). But now the last step still eludes me, how do I find the angles between my ship axes and the vector? The vector is already normalized, all I need are angles for pitch, roll, and yaw, basically the reverse of what I'm doing in the beginning, but vessel-local.

Here's the code so-far, you can see at the bottom where I've been trying different ways to get the angles I need, but none are quite right. (yes, I realize localDirMag is redundant since the vector is normalized).
Code:
			VECTOR3 attitude = _V(0, 0, 0);
			double x = 70*RAD; //pitch
			double y = 90*RAD; //heading
			double z = attitude.z;

			MATRIX3 rotxmatrix = _M(1, 0, 0,
				0, cos(x), sin(x),
				0, -sin(x), cos(x));

			MATRIX3 rotymatrix = _M(cos(y), 0, sin(y),
				0, 1, 0,
				-sin(y), 0, cos(y));

			VECTOR3 tgtDirVector = mul(rotxmatrix, _V(0, 0, 1));
			tgtDirVector = mul(rotymatrix, _V(tgtDirVector.x, tgtDirVector.y, tgtDirVector.z));

			VECTOR3 tgtRotVector = mul(rotxmatrix, _V(0, 1, 0));
			tgtRotVector = mul(rotymatrix, _V(tgtRotVector.x, tgtRotVector.y, tgtRotVector.z));

			VECTOR3 locTgtDirVector;
			VECTOR3 locTgtRotVector;
			HorizonInvRot(tgtDirVector, locTgtDirVector);
			HorizonInvRot(tgtRotVector, locTgtRotVector);

			double locDirMag = length(locTgtDirVector);
			//double pitchang = acos(locTgtDirVector.z / locDirMag) * DEG;
			double pitchang = atan2(locTgtDirVector.y, locTgtDirVector.z) * DEG;
			//double yawang = asin(locTgtDirVector.x / locDirMag) * DEG;
			//double rollang = acos(locTgtRotVector.y / locDirMag) * DEG;
 
Top