Skip to content

Commit fb88464

Browse files
Update SimplePublisher and SimpleSubscriber WPF samples with more explicit handling of the ROS node lifecycle w.r.t. the WPF lifecycle
1 parent 06d492d commit fb88464

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

ROS_dotNET_ROS_SAMPLES.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ EndProject
3434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplePublisher", "SimplePublisher\SimplePublisher\SimplePublisher.csproj", "{669CB812-A371-4374-97DE-1857BF34A886}"
3535
EndProject
3636
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleSubscriber", "SimpleSubscriber\SimpleSubscriber\SimpleSubscriber.csproj", "{24AE8542-22EA-4D2A-B589-0882AF18E80B}"
37+
ProjectSection(ProjectDependencies) = postProject
38+
{8B7DB66C-9C03-4078-9276-B6A760CD4C2C} = {8B7DB66C-9C03-4078-9276-B6A760CD4C2C}
39+
EndProjectSection
3740
EndProject
3841
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ROS_ImageWPF", "ROS_ImageUtils\ROS_ImageWPF.csproj", "{E0E04AFE-E2CF-4A04-A331-76FE05DCC749}"
3942
EndProject

SimplePublisher/SimplePublisher/MainWindow.xaml.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,49 @@ namespace SimplePublisher
2424
/// </summary>
2525
public partial class MainWindow : Window
2626
{
27-
Publisher<Messages.tf.tfMessage> pub;
27+
Publisher<Messages.std_msgs.String> pub;
2828
NodeHandle nh;
29+
30+
private bool closing;
31+
private Thread pubthread;
32+
2933
public MainWindow()
3034
{
3135
InitializeComponent();
3236

33-
ROS.Init(new string[0], "simplePublisher");
37+
ROS.Init(new string[0], "wpf_talker");
3438
nh = new NodeHandle();
3539

36-
pub = nh.advertise<Messages.tf.tfMessage>("/tf_test", 1000, true);
40+
pub = nh.advertise<Messages.std_msgs.String>("/chatter", 1, false);
3741

38-
new Thread(() =>
42+
pubthread = new Thread(() =>
3943
{
4044
int i = 0;
41-
while (ROS.ok)
45+
Messages.std_msgs.String msg;
46+
while (ROS.ok && !closing)
4247
{
43-
Messages.tf.tfMessage msg = new Messages.tf.tfMessage();
44-
msg.transforms = new Messages.geometry_msgs.TransformStamped[1];
45-
msg.transforms[0] = new Messages.geometry_msgs.TransformStamped();
46-
msg.transforms[0].header.seq = (uint)i++;
47-
pub.publish(msg);
48-
Thread.Sleep(100);
48+
msg = new Messages.std_msgs.String("foo " + (i++));
49+
pub.publish(msg);
4950
Dispatcher.Invoke(new Action(() =>
50-
{
51-
l.Content = "Sending: " + msg.transforms[0].header.seq;
52-
}));
51+
{
52+
l.Content = "Sending: " + msg.data;
53+
}),new TimeSpan(0,0,1));
54+
Thread.Sleep(100);
5355
}
54-
}).Start();
55-
56+
});
57+
pubthread.Start();
58+
}
59+
60+
protected override void OnClosed(EventArgs e)
61+
{
62+
if (!closing)
63+
{
64+
closing = true;
65+
pubthread.Join();
66+
}
67+
ROS.shutdown();
68+
ROS.waitForShutdown();
69+
base.OnClosed(e);
5670
}
5771
}
5872
}

SimplePublisher/SimplePublisher/SimplePublisher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<DebugType>full</DebugType>
2424
<Optimize>false</Optimize>
2525
<OutputPath>bin\Debug\</OutputPath>
26-
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<DefineConstants>DEBUG</DefineConstants>
2727
<ErrorReport>prompt</ErrorReport>
2828
<WarningLevel>4</WarningLevel>
2929
</PropertyGroup>

SimpleSubscriber/SimpleSubscriber/MainWindow.xaml.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,30 @@ public partial class MainWindow : Window
2626
{
2727
Subscriber<Messages.std_msgs.String> sub;
2828
NodeHandle nh;
29-
DateTime before;
29+
3030
public MainWindow()
3131
{
32-
before = DateTime.Now;
3332
InitializeComponent();
3433

35-
ROS.ROS_MASTER_URI = "http://10.0.2.226:11311";
36-
ROS.ROS_HOSTNAME = "10.0.2.226";
37-
ROS.Init(new string[0], "simpleSubscriber");
34+
ROS.Init(new string[0], "wpf_listener");
3835
nh = new NodeHandle();
3936

40-
sub = nh.subscribe<Messages.std_msgs.String>("/my_topic", 10, subCallback);
37+
sub = nh.subscribe<Messages.std_msgs.String>("/chatter", 10, subCallback);
4138
}
4239

4340
public void subCallback(Messages.std_msgs.String msg)
4441
{
4542
Dispatcher.Invoke(new Action(() =>
4643
{
47-
TimeSpan dif = DateTime.Now.Subtract(before);
48-
l.Content = "Receieved: " + msg.data + "\n" + Math.Round(dif.TotalMilliseconds, 2) + " ms"; ;
49-
}));
44+
l.Content = "Receieved:\n" + msg.data;
45+
}), new TimeSpan(0,0,1));
46+
}
5047

51-
before = DateTime.Now;
48+
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
49+
{
50+
ROS.shutdown();
51+
ROS.waitForShutdown();
52+
base.OnClosing(e);
5253
}
5354
}
5455
}

SimpleSubscriber/SimpleSubscriber/SimpleSubscriber.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<DebugType>full</DebugType>
2424
<Optimize>false</Optimize>
2525
<OutputPath>bin\Debug\</OutputPath>
26-
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<DefineConstants>DEBUG</DefineConstants>
2727
<ErrorReport>prompt</ErrorReport>
2828
<WarningLevel>4</WarningLevel>
2929
</PropertyGroup>
@@ -53,9 +53,6 @@
5353
<Reference Include="WindowsBase" />
5454
<Reference Include="PresentationCore" />
5555
<Reference Include="PresentationFramework" />
56-
<Reference Include="XmlRpc_Wrapper">
57-
<HintPath>..\..\XmlRpc_Wrapper\bin\x64\Debug\XmlRpc_Wrapper.dll</HintPath>
58-
</Reference>
5956
</ItemGroup>
6057
<ItemGroup>
6158
<ApplicationDefinition Include="App.xaml">
@@ -105,6 +102,10 @@
105102
<Project>{586A3B66-104A-4182-82C2-88DC120F10AC}</Project>
106103
<Name>ROS_C-Sharp</Name>
107104
</ProjectReference>
105+
<ProjectReference Include="..\..\XmlRpc_Wrapper\XmlRpc_Wrapper.csproj">
106+
<Project>{8B7DB66C-9C03-4078-9276-B6A760CD4C2C}</Project>
107+
<Name>XmlRpc_Wrapper</Name>
108+
</ProjectReference>
108109
</ItemGroup>
109110
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
110111
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

0 commit comments

Comments
 (0)