diff --git a/Cargo.lock b/Cargo.lock index 6069b99..c113a5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "autocfg" version = "1.3.0" @@ -169,6 +218,12 @@ dependencies = [ "libloading", ] +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -192,6 +247,16 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + [[package]] name = "env_logger" version = "0.10.2" @@ -205,6 +270,19 @@ dependencies = [ "termcolor", ] +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -449,6 +527,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.11" @@ -608,7 +692,7 @@ dependencies = [ "bytes", "chrono", "derivative", - "env_logger", + "env_logger 0.10.2", "foreign-types", "futures", "gethostname", @@ -1076,6 +1160,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.9.1" @@ -1096,6 +1186,8 @@ name = "voraus-ros-bridge" version = "0.1.0" dependencies = [ "builtin_interfaces", + "env_logger 0.11.5", + "log", "opcua", "openssl", "rclrs", diff --git a/Cargo.toml b/Cargo.toml index 48481fa..9dae85e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/README.md b/README.md index f65d0cf..7acb7eb 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/main.rs b/src/main.rs index 85fc672..6a9d222 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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}; @@ -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::("enable_impedance_control", { @@ -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 = vec![]; match x { Variant::Array(unwrapped) => { @@ -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) } diff --git a/src/opc_ua_client.rs b/src/opc_ua_client.rs index e3ff8fa..b96d0cc 100644 --- a/src/opc_ua_client.rs +++ b/src/opc_ua_client.rs @@ -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, @@ -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(); @@ -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 = [node_id] @@ -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) {