-
Notifications
You must be signed in to change notification settings - Fork 29
multi-robot support with parallel spawn #294
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
Draft
anishk85
wants to merge
3
commits into
JdeRobot:humble-devel
Choose a base branch
from
anishk85:multi-robot-parallel-spawn
base: humble-devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
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,116 @@ | ||
| """Agent group abstraction for the Robotics Application Manager. | ||
|
|
||
| RAM's lifecycle operations (play / pause / resume / reset / stop) used to act on | ||
| a single application process. Multi-agent exercises (e.g. drone cat-and-mouse, | ||
| which runs a "cat" and a "mouse") need them to act on *all* the agents at once. | ||
|
|
||
| Rather than teaching the RAM FiniteStateMachine about multiple agents, the FSM | ||
| stays single-target: it fires one transition, and that transition fans the | ||
| operation out over every member of this group. So a single ``pause`` trigger | ||
| becomes "suspend agent0, suspend agent1, ... ", a single ``reset`` becomes a | ||
| sequence over each agent, and so on. The FSM definition never changes; only the | ||
| execution layer behind each handler iterates the group. | ||
|
|
||
| This holds N agents (1 for most exercises, 2 for cat-mouse, more in future | ||
| multi-robot exercises) and exposes the per-agent *process* operations. World / | ||
| simulator level calls (pause_sim, reset_sim) remain single calls in the manager, | ||
| because a shared Gazebo world has one physics clock and cannot be paused | ||
| per-agent. | ||
| """ | ||
|
|
||
| import os | ||
| import signal | ||
|
|
||
| import psutil | ||
|
|
||
| from robotics_application_manager.libs import stop_process_and_children | ||
| from robotics_application_manager.ram_logging import LogManager | ||
|
|
||
|
|
||
| class AgentGroup: | ||
| """A set of agent processes managed as one unit. | ||
|
|
||
| Each lifecycle method fans the operation out over every member, so callers | ||
| treat the whole group as if it were a single agent. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| # name -> subprocess.Popen | ||
| self._agents = {} | ||
|
|
||
| # ----- membership ------------------------------------------------------- | ||
|
|
||
| def add(self, name, proc): | ||
| """Register an agent process under a name (e.g. 'agentA', 'agentB').""" | ||
| self._agents[name] = proc | ||
|
|
||
| def clear(self): | ||
| """Forget all members without touching the processes.""" | ||
| self._agents = {} | ||
|
|
||
| def names(self): | ||
| return list(self._agents.keys()) | ||
|
|
||
| def __len__(self): | ||
| return len(self._agents) | ||
|
|
||
| def __bool__(self): | ||
| # Lets callers keep writing `if self.application_processes:` | ||
| return bool(self._agents) | ||
|
|
||
| def __iter__(self): | ||
| return iter(self._agents.values()) | ||
|
|
||
| # ----- group lifecycle operations (each fans out over all members) ------ | ||
|
|
||
| def pause_all(self): | ||
| """Suspend every agent process and its whole child tree (SIGSTOP).""" | ||
| for proc in self._agents.values(): | ||
| self._apply_to_tree(proc, lambda child: child.suspend()) | ||
|
|
||
| def resume_all(self): | ||
| """Resume every agent process and its whole child tree (SIGCONT).""" | ||
| for proc in self._agents.values(): | ||
| self._apply_to_tree(proc, lambda child: child.resume()) | ||
|
|
||
| def signal_stop_all(self): | ||
| """SIGSTOP each top-level agent process (used to gate them before | ||
| the simulator unpauses, so no agent acts on a still-paused world).""" | ||
| self._signal_all(signal.SIGSTOP) | ||
|
|
||
| def signal_cont_all(self): | ||
| """SIGCONT each top-level agent process (release after unpause).""" | ||
| self._signal_all(signal.SIGCONT) | ||
|
|
||
| def kill_all(self): | ||
| """Terminate every agent process and its children, then empty the group.""" | ||
| for name, proc in self._agents.items(): | ||
| try: | ||
| stop_process_and_children(proc) | ||
| except Exception: | ||
| LogManager.logger.exception(f"Error stopping agent '{name}'") | ||
| self.clear() | ||
|
|
||
| # ----- helpers ---------------------------------------------------------- | ||
|
|
||
| def _signal_all(self, sig): | ||
| for proc in self._agents.values(): | ||
| try: | ||
| os.kill(proc.pid, sig) | ||
| except ProcessLookupError: | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def _apply_to_tree(proc, fn): | ||
| """Run ``fn`` on the process and every descendant, tolerating races.""" | ||
| try: | ||
| parent = psutil.Process(proc.pid) | ||
| tree = parent.children(recursive=True) | ||
| tree.append(parent) | ||
| for child in tree: | ||
| try: | ||
| fn(child) | ||
| except psutil.NoSuchProcess: | ||
| pass | ||
| except psutil.NoSuchProcess: | ||
| pass |
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 |
|---|---|---|
|
|
@@ -103,7 +103,7 @@ def unpause(self): | |
| 10000, | ||
| ) | ||
|
|
||
| def reset(self, robot_entity=None): | ||
| def reset(self, robot_entities=None): | ||
| node = Node() | ||
|
|
||
| node.request( | ||
|
|
@@ -114,14 +114,20 @@ def reset(self, robot_entity=None): | |
| 10000, | ||
| ) | ||
|
|
||
| if robot_entity is not None: | ||
| node.request( | ||
| f"/world/default/remove", | ||
| Entity(name=robot_entity, type=Entity.MODEL), | ||
| Entity, | ||
| Boolean, | ||
| 5000, | ||
| ) | ||
| # remove each robot entity before resetting the world (a world reset on | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use capital letters |
||
| # its own doesn't clear runtime-spawned models). accepts a single name | ||
| # or a list of them, so one robot and N robots take the same path. | ||
| if robot_entities: | ||
| if isinstance(robot_entities, str): | ||
| robot_entities = [robot_entities] | ||
| for entity in robot_entities: | ||
| node.request( | ||
| f"/world/default/remove", | ||
| Entity(name=entity, type=Entity.MODEL), | ||
| Entity, | ||
| Boolean, | ||
| 5000, | ||
| ) | ||
|
|
||
| node.request( | ||
| f"/world/default/control", | ||
|
|
||
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
Oops, something went wrong.
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.