-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathFibonacciActionClientConsoleExample.cs
126 lines (104 loc) · 4.02 KB
/
FibonacciActionClientConsoleExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
© Siemens AG, 2019
Author: Berkay Alp Cakal (berkay_alp.cakal.ct@siemens.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<http://www.apache.org/licenses/LICENSE-2.0>.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
- Added ROS2 action support
© Siemens AG 2025, Mehmet Emre Cakal, emre.cakal@siemens.com/m.emrecakal@gmail.com
*/
using System;
using RosSharp.RosBridgeClient;
using RosSharp.RosBridgeClient.Actionlib;
#if !ROS2
using System.Threading;
using RosSharp.RosBridgeClient.MessageTypes.Actionlib;
#else
using RosSharp.RosBridgeClient.MessageTypes.ActionTutorialsInterfaces;
#endif
namespace RosSharp.RosBridgeClientTest
{
class FibonacciActionClientConsoleExample
{
static readonly string uri = "ws://localhost:9090";
static readonly string actionName = "/fibonacci";
private static RosSocket rosSocket;
private static FibonacciActionClient fibonacciActionClient;
public static void Main(string[] args)
{
rosSocket = new RosSocket(new RosBridgeClient.Protocols.WebSocketNetProtocol(uri));
// Initialize Client
fibonacciActionClient = new FibonacciActionClient(actionName, rosSocket);
fibonacciActionClient.Initialize();
// Send goal
Console.WriteLine("Press any key to send goal with fibonacci order 5.");
Console.ReadKey(true);
#if !ROS2
fibonacciActionClient.fibonacciOrder = 5;
#else
fibonacciActionClient.SetActionGoal(new FibonacciGoal
{
order = 5
});
Console.WriteLine("Order is: " + fibonacciActionClient.GetActionGoal().args.order);
#endif
fibonacciActionClient.SendGoal();
// Get feedback, status and result
#if !ROS2
do
{
Console.WriteLine(fibonacciActionClient.GetFeedbackString());
Console.WriteLine(fibonacciActionClient.GetStatusString());
Thread.Sleep(100);
} while (fibonacciActionClient.goalStatus.status != GoalStatus.SUCCEEDED);
Thread.Sleep(500);
Console.WriteLine(fibonacciActionClient.GetResultString());
Console.WriteLine(fibonacciActionClient.GetStatusString());
#endif
// Cancel goal
Console.WriteLine("\nPress any key to send goal with fibonacci order 50.");
Console.ReadKey(true);
#if !ROS2
fibonacciActionClient.fibonacciOrder = 50;
#else
fibonacciActionClient.SetActionGoal(new FibonacciGoal
{
order = 50
});
#endif
fibonacciActionClient.SendGoal();
Console.WriteLine("\nPress any key to cancel the goal.");
Console.ReadKey(true);
#if !ROS2
fibonacciActionClient.CancelGoal();
Thread.Sleep(1000);
Console.WriteLine(fibonacciActionClient.GetResultString());
Console.WriteLine(fibonacciActionClient.GetFeedbackString());
Console.WriteLine(fibonacciActionClient.GetStatusString());
// Terminate client
fibonacciActionClient.Terminate();
#else
if (!fibonacciActionClient.lastResultSuccess)
{
Console.WriteLine("Action not completed. Cancelling...");
fibonacciActionClient.CancelGoal();
Console.ReadKey(true);
}
else
{
Console.WriteLine("Action completed.");
}
#endif
// End of console example
Console.WriteLine("\nPress any key to close.");
Console.ReadKey(true);
rosSocket.Close();
}
}
}