Source code for scenarioAttitudeConstrainedManeuver

#
#  ISC License
#
#  Copyright (c) 2021, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
#
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#
#  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

r"""
Overview
--------

This script sets up a 6-DOF spacecraft which is orbiting the Earth, in the presence of the Sun.
The spacecraft is modelled according to the specifics of the Bevo-2 satellite, that has a sensitive
star tracker aligned with the x body axis and two sun sensors aligned with the y and z body axes.
In contrast with :ref:`scenarioAttitudeConstraintViolation` the goal of this scenario is to illustrate
how to set up a Basilisk simulation using the :ref:`constrainedAttitudeManeuver` module to perform a
slew maneuver while ensuring constraint compliance.

The script is found in the folder ``basilisk/examples`` and executed by using::

      python3 scenarioAttitudeConstrainedManeuver.py

This simulation is set up identically to :ref:`scenarioAttitudeConstraintViolation`. The reader is referred
to this scenario for a detailed description of the setup. The only difference in this scenario is that the
constraint-naive :ref:`inertial3D` module for attitude pointing is replaced with the :ref:`constrainedAttitudeManeuver`
module.

Illustration of Simulation Results
----------------------------------

Each run of the script produces 6 figures. Figures 1-4 report, respectively, attitude error, RW motor torque, rate
tracking error, and RW speed. These plots are only relevant to the spacecraft / RW dynamics. Figures 5 and 6
show the angle between the boresight vector of the star tracker and the Sun (fig. 5), and of the sun sensor(s) and
the Sun (fig. 6). Each plot features a dashed line that represents an angular threshold for that specific instrument.

Each plot describes a slew maneuver performed from an initial inertial attitude :math:`\sigma_{\mathcal{B/N},i}` to
a final inertial attitude :math:`\sigma_{\mathcal{B/N},f}`. In :ref:`scenarioAttitudeConstraintViolation`, these
sets of attitudes and constraints were chosen to highlight specific constraint violations. This scenario shows how,
using :ref:`constrainedAttitudeManeuver`, the constraints are not violated.

::

    show_plots = True, use2SunSensors = False, starTrackerFov = 20, sunSensorFov = 70, attitudeSetCase = 0

This case features the violation of the keep in constraint of the sun sensor only when :ref:`inertial3D` is used.
Just for this case, only the sun sensor along the y body axis is considered. Now, the keep in constraint is not violated
as the boresight angle never exceeds the 70 def field of view of the instrument.

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuver5020700.svg
   :align: center

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuver6020700.svg
   :align: center

::

    show_plots = True, use2SunSensors = True, starTrackerFov = 20, sunSensorFov = 70, attitudeSetCase = 1

In this case, using :ref:`inertial3D`, both the sun sensor boresights exceed the respective thresholds.
In this scenario, however, they do not.

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuver5120701.svg
   :align: center

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuver6120701.svg
   :align: center

::

    show_plots = True, use2SunSensors = True, starTrackerFov = 20, sunSensorFov = 70, attitudeSetCase = 2

In this case, :ref:`inertial3D` violates the keep out constraint of the star tracker, alongside with the keep in
constraints for both the sun sensors. The following simulation shows how all the constraints are respected.

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuver5120702.svg
   :align: center

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuver6120702.svg
   :align: center

Comparing Distance-Based Search Modes
-------------------------------------

An optional comparison uses attitude case 4, the alternate start from issue #1726,
with one Sun sensor, a 20-degree star-tracker keep-out cone, and a 70-degree
Sun-sensor keep-in cone::

    figures, comparison = run(True, False, 20, 70, 4, compareSearchModes=True)

This case starts at :math:`\boldsymbol\sigma_{B/N} = [-3/7, -2/7, 0]` and uses
the same target as case 0. Both searches use ``costFcnType = 0`` on an
identical grid with refinement ``N = 8``. The default distance heuristic runs
first, followed by ``useShortestPath = True``, which uses :math:`h = \theta/4`.
The spacecraft and celestial-body states do not advance between the two plans.
The subsequent dynamics simulation and figures 1--6 use the shortest-path reference.
Those six figure names have a ``ShortestPath`` suffix, so saving both modes for
the same attitude case preserves their separate results.
Without ``compareSearchModes=True``, the scenario retains its existing effort-based
planning and returns only the figure dictionary.

The comparison adds an MRP waypoint overlay and a table of accumulated MRP edge
distance and expanded nodes. The table evaluates the planner's shadow-aware edge
metric, rather than the length of the plotted polyline or ``pathCost`` (fitted
control effort). For this case the grid distance decreases from approximately
1.19731 to 1.13077, a 5.6 percent reduction, with more search work.

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuverSearchPaths020704.svg
   :align: center

.. image:: /_images/Scenarios/scenarioAttitudeConstrainedManeuverSearchComparison020704.svg
   :align: center

The overlay maps waypoints into the unit MRP sphere. Lines across shadow switches
are omitted, and square markers identify their endpoints; these coordinate jumps
are not physical rotations through the sphere. The paths connect discrete grid
waypoints and do not depict the fitted reference. Shortest-path optimality applies
to accumulated grid edge distance, not control effort or continuous cone clearance.
Use the existing boresight-angle plots to assess the simulated maneuver.

``comparison`` contains ``default`` and ``shortest`` entries, each with the original
``waypoints``, ``edgeDistance`` (dimensionless), and ``expandedNodeCount``. The
native module tests validate shortest-path costs against an independent Dijkstra
search; the example illustrates the resulting path and search-work differences.

"""

import os

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import numpy as np
from Basilisk import __path__
from Basilisk.architecture import messaging
from Basilisk.fswAlgorithms import (mrpFeedback, attTrackingError, constrainedAttitudeManeuver, rwMotorTorque)
from Basilisk.simulation import (reactionWheelStateEffector, simpleNav, spacecraft, boreAngCalc)
from Basilisk.utilities import (
    SimulationBaseClass,
    macros,
    orbitalMotion,
    simIncludeGravBody,
    simIncludeRW,
    vizSupport,
)
from Basilisk.utilities import simHelpers

bskPath = __path__[0]
fileName = os.path.basename(os.path.splitext(__file__)[0])



[docs] def run(show_plots, use2SunSensors, starTrackerFov, sunSensorFov, attitudeSetCase, *, compareSearchModes=False): """Simulate a constrained maneuver, optionally comparing distance-search modes. :param show_plots: Whether to display the figures interactively. :param use2SunSensors: Whether to include the second Sun sensor along body z. :param starTrackerFov: Star-tracker keep-out half-angle in degrees. :param sunSensorFov: Sun-sensor keep-in half-angle in degrees. :param attitudeSetCase: Attitude pair index; case 4 is the alternate-start comparison. :param compareSearchModes: Compare both distance searches, then simulate the shortest path. :return: Figure dictionary, or ``(figures, comparison)`` when comparing search modes. """ # Create simulation variable names simTaskName = "simTask" simProcessName = "simProcess" # Create a sim module as an empty container scSim = SimulationBaseClass.SimBaseClass() scSim.SetProgressBar(False) # create the simulation process dynProcess = scSim.CreateNewProcess(simProcessName) # create the dynamics task and specify the simulation time and integration update time simulationTime = macros.min2nano(3.5) simulationTimeStep = macros.sec2nano(0.01) dynProcess.addTask(scSim.CreateNewTask(simTaskName, simulationTimeStep)) # # setup the simulation tasks/objects # # initialize spacecraft object and set properties scObject = spacecraft.Spacecraft() scObject.ModelTag = "Bevo2-Sat" # add spacecraft object to the simulation process scSim.AddModelToTask(simTaskName, scObject, 1) # setup Gravity Body gravFactory = simIncludeGravBody.gravBodyFactory() # Next a series of gravitational bodies are included gravBodies = gravFactory.createBodies('earth', 'sun') planet = gravBodies['earth'] planet.isCentralBody = True mu = planet.mu # The configured gravitational bodies are added to the spacecraft dynamics with the usual command: gravFactory.addBodiesTo(scObject) # Next, the default SPICE support module is created and configured. timeInitString = "2021 JANUARY 15 00:28:30.0" # The following is a support macro that creates a `spiceObject` instance spiceObject = gravFactory.createSpiceInterface(time=timeInitString, epochInMsg=True) # Earth is gravity center spiceObject.zeroBase = 'Earth' # The SPICE object is added to the simulation task list. scSim.AddModelToTask(simTaskName, gravFactory.spiceObject, 2) # The gravitational body is connected to the spacecraft object gravFactory.addBodiesTo(scObject) # setup the orbit using classical orbit elements oe = orbitalMotion.ClassicElements() oe.a = 7000. * 1000 # meters oe.e = 0.0001 oe.i = 33.3 * macros.D2R oe.Omega = 148.2 * macros.D2R oe.omega = 347.8 * macros.D2R oe.f = 135 * macros.D2R rN, vN = orbitalMotion.elem2rv(mu, oe) # sets of initial attitudes that yield the desired constraint violations (attitudeSetCase) sigma_BN_start = [ # MRPs [-] [0.522, -0.065, 0.539], # to violate one keepIn only [0.314, -0.251, 0.228], # to violate two keepIn and not keepOut [-0.378, 0.119, -0.176], # to violate keepOut and both keepIn [-0.412, 0.044, -0.264], # to violate keepOut only [-3.0 / 7.0, -2.0 / 7.0, 0.0], # Alternate start for issue #1726 ] # To set the spacecraft initial conditions, the following initial position and velocity variables are set: scObject.hub.r_CN_NInit = rN # m - r_BN_N scObject.hub.v_CN_NInit = vN # m/s - v_BN_N scObject.hub.sigma_BNInit = sigma_BN_start[attitudeSetCase] # MRP set to customize initial inertial attitude scObject.hub.omega_BN_BInit = [[0.], [0.], [0.]] # rad/s - omega_CN_B # define the simulation inertia I = [0.02 / 3, 0., 0., 0., 0.1256 / 3, 0., 0., 0., 0.1256 / 3] scObject.hub.mHub = 4.0 # kg - spacecraft mass scObject.hub.r_BcB_B = [[0.0], [0.0], [0.0]] # m - position vector of body-fixed point B relative to CM scObject.hub.IHubPntBc_B = simHelpers.np2EigenMatrix3d(I) # # add RW devices # # Make RW factory instance rwFactory = simIncludeRW.rwFactory() # store the RW dynamical model type varRWModel = messaging.BalancedWheels # create each RW by specifying the RW type, the spin axis gsHat, plus optional arguments maxMomentum = 0.01 maxSpeed = 6000. # [RPM] rwFactory.create() expects Omega_max in RPM and converts internally RW1 = rwFactory.create('custom', [1, 0, 0], Omega=0. # RPM , Omega_max=maxSpeed , maxMomentum=maxMomentum , u_max=0.001 , RWModel=varRWModel) RW2 = rwFactory.create('custom', [0, 1, 0], Omega=0. # RPM , Omega_max=maxSpeed , maxMomentum=maxMomentum , u_max=0.001 , RWModel=varRWModel) RW3 = rwFactory.create('custom', [0, 0, 1], Omega=0. # RPM , Omega_max=maxSpeed , maxMomentum=maxMomentum , u_max=0.001 , RWModel=varRWModel) numRW = rwFactory.getNumOfDevices() # create RW object container and tie to spacecraft object rwStateEffector = reactionWheelStateEffector.ReactionWheelStateEffector() rwStateEffector.ModelTag = "RW_cluster" rwFactory.addToSpacecraft(scObject.ModelTag, rwStateEffector, scObject) # add RW object array to the simulation process scSim.AddModelToTask(simTaskName, rwStateEffector, 2) # add the simple Navigation sensor module sNavObject = simpleNav.SimpleNav() sNavObject.ModelTag = "SimpleNavigation" scSim.AddModelToTask(simTaskName, sNavObject) # # setup the FSW algorithm tasks # # sets of initial attitudes that yield the desired constraint violations (attitudeSetCase) sigma_BN_target = [ # MRPs [-] [0.342, 0.223, -0.432], # to violate one keepIn only [0.326, -0.206, -0.823], # to violate two keepIn and not keepOut [0.350, 0.220, -0.440], # to violate keepOut and both keepIn [0.350, 0.220, -0.440], # to violate keepOut only [0.342, 0.223, -0.432], # Alternate start uses the case 0 target ] # setup readManeuver guidance module CAM = constrainedAttitudeManeuver.ConstrainedAttitudeManeuver(8) CAM.ModelTag = "constrainedAttitudeManeuvering" CAM.sigma_BN_goal = sigma_BN_target[attitudeSetCase] CAM.omega_BN_B_goal = [0, 0, 0] CAM.avgOmega = 0.04 CAM.BSplineType = 0 CAM.costFcnType = 0 if compareSearchModes else 1 CAM.appendKeepOutDirection([1,0,0], starTrackerFov*macros.D2R) CAM.appendKeepInDirection([0,1,0], sunSensorFov*macros.D2R) scSim.AddModelToTask(simTaskName, CAM) # setup the attitude tracking error evaluation module attError = attTrackingError.attTrackingError() attError.ModelTag = "attErrorInertial3D" scSim.AddModelToTask(simTaskName, attError) # setup the MRP Feedback control module mrpControl = mrpFeedback.mrpFeedback() mrpControl.ModelTag = "mrpFeedback" scSim.AddModelToTask(simTaskName, mrpControl) decayTime = 10.0 xi = 1.0 mrpControl.Ki = -1 # make value negative to turn off integral feedback mrpControl.P = 3*np.max(I)/decayTime mrpControl.K = (mrpControl.P/xi)*(mrpControl.P/xi)/np.max(I) mrpControl.integralLimit = 2. / mrpControl.Ki * 0.1 # add module that maps the Lr control torque into the RW motor torques rwMotorTorqueObj = rwMotorTorque.rwMotorTorque() rwMotorTorqueObj.ModelTag = "rwMotorTorque" scSim.AddModelToTask(simTaskName, rwMotorTorqueObj) # Make the RW control all three body axes controlAxes_B = [1, 0, 0, 0, 1, 0, 0, 0, 1] rwMotorTorqueObj.controlAxes_B = controlAxes_B # Boresight vector modules. stBACObject = boreAngCalc.BoreAngCalc() stBACObject.ModelTag = "starTrackerBoresight" stBACObject.boreVec_B = [1., 0., 0.] # boresight in body frame scSim.AddModelToTask(simTaskName, stBACObject) ssyBACObject = boreAngCalc.BoreAngCalc() ssyBACObject.ModelTag = "SunSensorBoresight" ssyBACObject.boreVec_B = [0., 1., 0.] # boresight in body frame scSim.AddModelToTask(simTaskName, ssyBACObject) if use2SunSensors: CAM.appendKeepInDirection([0,0,1], sunSensorFov*macros.D2R) sszBACObject = boreAngCalc.BoreAngCalc() sszBACObject.ModelTag = "SunSensorBoresight" sszBACObject.boreVec_B = [0., 0., 1.] # boresight in body frame scSim.AddModelToTask(simTaskName, sszBACObject) # # Setup data logging before the simulation is initialized # numDataPoints = 500 samplingTime = simHelpers.samplingTime(simulationTime, simulationTimeStep, numDataPoints) sNavRec = sNavObject.attOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, sNavRec) CAMRec = CAM.attRefOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, CAMRec) dataRec = scObject.scStateOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, dataRec) rwMotorLog = rwMotorTorqueObj.rwMotorTorqueOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, rwMotorLog) attErrorLog = attError.attGuidOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, attErrorLog) stBACOLog = stBACObject.angOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, stBACOLog) ssyBACOLog = ssyBACObject.angOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, ssyBACOLog) if use2SunSensors: sszBACOLog = sszBACObject.angOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, sszBACOLog) # To log the RW information, the following code is used: mrpLog = rwStateEffector.rwSpeedOutMsg.recorder(samplingTime) scSim.AddModelToTask(simTaskName, mrpLog) # A message is created that stores an array of the Omega wheel speeds rwLogs = [] for item in range(numRW): rwLogs.append(rwStateEffector.rwOutMsgs[item].recorder(samplingTime)) scSim.AddModelToTask(simTaskName, rwLogs[item]) # # create simulation messages # # create the FSW vehicle configuration message # use the same inertia in the FSW algorithm as in the simulation vehicleConfigOut = messaging.VehicleConfigMsgPayload(ISCPntB_B=I) vcMsg = messaging.VehicleConfigMsg().write(vehicleConfigOut) # Setup the FSW RW configuration message. fswRwParamMsg = rwFactory.getConfigMessage() # link messages attError.attNavInMsg.subscribeTo(sNavObject.attOutMsg) sNavObject.scStateInMsg.subscribeTo(scObject.scStateOutMsg) CAM.scStateInMsg.subscribeTo(scObject.scStateOutMsg) CAM.vehicleConfigInMsg.subscribeTo(vcMsg) CAM.keepOutCelBodyInMsg.subscribeTo(spiceObject.planetStateOutMsgs[1]) CAM.keepInCelBodyInMsg.subscribeTo(spiceObject.planetStateOutMsgs[1]) attError.attNavInMsg.subscribeTo(sNavObject.attOutMsg) attError.attRefInMsg.subscribeTo(CAM.attRefOutMsg) mrpControl.guidInMsg.subscribeTo(attError.attGuidOutMsg) mrpControl.vehConfigInMsg.subscribeTo(vcMsg) mrpControl.rwParamsInMsg.subscribeTo(fswRwParamMsg) mrpControl.rwSpeedsInMsg.subscribeTo(rwStateEffector.rwSpeedOutMsg) rwMotorTorqueObj.rwParamsInMsg.subscribeTo(fswRwParamMsg) rwMotorTorqueObj.vehControlInMsg.subscribeTo(mrpControl.cmdTorqueOutMsg) rwStateEffector.rwMotorCmdInMsg.subscribeTo(rwMotorTorqueObj.rwMotorTorqueOutMsg) # Boresight modules stBACObject.scStateInMsg.subscribeTo(scObject.scStateOutMsg) stBACObject.celBodyInMsg.subscribeTo(spiceObject.planetStateOutMsgs[1]) ssyBACObject.scStateInMsg.subscribeTo(scObject.scStateOutMsg) ssyBACObject.celBodyInMsg.subscribeTo(spiceObject.planetStateOutMsgs[1]) if use2SunSensors: sszBACObject.scStateInMsg.subscribeTo(scObject.scStateOutMsg) sszBACObject.celBodyInMsg.subscribeTo(spiceObject.planetStateOutMsgs[1]) # Vizard Visualization Option # --------------------------- if vizSupport.vizFound: viz = vizSupport.enableUnityVisualization(scSim, simTaskName, scObject, # saveFile=__file__ ) vizSupport.createConeInOut(viz, toBodyName='sun_planet_data', coneColor = 'r', normalVector_B=[1, 0, 0], incidenceAngle=starTrackerFov*macros.D2R, isKeepIn=False, coneHeight=10.0, coneName='sunKeepOut') vizSupport.createConeInOut(viz, toBodyName='sun_planet_data', coneColor = 'g', normalVector_B=[0, 1, 0], incidenceAngle=sunSensorFov*macros.D2R, isKeepIn=True, coneHeight=10.0, coneName='sunKeepIn') if use2SunSensors: vizSupport.createConeInOut(viz, toBodyName='sun_planet_data', coneColor = 'b', normalVector_B=[0, 0, 1], incidenceAngle=sunSensorFov*macros.D2R, isKeepIn=True, coneHeight=10.0, coneName='sunKeepIn') # initialize Simulation: This function runs the self_init() # cross_init() and reset() routines on each module. scSim.InitializeSimulation() if compareSearchModes: comparison = {"default": _search_result(CAM)} CAM.useShortestPath = True CAM.Reset(0) # [ns] Replan before advancing any simulation state. comparison["shortest"] = _search_result(CAM) # configure a simulation stop time and execute the simulation run scSim.ConfigureStopTime(simulationTime) scSim.ExecuteSimulation() # retrieve the logged data # RW dataUsReq = rwMotorLog.motorTorque dataSigmaBR = attErrorLog.sigma_BR dataOmegaBR = attErrorLog.omega_BR_B dataOmegaRW = mrpLog.wheelSpeeds dataSTMissAngle = stBACOLog.missAngle dataSSyMissAngle = ssyBACOLog.missAngle if use2SunSensors: dataSSzMissAngle = sszBACOLog.missAngle dataRW = [] for i in range(numRW): dataRW.append(rwLogs[i].u_current) np.set_printoptions(precision=16) # Displays the plots relative to the S/C attitude, maneuver, RW speeds and torques and boresight angles timeData = rwMotorLog.times() * macros.NANO2MIN plot_attitude_error(timeData, dataSigmaBR) figureList = {} pltName = fileName + "1" + str(int(use2SunSensors)) + str(starTrackerFov) + str(sunSensorFov) + str(attitudeSetCase) figureList[pltName] = plt.figure(1) plot_rw_motor_torque(timeData, dataUsReq, dataRW, numRW) pltName = fileName + "2" + str(int(use2SunSensors)) + str(starTrackerFov) + str(sunSensorFov) + str(attitudeSetCase) figureList[pltName] = plt.figure(2) plot_rate_error(timeData, dataOmegaBR) pltName = fileName + "3" + str(int(use2SunSensors)) + str(starTrackerFov) + str(sunSensorFov) + str(attitudeSetCase) figureList[pltName] = plt.figure(3) plot_rw_speeds(timeData, dataOmegaRW, numRW) pltName = fileName + "4" + str(int(use2SunSensors)) + str(starTrackerFov) + str(sunSensorFov) + str(attitudeSetCase) figureList[pltName] = plt.figure(4) plot_st_miss_angle(timeData, dataSTMissAngle, starTrackerFov) pltName = fileName + "5" + str(int(use2SunSensors)) + str(starTrackerFov) + str(sunSensorFov) + str(attitudeSetCase) figureList[pltName] = plt.figure(5) dataSS = [dataSSyMissAngle] if use2SunSensors: dataSS.append(dataSSzMissAngle) plot_ss_miss_angle(timeData, dataSS, sunSensorFov) pltName = fileName + "6" + str(int(use2SunSensors)) + str(starTrackerFov) + str(sunSensorFov) + str(attitudeSetCase) figureList[pltName] = plt.figure(6) if compareSearchModes: figureList = {name + "ShortestPath": figure for name, figure in figureList.items()} suffix = f"{int(use2SunSensors)}{starTrackerFov}{sunSensorFov}{attitudeSetCase}" figureList[fileName + "SearchPaths" + suffix] = plot_search_paths(comparison) figureList[fileName + "SearchComparison" + suffix] = plot_search_comparison(comparison, CAM.N) if show_plots: plt.show() # close the plots being saved off to avoid over-writing old and new figures plt.close("all") if compareSearchModes: return figureList, comparison return figureList
def _edge_distance(first, second): """Evaluate the planner's dimensionless MRP edge metric for two waypoints.""" first_node = constrainedAttitudeManeuver.Node() second_node = constrainedAttitudeManeuver.Node() first_node.sigma_BN = first second_node.sigma_BN = second return constrainedAttitudeManeuver.distance(first_node, second_node) def _search_result(model): """Copy the path and search metrics before a subsequent reset invalidates them.""" waypoints = np.array([[model.returnPathCoord(i, j) for j in range(3)] for i in range(model.path.N)]) edge_distance = sum(_edge_distance(first, second) for first, second in zip(waypoints[:-1], waypoints[1:])) return {"waypoints": waypoints, "edgeDistance": edge_distance, "expandedNodeCount": model.expandedNodeCount} def _principal_path_segments(waypoints): """Return unit-sphere waypoints and line segments separated at shadow switches.""" principal = np.array(waypoints, dtype=float, copy=True) norm_squared = np.sum(principal**2, axis=1) outside = norm_squared > 1.0 principal[outside] *= -1.0 / norm_squared[outside, None] segments = [] switches = [] start = 0 tolerance = 1e-12 # [-] Allow roundoff when comparing MRP distances. for index in range(1, len(principal)): first, second = principal[index - 1:index + 1] if np.linalg.norm(second - first) > _edge_distance(first, second) + tolerance: segments.append(principal[start:index]) switches.extend([first, second]) start = index segments.append(principal[start:]) return principal, segments, np.array(switches).reshape(-1, 3)
[docs] def plot_search_paths(comparison): """Overlay the grid paths inside the unit MRP sphere, omitting shadow-switch edges.""" fig = plt.figure(figsize=(8, 7)) ax = fig.add_subplot(projection="3d") azimuth = np.linspace(0.0, 2.0 * np.pi, 25) # [rad] polar = np.linspace(0.0, np.pi, 13) # [rad] ax.plot_wireframe(np.outer(np.cos(azimuth), np.sin(polar)), np.outer(np.sin(azimuth), np.sin(polar)), np.outer(np.ones_like(azimuth), np.cos(polar)), color="0.8", alpha=0.35, linewidth=0.5) switch_label = "Shadow-switch endpoints" for key, label, color, style in [ ("default", "Default heuristic", "tab:orange", "--"), ("shortest", r"Shortest path ($h=\theta/4$)", "tab:blue", "-"), ]: principal, segments, switches = _principal_path_segments(comparison[key]["waypoints"]) for index, segment in enumerate(segments): ax.plot(*segment.T, linestyle=style, marker="o", markersize=4, color=color, label=label if index == 0 else None) if len(switches): ax.scatter(*switches.T, marker="s", facecolors="none", edgecolors=color, s=70, label=switch_label) switch_label = None ax.scatter(*principal[0], color="black", marker="*", s=110, label="Start") ax.scatter(*principal[-1], color="black", marker="X", s=65, label="Goal") ax.set(xlabel=r"$\sigma_1$ [-]", ylabel=r"$\sigma_2$ [-]", zlabel=r"$\sigma_3$ [-]", xlim=(-1.05, 1.05), ylim=(-1.05, 1.05), zlim=(-1.05, 1.05), title="Distance-search grid paths") ax.set_box_aspect((1, 1, 1)) ax.view_init(elev=25, azim=-60) ax.legend(loc="upper left", fontsize=9) fig.text(0.5, 0.02, "Unit MRP sphere; connecting lines are omitted at shadow switches.", ha="center", fontsize=9) fig.tight_layout(rect=(0, 0.04, 1, 1)) return fig
[docs] def plot_search_comparison(comparison, refinement): """Tabulate actual grid distance and node expansions for the two searches.""" fig, ax = plt.subplots(figsize=(8, 3)) ax.axis("off") rows = [ [label, f'{comparison[key]["edgeDistance"]:.5f}', str(comparison[key]["expandedNodeCount"])] for key, label in [("default", "Default heuristic"), ("shortest", r"Shortest path ($h=\theta/4$)")] ] table = ax.table(cellText=rows, colLabels=["Search mode", "MRP edge distance [-]", "Expanded nodes"], cellLoc="center", bbox=(0, 0.3, 1, 0.5)) table.auto_set_font_size(False) table.set_fontsize(11) for (row, _), cell in table.get_celld().items(): if row == 0: cell.set_facecolor("0.9") cell.set_text_props(weight="bold") reduction = 100.0 * (1.0 - comparison["shortest"]["edgeDistance"] / comparison["default"]["edgeDistance"]) ax.set_title(f"Grid distance and search work (N = {refinement})") ax.text(0.5, 0.15, f"{reduction:.1f}% shorter grid path with the optional heuristic", transform=ax.transAxes, ha="center") ax.text(0.5, 0.02, "Grid distance does not measure fitted control effort or continuous cone clearance.", transform=ax.transAxes, ha="center", fontsize=9) fig.tight_layout() return fig
# Plotting RW functions
[docs] def plot_attitude_error(timeData, dataSigmaBR): """Plot the attitude errors.""" plt.figure(1) for idx in range(3): plt.plot(timeData, dataSigmaBR[:, idx], color=simHelpers.getLineColor(idx, 3), label=r'$\sigma_' + str(idx) + '$') plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel(r'Attitude Error $\sigma_{B/R}$')
[docs] def plot_rw_cmd_torque(timeData, dataUsReq, numRW): """Plot the RW command torques.""" plt.figure(2) for idx in range(3): plt.plot(timeData, dataUsReq[:, idx], '--', color=simHelpers.getLineColor(idx, numRW), label=r'$\hat u_{s,' + str(idx) + '}$') plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel('RW Motor Torque (Nm)')
[docs] def plot_rw_motor_torque(timeData, dataUsReq, dataRW, numRW): """Plot the RW actual motor torques.""" plt.figure(2) for idx in range(3): plt.plot(timeData, dataUsReq[:, idx], '--', color=simHelpers.getLineColor(idx, numRW), label=r'$\hat u_{s,' + str(idx) + '}$') plt.plot(timeData, dataRW[idx], color=simHelpers.getLineColor(idx, numRW), label='$u_{s,' + str(idx) + '}$') plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel('RW Motor Torque (Nm)')
[docs] def plot_rate_error(timeData, dataOmegaBR): """Plot the body angular velocity rate tracking errors.""" plt.figure(3) for idx in range(3): plt.plot(timeData, dataOmegaBR[:, idx], color=simHelpers.getLineColor(idx, 3), label=r'$\omega_{BR,' + str(idx) + '}$') plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel('Rate Tracking Error (rad/s) ')
[docs] def plot_rw_speeds(timeData, dataOmegaRW, numRW): """Plot the RW spin rates.""" plt.figure(4) for idx in range(numRW): plt.plot(timeData, dataOmegaRW[:, idx] / macros.RPM, color=simHelpers.getLineColor(idx, numRW), label=r'$\Omega_{' + str(idx) + '}$') plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel('RW Speed (RPM) ')
[docs] def plot_st_miss_angle(timeData, dataMissAngle, Fov): """Plot the miss angle between star tacker boresight and Sun.""" fig, ax = plt.subplots() trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes) dataFov = np.ones(len(timeData))*Fov plt.plot(timeData, dataFov, '--', color = 'r', label = r'f.o.v.') data = dataMissAngle*macros.R2D for idx in range(1): plt.plot(timeData, data, color=simHelpers.getLineColor(idx, 3), label=r'$\alpha $') plt.fill_between(timeData, 0, 1, where=dataFov >= data, facecolor='red', alpha = 0.4, interpolate=True, transform = trans) plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel('StarTracker/Sun angle \u03B1 (deg)')
[docs] def plot_ss_miss_angle(timeData, dataMissAngle, Fov): """Plot the miss angle between sun sensor(s) boresight and Sun.""" fig, ax = plt.subplots() trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes) dataFov = np.ones(len(timeData))*Fov plt.plot(timeData, dataFov, '--', color = 'r', label = r'f.o.v.') data = [] for d in dataMissAngle: data.append(d*macros.R2D) for idx in range(len(data)): plt.plot(timeData, data[idx], color=simHelpers.getLineColor(idx, 3), label=r'$\beta_{' + str(idx+1) + '}$') dataMinAngle = 180*np.ones(len(timeData)) for i in range(len(timeData)): for j in range(len(data)): if data[j][i] < dataMinAngle[i]: dataMinAngle[i] = data[j][i] plt.fill_between(timeData, 0, 1, where = dataFov < dataMinAngle, facecolor='red', alpha = 0.4, interpolate=True, transform = trans) plt.legend(loc='lower right') plt.xlabel('Time [min]') plt.ylabel('SunSensor/Sun angle \u03B2 (deg)')
if __name__ == "__main__": run( True, # show_plots True, # use2SunSensors 30, # starTrackerFov 70, # sunSensorFov 3 # attitudeSetCase )