-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
canbus_stats: Periodically report canbus interface statistics
Add support or a new get_canbus_status command to canserial.c . Add new canbus_stats.py module that will periodically query canbus mcus for connection status information. Signed-off-by: Kevin O'Connor <[email protected]>
- Loading branch information
1 parent
03c3054
commit ee9814f
Showing
4 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Report canbus connection status | ||
# | ||
# Copyright (C) 2025 Kevin O'Connor <[email protected]> | ||
# | ||
# This file may be distributed under the terms of the GNU GPLv3 license. | ||
|
||
class PrinterCANBusStats: | ||
def __init__(self, config): | ||
self.printer = config.get_printer() | ||
self.name = config.get_name().split()[-1] | ||
self.mcu = None | ||
self.get_canbus_status_cmd = None | ||
self.status = {'rx_error': None, 'tx_error': None, 'bus_state': None} | ||
self.printer.register_event_handler("klippy:connect", self._connect) | ||
def _connect(self): | ||
# Lookup mcu | ||
mcu_name = self.name | ||
if mcu_name != 'mcu': | ||
mcu_name = 'mcu ' + mcu_name | ||
self.mcu = self.printer.lookup_object(mcu_name) | ||
# Lookup status query command | ||
if self.mcu.try_lookup_command("get_canbus_status") is None: | ||
return | ||
self.get_canbus_status_cmd = self.mcu.lookup_query_command( | ||
"get_canbus_status", | ||
"canbus_status rx_error=%u tx_error=%u canbus_bus_state=%u") | ||
# Register periodic query timer | ||
reactor = self.printer.get_reactor() | ||
reactor.register_timer(self.query_event, reactor.NOW) | ||
def query_event(self, eventtime): | ||
params = self.get_canbus_status_cmd.send() | ||
rx = params['rx_error'] | ||
tx = params['tx_error'] | ||
state = params['canbus_bus_state'] | ||
self.status = {'rx_error': rx, 'tx_error': tx, 'bus_state': state} | ||
return eventtime + 1. | ||
def stats(self, eventtime): | ||
status = self.status | ||
if status['rx_error'] is None: | ||
return (False, '') | ||
return (False, 'canstat_%s: rx_error=%d tx_error=%d bus_state=%s' | ||
% (self.name, status['rx_error'], status['tx_error'], | ||
status['bus_state'])) | ||
def get_status(self, eventtime): | ||
return self.status | ||
|
||
def load_config_prefix(config): | ||
return PrinterCANBusStats(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
// | ||
// Copyright (C) 2019 Eug Krashtan <[email protected]> | ||
// Copyright (C) 2020 Pontus Borg <[email protected]> | ||
// Copyright (C) 2021 Kevin O'Connor <[email protected]> | ||
// Copyright (C) 2021-2025 Kevin O'Connor <[email protected]> | ||
// | ||
// This file may be distributed under the terms of the GNU GPLv3 license. | ||
|
||
|
@@ -318,6 +318,22 @@ DECL_TASK(canserial_rx_task); | |
* Setup and shutdown | ||
****************************************************************/ | ||
|
||
DECL_ENUMERATION("canbus_bus_state", "active", CANBUS_STATE_ACTIVE); | ||
DECL_ENUMERATION("canbus_bus_state", "warn", CANBUS_STATE_WARN); | ||
DECL_ENUMERATION("canbus_bus_state", "passive", CANBUS_STATE_PASSIVE); | ||
DECL_ENUMERATION("canbus_bus_state", "off", CANBUS_STATE_OFF); | ||
|
||
void | ||
command_get_canbus_status(uint32_t *args) | ||
{ | ||
struct canbus_status status; | ||
canhw_get_status(&status); | ||
sendf("canbus_status rx_error=%u tx_error=%u canbus_bus_state=%u" | ||
, status.rx_error, status.tx_error, status.bus_state); | ||
} | ||
DECL_COMMAND_FLAGS(command_get_canbus_status, HF_IN_SHUTDOWN | ||
, "get_canbus_status"); | ||
|
||
void | ||
command_get_canbus_id(uint32_t *args) | ||
{ | ||
|