DotCloud/Assets/Ros2ForUnity/Scripts/ROS2ServiceExample.cs

54 lines
1.8 KiB
C#

// Copyright 2019-2021 Robotec.ai.
//
// 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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ROS2;
using addTwoIntsReq = example_interfaces.srv.AddTwoInts_Request;
using addTwoIntsResp = example_interfaces.srv.AddTwoInts_Response;
/// <summary>
/// An example class provided for testing of basic ROS2 service
/// </summary>
public class ROS2ServiceExample : MonoBehaviour
{
private ROS2UnityComponent ros2Unity;
private ROS2Node ros2Node;
private IService<addTwoIntsReq, addTwoIntsResp> addTwoIntsService;
void Start()
{
ros2Unity = GetComponent<ROS2UnityComponent>();
if (ros2Unity.Ok())
{
if (ros2Node == null)
{
ros2Node = ros2Unity.CreateNode("ROS2UnityService");
addTwoIntsService = ros2Node.CreateService<addTwoIntsReq, addTwoIntsResp>(
"add_two_ints", addTwoInts);
}
}
}
public example_interfaces.srv.AddTwoInts_Response addTwoInts( example_interfaces.srv.AddTwoInts_Request msg)
{
Debug.Log("Incoming Service Request A=" + msg.A + " B=" + msg.B);
example_interfaces.srv.AddTwoInts_Response response = new example_interfaces.srv.AddTwoInts_Response();
response.Sum = msg.A + msg.B;
return response;
}
}