-
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.
Merge branch 'ros2-rust:main' into better_error_handling
- Loading branch information
Showing
8 changed files
with
615 additions
and
12 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,16 @@ | ||
[package] | ||
name = "rust_pubsub" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name="simple_publisher" | ||
path="src/simple_publisher.rs" | ||
|
||
[[bin]] | ||
name="simple_subscriber" | ||
path="src/simple_subscriber.rs" | ||
|
||
[dependencies] | ||
rclrs = "*" | ||
std_msgs = "*" |
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,14 @@ | ||
<package format="3"> | ||
<name>rust_pubsub</name> | ||
<version>0.0.0</version> | ||
<description>TODO: Package description.</description> | ||
<maintainer email="user@todo.todo">user</maintainer> | ||
<license>TODO: License declaration.</license> | ||
|
||
<depend>rclrs</depend> | ||
<depend>std_msgs</depend> | ||
|
||
<export> | ||
<build_type>ament_cargo</build_type> | ||
</export> | ||
</package> |
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,36 @@ | ||
use rclrs::{create_node, Context, Node, Publisher, RclrsError, QOS_PROFILE_DEFAULT}; | ||
use std::{sync::Arc, thread, time::Duration}; | ||
use std_msgs::msg::String as StringMsg; | ||
struct SimplePublisherNode { | ||
node: Arc<Node>, | ||
_publisher: Arc<Publisher<StringMsg>>, | ||
} | ||
impl SimplePublisherNode { | ||
fn new(context: &Context) -> Result<Self, RclrsError> { | ||
let node = create_node(context, "simple_publisher").unwrap(); | ||
let _publisher = node | ||
.create_publisher("publish_hello", QOS_PROFILE_DEFAULT) | ||
.unwrap(); | ||
Ok(Self { node, _publisher }) | ||
} | ||
|
||
fn publish_data(&self, increment: i32) -> Result<i32, RclrsError> { | ||
let msg: StringMsg = StringMsg { | ||
data: format!("Hello World {}", increment), | ||
}; | ||
self._publisher.publish(msg).unwrap(); | ||
Ok(increment + 1_i32) | ||
} | ||
} | ||
|
||
fn main() -> Result<(), RclrsError> { | ||
let context = Context::new(std::env::args()).unwrap(); | ||
let publisher = Arc::new(SimplePublisherNode::new(&context).unwrap()); | ||
let publisher_other_thread = Arc::clone(&publisher); | ||
let mut count: i32 = 0; | ||
thread::spawn(move || loop { | ||
thread::sleep(Duration::from_millis(1000)); | ||
count = publisher_other_thread.publish_data(count).unwrap(); | ||
}); | ||
rclrs::spin(publisher.node.clone()) | ||
} |
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,52 @@ | ||
use rclrs::{create_node, Context, Node, RclrsError, Subscription, QOS_PROFILE_DEFAULT}; | ||
use std::{ | ||
env, | ||
sync::{Arc, Mutex}, | ||
thread, | ||
time::Duration, | ||
}; | ||
use std_msgs::msg::String as StringMsg; | ||
pub struct SimpleSubscriptionNode { | ||
node: Arc<Node>, | ||
_subscriber: Arc<Subscription<StringMsg>>, | ||
data: Arc<Mutex<Option<StringMsg>>>, | ||
} | ||
impl SimpleSubscriptionNode { | ||
fn new(context: &Context) -> Result<Self, RclrsError> { | ||
let node = create_node(context, "simple_subscription").unwrap(); | ||
let data: Arc<Mutex<Option<StringMsg>>> = Arc::new(Mutex::new(None)); | ||
let data_mut: Arc<Mutex<Option<StringMsg>>> = Arc::clone(&data); | ||
let _subscriber = node | ||
.create_subscription::<StringMsg, _>( | ||
"publish_hello", | ||
QOS_PROFILE_DEFAULT, | ||
move |msg: StringMsg| { | ||
*data_mut.lock().unwrap() = Some(msg); | ||
}, | ||
) | ||
.unwrap(); | ||
Ok(Self { | ||
node, | ||
_subscriber, | ||
data, | ||
}) | ||
} | ||
fn data_callback(&self) -> Result<(), RclrsError> { | ||
if let Some(data) = self.data.lock().unwrap().as_ref() { | ||
println!("{}", data.data); | ||
} else { | ||
println!("No message available yet."); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
fn main() -> Result<(), RclrsError> { | ||
let context = Context::new(env::args()).unwrap(); | ||
let subscription = Arc::new(SimpleSubscriptionNode::new(&context).unwrap()); | ||
let subscription_other_thread = Arc::clone(&subscription); | ||
thread::spawn(move || loop { | ||
thread::sleep(Duration::from_millis(1000)); | ||
subscription_other_thread.data_callback().unwrap() | ||
}); | ||
rclrs::spin(subscription.node.clone()) | ||
} |
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