Skip to content

Commit

Permalink
feat: Replace println with proper logging
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-caspers committed Sep 18, 2024
1 parent 27b94c2 commit d42c123
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
94 changes: 93 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ voraus_interfaces = { version = "0.1.0" }
rclrs = { version = "0.4.1" }
rosidl_runtime_rs = { version = "0.4.1" }
tokio = "1.38.0"
log = "0.4.22"
env_logger = "0.11.5"

[dev-dependencies]
rclrs = { version = "0.4.1" }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Run `cargo build --release`
### Run the voraus-ros-bridge

Run `cargo run --release`
In order to get log output, run `RUST_OPCUA_LOG=INFO cargo run --release`

## via ROS:

Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
mod opc_ua_client;

use opc_ua_client::OPCUAClient;
use opcua::types::Variant;
use rclrs::{create_node, Context, RclrsError};
use opc_ua_client::OPCUAClient;
use std::{env, sync::{Arc, Mutex}};
use ros_services::ROSServices;
use std::{
env,
sync::{Arc, Mutex},
};

mod ros_publisher;
mod ros_services;
use log::{debug, info};

use ros_publisher::{create_joint_state_msg, RosPublisher};

Expand All @@ -23,7 +27,7 @@ fn main() -> Result<(), RclrsError> {
let Ok(_connection_result) = opc_ua_client.lock().unwrap().connect() else {
panic!("Connection could not be established, but is required.");
};

let ros_services = Arc::new(ROSServices::new(Arc::clone(&opc_ua_client)));
let _enable_impedance_control =
node_copy.create_service::<std_srvs::srv::Empty, _>("enable_impedance_control", {
Expand All @@ -34,7 +38,7 @@ fn main() -> Result<(), RclrsError> {
let callback = {
let provider = Arc::clone(&joint_state_publisher);
move |x: Variant| {
println!("Value = {:?}", &x);
debug!("Value = {:?}", &x);
let mut data_value: Vec<f64> = vec![];
match x {
Variant::Array(unwrapped) => {
Expand All @@ -59,8 +63,8 @@ fn main() -> Result<(), RclrsError> {
.create_subscription(1, "100111", callback, 10)
.expect("ERROR: Got an error while subscribing to variables");
// Loops forever. The publish thread will call the callback with changes on the variables
println!("Starting OPC UA client");
info!("Starting OPC UA client");
let _session = opc_ua_client.lock().unwrap().run_async();
println!("Spinning ROS");
info!("Spinning ROS");
rclrs::spin(node_copy)
}
11 changes: 6 additions & 5 deletions src/opc_ua_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use opcua::types::{
CallMethodRequest, MessageSecurityMode, MonitoredItemCreateRequest, NodeId, StatusCode, TimestampsToReturn, UserTokenPolicy, Variant
};
use tokio::sync::oneshot;
use log::debug;

pub struct OPCUAClient {
endpoint: String,
Expand Down Expand Up @@ -71,8 +72,8 @@ impl OPCUAClient {
if self.session.is_none() {
panic!("Not connected. Can't create subscriptions.");
}
println!(
"Creating a subscription for ns={};{} to indirectly call the callback every {}ms.",
debug!(
"Creating a subscription for ns={};{} to indirectly call the callback every {} ms.",
namespace, node_id, period_ms
);
let cloned_session_lock = self.session.clone().unwrap();
Expand All @@ -93,13 +94,13 @@ impl OPCUAClient {
priority,
publishing_enabled,
DataChangeCallback::new(move |changed_monitored_items| {
println!("Data change from server:");
debug!("Data change from server:");
changed_monitored_items
.iter()
.for_each(|item| callback(extract_value(item)));
}),
)?;
println!("Created a subscription with id = {}", subscription_id);
debug!("Created a subscription with id = {}", subscription_id);

// Create some monitored items
let items_to_create: Vec<MonitoredItemCreateRequest> = [node_id]
Expand Down Expand Up @@ -128,7 +129,7 @@ impl OPCUAClient {
input_arguments: None,
};
let result = session.call(method).unwrap();
println!("result of call: {:?}", result);
debug!("result of call: {:?}", result);
}

pub fn run(&self) {
Expand Down

0 comments on commit d42c123

Please sign in to comment.