UsingSimulink v4
UsingSimulink v4
UsingSimulink v4
A CO-SIMULATION GUIDE
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 2
_____________________________________________________________________________________
VGT ............................................................................................................................................................................................................................... 26
Create a Fixed Vector ............................................................................................................................................................................................ 26
1 - C ONNECTION A RCHITECTURE
To connect STK with the external world there are basically three ways:
1) Using external file – This is a completely off-line solution. Using a parser the outcomes from both
MATLAB and STK can be made available for data ingestion and evaluation from the other software;
2) Using Connect – This solution is available when the MATLAB connector is installed as STK plugin
and allows MATLAB to act as a client respect to STK, which is a server in this case;
3) Using COM – COM technology is available in Windows environment and allows third-parties
software to exchange data thorough COM objects. This is a flexible, object oriented approach that
also allows to send connect commands using the COM interface.
COM objects are used to connect STK with Simulink; this does not require to have the MATLAB Connector
installed. A set of code snippets is listed in Annex B.
Using the COM paradigm is possible to embed an STK scenario (and to get back numerical values from it at
each integration step) by using a special Simulink block that is called S-Function (see next Section for additional
information). Through the S-Function any of the data providers available in STK can be imported into the
Simulink plant and used as input data for custom models.
To explain how we can take benefit from introducing STK into a Simulink model an attitude control example
has been build.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 3
_____________________________________________________________________________________
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 4
_____________________________________________________________________________________
2 T HE S IMULINK P LANT
In the proposed Simulink model STK is embedded by using an S-Function that loads the proper scenario and
then, at each integration step:
1) Get from STK (using data providers) the current attitude state (quaternion, angular speeds and Euler
angles) of two satellites:
a. The first (called Reference) is directly managed by STK. It is has a Multi-Segment attitude
profile whose segments can be changed over time from the satellite’s attitude properties.
This allows the user to emulate attitude change commands sent to the satellite;
b. The second (called True) has instead a Real Time attitude propagator that receives data from
the Simulink model.
2) Receives the updated attitude of the True satellite and displays it in STK
1) Open Loop:
Using this option we force the True satellite to get the commanded attitude There is no feedback in this
configuration: the satellite remains in the commanded attitude state until we change something.
In the Open Loop block we can change any of the Euler angle independently from the others. Virtually any
attitude profile can be designed in Simulink and then inserted into STK for successive analysis and trade
studies.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 5
_____________________________________________________________________________________
2) Closed loop:
Using this option we can directly use custom control law and integrate attitude dynamics. The true attitude
state is compared with the reference one and, according with the implemented control law, a torque is
applied. Attitude dynamics and kinematics equations are then integrated to push back the new attitude in
STK.
In the following Figure is shown the Simulink plant that emulates the onboard attitude control system. The
most important blocks (Navigation, Guidance & Control, actuators and equations of motion) are here
presented in the most generic form: in such a way that the model itself can be properly modified for specific
purposes.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 6
_____________________________________________________________________________________
Here the user has just the ability to inject a white noise over the true quaternion values; by implementing a
proper measurement bias model the attitude stability performances can be investigated in detail.
In the Figure below are shown the True satellite YPR error angles and the measured angular speed for a nadir
pointing attitude with white noise.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 7
_____________________________________________________________________________________
2.3 – G UID ANCE & C ONTROL
The Guidance & Control block hosts the control feedbacks law. A control law receives the attitude error data
and outputs the commanded torques across the three body axes. In this example a simple Proportional
Derivative controller is used; it has been coded in a MATLAB script embedded to the Simulink model.
Here below is shown a screenshot from STK that reports the angular speed of the satellite after a
commanded Sun pointing state from an inertially fixed state. Note how the angular velocity along the Z body
axis as a stationary state of +1 deg/sec (as expected from the chosen attitude profile).
Fig 7 – Angular Rates achieved while following target angular speed (6 and 0 deg/sec)
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 8
_____________________________________________________________________________________
2.4 – A CTUATORS
The Actuators block hosts the wheel/reaction thruster model. It is configurable as well; in this example
there’s just a slope limiter and a saturation filter.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 9
_____________________________________________________________________________________
2.6.1 – A T T I T U D E D Y N A MI C S
The Attitude Dynamics block solves the Euler’s equation at each time step. Here the satellite inertia matrix is
defined and used.
The block outputs the angular speed respect to the body axes form the applied torque.
2.6.3 – K I N E M AT I CS
Finally, the Kinematics block returns the commanded Euler angles to STK.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 10
_____________________________________________________________________________________
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 11
_____________________________________________________________________________________
Additional reference profiles can be defined directly from the STK attitude property page by editing the Multi
Segment attitude profile elements.
Before running the simulation be sure that the scenario path contained into the startup callback is the correct
one, otherwise you’ll get an exception from the Simulink code:
Once the simulation is started from Simulink, the STK time will be slaved to the Simulink clock. By default
this is a discrete time step with 0.1 sec. as integration interval; this allows to capture dynamics that are in the
order of tents of second.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 12
_____________________________________________________________________________________
2.7.1 – C A SE S T U DI ES
POINTING ERROR AT NADIR
Run the Simulink model and press the YPR (VVLH) button with 0, 0, 0 as Yaw, Pitch and Roll value; this
means that we want a nadir pointing attitude, with the spacecraft’s X body axes in the direction of the
velocity.
If the initial attitude is different from the nadir pointing wait for the True satellite to reach the target attitude
and run the Angular Speed graph that is available as quick report. Note as, after the transient phase, the angular
speed values are very little and stationary over time.
At this point, go to the Navigation block and change the Noise Power value of the Band-Limited White
Noise block (I’ve put it equal to one). By doing this, we are adding attitude estimation uncertainties since we
may be interested in seeing which are the effects in terms of pointing errors.
The white noise insertion has the effect that the Guidance systems continuously outputs commanded torques
to correct errors, and this has an effect on both YPR angles and angular speeds. In particular the angular
errors lead to an error distance between the nominal and the effective point spotted an Earth.
Update the Angular Speed graph and run the Pointing Error one. The results should be like this:
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 13
_____________________________________________________________________________________
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 14
_____________________________________________________________________________________
3 – A TTITUDE D YNAMICS AND K INEMATICS
3.1 - E ULER E QU ATIONS
For a rigid body the following equation applies:
= ω (1)
, where H is the angular momentum, ω the angular velocity vector and I is the inertia matrix. Both H and ω
are expressed in the body frame. We also define the inertia matrix as
I is real, symmetric and coordinate dependent matrix (also called tensor). When the axis are aligned with the
principal axis of inertial it can be expressed as
0 0
=0 0
0 0
If we apply a torque T to a free-floating object it will change its spin rate. For a rigid body, a reference system
centered in the center of mass (CM) frame, and ω resolved in the body axis frame the following is valid:
= = + × (2)
From this equation we can get the following scalar relations in a body fixed, principal axis center of mass
frame:
Those equations are called Euler equations. No general solution exists, but there are particular solutions for
simple torques.
The simplest case is a free body, with no applied external torques. In this case the Euler equations can be
written as:
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 15
+ − = 0
_____________________________________________________________________________________
With no external torques the kinetic energy is constant and equals to:
1
= + +
2
3.2.1 – E U L ER A N G LE S
Any orientation can be achieved by composing three elemental rotations. The elemental rotations can either
occur about the axes of the fixed coordinate system (in this case they are also known as YPR angles) or about
the axes of a rotating coordinate system, which is initially aligned with the fixed one, and modifies its
orientation after each elemental rotation (intrinsic rotations). The latter case we are dealing with Euler angles.
The Euler angles are called as φ, θ, ψ and in this case we consider the 321 sequence, so we start rotating the
reference frame of an angle ψ around the Z axis, then we rotate the rotated frame of an angle θ around the Y’
axis and then we rotate of an angle φ around the X’’ axis, where XYZ, X’Y’Z’ and X’’Y’’Z’’ are three
consecutive frames obtained by rotating three times the reference frame.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 16
_____________________________________________________________________________________
The rotation matrix between a starting (reference) frame and final one is written as:
!" "
!
Where $ is the final frame after a 321 sequence and $ is the starting frame.
!# #
2.2.2 - Q U AT E R N I O N S
The Euler’s theorem says:
The orientation of a body is uniquely specified by a vector giving the direction of a body axis and a scalar specifying a rotation
angle about the axis.
So, to univocally specify a certain rotation about a reference frame we need to state 4 parameters. In
0 = 0" 4 + 0 5 + 0# 6 + 03 (6)
0" 4 + 0 5 + 0# 6 is the vector part of the quaternion and is purely imaginary (4 = 5 = 7 = −1), whereas
03 is the real (scalar part). The vector part defines a vector in the imaginary space that have components
8 , 8 , 8 .
k
uz
uy
i ux j
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 17
_____________________________________________________________________________________
0" = 8 ∗ ;4<= 2
0 = 8 ∗ ;4<= 2
0# = 8 ∗ ;4<= 2
03 = >?;= 2
(7)
The magnitude of a quaternion is defined as |0| = A0" + 0 + 0# + 03 . Unit quaternions (|0| = 1),
also known as versors, provide a convenient mathematical notation for representing orientations and
rotations of objects in three dimensions.
The identity quaternion (no rotation – initial orientation) is written as:
B0" 0 0# 03 C = B0 0 0 1C
Other important (normal) quaternions are B1 0 0 0C, B0 1 0 0C and B0 0 1 0C that represent Euler axis
along principal axis with no rotation.
The conjugate of the quaternion q is written as:
0 ∗ = −0" 4 − 0 5 − 0# 6 + 03
0∗
So it has an inverted vectorial part. The inverse of a quaternion is its conjugate normalized:
0 D" =
|0|
The conjugate and the inverse quaternion are the same for a unit quaternion.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 18
_____________________________________________________________________________________
3.3 - K INEMATICS
The kinematic equations relate the angular velocity along the body axes and the orientation of the satellite
respect to the inertial frame. They can be written as:
The rotational kinematics can also be expressed in terms of angular velocity vector and Euler angles:
−* sin( + /
$ = * cos( sin/ + ( cos/
* cos( cos/ − ( sin/
(9)
Conversely, the rate of change of Euler angles can be expressed in terms of the components of the angular
velocity vector as:
Note as, for this sequence, there is a singularity for θ= . In this case the * value is not defined.
J
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 19
_____________________________________________________________________________________
3.4.1 - S P A C EC R AF T A N G U L A R R A T E ONLY
If we want to control the spacecraft angular rate only, the following asymptotic condition shall be satisfied:
The control laws that accomplish these objectives can be written as:
3.4.2 - S P A C EC R AF T A N G U L A R R A T E AN D A T T I T U DE
In this case we want to control both the angular rates and the attitude (Euler angles or quaternions) over time
The conditions to be satisfied are the following:
, where the quaternion error is defined as (0U = current quaternion, 0LVW = reference quaternion)
0S = 0UD" 0LVW
We have proportional and derivative gains in this case to satisfy both the conditions.
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 20
_____________________________________________________________________________________
close all
clear all
clc
Once defined some variables, we can put them directly into the S-Function code by setting a vector called
stkParams and filling it with the variables we need. The set_param routine assures that the data are correctly
forwarded into the S-Function code.
function SFunctionNoSensors(block)
setup(block);
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 21
_____________________________________________________________________________________
function setup(block)
%% Register methods
block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Output);
block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
The PostPropagationSetup method initializes one DWork vector with the name Euler321:
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'Euler321';
block.Dwork(1).Dimensions = 3;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
The SetInpPortFrameData method configures the sample time of the input and output ports:
block.InputPort(idx).SamplingMode = fd;
block.OutputPort(1).SamplingMode = fd;
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 22
_____________________________________________________________________________________
block.OutputPort(2).SamplingMode = fd;
block.OutputPort(3).SamplingMode = fd;
block.OutputPort(4).SamplingMode = fd;
block.OutputPort(5).SamplingMode = fd;
block.OutputPort(6).SamplingMode = fd;
The Start method defines the initial state of the input vector:
function Start(block)
%define a null initial state
block.Dwork(1).Data = [0, 0, 0];
Finally, the Output method is used to get information from STK and to send them out of the S-Function
block:
function Output(block)
%get the root and the access objects from the array
root = stkParameters{2};
sat = stkParameters{3};
ref = stkParameters{4};
%update the feedback – get the current Euler angles from the input
block.Dwork(1).Data = block.InputPort(1).Data;
yaw = block.Dwork(1).Data(1);
pitch = block.Dwork(1).Data(2);
roll = block.Dwork(1).Data(3);
%get the Euler attitude of the body frames respect to the ICRF frame
satBodyAxes = sat.Vgt.Axes.Item('Body');
satAttitudeEuler = satBodyAxes.FindInAxes(root.CurrentTime,
sat.Vgt.Axes.Item('ICRF')).Orientation.QueryEulerAnglesArray('e321');
refBodyAxes = ref.Vgt.Axes.Item('Body');
refAttitudeEuler = refBodyAxes.FindInAxes(root.CurrentTime,
ref.Vgt.Axes.Item('ICRF')).Orientation.QueryEulerAnglesArray('e321');
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 23
_____________________________________________________________________________________
q4 = cell2mat(Results2.DataSets.GetDataSetByName('q4').GetValues);
wx = cell2mat(Results2.DataSets.GetDataSetByName('wx').GetValues);
wy = cell2mat(Results2.DataSets.GetDataSetByName('wy').GetValues);
wz = cell2mat(Results2.DataSets.GetDataSetByName('wz').GetValues);
%satAttitudeState = [q1(1,1), q2(1,1), q3(1,1), q4(1,1), wx(1,1), wy(1,1), wz(1,1)];
satQuaternion = [q1(1,1), q2(1,1), q3(1,1), q4(1,1)];
satAngularSpeed = [wx(1,1), wy(1,1), wz(1,1)];
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 24
_____________________________________________________________________________________
C REATE A N EW S CENARIO
%Check if a scenario exists, close and start a new scenario
if isempty(root.CurrentScenario)
root.NewScenario('ScenarioName')
else
root.CloseScenario;
root.NewScenario('ScenarioName')
end
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 25
_____________________________________________________________________________________
D ATA P ROVIDERS
G E T L AT /L O N FROM A S AT E LLI T E
% define a data provider and the elements to get from it
LLAState = satObj.DataProviders.Item('LLA State').Group.Item('Fixed');
Elems = {'Time';'Lat';'Lon'};
% define the time span for a variable data provider
satStartTime = satObj.Propagator.EphemerisInterval.FindStartTime;
satStopTime = satObj.Propagator.EphemerisInterval.FindStopTime;
% execute the query and get back the results
Results = LLAState.ExecElements(satStartTime, satStopTime, 10, Elems);
time = cell2mat(Results.DataSets.GetDataSetByName('Time').GetValues);
Lat = cell2mat(Results.DataSets.GetDataSetByName('Lat').GetValues);
Long = cell2mat(Results.DataSets.GetDataSetByName('Lon').GetValues);
G ET T HE A T T I T U D E Q U AT ER N I O N S
satObj=root.GetObjectFromPath('/Satellite/Satellite1);
% define a data provider and the elements to get from it
state = satObj.DataProviders.Item('Attitude Quaternions');
Elems = {'q1';'q2';'q3';'q4';'wx';'wy';'wz'};
% execute the query for the current time and get back the results
Results = state.ExecElements(root.CurrentTime, root.CurrentTime, 10, Elems);
q1 = cell2mat(Results.DataSets.GetDataSetByName('q1').GetValues);
q2 = cell2mat(Results.DataSets.GetDataSetByName('q2').GetValues);
q3 = cell2mat(Results.DataSets.GetDataSetByName('q3').GetValues);
q4 = cell2mat(Results.DataSets.GetDataSetByName('q4').GetValues);
wx = cell2mat(Results.DataSets.GetDataSetByName('wx').GetValues);
wy = cell2mat(Results.DataSets.GetDataSetByName('wy').GetValues);
wz = cell2mat(Results.DataSets.GetDataSetByName('wz').GetValues);
E X T R A CT I N G E L E M EN T S WITH P R E -D AT A
%get a sensor object from the root
objSensor1=root.GetObjectFromPath('/Satellite/Satellite1/Sensor/Sensor1');
%define the data provider to report about Sun vector
sensorVect = objSensor1.DataProviders.Item('Vector Choose Axes');
dataProvSun = sensorVect.Group.Item('Sun');
%Choose the referense system you want to report the Sun data
dataProvSun.PreData = '/Satellite/Satellite1/Sensor/Sensor1 Body';
rptElems = {'Time';'RightAscension';'Declination'};
A CCESS
G E T AER D AT A
%get the objects for access calculation
objSun = root.GetObjectFromPath('/Planet/Sun');
objSensor1=root.GetObjectFromPath('/Satellite/Satellite1/Sensor/Sensor1');
%calculate the access
sunAccess = objSensor1.GetAccessToObject(objSun);
%get the access data
AER = sunAccess.DataProviders.Item('AER Data').Group.Item('BodyFixed').Exec(currentTime,currentTime,60);
azimuth = cell2mat(AER.DataSets.Item(2).GetValues);
elevation = cell2mat(AER.DataSets.Item(3).GetValues);
%%%%%%%% NOTE THAT IF THE ACCESS IS FROM A MOVING OBJECT BY DEFAULT THE Az/El DATA ARE RELEVANT TO THE OBJECT
VELOCITY VECTOR- BODY FIXED HAS TO BE SPECIFIED %%%%%%%%%%%%%%%%%%%%%%%%%%%
VGT
C R E AT E A FIXED VECTOR
% get the satellite object
objSat=root.GetObjectFromPath('/Satellite/Probe1');
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 26
_____________________________________________________________________________________
%define a new vector (type = Fixed in Axes)
VectFactory = vgtSat.Vectors.Factory;
fixedAxesVector = VectFactory.Create('VectorTest','','eCrdnVectorTypeFixedInAxes');
fixedAxesVector.ReferenceAxes.SetAxes(provider);
fixedAxesVector.Direction.AssignXYZ(0,0,1);
______________________________________________________________________________________________________________________________
Using Simulink and STK for Attitude Modeling– A co-simulation guide 27