Example for monitoring different statuses of a robot using the CreOS SDK client library.
The System Monitor tool is a CLI tool that uses the CreOS SDK client library to monitor different statuses of an Avular robot. You can install the System Monitor by installing the creos-system-monitor package and its dependencies as follows, replacing <version> with the version number of the CreOS SDK you want to install, and <architecture> with the architecture of your system (e.g., amd64, arm64):
Below you can find all C++ source files for this tool. The tool is split into four different notifiers, each responsible for monitoring a different status of the robot:
Each notifier subscribes or pulls a value from one or more data sources, and prints the value to the console. They also only print values if they have changed since the last time they were printed. The main.cpp file contains the main function that creates a Client instance and initializes the notifiers using interface instances provided by this client.
15#include <spdlog/spdlog.h>
16#include <creos/client.hpp>
17#include <creos/messages/state.hpp>
21#include "battery_notifier.hpp"
22#include "control_source_notifier.hpp"
23#include "state_notifier.hpp"
24#include "system_info.hpp"
26int main(
int argc,
char* argv[]) {
29 std::unique_ptr<creos::Client> client;
32 std::string host = argv[1];
33 client = std::make_unique<creos::Client>(host);
36 client = std::make_unique<creos::Client>();
39 get_system_info(client->system_info());
40 battery_notifier(client->system_info());
41 state_notifier(client->diagnostics());
42 control_source_notifier(client->setpoint_control());
12#include <spdlog/spdlog.h>
13#include <creos/messages/battery_status.hpp>
14#include <creos/system_info_interface.hpp>
20 bool printed_header =
false;
21 auto print_header = [&printed_header]() {
22 if (!printed_header) {
23 spdlog::info(
"Battery status:");
24 printed_header =
true;
28 auto fuzzy_compare = [&](
float a,
float b,
float diff) ->
bool {
29 return (std::abs(a - b) >= diff && !std::isnan(a) && !std::isnan(b)) || first_call;
32 if (battery_status.state != previous_battery_status.state || first_call) {
34 std::string state =
"";
35 switch (battery_status.state) {
46 state =
"Discharging";
55 throw std::runtime_error(
"Unknown battery state");
57 spdlog::info(
" - State: {}", state);
58 previous_battery_status.state = battery_status.state;
61 if (fuzzy_compare(battery_status.voltage, previous_battery_status.voltage, 0.3f)) {
63 spdlog::info(
" - Voltage: {} V", battery_status.voltage);
64 previous_battery_status.voltage = battery_status.voltage;
67 if (fuzzy_compare(battery_status.state_of_charge, previous_battery_status.state_of_charge, 0.01f)) {
69 spdlog::info(
" - State of charge: {:.2f}%", battery_status.state_of_charge * 100);
70 previous_battery_status.state_of_charge = battery_status.state_of_charge;
73 if (fuzzy_compare(battery_status.state_of_health, previous_battery_status.state_of_health, 0.01f)) {
75 spdlog::info(
" - State of health: {:.2f}%", battery_status.state_of_health * 100);
76 previous_battery_status.state_of_health = battery_status.state_of_health;
79 if (fuzzy_compare(battery_status.temperature, previous_battery_status.temperature, 0.5f)) {
81 spdlog::info(
" - Temperature: {} °C", battery_status.temperature);
82 previous_battery_status.temperature = battery_status.temperature;
85 if (battery_status.alerts != previous_battery_status.alerts || first_call) {
87 spdlog::info(
" - Alerts:");
88 previous_battery_status.alerts = battery_status.alerts;
90 bool has_alerts =
false;
91 if (battery_status.alerts.cell_under_voltage) {
92 spdlog::info(
" - Cell under voltage");
95 if (battery_status.alerts.cell_over_voltage) {
96 spdlog::info(
" - Cell over voltage");
99 if (battery_status.alerts.over_current_charging) {
100 spdlog::info(
" - Over current charging");
103 if (battery_status.alerts.over_current_discharging) {
104 spdlog::info(
" - Over current discharging");
107 if (battery_status.alerts.overload_discharging) {
108 spdlog::info(
" - Overload discharging");
111 if (battery_status.alerts.latched_overload_discharging) {
112 spdlog::info(
" - Latched overload discharging");
115 if (battery_status.alerts.short_circuit) {
116 spdlog::info(
" - Short circuit");
119 if (battery_status.alerts.latched_short_circuit) {
120 spdlog::info(
" - Latched short circuit");
123 if (battery_status.alerts.over_temperature_charging) {
124 spdlog::info(
" - Over temperature charging");
127 if (battery_status.alerts.over_temperature_discharging) {
128 spdlog::info(
" - Over temperature discharging");
131 if (battery_status.alerts.under_temperature_charging) {
132 spdlog::info(
" - Under temperature charging");
135 if (battery_status.alerts.under_temperature_discharging) {
136 spdlog::info(
" - Under temperature discharging");
139 if (battery_status.alerts.afe_alert) {
140 spdlog::info(
" - AFE alert");
143 if (battery_status.alerts.precharge_timeout) {
144 spdlog::info(
" - Precharge timeout");
147 if (battery_status.alerts.overcharge) {
148 spdlog::info(
" - Overcharge");
152 spdlog::info(
" - No alerts active");
The interface for retrieving general system information of an Avular robot.
Definition system_info_interface.hpp:28
virtual SubscriptionId subscribeToBatteryStatus(const std::function< void(const creos_messages::BatteryStatus &)> &callback)=0
Subscribe to battery status updates.
BatteryStatus message contains the current battery state.
Definition battery_status.hpp:28
@ kError
The battery is in an error state.
Definition battery_status.hpp:36
@ kOffline
The battery is offline or not connected.
Definition battery_status.hpp:32
@ kUnknown
The state is unknown.
Definition battery_status.hpp:31
@ kBalancing
The battery is balancing between cells.
Definition battery_status.hpp:35
@ kDischarging
The battery is discharging.
Definition battery_status.hpp:34
@ kCharging
The battery is charging.
Definition battery_status.hpp:33
11#include <spdlog/spdlog.h>
12#include <creos/messages/control_source.hpp>
13#include <creos/setpoint_control_interface.hpp>
19 if (control_source != previous_control_source) {
20 std::string control_source_str =
"";
21 switch (control_source) {
23 control_source_str =
"Disabled";
26 control_source_str =
"Manual";
29 control_source_str =
"Autonomous";
32 control_source_str =
"User";
35 throw std::runtime_error(
"Unknown control source");
38 spdlog::info(
"Control source: {}", control_source_str);
40 previous_control_source = control_source;
The interface for sending control setpoints to an Avular robot.
Definition setpoint_control_interface.hpp:30
virtual SubscriptionId subscribeToCurrentControlSource(const std::function< void(const creos_messages::ControlSource &)> &callback)=0
Subscribe to the current control source of the robot.
ControlSource
Control source message containing the control source of the robot.
Definition control_source.hpp:28
@ kAutonomous
The robot is controlled autonomously.
@ kUser
The robot is controlled by a user.
@ kDisabled
The robot is disabled.
@ kManual
The robot is controlled manually.
11#include <spdlog/spdlog.h>
12#include <creos/diagnostics_interface.hpp>
13#include <creos/messages/state.hpp>
20 std::string ready_state =
"";
23 ready_state =
"Unknown";
26 ready_state =
"NotReady";
29 ready_state =
"PreArm";
32 ready_state =
"Active";
35 ready_state =
"Waiting";
38 ready_state =
"Failsafe";
41 ready_state =
"Error";
44 throw std::runtime_error(
"Unknown ready state");
49 previous_state = state;
The interface for retrieving diagnostic information from an Avular robot.
Definition diagnostics_interface.hpp:28
virtual SubscriptionId subscribeToState(const std::function< void(const creos_messages::State &)> &callback)=0
Subscribe to state information of the robot.
The state of the robot.
Definition state.hpp:28
std::string current_action
A textual description of the current action the robot is performing.
Definition state.hpp:52
Ready ready_state
The ready state of the robot.
Definition state.hpp:47
@ kUnknown
The ready state is unknown.
@ kFailsafe
A safety system has been triggered, and the robot is in a failsafe state.
@ kActive
The robot is ready to perform operations.
@ kNotReady
The robot is not ready to perform operations. e.g. when the robot is booting.
@ kError
The robot has a platform error and is not able to perform operations. This should not be used for sof...
@ kWaiting
The robot is operational, but waiting for user input.
@ kPreArm
The robot is in a safety state where checks are being performed. User interaction may be required.