-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Implement helper ros_subscriber
Co-authored-by: Jan-Niklas Burfeind <[email protected]>
- Loading branch information
1 parent
85b3ca4
commit ca526b5
Showing
1 changed file
with
57 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use rclrs::{create_node, Context, Node, RclrsError, Subscription, QOS_PROFILE_DEFAULT}; | ||
use sensor_msgs::msg::JointState as JointStateMsg; | ||
use std::{ | ||
env, | ||
sync::{ | ||
atomic::{AtomicU32, Ordering}, | ||
Arc, Mutex, | ||
}, | ||
}; | ||
|
||
pub struct Subscriber { | ||
pub num_messages: AtomicU32, | ||
pub data: Arc<Mutex<Option<JointStateMsg>>>, | ||
pub node: Arc<Node>, | ||
subscription: Mutex<Option<Arc<Subscription<JointStateMsg>>>>, | ||
} | ||
|
||
impl Subscriber { | ||
pub fn new(name: &str, topic: &str) -> Result<Arc<Self>, RclrsError> { | ||
let context = Context::new(env::args())?; | ||
let node = create_node(&context, name)?; | ||
let subscriber = Arc::new(Subscriber { | ||
num_messages: 0.into(), | ||
data: Arc::new(Mutex::new(None)), | ||
node, | ||
subscription: None.into(), | ||
}); | ||
|
||
let minimal_subscriber_aux = Arc::clone(&subscriber); | ||
let subscription = subscriber.node.create_subscription::<JointStateMsg, _>( | ||
topic, | ||
QOS_PROFILE_DEFAULT, | ||
move |msg: JointStateMsg| { | ||
minimal_subscriber_aux.callback(msg); | ||
}, | ||
)?; | ||
*subscriber.subscription.lock().unwrap() = Some(subscription); | ||
Ok(subscriber) | ||
} | ||
fn callback(&self, msg: JointStateMsg) { | ||
self.num_messages.fetch_add(1, Ordering::SeqCst); | ||
println!("msg {}", msg.clone().position.first().unwrap()); | ||
*self.data.lock().unwrap() = Some(msg); | ||
|
||
println!( | ||
"data {}", | ||
self.data | ||
.lock() | ||
.unwrap() | ||
.as_mut() | ||
.unwrap() | ||
.position | ||
.first() | ||
.unwrap() | ||
); | ||
} | ||
} |