-
Notifications
You must be signed in to change notification settings - Fork 13
Actor Status Monitoring & Saving to File #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
a43925e
Added first instance of README.
486e676
Improving README.
017e38d
WIP Improving README.
6316ca7
Merge branch 'ground-station-implementation' into create_README
d671614
Added missing GIF.
578b9ea
Fixing small name bug.
46cc5a6
Update README.md
GabrieleMeoni e925e93
Update README.md
GabrieleMeoni dc73034
Update README.md
GabrieleMeoni b67aa32
Update README.md
GabrieleMeoni be4babb
Update README.md
GabrieleMeoni 62d2a7c
Update README.md
GabrieleMeoni c3b4ef3
Update README.md
GabrieleMeoni c42bfe6
Updated README.
9040491
Updating README.
2f7b840
Added OperationsMonitor
gomezzz 80badd4
Added test for operations monitor
gomezzz 44d62bf
Added readme entry of status monitoring
gomezzz c956a7b
Update README.md
gomezzz a8063b8
Upadting readme to support single examples.
9e802ed
Corrected example of add other actors.
7daa15b
Merge branch 'create_README' into monitoring
gomezzz 6b5ace5
Renamed variables
gomezzz 7e45fca
Update README.md
GabrieleMeoni 09a2ad7
Update README.md
GabrieleMeoni 6444605
Update README.md
GabrieleMeoni 6568971
Update README.md
GabrieleMeoni 627e693
Update README.md
GabrieleMeoni 16b6bce
Update README.md
GabrieleMeoni 2aa197d
Update README.md
GabrieleMeoni 03de28d
Added suggestions.
36b7d4d
Moving images to resources.
fb25ec3
Fixing small error.
5f1548f
Improved constraints image.
65ce98b
Removed missing `PASEOS` and converted into PASEOS.
428710a
Update README.md
GabrieleMeoni bd04a24
Update README.md
GabrieleMeoni e1a3e41
Update README.md
GabrieleMeoni f7ad578
Adding first corrections and first round of examples structure.
6ff8a86
Restructuring examples.
ee85021
Changing image format.
5657e75
Merge branch 'monitoring' into create_README
1b34f43
Fixing wrong merge.
8b29b4c
Fixing wrong links to output files.
50135f2
Fixing visualisation image position.
af94a82
Merge branch 'main' into monitoring
gomezzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Simple test for the operations monitor""" | ||
|
|
||
| import asyncio | ||
| import pytest | ||
| import pykep as pk | ||
|
|
||
| import paseos | ||
| from paseos import ActorBuilder, SpacecraftActor, load_default_cfg | ||
|
|
||
|
|
||
| async def wait_for_activity(sim): | ||
| while sim._is_running_activity is True: | ||
| await asyncio.sleep(0.1) | ||
|
|
||
|
|
||
| # tell pytest to create an event loop and execute the tests using the event loop | ||
| @pytest.mark.asyncio | ||
| async def test_monitor(): | ||
| """Test to see if we can log while performing an activity and write to file then. | ||
| To fully judge the outcome, have a look at the generated test file.""" | ||
| # Define central body | ||
| earth = pk.planet.jpl_lp("earth") | ||
|
|
||
| # Define local actor | ||
| sat1 = ActorBuilder.get_actor_scaffold("sat1", SpacecraftActor, pk.epoch(0)) | ||
| ActorBuilder.set_orbit(sat1, [10000000, 0, 0], [0, 8000.0, 0], pk.epoch(0), earth) | ||
| ActorBuilder.set_power_devices(sat1, 500, 10000, 1) | ||
|
|
||
| # init simulation | ||
| cfg = load_default_cfg() # loading cfg to modify defaults | ||
| cfg.sim.dt = 0.1 # setting lower timestep to run things quickly | ||
| cfg.sim.activity_timestep = 0.1 | ||
| cfg.io.logging_interval = 0.25 # log every 0.25 seconds | ||
| cfg.sim.time_multiplier = 10 # speed up execution for convenience | ||
| sim = paseos.init_sim(sat1, cfg) | ||
|
|
||
| async def func1(args): | ||
| await asyncio.sleep(0.5) | ||
|
|
||
| async def func2(args): | ||
| await asyncio.sleep(1.0) | ||
|
|
||
| # Register an activity that draws 10 watt per second | ||
| sim.register_activity( | ||
| "Activity_1", activity_function=func1, power_consumption_in_watt=2 | ||
| ) | ||
|
|
||
| sim.register_activity( | ||
| "Activity_2", activity_function=func2, power_consumption_in_watt=10 | ||
| ) | ||
|
|
||
| # Run the activity | ||
| sim.perform_activity("Activity_1") | ||
| await wait_for_activity(sim) | ||
|
|
||
| sim.perform_activity("Activity_2") | ||
| await wait_for_activity(sim) | ||
|
|
||
| sim.save_status_log_csv("test.csv") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import csv | ||
|
|
||
| from loguru import logger | ||
| from dotmap import DotMap | ||
| import pykep as pk | ||
|
|
||
| from paseos.actors.base_actor import BaseActor | ||
| from paseos.actors.spacecraft_actor import SpacecraftActor | ||
|
|
||
|
|
||
| class OperationsMonitor: | ||
| """This class is used to track actor status and activities over time.""" | ||
|
|
||
| def __init__(self, actor_name): | ||
| """Initializes the OperationsMonitor | ||
|
|
||
| Args: | ||
| actor_name (str): Name of the local actor. | ||
| """ | ||
| logger.trace("Initializing OperationsMonitor for " + actor_name) | ||
| self._actor_name = actor_name | ||
| self._log = DotMap(_dynamic=False) | ||
| self._log.timesteps = [] | ||
| self._log.current_activity = [] | ||
| self._log.state_of_charge = [] | ||
| self._log.is_in_eclipse = [] | ||
| self._log.known_actors = [] | ||
| self._log.position = [] | ||
| self._log.velocity = [] | ||
|
|
||
| def log( | ||
| self, | ||
| local_actor: BaseActor, | ||
| known_actors: list, | ||
| ): | ||
| """Log the current time step. | ||
|
|
||
| Args: | ||
| local_actor (BaseActor): The local actors whose status we are monitoring. | ||
| known_actors (list): List of names of the known actors. | ||
| """ | ||
| logger.trace("Logging iteration") | ||
| assert local_actor.name == self._actor_name, ( | ||
| "Expected actor's name was" + self._actor_name | ||
| ) | ||
| self._log.timesteps.append(local_actor.local_time.mjd2000 * pk.DAY2SEC) | ||
| self._log.current_activity.append(local_actor.current_activity) | ||
| self._log.position.append(local_actor._last_position) | ||
| self._log.velocity.append(local_actor._last_velocity) | ||
| self._log.known_actors.append(known_actors) | ||
| if isinstance(local_actor, SpacecraftActor): | ||
| self._log.state_of_charge.append(local_actor.state_of_charge) | ||
| else: | ||
| self._log.state_of_charge.append(1.0) | ||
|
|
||
| if local_actor._last_eclipse_status is None: | ||
| self._log.is_in_eclipse.append(False) | ||
| else: | ||
| self._log.is_in_eclipse.append(local_actor._last_eclipse_status) | ||
|
|
||
| def save_to_csv(self, filename): | ||
| """Write the created log file to a csv file. | ||
|
|
||
| Args: | ||
| filename (str): File to store the log in. | ||
| """ | ||
| logger.trace("Writing status log file to " + filename) | ||
| with open(filename, "w", newline="") as f: | ||
| w = csv.DictWriter(f, self._log.keys()) | ||
| w.writeheader() | ||
| for i in range(len(self._log.timesteps)): | ||
| row = { | ||
| "timesteps": self._log.timesteps[i], | ||
| "current_activity": self._log.current_activity[i], | ||
| "position": self._log.position[i], | ||
| "velocity": self._log.velocity[i], | ||
| "known_actors": self._log.known_actors[i], | ||
| "state_of_charge": self._log.state_of_charge[i], | ||
| "is_in_eclipse": self._log.is_in_eclipse[i], | ||
| } | ||
| w.writerow(row) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.