|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "22082c62", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# RSO Inspection\n", |
| 9 | + "\n", |
| 10 | + "This example demonstrates the configuration of a resident space object (RSO) inspection\n", |
| 11 | + "environment, in which a servicer spacecraft circumnavigates a RSO to image the illuminated\n", |
| 12 | + "facets.\n", |
| 13 | + "\n", |
| 14 | + "\n", |
| 15 | + "RLlib is actively developed and can change significantly from version to version. For this\n", |
| 16 | + "script, the following version is used:" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "id": "fde78b08", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "from importlib.metadata import version\n", |
| 27 | + "from bsk_rl import sats, obs, act, ConstellationTasking, scene, data\n", |
| 28 | + "from bsk_rl.obs.relative_observations import rso_imaged_regions\n", |
| 29 | + "from bsk_rl.utils.orbital import fibonacci_sphere\n", |
| 30 | + "from bsk_rl.sim import dyn, fsw\n", |
| 31 | + "import types\n", |
| 32 | + "import numpy as np\n", |
| 33 | + "from Basilisk.architecture import bskLogging\n", |
| 34 | + "from functools import partial\n", |
| 35 | + "from bsk_rl.utils.orbital import random_orbit, random_unit_vector, relative_to_chief\n", |
| 36 | + "from Basilisk.utilities.orbitalMotion import elem2rv\n", |
| 37 | + "from Basilisk.utilities.RigidBodyKinematics import C2MRP\n", |
| 38 | + "\n", |
| 39 | + "bskLogging.setDefaultLogLevel(bskLogging.BSK_WARNING)\n", |
| 40 | + "\n", |
| 41 | + "version(\"ray\") # Parent package of RLlib" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "id": "e5875010", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "## Defining the Satellites\n", |
| 50 | + "\n", |
| 51 | + "First, the RSO satellite is configured. It is given support for nadir pointing through\n", |
| 52 | + "the ``ImagingDynModel`` and ``Downlink`` action." |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "id": "48b72ea2", |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "class RSOSat(sats.Satellite):\n", |
| 63 | + " observation_spec = [\n", |
| 64 | + " obs.SatProperties(dict(prop=\"one\", fn=lambda _: 1.0)),\n", |
| 65 | + " ]\n", |
| 66 | + " action_spec = [act.Downlink(duration=1e9)]\n", |
| 67 | + " dyn_type = types.new_class(\n", |
| 68 | + " \"Dyn\", (dyn.ImagingDynModel, dyn.ConjunctionDynModel, dyn.RSODynModel)\n", |
| 69 | + " )\n", |
| 70 | + " fsw_type = fsw.ContinuousImagingFSWModel\n", |
| 71 | + "\n", |
| 72 | + "\n", |
| 73 | + "rso_sat_args = dict(\n", |
| 74 | + " conjunction_radius=2.0,\n", |
| 75 | + " K=7.0 / 20,\n", |
| 76 | + " P=35.0 / 20,\n", |
| 77 | + " Ki=1e-6,\n", |
| 78 | + " dragCoeff=0.0,\n", |
| 79 | + " batteryStorageCapacity=1e9,\n", |
| 80 | + " storedCharge_Init=1e9,\n", |
| 81 | + " wheelSpeeds=[0.0, 0.0, 0.0],\n", |
| 82 | + " u_max=1.0,\n", |
| 83 | + ")" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "markdown", |
| 88 | + "id": "fcbb42c7", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "The inspector satellite has a more complex configuration." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "id": "e01b5686", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "def sun_hat_chief(self, other):\n", |
| 102 | + " r_SN_N = (\n", |
| 103 | + " self.simulator.world.gravFactory.spiceObject.planetStateOutMsgs[\n", |
| 104 | + " self.simulator.world.sun_index\n", |
| 105 | + " ]\n", |
| 106 | + " .read()\n", |
| 107 | + " .PositionVector\n", |
| 108 | + " )\n", |
| 109 | + " r_BN_N = self.dynamics.r_BN_N\n", |
| 110 | + " r_SN_N = np.array(r_SN_N)\n", |
| 111 | + " r_SB_N = r_SN_N - r_BN_N\n", |
| 112 | + " r_SB_N_hat = r_SB_N / np.linalg.norm(r_SB_N)\n", |
| 113 | + " HN = other.dynamics.HN\n", |
| 114 | + " return HN @ r_SB_N_hat\n", |
| 115 | + "\n", |
| 116 | + "\n", |
| 117 | + "class InspectorSat(sats.Satellite):\n", |
| 118 | + " observation_spec = [\n", |
| 119 | + " obs.SatProperties(\n", |
| 120 | + " dict(prop=\"dv_available\", norm=10),\n", |
| 121 | + " dict(prop=\"inclination\", norm=np.pi),\n", |
| 122 | + " # dict(prop=\"laan_rel_sun\", norm=np.pi),\n", |
| 123 | + " # dict(prop=\"altitude\", norm=1000),\n", |
| 124 | + " ),\n", |
| 125 | + " obs.ResourceRewardWeight(),\n", |
| 126 | + " obs.RelativeProperties(\n", |
| 127 | + " dict(prop=\"r_DC_Hc\", norm=500),\n", |
| 128 | + " dict(prop=\"v_DC_Hc\", norm=5),\n", |
| 129 | + " dict(\n", |
| 130 | + " prop=\"rso_imaged_regions\",\n", |
| 131 | + " fn=partial(\n", |
| 132 | + " rso_imaged_regions,\n", |
| 133 | + " region_centers=fibonacci_sphere(15),\n", |
| 134 | + " frame=\"chief_hill\",\n", |
| 135 | + " ),\n", |
| 136 | + " ),\n", |
| 137 | + " dict(prop=\"sun_hat_Hc\", fn=sun_hat_chief),\n", |
| 138 | + " chief_name=\"RSO\",\n", |
| 139 | + " ),\n", |
| 140 | + " obs.Eclipse(norm=5700),\n", |
| 141 | + " obs.Time(),\n", |
| 142 | + " ]\n", |
| 143 | + " action_spec = [\n", |
| 144 | + " act.ImpulsiveThrustHill(\n", |
| 145 | + " chief_name=\"RSO\",\n", |
| 146 | + " max_dv=1.0,\n", |
| 147 | + " max_drift_duration=5700.0 * 2,\n", |
| 148 | + " fsw_action=\"action_inspect_rso\",\n", |
| 149 | + " )\n", |
| 150 | + " ]\n", |
| 151 | + " dyn_type = types.new_class(\n", |
| 152 | + " \"Dyn\", (dyn.MaxRangeDynModel, dyn.ConjunctionDynModel, dyn.RSOInspectorDynModel)\n", |
| 153 | + " )\n", |
| 154 | + " fsw_type = types.new_class(\n", |
| 155 | + " \"FSW\",\n", |
| 156 | + " (\n", |
| 157 | + " fsw.SteeringFSWModel,\n", |
| 158 | + " fsw.MagicOrbitalManeuverFSWModel,\n", |
| 159 | + " fsw.RSOInspectorFSWModel,\n", |
| 160 | + " ),\n", |
| 161 | + " )\n", |
| 162 | + "\n", |
| 163 | + "\n", |
| 164 | + "inspector_sat_args = dict(\n", |
| 165 | + " imageAttErrorRequirement=1.0,\n", |
| 166 | + " imageRateErrorRequirement=None,\n", |
| 167 | + " instrumentBaudRate=1,\n", |
| 168 | + " dataStorageCapacity=1e6,\n", |
| 169 | + " batteryStorageCapacity=1e9,\n", |
| 170 | + " storedCharge_Init=1e9,\n", |
| 171 | + " conjunction_radius=2.0,\n", |
| 172 | + " dv_available_init=10.0,\n", |
| 173 | + " max_range_radius=1000,\n", |
| 174 | + " chief_name=\"RSO\",\n", |
| 175 | + " u_max=1.0,\n", |
| 176 | + ")" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "id": "832adeb8", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "## Environment Generation\n", |
| 185 | + "\n", |
| 186 | + "A satellite argument randomizer is " |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": null, |
| 192 | + "id": "f6e96922", |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [], |
| 195 | + "source": [ |
| 196 | + "def sat_arg_randomizer(satellites):\n", |
| 197 | + " alt = np.random.uniform(500, 1100)\n", |
| 198 | + " R_E = 6371.0 # km\n", |
| 199 | + " e = np.random.uniform(0.0, 1 - (500 + R_E) / (alt + R_E))\n", |
| 200 | + " chief_orbit = random_orbit(alt=alt, e=e)\n", |
| 201 | + "\n", |
| 202 | + " inspectors = [sat for sat in satellites if \"Inspector\" in sat.name]\n", |
| 203 | + " rso = [satellite for satellite in satellites if satellite.name == \"RSO\"][0]\n", |
| 204 | + "\n", |
| 205 | + " args = {}\n", |
| 206 | + " for inspector in inspectors:\n", |
| 207 | + " relative_randomizer = relative_to_chief(\n", |
| 208 | + " chief_name=\"RSO\",\n", |
| 209 | + " chief_orbit=chief_orbit,\n", |
| 210 | + " deputy_relative_state={\n", |
| 211 | + " inspector.name: lambda: np.concatenate(\n", |
| 212 | + " (\n", |
| 213 | + " random_unit_vector() * np.random.uniform(250, 750),\n", |
| 214 | + " random_unit_vector() * np.random.uniform(0, 0.3),\n", |
| 215 | + " )\n", |
| 216 | + " ),\n", |
| 217 | + " },\n", |
| 218 | + " )\n", |
| 219 | + " args.update(relative_randomizer([rso, inspector]))\n", |
| 220 | + "\n", |
| 221 | + " # Align RSO Hill frame for initial nadir pointing\n", |
| 222 | + " mu = rso.sat_args_generator[\"mu\"]\n", |
| 223 | + " r_N, v_N = elem2rv(mu, args[rso][\"oe\"])\n", |
| 224 | + " r_hat = r_N / np.linalg.norm(r_N)\n", |
| 225 | + " v_hat = v_N / np.linalg.norm(v_N)\n", |
| 226 | + " x = r_hat\n", |
| 227 | + " z = np.cross(r_hat, v_hat)\n", |
| 228 | + " z = z / np.linalg.norm(z)\n", |
| 229 | + " y = np.cross(z, x)\n", |
| 230 | + " HN = np.array([x, y, z])\n", |
| 231 | + " BH = np.eye(3)\n", |
| 232 | + " args[rso][\"sigma_init\"] = C2MRP(BH @ HN)\n", |
| 233 | + "\n", |
| 234 | + " a = chief_orbit.a\n", |
| 235 | + " T = np.sqrt(a**3 / mu) * 2 * np.pi\n", |
| 236 | + " omega_BN_N = z * 2 * np.pi / T\n", |
| 237 | + " args[rso][\"omega_init\"] = BH @ HN @ omega_BN_N\n", |
| 238 | + "\n", |
| 239 | + " return args\n" |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "code", |
| 244 | + "execution_count": null, |
| 245 | + "id": "f19447ce", |
| 246 | + "metadata": {}, |
| 247 | + "outputs": [], |
| 248 | + "source": [ |
| 249 | + "env = ConstellationTasking(\n", |
| 250 | + " satellites=[\n", |
| 251 | + " RSOSat(\"RSO\", sat_args=rso_sat_args),\n", |
| 252 | + " InspectorSat(\"Inspector\", sat_args=inspector_sat_args, obs_type=dict),\n", |
| 253 | + " ],\n", |
| 254 | + " sat_arg_randomizer=sat_arg_randomizer,\n", |
| 255 | + " scenario=scene.SphericalRSO(\n", |
| 256 | + " n_points=100,\n", |
| 257 | + " radius=1.0,\n", |
| 258 | + " theta_max=np.radians(30),\n", |
| 259 | + " range_max=250,\n", |
| 260 | + " theta_solar_max=np.radians(60),\n", |
| 261 | + " ),\n", |
| 262 | + " rewarder=(\n", |
| 263 | + " data.RSOInspectionReward(),\n", |
| 264 | + " data.ResourceReward(\n", |
| 265 | + " resource_fn=lambda sat: sat.fsw.dv_available\n", |
| 266 | + " if isinstance(sat.fsw, fsw.MagicOrbitalManeuverFSWModel)\n", |
| 267 | + " else 0.0,\n", |
| 268 | + " reward_weight=np.random.uniform(0.0, 0.5),\n", |
| 269 | + " ),\n", |
| 270 | + " ),\n", |
| 271 | + " time_limit=60000,\n", |
| 272 | + " sim_rate=5.0,\n", |
| 273 | + " log_level=\"INFO\",\n", |
| 274 | + ")" |
| 275 | + ] |
| 276 | + }, |
| 277 | + { |
| 278 | + "cell_type": "code", |
| 279 | + "execution_count": null, |
| 280 | + "id": "0fcfc081", |
| 281 | + "metadata": {}, |
| 282 | + "outputs": [], |
| 283 | + "source": [ |
| 284 | + "env.reset()\n", |
| 285 | + "for i in range(10):\n", |
| 286 | + " env.step(dict(RSO=0, Inspector=[0.1, 0.1, 0.1, 100]))" |
| 287 | + ] |
| 288 | + } |
| 289 | + ], |
| 290 | + "metadata": { |
| 291 | + "kernelspec": { |
| 292 | + "display_name": ".venv", |
| 293 | + "language": "python", |
| 294 | + "name": "python3" |
| 295 | + }, |
| 296 | + "language_info": { |
| 297 | + "codemirror_mode": { |
| 298 | + "name": "ipython", |
| 299 | + "version": 3 |
| 300 | + }, |
| 301 | + "file_extension": ".py", |
| 302 | + "mimetype": "text/x-python", |
| 303 | + "name": "python", |
| 304 | + "nbconvert_exporter": "python", |
| 305 | + "pygments_lexer": "ipython3", |
| 306 | + "version": "3.10.11" |
| 307 | + } |
| 308 | + }, |
| 309 | + "nbformat": 4, |
| 310 | + "nbformat_minor": 5 |
| 311 | +} |
0 commit comments