Files
NODEDC_MISSION_CORE/plugins/vesc/runtime/drive_diagnostics.py
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

86 lines
4.8 KiB
Python

"""No-I/O recorder for existing reads/commands; opt-in never changes ownership."""
try:
from .bounded_diagnostics import BoundedDiagnostics
except ModuleNotFoundError:
import runpy
from pathlib import Path
BoundedDiagnostics=runpy.run_path(str(Path(__file__).resolve().parents[3]/
"packages/plugin-sdk/python/missioncore_plugin_sdk/bounded_diagnostics.py"))["BoundedDiagnostics"]
try:
from .diagnostic_options import LEGACY_OPTIONS, diagnostic_options
except ModuleNotFoundError:
import runpy
from pathlib import Path
_options=runpy.run_path(str(Path(__file__).resolve().parents[3]/
"packages/plugin-sdk/python/missioncore_plugin_sdk/diagnostic_options.py"))
LEGACY_OPTIONS,diagnostic_options=_options["LEGACY_OPTIONS"],_options["diagnostic_options"]
class DriveDiagnostics(BoundedDiagnostics):
def __init__(self,root):
super().__init__('vesc')
from .session_log import SessionLog
self.log=SessionLog(root)
self.stop_after=None
self.options=dict(LEGACY_OPTIONS)
def add(self,event,**data):
if not self.active():return
row=super().add(event,**data)
if row is not None:
try:self.log.append(row)
except Exception:self.rejected+=1
def exchange(self,body):
if not isinstance(body,dict) or set(body)-{'id','enabled','after','vehicle','core_events','relay_events','export','stop_after','options'}:
raise ValueError('Invalid diagnostic request')
if type(body.get('enabled')) is not bool or type(body.get('after')) is not int or body['after']<0:
raise ValueError('Invalid diagnostic state')
stop_after=body.get('stop_after')
if 'stop_after' in body and (type(stop_after) is not int or not 0<=stop_after<=2**53-1 or body['enabled']):
raise ValueError('Invalid diagnostic stop cursor')
options=diagnostic_options(body.get('options'))
if body['id']==self.id and options!=self.options:
raise ValueError('Diagnostic capture options changed')
previous=self.id
self.options=options
if body['enabled'] or body['id']!=self.id:
try:self.log.start(body['id'],body.get('vehicle','Rover'),create=body['enabled'],options=options)
except OSError as error:
self.log.id=body['id'];self.log.error=type(error).__name__;self.log.closed=True
self.configure(body['id'],False)
return {**self.snapshot(body['after'],24),'id':body['id'],'storage':self.log.status()}
if previous!=body['id']:self.stop_after=None
if stop_after is not None:
if self.stop_after is not None and self.stop_after!=stop_after:
raise ValueError('Diagnostic stop cursor changed')
self.stop_after=stop_after
stop_after=self.stop_after
if options['core_trace']:self.log.external('core',body.get('core_events',[]))
self.log.external('node',body.get('relay_events',[]))
if not body['enabled'] and self.active():self.add('capture_stopped')
self.configure(body['id'],body['enabled'] and stop_after is None and not self.log.closed and not self.log.closing and not self.log.error)
# Stop acquisition now; preserve only the already-created Core/browser
# tail until its final sequence has been accepted by the disk writer.
draining=stop_after is not None and self.log.external_ack.get('core',0)<stop_after
if (not body['enabled'] or stop_after is not None) and not draining:self.log.finish()
if previous!=self.id:
from . import VERSION
self.add('capture_started',runtime_version=VERSION,options=options)
result={**self.snapshot(body['after'],0 if stop_after is not None or not options['live'] else 24),
'storage':self.log.status(),'stop_tail_v1':True,'options':dict(options)}
if stop_after is not None:result['stop_after']=stop_after
if body.get('export'):
spec=body['export']
result['export']={**self.log.chunk(body['id'],spec['offset'],spec.get('total_bytes'),
limit=spec.get('chunk_bytes',4096),snapshot=spec.get('snapshot')),'token':spec.get('token')}
return result
def sample(self,device,value,receiver,owner):
if not self.active() or not self.options['motor_samples']:return
self.add('sample',device_id=device.id,device_session=device.session,
owner=owner,receiver=receiver,control=getattr(getattr(device,"link",None),"last_rpc",{}).get("control"),
can_bridge=getattr(getattr(device,"link",None),"last_rpc",{}).get("can_bridge"),values={k:value.get(k) for k in (
'motor_current_a','input_current_a','input_voltage_v','erpm','duty','fault_code',
'tachometer','tachometer_abs','id_current_a','iq_current_a','vd_v','vq_v','mos_temperature_c')})