|
| 1 | +// Copyright 2019-2022 Robotec.ai. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using UnityEngine; |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Threading; |
| 19 | +using ROS2; |
| 20 | + |
| 21 | +namespace ROS2 |
| 22 | +{ |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The principal class for handling ros2 nodes and executables. |
| 26 | + /// Use this to create ros2 node, check ros2 status. |
| 27 | + /// Spins and executes actions (e. g. clock, sensor publish triggers) in a dedicated thread |
| 28 | + /// TODO: this is meant to be used as a one-of (a singleton). Enforce. However, things should work |
| 29 | + /// anyway with more than one since the underlying library can handle multiple init and shutdown calls, |
| 30 | + /// and does node name uniqueness check independently. |
| 31 | + /// </summary> |
| 32 | + public class ROS2UnityCore |
| 33 | + { |
| 34 | + private ROS2ForUnity ros2forUnity; |
| 35 | + private List<ROS2Node> nodes; |
| 36 | + private List<INode> ros2csNodes; // For performance in spinning |
| 37 | + private List<Action> executableActions; |
| 38 | + private bool quitting = false; |
| 39 | + private int interval = 2; // Spinning / executor interval in ms |
| 40 | + private object mutex = new object(); |
| 41 | + private double spinTimeout = 0.0001; |
| 42 | + |
| 43 | + public bool Ok() |
| 44 | + { |
| 45 | + lock (mutex) |
| 46 | + { |
| 47 | + return (nodes != null && ros2forUnity.Ok()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public ROS2UnityCore() |
| 52 | + { |
| 53 | + lock (mutex) |
| 54 | + { |
| 55 | + ros2forUnity = new ROS2ForUnity(); |
| 56 | + nodes = new List<ROS2Node>(); |
| 57 | + ros2csNodes = new List<INode>(); |
| 58 | + executableActions = new List<Action>(); |
| 59 | + |
| 60 | + Thread publishThread = new Thread(() => Tick()); |
| 61 | + publishThread.Start(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public ROS2Node CreateNode(string name) |
| 66 | + { |
| 67 | + lock (mutex) |
| 68 | + { |
| 69 | + foreach (ROS2Node n in nodes) |
| 70 | + { // Assumed to be a rare operation on rather small (<1k) list |
| 71 | + if (n.name == name) |
| 72 | + { |
| 73 | + throw new InvalidOperationException("Cannot create node " + name + ". A node with this name already exists!"); |
| 74 | + } |
| 75 | + } |
| 76 | + ROS2Node node = new ROS2Node(name); |
| 77 | + nodes.Add(node); |
| 78 | + ros2csNodes.Add(node.node); |
| 79 | + return node; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void RemoveNode(ROS2Node node) |
| 84 | + { |
| 85 | + lock (mutex) |
| 86 | + { |
| 87 | + ros2csNodes.Remove(node.node); |
| 88 | + nodes.Remove(node); //Node will be later deleted if unused, by GC |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Works as a simple executor registration analogue. These functions will be called with each Tick() |
| 94 | + /// Actions need to take care of correct call resolution by checking in their body (TODO) |
| 95 | + /// Make sure actions are lightweight (TODO - separate out threads for spinning and executables?) |
| 96 | + /// </summary> |
| 97 | + public void RegisterExecutable(Action executable) |
| 98 | + { |
| 99 | + lock (mutex) |
| 100 | + { |
| 101 | + executableActions.Add(executable); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public void UnregisterExecutable(Action executable) |
| 106 | + { |
| 107 | + lock (mutex) |
| 108 | + { |
| 109 | + executableActions.Remove(executable); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// "Executor" thread will tick all clocks and spin the node |
| 115 | + /// </summary> |
| 116 | + private void Tick() |
| 117 | + { |
| 118 | + while (!quitting) |
| 119 | + { |
| 120 | + if (Ok()) |
| 121 | + { |
| 122 | + lock (mutex) |
| 123 | + { |
| 124 | + foreach (Action action in executableActions) |
| 125 | + { |
| 126 | + action(); |
| 127 | + } |
| 128 | + Ros2cs.SpinOnce(ros2csNodes, spinTimeout); |
| 129 | + } |
| 130 | + } |
| 131 | + Thread.Sleep(interval); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public void DestroyNow() |
| 136 | + { |
| 137 | + quitting = true; |
| 138 | + ros2forUnity.DestroyROS2ForUnity(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} // namespace ROS2 |
0 commit comments