ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hello @jernejp,
I'm not an expert on ROS#, but I found an example in their repository tests file: https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosCommunicationTests.cs#L61
public void ServiceCallTest()
{
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
string json = JsonConvert.SerializeObject(comm);
Assert.AreEqual("{\"service\":\"myservice\",\"args\":{\"secs\":0,\"nsecs\":0}," +
"\"fragment_size\":2147483647,\"compression\":\"none\",\"op\":\"call_service\",\"id\":\"myid\"}",
json);
Console.WriteLine("JSON:\n" + JsonConvert.SerializeObject(comm, Formatting.Indented) + "\n");
}
On this first part, I see you need to pass some types
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
Then, you must have a response string json = JsonConvert.SerializeObject(comm);
I'm not sure how they handle that, because, as far as I know, ROS services are asynchronous. And this code doesn't seem to handle it like that. (In C++ and Python, for example, we handle it using callbacks)
After that, it seems you must be able to convert the string in JSON format to an object that fits your application.
I hope it can help you.
2 | No.2 Revision |
Hello @jernejp,
I'm not an expert on ROS#, but I found an example in their repository tests file: https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosCommunicationTests.cs#L61
public void ServiceCallTest()
{
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
string json = JsonConvert.SerializeObject(comm);
Assert.AreEqual("{\"service\":\"myservice\",\"args\":{\"secs\":0,\"nsecs\":0}," +
"\"fragment_size\":2147483647,\"compression\":\"none\",\"op\":\"call_service\",\"id\":\"myid\"}",
json);
Console.WriteLine("JSON:\n" + JsonConvert.SerializeObject(comm, Formatting.Indented) + "\n");
}
On this first part, I see you need to pass some types
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
Then, you must have a response string json = JsonConvert.SerializeObject(comm);
I'm not sure how they handle that, because, as far as I know, ROS services are asynchronous. And this code doesn't seem to handle it like that. (In C++ and Python, for example, we handle it using callbacks)
After that, it seems you must be able to convert the string in JSON format to an object that fits your application.
I hope it can help you.
3 | No.3 Revision |
Hello @jernejp,
I'm not an expert on ROS#, but I found an example in their repository tests file: https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosCommunicationTests.cs#L61
public void ServiceCallTest()
{
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
string json = JsonConvert.SerializeObject(comm);
Assert.AreEqual("{\"service\":\"myservice\",\"args\":{\"secs\":0,\"nsecs\":0}," +
"\"fragment_size\":2147483647,\"compression\":\"none\",\"op\":\"call_service\",\"id\":\"myid\"}",
json);
Console.WriteLine("JSON:\n" + JsonConvert.SerializeObject(comm, Formatting.Indented) + "\n");
}
On this first part, I see you need to pass some types
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
Then, you must have a response string json = JsonConvert.SerializeObject(comm);
After that, it seems you must be able to convert the string in JSON format to an object that fits your application.
I hope it can help you.
4 | No.4 Revision |
Hello @jernejp,
I'm not an expert on ROS#, but I found an example in their repository tests file:
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosCommunicationTests.cs#L61using a console socket:
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs
Line 58: service is called
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs#L58
rosSocket.CallService<rosapi.GetParamRequest, rosapi.GetParamResponse>("/rosapi/get_param", ServiceCallHandler, new rosapi.GetParamRequest("/rosdistro", "default"));
Line 78: handling service response
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs#L78
public private static void ServiceCallTest()
ServiceCallHandler(rosapi.GetParamResponse message)
{
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
string json = JsonConvert.SerializeObject(comm);
Assert.AreEqual("{\"service\":\"myservice\",\"args\":{\"secs\":0,\"nsecs\":0}," +
"\"fragment_size\":2147483647,\"compression\":\"none\",\"op\":\"call_service\",\"id\":\"myid\"}",
json);
Console.WriteLine("JSON:\n" Console.WriteLine("ROS distro: " + JsonConvert.SerializeObject(comm, Formatting.Indented) + "\n");
message.value);
}
On this first part, I see you need to pass some types
Communication comm = new ServiceCall<std_msgs.Time>("myid", "myservice", new std_msgs.Time());
Then, you must have a response string json = JsonConvert.SerializeObject(comm);
I hope it can help you.
5 | No.5 Revision |
Hello @jernejp,
I'm not an expert on ROS#, but I found an example in their repository using a console socket:
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cshttps://github.com/siemens/ros-sharp/blob/711396cf78578cc95aca49d2f6443ed406440376/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs
Line 58: service is called
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs#L58https://github.com/siemens/ros-sharp/blob/711396cf78578cc95aca49d2f6443ed406440376/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs#L58
rosSocket.CallService<rosapi.GetParamRequest, rosapi.GetParamResponse>("/rosapi/get_param", ServiceCallHandler, new rosapi.GetParamRequest("/rosdistro", "default"));
Line 78: handling service response
https://github.com/siemens/ros-sharp/blob/master/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs#L78https://github.com/siemens/ros-sharp/blob/711396cf78578cc95aca49d2f6443ed406440376/Libraries/RosBridgeClientTest/RosSocketConsoleExample.cs#L78
private static void ServiceCallHandler(rosapi.GetParamResponse message)
{
Console.WriteLine("ROS distro: " + message.value);
}
I hope it can help you.