Skip to content

Commit

Permalink
Add WebSockets communication capabilities (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: elvio.aruta98 <[email protected]>
  • Loading branch information
leandropineda and elvio.aruta98 authored Jan 5, 2024
1 parent 20586eb commit f4410c2
Show file tree
Hide file tree
Showing 12 changed files with 1,054 additions and 49 deletions.
2 changes: 1 addition & 1 deletion mir_connector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ flake8 --max-line-length=100 --exclude venv
```bash
# Create the virtualenv if not active already
virtualenv venv/
pip install -e '.[test]'
pip install -e '.[dev]'
. venv/bin/activate
pytest -v
```
Expand Down
1 change: 1 addition & 0 deletions mir_connector/config/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
!.gitignore
!.example.*
!example.env
!my_fleet.example.yaml
39 changes: 39 additions & 0 deletions mir_connector/config/my_fleet.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# There can be multiple robot configurations in a single file
# The InOrbit Robot ID is the key for each robot configuration
'mir100-1':
# Robot key for InOrbit Connect robots, see https://api.inorbit.ai/docs/index.html#operation/generateRobotKey
# Delete it if not used
inorbit_robot_key: <key>
# This is the timezone of the robot location. It must be a valid pytz timezone: https://pythonhosted.org/pytz/#helpers
location_tz: America/Los_Angeles
# Connector log level. Valid values are DEBUG, INFO, WARNING, ERROR, CRITICAL
log_level: INFO
# Camera configuration. Multiple cameras can be configured
cameras:
- video_url: rtsp://username:[email protected]:554/cam/realmonitor?channel=1&subtype=0
rate: 2 # Frames per second (optional)
scaling: 0.3 # Percentage of the original image size (optional)
quality: 60 # JPEG quality (0-100) (optional)
# Configuration specific for the connector that will connect this robot
connector_type: mir100
connector_config:
mir_host_address: localhost
mir_host_port: 80
mir_ws_port: 9090
mir_use_ssl: False
mir_username: username
mir_password: password
mir_api_version: v2.0
# Toggle InOrbit Mission Tracking features. https://developer.inorbit.ai/tutorials#mission-tracking-tutorial
# Mission Tracking features are not available on every InOrbit edition.
enable_mission_tracking: false
# Configuration for running user scripts
user_scripts:
# Folder where the user scripts are. If not set, "~/.inorbit_connectors/connector-<robot_id>/local/" will be used
path: /home/inorbit/mir100_connector/user_scripts_mir100-1
# Environment variables that will be available to the user scripts (Optional)
env_vars:
INORBIT_API_KEY: foo
MIR_API_ENDPOINT: 10.0.0.1:80
MIR_USERNAME: username
MIR_PASSWORD: password
10 changes: 8 additions & 2 deletions mir_connector/inorbit_mir_connector/config/mir100_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"cameras": [],
"connector_type": "mir100",
"connector_config": {
"mir_base_url": "http://localhost:80",
"mir_host_address": "localhost",
"mir_host_port": 80,
"mir_ws_port": 9090,
"mir_use_ssl": False,
"mir_username": "",
"mir_password": "",
"enable_mission_tracking": True,
Expand All @@ -35,7 +38,10 @@ class MiR100ConfigModel(BaseModel):
Specific configuration for MiR100 connector.
"""

mir_base_url: str
mir_host_address: str
mir_host_port: int
mir_ws_port: int
mir_use_ssl: bool
mir_username: str
mir_password: str
mir_api_version: str
Expand Down
33 changes: 25 additions & 8 deletions mir_connector/inorbit_mir_connector/src/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from time import sleep
import math
import os
import psutil
from inorbit_edge.robot import COMMAND_CUSTOM_COMMAND
from inorbit_edge.robot import COMMAND_MESSAGE
from inorbit_edge.robot import COMMAND_NAV_GOAL
from inorbit_edge.robot import RobotSession
from inorbit_edge.video import OpenCVCamera
from .mir_api import MirApiV2
from .mir_api import MirWebSocketV2
from .mission import MirInorbitMissionTracking
from ..config.mir100_model import MiR100Config

Expand Down Expand Up @@ -45,9 +45,19 @@ def __init__(self, robot_id: str, config: MiR100Config) -> None:

# Configure the connection to the robot
self.mir_api = MirApiV2(
mir_base_url=config.connector_config.mir_base_url,
mir_host_address=config.connector_config.mir_host_address,
mir_username=config.connector_config.mir_username,
mir_password=config.connector_config.mir_password,
mir_host_port=config.connector_config.mir_host_port,
mir_use_ssl=config.connector_config.mir_use_ssl,
loglevel=log_level,
)

# Configure the ws connection to the robot
self.mir_ws = MirWebSocketV2(
mir_host_address=config.connector_config.mir_host_address,
mir_ws_port=config.connector_config.mir_ws_port,
mir_use_ssl=config.connector_config.mir_use_ssl,
loglevel=log_level,
)

Expand Down Expand Up @@ -247,16 +257,22 @@ def start(self):
"mode_text": mode_text,
"robot_model": self.status["robot_model"],
"waiting_for": self.mission_tracking.waiting_for_text,
# NOTE: System stats come from the system where the connector is running,
# likely a server and not the robot itself
# TODO(b-Tomas): Report more system stats
# TODO(b-Tomas): Include this in the edge-sdk
"cpu": psutil.cpu_percent(), # System-wide load average since last call (0-100)
"memory_usage": psutil.virtual_memory().percent, # Memory usage percentage (0-100)
}
self.logger.debug(f"Publishing key values: {key_values}")
self.inorbit_sess.publish_key_values(key_values)

# Reporting system stats
# TODO(b-Tomas): Report more system stats

cpu_usage = self.mir_ws.get_cpu_usage()
disk_usage = self.mir_ws.get_disk_usage()
memory_usage = self.mir_ws.get_memory_usage()
self.inorbit_sess.publish_system_stats(
cpu_load_percentage=cpu_usage,
hdd_usage_percentage=disk_usage,
ram_usage_percentage=memory_usage,
)

# publish mission data
try:
self.mission_tracking.report_mission(self.status, self.metrics)
Expand All @@ -266,4 +282,5 @@ def start(self):
def stop(self):
"""Exit the Connector cleanly."""
self._should_run = False
self.mir_ws.disconnect()
self.inorbit_sess.disconnect()
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .mir_api_base import MirApiBaseClass # noqa: F401
from .mir_api_v2 import MirApiV2 # noqa: F401
from .mir_api_v2 import MirWebSocketV2 # noqa: F401
Loading

0 comments on commit f4410c2

Please sign in to comment.