From 74cf1dee7030204bf3abbfce5112ed79b7122f99 Mon Sep 17 00:00:00 2001 From: Mark Stephenson Date: Mon, 25 Nov 2024 15:21:52 -0800 Subject: [PATCH] Issue #144: TEMP ipynb example --- examples/continuous_orbit_manuevers.ipynb | 185 ++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 examples/continuous_orbit_manuevers.ipynb diff --git a/examples/continuous_orbit_manuevers.ipynb b/examples/continuous_orbit_manuevers.ipynb new file mode 100644 index 00000000..f702c7bb --- /dev/null +++ b/examples/continuous_orbit_manuevers.ipynb @@ -0,0 +1,185 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Getting Started\n", + "This tutorial demonstrates the configuration and use of a simple BSK-RL environment.\n", + "BSK-RL and dependencies should already be installed at this point (see [Installation](../install.rst)\n", + "if you haven't installed the package yet).\n", + "\n", + "## Load Modules\n", + "In this tutorial, the environment will be created with `gym.make`, so it is necessary to\n", + "import the top-level `bsk_rl` module as well as `gym` and `bsk_rl` components." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from functools import partial\n", + "from bsk_rl import act, obs, sats, ConstellationTasking\n", + "from bsk_rl.sim import dyn, fsw\n", + "from bsk_rl.utils.orbital import relative_to_chief, random_orbit\n", + "\n", + "from Basilisk.architecture import bskLogging\n", + "\n", + "bskLogging.setDefaultLogLevel(bskLogging.BSK_WARNING)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If no errors were raised, you have a functional installation of `bsk_rl`.\n", + "\n", + "## Configure the Satellite\n", + "[Satellites](../api_reference/sats/index.rst) are configurable agents in the environment.\n", + "To make a new environment, start by specifying the [observations](../api_reference/obs/index.rst)\n", + "and [actions](../api_reference/act/index.rst) of a satellite type, as well as the underlying\n", + "Basilisk [simulation](../api_reference/sim/index.rst) models used by the satellite." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class TumbleSat(sats.Satellite):\n", + " observation_spec = [\n", + " obs.SatProperties(dict(prop=\"r_BN_N\"), dict(prop=\"v_BN_N\")),\n", + " ]\n", + " action_spec = [act.Drift()]\n", + " dyn_type = dyn.ConjunctionDynModel\n", + " fsw_type = fsw.BasicFSWModel\n", + "\n", + "\n", + "class ThrustSat(sats.Satellite):\n", + " observation_spec = [\n", + " obs.SatProperties(dict(prop=\"r_BN_N\"), dict(prop=\"v_BN_N\")),\n", + " ]\n", + " action_spec = [act.MagicThrust(max_dv=100)]\n", + " dyn_type = dyn.ConjunctionDynModel\n", + " fsw_type = fsw.MagicOrbitalManeuverFSWModel\n", + "\n", + "\n", + "sat = ThrustSat(\"Thrusty\")\n", + "sat.action_space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Making the Environment\n", + "For this example, we will be using the single-agent [SatelliteTasking](../api_reference/index.rst) \n", + "environment. Along with passing the satellite that we configured, the environment takes\n", + "a [scenario](../api_reference/scene/index.rst), which defines the environment the\n", + "satellite is acting in, and a [rewarder](../api_reference/data/index.rst), which defines\n", + "how data collected from the scenario is rewarded." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "env = ConstellationTasking(\n", + " satellites=[\n", + " TumbleSat(\"Tumbler\"),\n", + " ThrustSat(\"Thrust-1\"),\n", + " ThrustSat(\"Thrust-2\"),\n", + " ],\n", + " sat_arg_randomizer=relative_to_chief(\n", + " chief_name=\"Tumbler\",\n", + " chief_orbit=partial(random_orbit, i=None),\n", + " deputy_relative_state={\n", + " \"Thrust-1\": np.array([50, 0, 0, -3, 0, 0]),\n", + " \"Thrust-2\": np.array([-50, 0, 0, 3, 0, 0]),\n", + " },\n", + " ),\n", + " time_limit=5700.0 * 3,\n", + " log_level=\"INFO\",\n", + ")\n", + "env.action_spaces" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Interacting with the Environment\n", + "\n", + "First, the environment is reset." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "observation, info = env.reset(seed=0)\n", + "print(observation)\n", + "print(info)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, we take the scan action (`action=0`) a few times. This allows for the satellite to\n", + "settle its attitude in the nadir pointing mode to satisfy imaging conditions. Note that \n", + "the logs show little or no data accumulated in the first two steps as it settles, but\n", + "achieves 60 reward (corresponding to 60 seconds of imaging) by the third step." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for _ in range(20):\n", + " actions = {}\n", + " # for sat in env.satellites:\n", + " # if sat.requires_retasking:\n", + " # if isinstance(sat, TumbleSat):\n", + " # actions[sat.name] = 0\n", + " # else:\n", + " # actions[sat.name] = np.concatenate(\n", + " # (np.random.uniform(-20, 20, 3), np.random.uniform(0, 200, 1))\n", + " # )\n", + "\n", + " observation, reward, terminated, truncated, info = env.step(actions)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.11" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}