|
| 1 | +import sys |
1 | 2 | from manager.manager.launcher.launcher_interface import ILauncher |
2 | 3 | from manager.manager.docker_thread.docker_thread import DockerThread |
3 | 4 | from manager.manager.vnc.vnc_server import Vnc_server |
|
10 | 11 | import os |
11 | 12 | import stat |
12 | 13 | from typing import List, Any |
| 14 | +from manager.ram_logging.log_manager import LogManager |
13 | 15 |
|
| 16 | +def call_gzservice(self, service, reqtype, reptype, timeout, req): |
| 17 | + command = f"gz service -s {service} --reqtype {reqtype} --reptype {reptype} --timeout {timeout} --req '{req}'" |
| 18 | + subprocess.call( |
| 19 | + f"{command}", |
| 20 | + shell=True, |
| 21 | + stdout=sys.stdout, |
| 22 | + stderr=subprocess.STDOUT, |
| 23 | + bufsize=1024, |
| 24 | + universal_newlines=True, |
| 25 | + ) |
14 | 26 |
|
15 | | -class LauncherGzsimView(ILauncher): |
| 27 | +def call_service(self, service, service_type, request_data="{}"): |
| 28 | + command = f"ros2 service call {service} {service_type} '{request_data}'" |
| 29 | + subprocess.call( |
| 30 | + f"{command}", |
| 31 | + shell=True, |
| 32 | + stdout=sys.stdout, |
| 33 | + stderr=subprocess.STDOUT, |
| 34 | + bufsize=1024, |
| 35 | + universal_newlines=True, |
| 36 | + ) |
| 37 | + |
| 38 | +def is_ros_service_available(self, service_name): |
| 39 | + try: |
| 40 | + result = subprocess.run( |
| 41 | + ["ros2", "service", "list", "--include-hidden-services"], |
| 42 | + capture_output=True, |
| 43 | + text=True, |
| 44 | + check=True, |
| 45 | + ) |
| 46 | + return service_name in result.stdout |
| 47 | + except subprocess.CalledProcessError as e: |
| 48 | + LogManager.logger.exception(f"Error checking service availability: {e}") |
| 49 | + return False |
| 50 | + |
| 51 | +class LauncherGzsim(ILauncher): |
16 | 52 | display: str |
17 | 53 | internal_port: int |
18 | 54 | external_port: int |
@@ -73,6 +109,41 @@ def terminate(self): |
73 | 109 | def died(self): |
74 | 110 | pass |
75 | 111 |
|
| 112 | + def pause(self): |
| 113 | + call_gzservice( |
| 114 | + "$(gz service -l | grep '^/world/\w*/control$')", |
| 115 | + "gz.msgs.WorldControl", |
| 116 | + "gz.msgs.Boolean", |
| 117 | + "3000", |
| 118 | + "pause: true", |
| 119 | + ) |
| 120 | + |
| 121 | + def unpause(self): |
| 122 | + call_gzservice( |
| 123 | + "$(gz service -l | grep '^/world/\w*/control$')", |
| 124 | + "gz.msgs.WorldControl", |
| 125 | + "gz.msgs.Boolean", |
| 126 | + "3000", |
| 127 | + "pause: false", |
| 128 | + ) |
| 129 | + |
| 130 | + def reset(self): |
| 131 | + if is_ros_service_available("/drone0/platform/state_machine/_reset"): |
| 132 | + call_service( |
| 133 | + "/drone0/platform/state_machine/_reset", |
| 134 | + "std_srvs/srv/Trigger", |
| 135 | + "{}", |
| 136 | + ) |
| 137 | + call_gzservice( |
| 138 | + "$(gz service -l | grep '^/world/\w*/control$')", |
| 139 | + "gz.msgs.WorldControl", |
| 140 | + "gz.msgs.Boolean", |
| 141 | + "3000", |
| 142 | + "reset: {all: true}", |
| 143 | + ) |
| 144 | + if is_ros_service_available("/drone0/controller/_reset"): |
| 145 | + call_service("/drone0/controller/_reset", "std_srvs/srv/Trigger", "{}") |
| 146 | + |
76 | 147 | def get_dri_path(self): |
77 | 148 | directory_path = "/dev/dri" |
78 | 149 | dri_path = "" |
|
0 commit comments