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

Add tlog back, default to while arm #3181

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions core/services/ardupilot_manager/api/v1/routers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from fastapi_versioning import versioned_api_route
from loguru import logger

from ardupilot_manager.mavlink_proxy.AbstractRouter import TLogCondition
from autopilot_manager import AutoPilotManager
from exceptions import InvalidFirmwareFile
from typedefs import Firmware, FlightController, Parameters, Serial, SITLFrame, Vehicle
Expand Down Expand Up @@ -207,6 +208,18 @@ def preferred_router() -> Any:
return autopilot.load_preferred_router()


@index_router_v1.post("/tlog_condition", summary="Set the condition for when to write Tlog files.")
def set_tlog_condition(condition: TLogCondition) -> Any:
logger.debug("Setting Tlog condition")
autopilot.set_tlog_condition(condition)
logger.debug(f"Tlog write condition set to {condition}")


@index_router_v1.get("/tlog_condition", summary="Retrieve Tlog file write condition")
def tlog_condition() -> Any:
return autopilot.get_tlog_condition()


@index_router_v1.get("/available_routers", summary="Retrieve preferred router")
def available_routers() -> Any:
return autopilot.get_available_routers()
Expand Down
7 changes: 7 additions & 0 deletions core/services/ardupilot_manager/autopilot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from firmware.FirmwareManagement import FirmwareManager
from flight_controller_detector.Detector import Detector as BoardDetector
from flight_controller_detector.linux.linux_boards import LinuxFlightController
from mavlink_proxy.AbstractRouter import TLogCondition
from mavlink_proxy.Endpoint import Endpoint, EndpointType
from mavlink_proxy.exceptions import EndpointAlreadyExists
from mavlink_proxy.Manager import Manager as MavlinkManager
Expand Down Expand Up @@ -390,6 +391,12 @@ def load_preferred_router(self) -> Optional[str]:
def get_available_routers(self) -> List[str]:
return [router.name() for router in self.mavlink_manager.available_interfaces()]

def get_tlog_condition(self) -> TLogCondition:
return self.mavlink_manager.tlog_condition()

def set_tlog_condition(self, tlog_condition: TLogCondition) -> None:
self.mavlink_manager.set_tlog_condition(tlog_condition)

async def start_sitl(self) -> None:
self._current_board = BoardDetector.detect_sitl()
if not self.firmware_manager.is_firmware_installed(self._current_board):
Expand Down
15 changes: 15 additions & 0 deletions core/services/ardupilot_manager/mavlink_proxy/AbstractRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shlex
import shutil
import tempfile
from enum import Enum
from typing import Any, List, Optional, Set, Type

from loguru import logger
Expand All @@ -18,6 +19,13 @@
)


class TLogCondition(str, Enum):
"""When to write tlog files."""

Always = "always"
WhileArmed = "while_armed"


class AbstractRouter(metaclass=abc.ABCMeta):
def __init__(self) -> None:
self._endpoints: Set[Endpoint] = set()
Expand All @@ -28,6 +36,7 @@ def __init__(self) -> None:
# to avoid any problem in __del__
self._binary = shutil.which(self.binary_name())
self._logdir = pathlib.Path(tempfile.gettempdir())
self._tlog_condition = TLogCondition.WhileArmed
self._version = self._get_version()

@staticmethod
Expand Down Expand Up @@ -182,6 +191,12 @@ def set_logdir(self, directory: pathlib.Path) -> None:
raise ValueError(f"Logging directory {directory} does not exist.")
self._logdir = directory

def tlog_condition(self) -> TLogCondition:
return self._tlog_condition

def set_tlog_condition(self, tlog_condition: TLogCondition) -> None:
self._tlog_condition = tlog_condition

def add_endpoint(self, endpoint: Endpoint) -> None:
self._validate_endpoint(endpoint)

Expand Down
21 changes: 19 additions & 2 deletions core/services/ardupilot_manager/mavlink_proxy/MAVLinkRouter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import configparser
import re
import subprocess
import tempfile
from typing import Optional

from mavlink_proxy.AbstractRouter import AbstractRouter
from mavlink_proxy.AbstractRouter import AbstractRouter, TLogCondition
from mavlink_proxy.Endpoint import Endpoint, EndpointType


Expand All @@ -21,6 +23,21 @@ def _get_version(self) -> Optional[str]:

return None

def _write_configuration_file(self) -> str:
def convert_tlog_condition(tlog_condition: TLogCondition) -> str:
match tlog_condition:
case TLogCondition.Always:
return "always"
case TLogCondition.WhileArmed:
return "while-armed"

config = configparser.ConfigParser()
config["General"] = {"LogMode": convert_tlog_condition(self.tlog_condition())}

with tempfile.NamedTemporaryFile("w", suffix=".conf", delete=False) as temp_file:
config.write(temp_file, space_around_delimiters=True)
return temp_file.name

def assemble_command(self, master_endpoint: Endpoint) -> str:
# Convert endpoint format to mavlink-router format
def convert_endpoint(endpoint: Endpoint) -> str:
Expand Down Expand Up @@ -63,7 +80,7 @@ def convert_endpoint(endpoint: Endpoint) -> str:
f"Master endpoint of type {master_endpoint.connection_type} not supported on MavlinkRouter."
)

return f"{self.binary()} {convert_endpoint(master_endpoint)} {endpoints} -l {self.logdir()} -T {self.logdir()}"
return f"{self.binary()} {convert_endpoint(master_endpoint)} {endpoints} -l {self.logdir()} -T {self.logdir()} -c {self._write_configuration_file()}"

@staticmethod
def name() -> str:
Expand Down
14 changes: 12 additions & 2 deletions core/services/ardupilot_manager/mavlink_proxy/MAVLinkServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from typing import Optional

from mavlink_proxy.AbstractRouter import AbstractRouter
from mavlink_proxy.AbstractRouter import AbstractRouter, TLogCondition
from mavlink_proxy.Endpoint import Endpoint, EndpointType


Expand Down Expand Up @@ -37,7 +37,17 @@ def convert_endpoint(endpoint: Endpoint) -> str:
return f"zenoh:{endpoint.place}:{endpoint.argument}"
raise ValueError(f"Endpoint of type {endpoint.connection_type} not supported on MAVLink-Server.")

endpoints = " ".join([convert_endpoint(endpoint) for endpoint in [master_endpoint, *self.endpoints()]])
def convert_tlog_condition(tlog_condition: TLogCondition) -> str:
match tlog_condition:
case TLogCondition.Always:
return "?when=always"
case TLogCondition.WhileArmed:
return "?when=while_armed"

tlog_condition_arg = convert_tlog_condition(self.tlog_condition())
logging_endpoint = f"tlogwriter://{self.logdir()}{tlog_condition_arg}"
str_endpoints = [convert_endpoint(endpoint) for endpoint in [master_endpoint, *self.endpoints()]]
endpoints = " ".join([*str_endpoints, logging_endpoint])

return f"{self.binary()} {endpoints}"

Expand Down
8 changes: 7 additions & 1 deletion core/services/ardupilot_manager/mavlink_proxy/Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import mavlink_proxy.MAVLinkServer
import mavlink_proxy.MAVP2P
import mavlink_proxy.MAVProxy
from mavlink_proxy.AbstractRouter import AbstractRouter
from mavlink_proxy.AbstractRouter import AbstractRouter, TLogCondition
from mavlink_proxy.Endpoint import Endpoint
from mavlink_proxy.exceptions import (
EndpointAlreadyExists,
Expand Down Expand Up @@ -171,6 +171,12 @@ def router_name(self) -> str:
def set_logdir(self, log_dir: pathlib.Path) -> None:
self.tool.set_logdir(log_dir)

def tlog_condition(self) -> TLogCondition:
return self.tool.tlog_condition()

def set_tlog_condition(self, tlog_condtion: TLogCondition) -> None:
self.tool.set_tlog_condition(tlog_condtion)

async def auto_restart_router(self) -> None:
"""Auto-restart Mavlink router process if it dies."""
while True:
Expand Down
2 changes: 1 addition & 1 deletion core/tools/mavlink_server/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Immediately exit on errors
set -e

VERSION="0.3.1"
VERSION="0.3.2"
PROJECT_NAME="mavlink-server"
REPOSITORY_ORG="bluerobotics"
REPOSITORY_NAME="$PROJECT_NAME"
Expand Down