Skip to content
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

[deleted] removal of set_environment #54

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions manager/manager/launcher/launcher_drones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import time
from typing import List, Any
import roslaunch
import rospy

from src.manager.manager.launcher.launcher_interface import ILauncher, LauncherException

import logging


class RosProcessListener(roslaunch.pmon.ProcessListener):
def __init__(self, *args, **kwargs):
self.callback = kwargs.get('callback', None)

def process_died(self, name, exit_code):
print(f"ROS process {name} terminated with code {exit_code}")
if self.callback is not None:
self.callback(name, exit_code)


class LauncherRosApi(ILauncher):
exercise_id: str
type: str
module: str
resource_folders: List[str]
model_folders: List[str]
plugin_folders: List[str]
parameters: List[str]
launch_file: str

# holder for roslaunch process
launch: Any = None
listener: Any = None

def run(self, callback: callable = None):
logging.getLogger("roslaunch").setLevel(logging.CRITICAL)

# expand variables in configuration paths
launch_file = os.path.expandvars(self.launch_file)

self.listener = RosProcessListener(callback=callback)
uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)
self.launch = roslaunch.parent.ROSLaunchParent(uuid, [launch_file], process_listeners=[self.listener])
self.launch.start()

if not self.launch.pm.is_alive():
raise LauncherException("Exception launching ROS")

def is_running(self):
return self.launch.pm.is_alive()

def wait_for_shutdown(self, timeout=30):
print("Waiting for ROS and Gazebo to shutdown")
start_time = rospy.Time.now().to_sec()
while not rospy.is_shutdown() and self.is_running():
if rospy.Time.now().to_sec() - start_time > timeout:
print("Timeout while waiting for ROS and Gazebo to shutdown")
break
rospy.sleep(0.5)

def terminate(self):
try:
self.launch.shutdown()
self.wait_for_shutdown()
except roslaunch.RLException:
print("Exception shutting down ROS")