-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rclcpp-like and rclpy-like executor
- Loading branch information
Showing
6 changed files
with
192 additions
and
38 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
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,84 @@ | ||
use std::env; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use anyhow::{Error, Result}; | ||
|
||
struct MinimalSubscriberInner { | ||
num_messages: u32, | ||
node: Arc<rclrs::Node>, | ||
subscription: Option<Arc<rclrs::Subscription<std_msgs::msg::String>>>, | ||
} | ||
|
||
struct MinimalSubscriber { | ||
inner: RwLock<MinimalSubscriberInner>, | ||
} | ||
|
||
impl MinimalSubscriber { | ||
pub fn new(name: &str, topic: &str) -> Result<Arc<Self>, rclrs::RclrsError> { | ||
let context = rclrs::Context::new(env::args())?; | ||
let node = rclrs::create_node(&context, name)?; | ||
let minimal_subscriber = Arc::new(MinimalSubscriber { | ||
inner: RwLock::new(MinimalSubscriberInner { | ||
num_messages: 0, | ||
node: Arc::clone(&node), | ||
subscription: None, | ||
}), | ||
}); | ||
|
||
let minimal_subscriber_aux = Arc::clone(&minimal_subscriber); | ||
let subscription = node.create_subscription::<std_msgs::msg::String, _>( | ||
topic, | ||
rclrs::QOS_PROFILE_DEFAULT, | ||
move |msg: std_msgs::msg::String| { | ||
minimal_subscriber_aux.callback(msg); | ||
}, | ||
)?; | ||
minimal_subscriber.inner.write().unwrap().subscription = Some(subscription); | ||
Ok(minimal_subscriber) | ||
} | ||
|
||
fn callback(&self, msg: std_msgs::msg::String) { | ||
self.inner.write().unwrap().num_messages += 1; | ||
println!("[{}] I heard: '{}'", self.node().name(), msg.data); | ||
println!( | ||
"[{}] (Got {} messages so far)", | ||
self.node().name(), | ||
self.inner.read().unwrap().num_messages | ||
); | ||
} | ||
|
||
pub fn node(&self) -> Arc<rclrs::Node> { | ||
Arc::clone(&self.inner.read().unwrap().node) | ||
} | ||
} | ||
|
||
fn main() -> Result<(), Error> { | ||
let publisher_context = rclrs::Context::new(env::args())?; | ||
let publisher_node = rclrs::create_node(&publisher_context, "minimal_publisher")?; | ||
|
||
let subscriber_node_one = MinimalSubscriber::new("minimal_subscriber_one", "topic")?; | ||
let subscriber_node_two = MinimalSubscriber::new("minimal_subscriber_two", "topic")?; | ||
|
||
let publisher = publisher_node | ||
.create_publisher::<std_msgs::msg::String>("topic", rclrs::QOS_PROFILE_DEFAULT)?; | ||
|
||
std::thread::spawn(move || -> Result<(), rclrs::RclrsError> { | ||
let mut message = std_msgs::msg::String::default(); | ||
let mut publish_count: u32 = 1; | ||
loop { | ||
message.data = format!("Hello, world! {}", publish_count); | ||
println!("Publishing: [{}]", message.data); | ||
publisher.publish(&message)?; | ||
publish_count += 1; | ||
std::thread::sleep(std::time::Duration::from_millis(500)); | ||
} | ||
}); | ||
|
||
let executor = rclrs::SingleThreadedExecutor::new(); | ||
|
||
executor.add_node(&publisher_node)?; | ||
executor.add_node(&subscriber_node_one.node())?; | ||
executor.add_node(&subscriber_node_two.node())?; | ||
|
||
executor.spin().map_err(|err| err.into()) | ||
} |
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
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